Skip to content

Commit

Permalink
fix: watch下 json依赖不生效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ximing committed Mar 20, 2019
1 parent 068b82f commit 6f2b02f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 91 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Mpbuilder {
await this.run();
log.info('开启watching');
this.watching.watch();
this.watching.watchEntry();
}

async run() {
Expand Down
31 changes: 1 addition & 30 deletions src/loader/taro-js-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,6 @@ function parseAst(type, ast, depComponents, sourceFilePath) {
node.decorators || []
)
);
} else if (node.id.name === 'App') {
componentClassName = '_App';
astPath.replaceWith(
t.classDeclaration(
t.identifier(componentClassName),
node.superClass,
node.body,
node.decorators || []
)
);
} else {
componentClassName = node.id.name;
}
Expand Down Expand Up @@ -199,16 +189,6 @@ function parseAst(type, ast, depComponents, sourceFilePath) {
node.decorators || []
)
);
} else if (node.id.name === 'App') {
componentClassName = '_App';
astPath.replaceWith(
t.ClassExpression(
t.identifier(componentClassName),
node.superClass,
node.body,
node.decorators || []
)
);
} else {
componentClassName = node.id.name;
}
Expand All @@ -227,7 +207,6 @@ function parseAst(type, ast, depComponents, sourceFilePath) {
const { node } = astPath;
const { source } = node;
let { value } = source;
const { specifiers } = node;
if (Util.isNpmPkg(value)) {
if (value === taroJsComponents) {
astPath.remove();
Expand Down Expand Up @@ -381,7 +360,6 @@ function parseAst(type, ast, depComponents, sourceFilePath) {
if (callee.name === 'require') {
const args = node.arguments;
const { value } = args[0];
const valueExtname = path.extname(value);
if (value.indexOf('.') === 0) {
let importPath = path.resolve(path.dirname(sourceFilePath), value);
importPath = Util.resolveScriptPath(importPath);
Expand Down Expand Up @@ -412,14 +390,6 @@ function parseAst(type, ast, depComponents, sourceFilePath) {
}
const taroMiniAppFrameworkPath = taroMiniAppFramework;
switch (type) {
case PARSE_AST_TYPE.ENTRY:
node.body.push(
template(
`App(require('${taroMiniAppFrameworkPath}').default.createApp(${exportVariableName}))`,
babylonConfig
)()
);
break;
case PARSE_AST_TYPE.PAGE:
node.body.push(
template(
Expand Down Expand Up @@ -474,6 +444,7 @@ module.exports = async function(asset, opts) {
code: contents
})
);
// asset.contents = resCode;
const parseRes = parseAst(PARSE_AST_TYPE.PAGE, ast, pageDepComponents);
asset.contents = parseRes.code;
const wxmlAsset = new Asset(pageWXMLPath, outputPath, { virtual_file: true });
Expand Down
47 changes: 25 additions & 22 deletions src/plugin/handleJSONComponentDep.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@ const path = require('path');
module.exports = class HandleJSONComponentDep {
apply(mpb) {
mpb.hooks.beforeEmitFile.tapPromise('HandleJSONComponentDep', async (asset) => {
const key = asset.getMeta('mbp-scan-json-dep');
if (key) {
// const key = asset.getMeta('mbp-scan-json-dep');
// TODO 并不是所有JSON都要进行这个判定的,先通过usingComponents这个key来判定是否是依赖,但是有点硬核,后面想下有没有更好的办法,上面通过 meta的方式也不行,主要是在watch的时候如何对新的asset设置meta
if (/\.json$/.test(asset.outputFilePath) && asset.contents) {
const code = JSON.parse(asset.contents);
const componets = code[key];
if (componets) {
// TODO 这里需要支持 alias
await Promise.all(
Object.keys(componets).map((componentName) => {
let filePath = '',
src = componets[componentName];
if (src[0] === '/') {
filePath = path.resolve(mpb.src, `.${src}`);
} else if (src[0] === '.') {
filePath = path.resolve(asset.dir, src);
} else {
filePath = path.resolve(asset.dir, `./${src}`);
}
mpb.scan.addAssetByEXT(
filePath.replace(mpb.src, ''),
path.resolve(mpb.dest, path.relative(mpb.src, filePath))
);
})
);
if (code.usingComponents) {
const componets = code.usingComponents;
if (componets) {
// TODO 这里需要支持 alias
await Promise.all(
Object.keys(componets).map((componentName) => {
let filePath = '',
src = componets[componentName];
if (src[0] === '/') {
filePath = path.resolve(mpb.src, `.${src}`);
} else if (src[0] === '.') {
filePath = path.resolve(asset.dir, src);
} else {
filePath = path.resolve(asset.dir, `./${src}`);
}
mpb.scan.addAssetByEXT(
filePath.replace(mpb.src, ''),
path.resolve(mpb.dest, path.relative(mpb.src, filePath))
);
})
);
}
}
}
return asset;
Expand Down
88 changes: 49 additions & 39 deletions src/plugin/minifyPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,64 @@ const workerpool = require('workerpool');

const pool = workerpool.pool();

function minify(contents, outputFilePath) {
function minifyJS(contents) {
const UglifyJS = require('uglify-js');
const jsonminify = require('jsonminify');
const result = UglifyJS.minify(contents);
if (result.error) console.error('[MinifyPlugin]', result.error);
if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
return result.code;
}
function minifyWXML(contents) {
const htmlmin = require('html-minifier');
if (/\.js$/.test(outputFilePath)) {
const result = UglifyJS.minify(contents);
if (result.error) console.error('[MinifyPlugin]', result.error);
if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
return result.code;
}
if (/\.json$/.test(outputFilePath)) {
return jsonminify(contents).toString();
}
if (/\.wxml$/.test(outputFilePath)) {
return htmlmin.minify(contents, {
removeComments: true,
keepClosingSlash: true,
collapseWhitespace: true,
caseSensitive: true
});
}
return htmlmin.minify(contents, {
removeComments: true,
keepClosingSlash: true,
collapseWhitespace: true,
caseSensitive: true
});
}
function minifyJSON(contents) {
const jsonminify = require('jsonminify');
return jsonminify(contents).toString();
}

module.exports = class MinifyPlugin {
constructor() {
this.js = true;
this.wxml = true;
this.json = true;
}

apply(mpb) {
if (mpb.optimization.minimize) {
if (typeof mpb.optimization.minimize === 'object') {
this.js = mpb.optimization.minimize.js;
this.wxml = mpb.optimization.minimize.wxml;
this.json = mpb.optimization.minimize.json;
} else if (mpb.optimization.minimize === false) {
this.js = false;
this.wxml = false;
this.json = false;
}
mpb.hooks.beforeEmitFile.tapPromise('MinifyPlugin', async (asset) => {
if (asset.contents) {
// if (/\.js$/.test(asset.outputFilePath)) {
// const result = UglifyJS.minify(asset.contents);
// if (result.error) console.error('[MinifyPlugin]', result.error);
// if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
// asset.contents = result.code;
// } else if (/\.json$/.test(asset.outputFilePath)) {
// asset.contents = jsonminify(asset.contents).toString();
// } else if (/\.wxml$/.test(asset.outputFilePath)) {
// asset.contents = asset.contents = htmlmin.minify(asset.contents, {
// removeComments: true,
// keepClosingSlash: true,
// collapseWhitespace: true,
// caseSensitive: true
// });
// }
if (/\.(js|json|wxml)$/.test(asset.outputFilePath)) {
asset.contents = await pool.exec(minify, [
asset.contents,
asset.outputFilePath
]);
if (/\.js$/.test(asset.outputFilePath) && this.js) {
// const result = UglifyJS.minify(asset.contents);
// if (result.error) console.error('[MinifyPlugin]', result.error);
// if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
// asset.contents = result.code;
asset.contents = await pool.exec(minifyJS, [asset.contents]);
} else if (/\.json$/.test(asset.outputFilePath) && this.json) {
// asset.contents = jsonminify(asset.contents).toString();
asset.contents = await pool.exec(minifyJSON, [asset.contents]);
} else if (/\.wxml$/.test(asset.outputFilePath) && this.wxml) {
// asset.contents = asset.contents = htmlmin.minify(asset.contents, {
// removeComments: true,
// keepClosingSlash: true,
// collapseWhitespace: true,
// caseSensitive: true
// });
asset.contents = await pool.exec(minifyWXML, [asset.contents]);
}
}
return Promise.resolve();
Expand Down

0 comments on commit 6f2b02f

Please sign in to comment.