1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
12 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
13 | };
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
18 | result["default"] = mod;
|
19 | return result;
|
20 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | const webpack_1 = __importDefault(require("webpack"));
|
23 | const path = __importStar(require("path"));
|
24 | const fs = __importStar(require("fs-extra"));
|
25 | const platforms_1 = __importDefault(require("./consts/platforms"));
|
26 | const queue_1 = require("./packages/utils/logger/queue");
|
27 | const index_1 = require("./packages/utils/logger/index");
|
28 | const chalk_1 = __importDefault(require("chalk"));
|
29 | const webpackConfig_1 = __importDefault(require("./config/webpackConfig"));
|
30 | const babel = __importStar(require("@babel/core"));
|
31 | const child_process_1 = require("child_process");
|
32 | const index_2 = __importDefault(require("./packages/utils/index"));
|
33 | const config_1 = __importDefault(require("./config/config"));
|
34 | const runBeforeParseTasks_1 = __importDefault(require("./tasks/runBeforeParseTasks"));
|
35 | const createH5Server_1 = __importDefault(require("./tasks/createH5Server"));
|
36 | const configurations_1 = require("./config/h5/configurations");
|
37 | const OS = __importStar(require("os"));
|
38 | const rd = __importStar(require("rd"));
|
39 | function nanachi(options = {}) {
|
40 | return __awaiter(this, void 0, void 0, function* () {
|
41 | const { watch = false, platform = 'wx', beta = false, betaUi = false, compress = false, compressOption = {}, huawei = false, typescript = false, rules = [], prevLoaders = [], postLoaders = [], prevJsLoaders = [], postJsLoaders = [], prevCssLoaders = [], postCssLoaders = [], plugins = [], analysis = false, silent = false, complete = () => { } } = options;
|
42 | function callback(err, stats) {
|
43 | if (err) {
|
44 | console.log(chalk_1.default.red(err.toString()));
|
45 | return;
|
46 | }
|
47 | showLog();
|
48 | const info = stats.toJson();
|
49 | if (stats.hasWarnings() && !silent) {
|
50 | info.warnings.forEach(warning => {
|
51 | if (!/Critical dependency: the request of a dependency is an expression/.test(warning)) {
|
52 | console.log(chalk_1.default.yellow('Warning:\n'), index_2.default.cleanLog(warning));
|
53 | }
|
54 | });
|
55 | }
|
56 | if (stats.hasErrors()) {
|
57 | info.errors.forEach(e => {
|
58 | console.error(chalk_1.default.red('Error:\n'), index_2.default.cleanLog(e));
|
59 | if (index_2.default.isMportalEnv()) {
|
60 | process.exit();
|
61 | }
|
62 | });
|
63 | }
|
64 | if (platform === 'h5') {
|
65 | const configPath = watch ? './config/h5/webpack.config.js' : './config/h5/webpack.config.prod.js';
|
66 | const webpackH5Config = require(configPath);
|
67 | if (typescript)
|
68 | webpackH5Config.entry += '.tsx';
|
69 | if (config_1.default['360mode']) {
|
70 | const cwd = process.cwd();
|
71 | if (!fs.existsSync(path.join(cwd, 'src'))) {
|
72 | fs.copySync(path.resolve(__dirname, './packages/360helpers/template'), path.resolve(cwd, 'src'));
|
73 | }
|
74 | }
|
75 | const compilerH5 = webpack_1.default(webpackH5Config);
|
76 | if (watch) {
|
77 | createH5Server_1.default(compilerH5);
|
78 | }
|
79 | else {
|
80 | compilerH5.run(function (err, stats) {
|
81 | if (config_1.default['360mode']) {
|
82 | const appPath = path.resolve(process.cwd(), 'src/app.js');
|
83 | let script = fs.readFileSync(appPath).toString();
|
84 | script = `import './dist/web/bundle.${stats.hash.slice(0, 10)}.js';\n${script}`;
|
85 | fs.writeFileSync(appPath, script, 'utf-8');
|
86 | const files = fs.readdirSync(webpackH5Config.output.path);
|
87 | fs.ensureDirSync(path.resolve(process.cwd(), './src/dist/web'));
|
88 | files.forEach(filename => {
|
89 | if (filename !== configurations_1.intermediateDirectoryName) {
|
90 | fs.copySync(path.resolve(webpackH5Config.output.path, filename), path.resolve(process.cwd(), './src/dist/web', filename));
|
91 | }
|
92 | });
|
93 | }
|
94 | if (err) {
|
95 | console.log(err);
|
96 | return;
|
97 | }
|
98 | const info = stats.toJson();
|
99 | if (stats.hasWarnings() && !silent) {
|
100 | info.warnings.forEach(warning => {
|
101 | if (!/Critical dependency: the request of a dependency is an expression/.test(warning)) {
|
102 | console.log(chalk_1.default.yellow('Warning:\n'), index_2.default.cleanLog(warning));
|
103 | }
|
104 | });
|
105 | }
|
106 | if (stats.hasErrors()) {
|
107 | info.errors.forEach(e => {
|
108 | console.error(chalk_1.default.red('Error:\n'), index_2.default.cleanLog(e));
|
109 | if (index_2.default.isMportalEnv()) {
|
110 | process.exit();
|
111 | }
|
112 | });
|
113 | }
|
114 | });
|
115 | }
|
116 | }
|
117 | complete(err, stats);
|
118 | }
|
119 | try {
|
120 | if (watch && config_1.default['360mode']) {
|
121 | throw new Error('360编译不支持watch模式');
|
122 | }
|
123 | if (!index_2.default.validatePlatform(platform, platforms_1.default)) {
|
124 | throw new Error(`不支持的platform:${platform}`);
|
125 | }
|
126 | const useTs = fs.existsSync(path.resolve(process.cwd(), './source/app.tsx'));
|
127 | if (useTs && !typescript) {
|
128 | throw '检测到app.tsx,请使用typescript模式编译(-t/--typescript)';
|
129 | }
|
130 | injectBuildEnv({
|
131 | platform,
|
132 | compress,
|
133 | huawei,
|
134 | typescript
|
135 | });
|
136 | getWebViewRules();
|
137 | yield runBeforeParseTasks_1.default({ platform, beta, betaUi, compress });
|
138 | if (compress) {
|
139 | postLoaders.unshift('nanachi-compress-loader');
|
140 | }
|
141 | const webpackConfig = webpackConfig_1.default({
|
142 | platform,
|
143 | compress,
|
144 | compressOption,
|
145 | beta,
|
146 | betaUi,
|
147 | plugins,
|
148 | typescript,
|
149 | analysis,
|
150 | prevLoaders,
|
151 | postLoaders,
|
152 | prevJsLoaders,
|
153 | postJsLoaders,
|
154 | prevCssLoaders,
|
155 | postCssLoaders,
|
156 | rules,
|
157 | huawei
|
158 | });
|
159 | const compiler = webpack_1.default(webpackConfig);
|
160 | if (watch) {
|
161 | compiler.watch({}, callback);
|
162 | }
|
163 | else {
|
164 | compiler.run(callback);
|
165 | }
|
166 | }
|
167 | catch (err) {
|
168 | callback(err);
|
169 | }
|
170 | });
|
171 | }
|
172 | function injectBuildEnv({ platform, compress, huawei, typescript }) {
|
173 | process.env.ANU_ENV = (platform === 'h5' ? 'web' : platform);
|
174 | config_1.default['buildType'] = platform;
|
175 | config_1.default['compress'] = compress;
|
176 | config_1.default['typescript'] = typescript;
|
177 | if (platform === 'quick') {
|
178 | config_1.default['huawei'] = huawei || false;
|
179 | }
|
180 | }
|
181 | function showLog() {
|
182 | if (index_2.default.isMportalEnv()) {
|
183 | let log = '';
|
184 | while (queue_1.build.length) {
|
185 | log += queue_1.build.shift() + (queue_1.build.length !== 0 ? '\n' : '');
|
186 | }
|
187 | console.log(log);
|
188 | }
|
189 | while (queue_1.warning.length) {
|
190 | index_1.warningLog(queue_1.warning.shift());
|
191 | }
|
192 | if (queue_1.error.length) {
|
193 | queue_1.error.forEach(function (error) {
|
194 | index_1.errorLog(error);
|
195 | });
|
196 | if (index_2.default.isMportalEnv()) {
|
197 | process.exit(1);
|
198 | }
|
199 | }
|
200 | }
|
201 | function getWebViewRoutes() {
|
202 | const pages = path.join(process.cwd(), 'source', 'pages');
|
203 | let webViewRoutes = [];
|
204 | if ('win32' === OS.platform()) {
|
205 | webViewRoutes = rd.readFilterSync(pages, /\.js$/).filter((jsfile) => {
|
206 | const reg = new RegExp("pages:\\s*(\\btrue\\b|\\[.+\\])");
|
207 | const content = fs.readFileSync(jsfile).toString();
|
208 | return reg.test(content);
|
209 | });
|
210 | }
|
211 | else {
|
212 | let bin = 'grep';
|
213 | let opts = ['-r', '-E', "pages:\\s*(\\btrue\\b|\\[.+\\])", pages];
|
214 | let ret = child_process_1.spawnSync(bin, opts).stdout.toString().trim();
|
215 | webViewRoutes = ret.split(/\s/)
|
216 | .filter(function (el) {
|
217 | return /\/pages\//.test(el);
|
218 | }).map(function (el) {
|
219 | return el.replace(/\:$/g, '');
|
220 | });
|
221 | }
|
222 | return webViewRoutes;
|
223 | }
|
224 | function getWebViewRules() {
|
225 | const cwd = process.cwd();
|
226 | if (config_1.default.buildType != 'quick')
|
227 | return;
|
228 | let webViewRoutes = getWebViewRoutes();
|
229 | webViewRoutes.forEach(function (pagePath) {
|
230 | return __awaiter(this, void 0, void 0, function* () {
|
231 | babel.transformFileSync(pagePath, {
|
232 | configFile: false,
|
233 | babelrc: false,
|
234 | comments: false,
|
235 | ast: true,
|
236 | presets: [
|
237 | require('@babel/preset-react')
|
238 | ],
|
239 | plugins: [
|
240 | [require('@babel/plugin-proposal-decorators'), { legacy: true }],
|
241 | [require('@babel/plugin-proposal-class-properties'), { loose: true }],
|
242 | require('@babel/plugin-proposal-object-rest-spread'),
|
243 | require('@babel/plugin-syntax-jsx'),
|
244 | require('./packages/babelPlugins/collectWebViewPage'),
|
245 | ]
|
246 | });
|
247 | });
|
248 | });
|
249 | const WebViewRules = config_1.default.WebViewRules;
|
250 | if (WebViewRules && WebViewRules.pages.length) {
|
251 | process.env.ANU_WEBVIEW = 'need_require_webview_file';
|
252 | }
|
253 | else {
|
254 | process.env.ANU_WEBVIEW = '';
|
255 | }
|
256 | }
|
257 | exports.default = nanachi;
|