Skip to content

Commit 6f2b02f

Browse files
committed
fix: watch下 json依赖不生效问题
1 parent 068b82f commit 6f2b02f

File tree

4 files changed

+76
-91
lines changed

4 files changed

+76
-91
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class Mpbuilder {
9393
await this.run();
9494
log.info('开启watching');
9595
this.watching.watch();
96+
this.watching.watchEntry();
9697
}
9798

9899
async run() {

src/loader/taro-js-loader.js

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,6 @@ function parseAst(type, ast, depComponents, sourceFilePath) {
154154
node.decorators || []
155155
)
156156
);
157-
} else if (node.id.name === 'App') {
158-
componentClassName = '_App';
159-
astPath.replaceWith(
160-
t.classDeclaration(
161-
t.identifier(componentClassName),
162-
node.superClass,
163-
node.body,
164-
node.decorators || []
165-
)
166-
);
167157
} else {
168158
componentClassName = node.id.name;
169159
}
@@ -199,16 +189,6 @@ function parseAst(type, ast, depComponents, sourceFilePath) {
199189
node.decorators || []
200190
)
201191
);
202-
} else if (node.id.name === 'App') {
203-
componentClassName = '_App';
204-
astPath.replaceWith(
205-
t.ClassExpression(
206-
t.identifier(componentClassName),
207-
node.superClass,
208-
node.body,
209-
node.decorators || []
210-
)
211-
);
212192
} else {
213193
componentClassName = node.id.name;
214194
}
@@ -227,7 +207,6 @@ function parseAst(type, ast, depComponents, sourceFilePath) {
227207
const { node } = astPath;
228208
const { source } = node;
229209
let { value } = source;
230-
const { specifiers } = node;
231210
if (Util.isNpmPkg(value)) {
232211
if (value === taroJsComponents) {
233212
astPath.remove();
@@ -381,7 +360,6 @@ function parseAst(type, ast, depComponents, sourceFilePath) {
381360
if (callee.name === 'require') {
382361
const args = node.arguments;
383362
const { value } = args[0];
384-
const valueExtname = path.extname(value);
385363
if (value.indexOf('.') === 0) {
386364
let importPath = path.resolve(path.dirname(sourceFilePath), value);
387365
importPath = Util.resolveScriptPath(importPath);
@@ -412,14 +390,6 @@ function parseAst(type, ast, depComponents, sourceFilePath) {
412390
}
413391
const taroMiniAppFrameworkPath = taroMiniAppFramework;
414392
switch (type) {
415-
case PARSE_AST_TYPE.ENTRY:
416-
node.body.push(
417-
template(
418-
`App(require('${taroMiniAppFrameworkPath}').default.createApp(${exportVariableName}))`,
419-
babylonConfig
420-
)()
421-
);
422-
break;
423393
case PARSE_AST_TYPE.PAGE:
424394
node.body.push(
425395
template(
@@ -474,6 +444,7 @@ module.exports = async function(asset, opts) {
474444
code: contents
475445
})
476446
);
447+
// asset.contents = resCode;
477448
const parseRes = parseAst(PARSE_AST_TYPE.PAGE, ast, pageDepComponents);
478449
asset.contents = parseRes.code;
479450
const wxmlAsset = new Asset(pageWXMLPath, outputPath, { virtual_file: true });

src/plugin/handleJSONComponentDep.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,32 @@ const path = require('path');
66
module.exports = class HandleJSONComponentDep {
77
apply(mpb) {
88
mpb.hooks.beforeEmitFile.tapPromise('HandleJSONComponentDep', async (asset) => {
9-
const key = asset.getMeta('mbp-scan-json-dep');
10-
if (key) {
9+
// const key = asset.getMeta('mbp-scan-json-dep');
10+
// TODO 并不是所有JSON都要进行这个判定的,先通过usingComponents这个key来判定是否是依赖,但是有点硬核,后面想下有没有更好的办法,上面通过 meta的方式也不行,主要是在watch的时候如何对新的asset设置meta
11+
if (/\.json$/.test(asset.outputFilePath) && asset.contents) {
1112
const code = JSON.parse(asset.contents);
12-
const componets = code[key];
13-
if (componets) {
14-
// TODO 这里需要支持 alias
15-
await Promise.all(
16-
Object.keys(componets).map((componentName) => {
17-
let filePath = '',
18-
src = componets[componentName];
19-
if (src[0] === '/') {
20-
filePath = path.resolve(mpb.src, `.${src}`);
21-
} else if (src[0] === '.') {
22-
filePath = path.resolve(asset.dir, src);
23-
} else {
24-
filePath = path.resolve(asset.dir, `./${src}`);
25-
}
26-
mpb.scan.addAssetByEXT(
27-
filePath.replace(mpb.src, ''),
28-
path.resolve(mpb.dest, path.relative(mpb.src, filePath))
29-
);
30-
})
31-
);
13+
if (code.usingComponents) {
14+
const componets = code.usingComponents;
15+
if (componets) {
16+
// TODO 这里需要支持 alias
17+
await Promise.all(
18+
Object.keys(componets).map((componentName) => {
19+
let filePath = '',
20+
src = componets[componentName];
21+
if (src[0] === '/') {
22+
filePath = path.resolve(mpb.src, `.${src}`);
23+
} else if (src[0] === '.') {
24+
filePath = path.resolve(asset.dir, src);
25+
} else {
26+
filePath = path.resolve(asset.dir, `./${src}`);
27+
}
28+
mpb.scan.addAssetByEXT(
29+
filePath.replace(mpb.src, ''),
30+
path.resolve(mpb.dest, path.relative(mpb.src, filePath))
31+
);
32+
})
33+
);
34+
}
3235
}
3336
}
3437
return asset;

src/plugin/minifyPlugin.js

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,64 @@ const workerpool = require('workerpool');
88

99
const pool = workerpool.pool();
1010

11-
function minify(contents, outputFilePath) {
11+
function minifyJS(contents) {
1212
const UglifyJS = require('uglify-js');
13-
const jsonminify = require('jsonminify');
13+
const result = UglifyJS.minify(contents);
14+
if (result.error) console.error('[MinifyPlugin]', result.error);
15+
if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
16+
return result.code;
17+
}
18+
function minifyWXML(contents) {
1419
const htmlmin = require('html-minifier');
15-
if (/\.js$/.test(outputFilePath)) {
16-
const result = UglifyJS.minify(contents);
17-
if (result.error) console.error('[MinifyPlugin]', result.error);
18-
if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
19-
return result.code;
20-
}
21-
if (/\.json$/.test(outputFilePath)) {
22-
return jsonminify(contents).toString();
23-
}
24-
if (/\.wxml$/.test(outputFilePath)) {
25-
return htmlmin.minify(contents, {
26-
removeComments: true,
27-
keepClosingSlash: true,
28-
collapseWhitespace: true,
29-
caseSensitive: true
30-
});
31-
}
20+
return htmlmin.minify(contents, {
21+
removeComments: true,
22+
keepClosingSlash: true,
23+
collapseWhitespace: true,
24+
caseSensitive: true
25+
});
26+
}
27+
function minifyJSON(contents) {
28+
const jsonminify = require('jsonminify');
29+
return jsonminify(contents).toString();
3230
}
3331

3432
module.exports = class MinifyPlugin {
33+
constructor() {
34+
this.js = true;
35+
this.wxml = true;
36+
this.json = true;
37+
}
38+
3539
apply(mpb) {
3640
if (mpb.optimization.minimize) {
41+
if (typeof mpb.optimization.minimize === 'object') {
42+
this.js = mpb.optimization.minimize.js;
43+
this.wxml = mpb.optimization.minimize.wxml;
44+
this.json = mpb.optimization.minimize.json;
45+
} else if (mpb.optimization.minimize === false) {
46+
this.js = false;
47+
this.wxml = false;
48+
this.json = false;
49+
}
3750
mpb.hooks.beforeEmitFile.tapPromise('MinifyPlugin', async (asset) => {
3851
if (asset.contents) {
39-
// if (/\.js$/.test(asset.outputFilePath)) {
40-
// const result = UglifyJS.minify(asset.contents);
41-
// if (result.error) console.error('[MinifyPlugin]', result.error);
42-
// if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
43-
// asset.contents = result.code;
44-
// } else if (/\.json$/.test(asset.outputFilePath)) {
45-
// asset.contents = jsonminify(asset.contents).toString();
46-
// } else if (/\.wxml$/.test(asset.outputFilePath)) {
47-
// asset.contents = asset.contents = htmlmin.minify(asset.contents, {
48-
// removeComments: true,
49-
// keepClosingSlash: true,
50-
// collapseWhitespace: true,
51-
// caseSensitive: true
52-
// });
53-
// }
54-
if (/\.(js|json|wxml)$/.test(asset.outputFilePath)) {
55-
asset.contents = await pool.exec(minify, [
56-
asset.contents,
57-
asset.outputFilePath
58-
]);
52+
if (/\.js$/.test(asset.outputFilePath) && this.js) {
53+
// const result = UglifyJS.minify(asset.contents);
54+
// if (result.error) console.error('[MinifyPlugin]', result.error);
55+
// if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
56+
// asset.contents = result.code;
57+
asset.contents = await pool.exec(minifyJS, [asset.contents]);
58+
} else if (/\.json$/.test(asset.outputFilePath) && this.json) {
59+
// asset.contents = jsonminify(asset.contents).toString();
60+
asset.contents = await pool.exec(minifyJSON, [asset.contents]);
61+
} else if (/\.wxml$/.test(asset.outputFilePath) && this.wxml) {
62+
// asset.contents = asset.contents = htmlmin.minify(asset.contents, {
63+
// removeComments: true,
64+
// keepClosingSlash: true,
65+
// collapseWhitespace: true,
66+
// caseSensitive: true
67+
// });
68+
asset.contents = await pool.exec(minifyWXML, [asset.contents]);
5969
}
6070
}
6171
return Promise.resolve();

0 commit comments

Comments
 (0)