Skip to content

Commit c7cd54b

Browse files
committed
feat: 支持分包
1 parent de697e6 commit c7cd54b

File tree

5 files changed

+88
-31
lines changed

5 files changed

+88
-31
lines changed

.babelrc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"presets": [
3+
["env", {
4+
"modules": false,
5+
"targets": {
6+
"browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
7+
}
8+
}],
9+
"stage-2"
10+
],
11+
"plugins": ["transform-runtime"],
12+
"env": {
13+
"test": {
14+
"presets": ["env", "stage-2"],
15+
"plugins": ["istanbul"]
16+
}
17+
}
18+
}

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

.eslintrc.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// http://eslint.org/docs/user-guide/configuring
2+
3+
module.exports = {
4+
root: true,
5+
parser: 'babel-eslint',
6+
parserOptions: {
7+
sourceType: 'module'
8+
},
9+
env: {
10+
browser: false,
11+
node: true,
12+
es6: true
13+
},
14+
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
15+
extends: 'standard',
16+
// required to lint *.vue files
17+
plugins: [
18+
'html'
19+
],
20+
// add your custom rules here
21+
'rules': {
22+
// allow paren-less arrow functions
23+
'arrow-parens': 0,
24+
// allow async-await
25+
'generator-star-spacing': 0,
26+
// allow debugger during development
27+
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0
28+
},
29+
globals: {
30+
App: true,
31+
Page: true,
32+
wx: true,
33+
getApp: true,
34+
getPage: true,
35+
requirePlugin: true
36+
}
37+
}

index.js

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
1+
const path = require('path');
2+
const relative = require('relative');
3+
14
function MpvuePlugin() {}
25

36
MpvuePlugin.prototype.apply = function(compiler) {
4-
const {options: {entry, plugins}} = compiler;
57
compiler.plugin('emit', function(compilation, callback) {
6-
let commonsChunkNames = [];
7-
// 获取所有的 chunk name
8-
plugins.forEach(item => {
9-
let { chunkNames } = item;
10-
if (item.constructor.name === 'CommonsChunkPlugin' && chunkNames) {
11-
commonsChunkNames = commonsChunkNames.concat(chunkNames);
12-
}
13-
})
14-
let pages = Object.keys(entry);
15-
compilation.chunks.forEach(commonChunk => {
16-
const { files, chunks: childChunks, name } = commonChunk;
17-
let commonWxssFile = files.find(item => item.endsWith('.wxss'));
18-
19-
if (commonsChunkNames.indexOf(name) > -1 && commonWxssFile) {
20-
childChunks.forEach(item => {
21-
let wxssFile = item.files.find(item => item.endsWith('.wxss'));
22-
if (item.name === 'app' && wxssFile) { // 过滤 app
23-
return;
24-
}
25-
try {
26-
if (compilation.assets[wxssFile]) {
27-
let wxss = compilation.assets[wxssFile].source();
28-
wxss = `@import "/${commonWxssFile}";\n${wxss}`;
29-
compilation.assets[wxssFile].source = () => wxss;
8+
Object.keys(compilation.entrypoints).forEach(key => {
9+
const entry = compilation.entrypoints[key];
10+
const { chunks } = entry;
11+
const entryChunk = chunks.pop();
12+
entryChunk.files.forEach(filePath => {
13+
const extname = path.extname(filePath);
14+
let content = compilation.assets[filePath].source();
15+
chunks.reverse().forEach(chunk => {
16+
chunk.files.forEach(childFile => {
17+
if (path.extname(childFile) === extname && compilation.assets[filePath]) {
18+
content = extname === '.wxss' ?
19+
`@import "${relative(filePath, childFile)}";\n${content}`
20+
: `require("${relative(filePath, childFile)}");\n${content}`;
3021
}
31-
} catch (error) {
32-
console.error(error, wxssFile)
33-
}
22+
})
23+
compilation.assets[filePath].source = () => content;
3424
})
35-
}
36-
});
25+
})
26+
})
3727
callback();
3828
});
3929
};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
"url": "https://github.com/mpvue/webpack-mpvue-asset-plugin/issues"
2323
},
2424
"homepage": "https://github.com/mpvue/webpack-mpvue-asset-plugin#readme",
25-
"description": ""
25+
"description": "",
26+
"dependencies": {
27+
"relative": "^3.0.2"
28+
}
2629
}

0 commit comments

Comments
 (0)