1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const path = require("path");
|
12 | const chokidar = require("chokidar");
|
13 | const _ = require("lodash");
|
14 | exports.default = (ctx) => {
|
15 | ctx.registerPlatform({
|
16 | name: 'ui',
|
17 | fn() {
|
18 | return __awaiter(this, void 0, void 0, function* () {
|
19 | const { H5_OUTPUT_NAME, RN_OUTPUT_NAME, TEMP_DIR, RN_TEMP_DIR, WEAPP_OUTPUT_NAME, QUICKAPP_OUTPUT_NAME, copyFileToDist, analyzeStyleFilesImport, analyzeFiles } = require('../../ui/common');
|
20 | const { Compiler } = require('../../h5');
|
21 | const { buildH5Script, buildForH5 } = require('../../ui/h5');
|
22 | const { buildForRN } = require('../../ui/rn');
|
23 | const { buildForWeapp } = require('../../ui/weapp');
|
24 | const { buildForQuickapp } = require('../../ui/quickapp');
|
25 | const { Compiler: RNCompiler } = require('../../rn_bak');
|
26 | const { uiIndex, isWatch } = ctx.runOpts;
|
27 | const { appPath, sourcePath } = ctx.paths;
|
28 | const { chalk, fs, resolveScriptPath, printLog, REG_STYLE, processTypeEnum, PLATFORMS } = ctx.helper;
|
29 | const projectConfig = ctx.initialConfig;
|
30 | let entryFilePath;
|
31 | if (uiIndex) {
|
32 | entryFilePath = resolveScriptPath(path.join(sourcePath, uiIndex));
|
33 | }
|
34 | else {
|
35 | entryFilePath = resolveScriptPath(path.join(sourcePath, 'index'));
|
36 | }
|
37 | const buildData = {
|
38 | appPath,
|
39 | projectConfig,
|
40 | sourceDirName: projectConfig.sourceRoot,
|
41 | outputDirName: projectConfig.outputRoot,
|
42 | sourceDir: sourcePath,
|
43 | entryFilePath,
|
44 | entryFileName: path.basename(entryFilePath),
|
45 | tempPath: path.join(appPath, TEMP_DIR),
|
46 | rnTempPath: path.join(appPath, RN_TEMP_DIR)
|
47 | };
|
48 | function buildEntry(uiIndex) {
|
49 | const { appPath, outputDirName } = buildData;
|
50 | let indexName = 'index';
|
51 | if (uiIndex) {
|
52 | indexName = path.basename(uiIndex, path.extname(uiIndex));
|
53 | }
|
54 | let content = '';
|
55 | platforms.forEach((item, index) => {
|
56 | let dir = item;
|
57 | if (item !== PLATFORMS.H5 && item !== PLATFORMS.RN && item !== PLATFORMS.QUICKAPP) {
|
58 | dir = WEAPP_OUTPUT_NAME;
|
59 | }
|
60 | content += `if (process.env.TARO_ENV === '${item}') {
|
61 | module.exports = require('./${dir}/${indexName}')
|
62 | module.exports.default = module.exports
|
63 | }`;
|
64 | if (index < platforms.length - 1) {
|
65 | content += ' else ';
|
66 | }
|
67 | else {
|
68 | content += ` else {
|
69 | module.exports = require('./${WEAPP_OUTPUT_NAME}/${indexName}')
|
70 | module.exports.default = module.exports
|
71 | }`;
|
72 | }
|
73 | });
|
74 | const outputDir = path.join(appPath, outputDirName);
|
75 | fs.writeFileSync(path.join(outputDir, `index.js`), content);
|
76 | }
|
77 | function watchFiles() {
|
78 | const { sourceDir, projectConfig, appPath, outputDirName, tempPath } = buildData;
|
79 | const platforms = _.get(buildData, 'projectConfig.ui.platforms');
|
80 | console.log('\n', chalk.gray('监听文件修改中...'), '\n');
|
81 | const watchList = [sourceDir];
|
82 | const uiConfig = projectConfig.ui;
|
83 | let extraWatchFiles;
|
84 | if (uiConfig && Array.isArray(uiConfig.extraWatchFiles)) {
|
85 | extraWatchFiles = uiConfig.extraWatchFiles;
|
86 | extraWatchFiles.forEach(item => {
|
87 | watchList.push(path.join(appPath, item.path));
|
88 | if (typeof item.handler === 'function')
|
89 | item.callback = item.handler({ buildH5Script });
|
90 | });
|
91 | }
|
92 | const watcher = chokidar.watch(watchList, {
|
93 | ignored: /(^|[/\\])\../,
|
94 | ignoreInitial: true
|
95 | });
|
96 | function syncWeappFile(filePath) {
|
97 | const outputDir = path.join(appPath, outputDirName, WEAPP_OUTPUT_NAME);
|
98 | copyFileToDist(filePath, sourceDir, outputDir, buildData);
|
99 |
|
100 | const extname = path.extname(filePath);
|
101 | if (REG_STYLE.test(extname)) {
|
102 | analyzeStyleFilesImport([filePath], sourceDir, outputDir, buildData);
|
103 | }
|
104 | else {
|
105 | analyzeFiles([filePath], sourceDir, outputDir, buildData);
|
106 | }
|
107 | }
|
108 | function syncQuickappFile(filePath) {
|
109 | const outputDir = path.join(appPath, outputDirName, QUICKAPP_OUTPUT_NAME);
|
110 | copyFileToDist(filePath, sourceDir, outputDir, buildData);
|
111 |
|
112 | const extname = path.extname(filePath);
|
113 | if (REG_STYLE.test(extname)) {
|
114 | analyzeStyleFilesImport([filePath], sourceDir, outputDir, buildData);
|
115 | }
|
116 | else {
|
117 | analyzeFiles([filePath], sourceDir, outputDir, buildData);
|
118 | }
|
119 | }
|
120 | function syncH5File(filePath, compiler) {
|
121 | const { sourceDir, appPath, outputDirName, tempPath } = buildData;
|
122 | const outputDir = path.join(appPath, outputDirName, H5_OUTPUT_NAME);
|
123 | let fileTempPath = filePath.replace(sourceDir, tempPath);
|
124 | fileTempPath = fileTempPath.replace(new RegExp(`${path.extname(fileTempPath)}$`), '');
|
125 | fileTempPath = resolveScriptPath(fileTempPath);
|
126 | compiler.processFiles(filePath);
|
127 | if (process.env.TARO_BUILD_TYPE === 'script') {
|
128 | buildH5Script(buildData);
|
129 | }
|
130 | else {
|
131 | copyFileToDist(fileTempPath, tempPath, outputDir, buildData);
|
132 |
|
133 | const extname = path.extname(filePath);
|
134 | if (REG_STYLE.test(extname)) {
|
135 | analyzeStyleFilesImport([fileTempPath], tempPath, outputDir, buildData);
|
136 | }
|
137 | else {
|
138 | analyzeFiles([fileTempPath], tempPath, outputDir, buildData);
|
139 | }
|
140 | }
|
141 | }
|
142 | function syncRNFile(filePath, compiler) {
|
143 | const { sourceDir, appPath, outputDirName, rnTempPath } = buildData;
|
144 | const outputDir = path.join(appPath, outputDirName, RN_OUTPUT_NAME);
|
145 | const fileTempPath = filePath.replace(sourceDir, rnTempPath);
|
146 | compiler.processFiles(filePath);
|
147 | copyFileToDist(fileTempPath, tempPath, outputDir, buildData);
|
148 |
|
149 | const extname = path.extname(filePath);
|
150 | if (REG_STYLE.test(extname)) {
|
151 | analyzeStyleFilesImport([fileTempPath], tempPath, outputDir, buildData);
|
152 | }
|
153 | else {
|
154 | analyzeFiles([fileTempPath], tempPath, outputDir, buildData);
|
155 | }
|
156 | }
|
157 | function handleChange(filePath, type, tips) {
|
158 | const relativePath = path.relative(appPath, filePath);
|
159 | const compiler = new Compiler(appPath);
|
160 | const rnCompiler = new RNCompiler(appPath);
|
161 | printLog(type, tips, relativePath);
|
162 | let processed = false;
|
163 | extraWatchFiles && extraWatchFiles.forEach(item => {
|
164 | if (filePath.indexOf(item.path.substr(2)) < 0)
|
165 | return;
|
166 | if (typeof item.callback === 'function') {
|
167 | item.callback();
|
168 | processed = true;
|
169 | }
|
170 | });
|
171 | if (processed)
|
172 | return;
|
173 | try {
|
174 | if (platforms && Array.isArray(platforms)) {
|
175 | platforms.includes(PLATFORMS.WEAPP) && syncWeappFile(filePath);
|
176 | platforms.includes(PLATFORMS.QUICKAPP) && syncQuickappFile(filePath);
|
177 | platforms.includes(PLATFORMS.H5) && syncH5File(filePath, compiler);
|
178 | platforms.includes(PLATFORMS.RN) && syncRNFile(filePath, rnCompiler);
|
179 | }
|
180 | else {
|
181 | syncWeappFile(filePath);
|
182 | syncH5File(filePath, compiler);
|
183 | }
|
184 | }
|
185 | catch (err) {
|
186 | console.log(err);
|
187 | }
|
188 | }
|
189 | watcher
|
190 | .on('add', filePath => handleChange(filePath, processTypeEnum.CREATE, '添加文件'))
|
191 | .on('change', filePath => handleChange(filePath, processTypeEnum.MODIFY, '文件变动'))
|
192 | .on('unlink', filePath => {
|
193 | for (const path in extraWatchFiles) {
|
194 | if (filePath.indexOf(path.substr(2)) > -1)
|
195 | return;
|
196 | }
|
197 | const relativePath = path.relative(appPath, filePath);
|
198 | printLog(processTypeEnum.UNLINK, '删除文件', relativePath);
|
199 | const weappOutputPath = path.join(appPath, outputDirName, WEAPP_OUTPUT_NAME);
|
200 | const quickappOutputPath = path.join(appPath, outputDirName, QUICKAPP_OUTPUT_NAME);
|
201 | const h5OutputPath = path.join(appPath, outputDirName, H5_OUTPUT_NAME);
|
202 | const fileTempPath = filePath.replace(sourceDir, tempPath);
|
203 | const fileWeappPath = filePath.replace(sourceDir, weappOutputPath);
|
204 | const fileQuickappPath = filePath.replace(sourceDir, quickappOutputPath);
|
205 | const fileH5Path = filePath.replace(sourceDir, h5OutputPath);
|
206 | fs.existsSync(fileTempPath) && fs.unlinkSync(fileTempPath);
|
207 | fs.existsSync(fileWeappPath) && fs.unlinkSync(fileWeappPath);
|
208 | fs.existsSync(fileQuickappPath) && fs.unlinkSync(fileQuickappPath);
|
209 | fs.existsSync(fileH5Path) && fs.unlinkSync(fileH5Path);
|
210 | });
|
211 | }
|
212 | const platforms = _.get(buildData, 'projectConfig.ui.platforms') || [PLATFORMS.WEAPP, PLATFORMS.H5];
|
213 | buildEntry(uiIndex);
|
214 | if (platforms && Array.isArray(platforms)) {
|
215 | platforms.includes(PLATFORMS.WEAPP) && (yield buildForWeapp(buildData));
|
216 | platforms.includes(PLATFORMS.QUICKAPP) && (yield buildForQuickapp(buildData));
|
217 | platforms.includes(PLATFORMS.H5) && (yield buildForH5(uiIndex, buildData));
|
218 | platforms.includes(PLATFORMS.RN) && (yield buildForRN(uiIndex, buildData));
|
219 | }
|
220 | else {
|
221 | yield buildForWeapp(buildData);
|
222 | yield buildForH5(uiIndex, buildData);
|
223 | }
|
224 | if (isWatch) {
|
225 | watchFiles();
|
226 | }
|
227 | });
|
228 | }
|
229 | });
|
230 | };
|