Skip to content

Commit 0dbedd8

Browse files
authored
Merge pull request #6 from mpvue/develop
Develop
2 parents cf3210d + 325d429 commit 0dbedd8

File tree

6 files changed

+115
-32
lines changed

6 files changed

+115
-32
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+
}

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# webpack-mpvue-plugin
2+
3+
> mpvue 资源路径解析插件
4+
5+
## 使用示例:
6+
7+
```js
8+
const MpvuePlugin = require('webpack-mpvue-asset-plugin')
9+
// webpack config
10+
{
11+
entry: [],
12+
output: {
13+
path: path.resolve(__dirname, 'dist'),
14+
filename: 'foo.bundle.js'
15+
},
16+
plugins: [
17+
new MpvuePlugin()
18+
]
19+
};
20+
```
21+
22+
bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)

index.js

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,33 @@
1+
const path = require('path');
2+
const upath = require('upath');
3+
const relative = require('relative');
4+
15
function MpvuePlugin() {}
26

37
MpvuePlugin.prototype.apply = function(compiler) {
4-
const {options: {entry, plugins}} = compiler;
58
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-
compilation.chunks.forEach(commonChunk => {
15-
const { files, chunks: childChunks, name } = commonChunk;
16-
let commonWxssFile = files.find(item => item.endsWith('.wxss'));
17-
18-
if (commonsChunkNames.indexOf(name) > -1 && commonWxssFile) {
19-
childChunks.forEach(item => {
20-
let wxssFile = item.files.find(item => item.endsWith('.wxss'));
21-
if (item.name === 'app' && wxssFile) { // 过滤 app
22-
return;
23-
}
24-
try {
25-
if (compilation.assets[wxssFile]) {
26-
let wxss = compilation.assets[wxssFile].source();
27-
wxss = `@import "/${commonWxssFile}";\n${wxss}`;
28-
compilation.assets[wxssFile].source = () => wxss;
9+
Object.keys(compilation.entrypoints).forEach(key => {
10+
const entry = compilation.entrypoints[key];
11+
const { chunks } = entry;
12+
const entryChunk = chunks.pop();
13+
entryChunk.files.forEach(filePath => {
14+
const extname = path.extname(filePath);
15+
let content = compilation.assets[filePath].source();
16+
chunks.reverse().forEach(chunk => {
17+
chunk.files.forEach(childFile => {
18+
if (path.extname(childFile) === extname && compilation.assets[filePath]) {
19+
const relativePath = upath.normalize(relative(filePath, childFile))
20+
content = extname === '.wxss' ?
21+
`@import "${relativePath}";\n${content}`
22+
: `require("${relativePath}");\n${content}`;
2923
}
30-
} catch (error) {
31-
console.error(error, wxssFile)
32-
}
24+
})
25+
compilation.assets[filePath].source = () => content;
3326
})
34-
}
35-
});
27+
})
28+
})
3629
callback();
3730
});
3831
};
3932

40-
module.exports = MpvuePlugin;
33+
module.exports = MpvuePlugin;

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-mpvue-asset-plugin",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"main": "index.js",
55
"directories": {
66
"lib": "lib"
@@ -22,5 +22,9 @@
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+
"upath": "^1.1.0"
29+
}
2630
}

0 commit comments

Comments
 (0)