Skip to content

Commit

Permalink
feat: 1.支持mock文件更新之后自动重新编译;2.修复页面分包js重复分离;3.优化dev模式的构建速度
Browse files Browse the repository at this point in the history
  • Loading branch information
jimwmg committed Feb 4, 2020
1 parent 55578e9 commit 7190a98
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 35 deletions.
2 changes: 1 addition & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
"feat", "fix", "docs", "style", "refactor", "test", "chore", "revert"
]],
'subject-full-stop': [0, 'never'],
'subject-case': [0, 'never']
'subject-case': [0, 'never'],
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
"lint-staged": {
"packages/**/*.js": "eslint --fix"
}
}
}
7 changes: 4 additions & 3 deletions packages/chameleon-tool/commanders/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ exports.getBuildPromise = async function (media, type) {
}
resolve();
})
})["catch"](e => {
})['catch'](e => {
let message = `clear file error! please remove direction ${outputpath} by yourself!`
cml.log.error(message);
throw new Error(e)
Expand All @@ -50,7 +50,8 @@ exports.getBuildPromise = async function (media, type) {
compiler.watch({
// watchOptions 示例
aggregateTimeout: 300,
poll: undefined
poll: undefined,
ignored: /node_modules/
}, (err, stats) => {

if (type === 'weex') {
Expand Down Expand Up @@ -335,6 +336,6 @@ exports.createConfigJson = function() {
})

cml.event.emit('config-json', result);

/* eslint-disable-next-line */
fse.outputFileSync(configJsonPath, JSON.stringify(result, '', 4))
}
24 changes: 15 additions & 9 deletions packages/chameleon-tool/commanders/web/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = function({webpackConfig, options, compiler}) {
if (compiler) {

if (options.hot === true) {
/* eslint-disable-next-line */
var hotMiddleware = require('webpack-hot-middleware')(compiler, {
heartbeat: 9000,
noInfo: true,
Expand All @@ -53,6 +54,7 @@ module.exports = function({webpackConfig, options, compiler}) {
}
if (cml.config.get().templateType === 'html') {
// handle fallback for HTML5 history API
/* eslint-disable-next-line */
app.use(require('connect-history-api-fallback')({
index: `${utils.getEntryName()}.html`
}))
Expand All @@ -61,9 +63,9 @@ module.exports = function({webpackConfig, options, compiler}) {

// serve pure static assets
var staticPath = webpackConfig.output.path;
app.use(express["static"](staticPath));
app.use(express['static'](staticPath));
let dist = path.join(cml.projectRoot, 'dist');
app.use(express["static"](dist));
app.use(express['static'](dist));

if (compiler && cml.config.get().templateType === 'smarty') {
// php-cgi
Expand All @@ -81,7 +83,8 @@ module.exports = function({webpackConfig, options, compiler}) {
let first = true;
compiler.watch({
aggregateTimeout: 300,
poll: undefined
poll: undefined,
ignored: /node_modules/
}, (err, stats) => {
if (first) {
startServer();
Expand All @@ -106,10 +109,10 @@ module.exports = function({webpackConfig, options, compiler}) {
let subpath = '';
// 未启动web端编译
if (!compiler) {
subpath = `web_empty.html`;
subpath = 'web_empty.html';
fse.copyFileSync(path.join(cml.root, 'configs/web_empty.html'), path.join(cml.utils.getDevServerPath(), 'web_empty.html'))
}
uri += `preview.html`;
uri += 'preview.html';
var entry = utils.getEntryName();
var jsbundle = `weex/${entry}.js`;
let staticParams = { jsbundle, subpath, buildType: cml.activePlatform };
Expand All @@ -131,13 +134,13 @@ module.exports = function({webpackConfig, options, compiler}) {
from: `/web/static/js/${cml.config.get().projectName}_(.+).js`,
to: `http://${config.ip}:${port}/static/js/${cml.config.get().projectName}.js`
}, {
from: `/web/static/js/manifest_(.+).js`,
from: '/web/static/js/manifest_(.+).js',
to: `http://${config.ip}:${port}/static/js/manifest.js`
}, {
from: `/web/static/js/vender_(.+).js`,
from: '/web/static/js/vender_(.+).js',
to: `http://${config.ip}:${port}/static/js/vender.js`
}, {
from: `/web/static/css/vender_(.+).css`,
from: '/web/static/css/vender_(.+).css',
to: `http://${config.ip}:${port}/static/css/vender.css`
}, {
from: `/web/static/css/${cml.config.get().projectName}_(.+).css`,
Expand All @@ -156,7 +159,9 @@ module.exports = function({webpackConfig, options, compiler}) {
if (maplist) {
maplist.forEach((item) => {
if (new RegExp(item.from).test(fullUrl)) {
/* eslint-disable-next-line */
var srcObj = url.parse(fullUrl);
/* eslint-disable-next-line */
var desObj = url.parse(item.to);
rOptions.host = desObj.host;
rOptions.hostname = desObj.hostname;
Expand All @@ -182,7 +187,8 @@ module.exports = function({webpackConfig, options, compiler}) {
function updateServerTpl() {
let serverPath = cml.utils.getDevServerPath();
let serverVersionFile = path.resolve(serverPath, '.server/server-tpl-verison.json');
let tplVersion = require(tpl["package"]).version;
/* eslint-disable-next-line */
let tplVersion = require(tpl['package']).version;
if (cml.utils.isFile(serverVersionFile)) {
let version = require(serverVersionFile).version;
if (version === tplVersion) {
Expand Down
7 changes: 6 additions & 1 deletion packages/chameleon-tool/configs/getCommonConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const config = require('./config.js');
const ChameleonErrorsWebpackPlugin = require('chameleon-errors-webpack-plugin');
const fs = require('fs');
const cmlUtils = require('chameleon-tool-utils');

const ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');
module.exports = function (options) {
let {
type,
Expand Down Expand Up @@ -193,6 +193,11 @@ module.exports = function (options) {
}
domain[key] = domain[key] + '__DEV_SPLIT__' + key;
})
commonConfig.plugins.push(
new ExtraWatchWebpackPlugin({
dirs: [path.join(cml.projectRoot, 'mock/api')]
})
)
}
// 兼容旧版api
commonConfig.plugins.push(new webpack.DefinePlugin({
Expand Down
47 changes: 27 additions & 20 deletions packages/chameleon-tool/configs/plugins/miniAppSubPkg.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const glob = require('glob');
const cmlUtils = require('chameleon-tool-utils');

/*
小程序分包:1 微信限制主包不能引用分包的组件 2 分包可以引用主包的组件 3 分包不能引用分包的组件
*/
Expand All @@ -11,7 +9,9 @@ class MiniAppSubPkg {
this.cmlType = options.cmlType;
this.root = options.root;
}

apply(compiler) {
/* eslint-disable-next-line */
let self = this;

if (compiler.hooks) {
Expand Down Expand Up @@ -43,28 +43,26 @@ class MiniAppSubPkg {
});
let newPages = pages.filter((item) => !subPagesArr.includes(item));
appJson.pages = newPages;
/* eslint-disable-next-line */
compilation.assets['app.json']._value = JSON.stringify(appJson, '', 4)// 重写app.json文件;
//处理分包的页面js
const subCompsArr = self.getSubpkgComp(subPagesRoot,compilation);
self.handleJsContent(subPagesArr,compilation);
self.handleJsContent(subCompsArr,compilation);
// 处理分包的页面js
const subCompsArr = self.getSubpkgComp(subPagesRoot, compilation);
// 修复重复对于页面的js分离操作,subCompsArr里面会进行分离
// self.handleJsContent(subPagesArr,compilation);
self.handleJsContent(subCompsArr, compilation);
callback();
}
}
getSubpkgComp(subPagesRoot,compilation){

getSubpkgComp(subPagesRoot, compilation) {
let assetsKeys = Object.keys(compilation.assets) || [];
let subPageCompKeys = assetsKeys.filter((assetsKey) => {
return subPagesRoot.some((item) => {
return assetsKey.startsWith(item) && assetsKey.endsWith('js');
});
});
subPageCompKeys = subPageCompKeys.map((k) => {
return k.replace('\.js','');
});
let subPageCompKeys = assetsKeys.filter((assetsKey) => subPagesRoot.some((item) => assetsKey.startsWith(item) && assetsKey.endsWith('js')));
subPageCompKeys = subPageCompKeys.map((k) => k.replace('\.js', ''));
return subPageCompKeys

}
handleJsContent(pathArr,compilation){

handleJsContent(pathArr, compilation) {
// 第二步将subpage中的js文件拷贝到pages/subpage中的js文件中; outputFileSync
// 第三步删除static/js 中的subpage的js文件;removeSync
let regStatic = /require\(.*?static\/js\/.*?\)\(\)/;
Expand All @@ -80,13 +78,22 @@ class MiniAppSubPkg {
let staticContent = compilation.assets[subPageStaticJSPath] && compilation.assets[subPageStaticJSPath].source();
if (staticContent) {
staticContent = staticContent.replace(regMainfest, '');
staticContent = staticContent.replace(/;$/,'()')
staticContent = staticContent.replace(/;$/, '()')
delete compilation.assets[subPageStaticJSPath];
// 注意 assets中的key configurable与否
}
let finalContent = content + '\n' + staticContent;

compilation.assets[subPageJSPath] && (compilation.assets[subPageJSPath]._value = finalContent);
// 修复分包依赖的js资源更新,打包结果不更新
if (compilation.assets[subPageJSPath]) {
compilation.assets[subPageJSPath] = {
source() {
return finalContent;
},
size() {
return finalContent.length;
}
}
}
});

}
Expand Down
22 changes: 22 additions & 0 deletions packages/chameleon-tool/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/chameleon-tool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"css-loader": "0.28.11",
"eventsource-polyfill": "0.9.6",
"express": "4.16.3",
"extra-watch-webpack-plugin": "^1.0.3",
"file-loader": "1.1.11",
"fs-extra": "6.0.1",
"glob": "7.1.2",
Expand Down

0 comments on commit 7190a98

Please sign in to comment.