From bea45bf4bea89faae5e85654898446a4e4dd650a Mon Sep 17 00:00:00 2001 From: aorz Date: Tue, 17 Jul 2018 10:29:13 +0800 Subject: [PATCH 1/8] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=88=86?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .babelrc | 18 ++++++++++++++++++ .editorconfig | 9 +++++++++ .eslintrc.js | 37 +++++++++++++++++++++++++++++++++++++ index.js | 50 ++++++++++++++++++++------------------------------ package.json | 5 ++++- yarn.lock | 19 +++++++++++++++++++ 6 files changed, 107 insertions(+), 31 deletions(-) create mode 100644 .babelrc create mode 100644 .editorconfig create mode 100644 .eslintrc.js create mode 100644 yarn.lock diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..c06df4d --- /dev/null +++ b/.babelrc @@ -0,0 +1,18 @@ +{ + "presets": [ + ["env", { + "modules": false, + "targets": { + "browsers": ["> 1%", "last 2 versions", "not ie <= 8"] + } + }], + "stage-2" + ], + "plugins": ["transform-runtime"], + "env": { + "test": { + "presets": ["env", "stage-2"], + "plugins": ["istanbul"] + } + } +} diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9d08a1a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 2 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..aafb909 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,37 @@ +// http://eslint.org/docs/user-guide/configuring + +module.exports = { + root: true, + parser: 'babel-eslint', + parserOptions: { + sourceType: 'module' + }, + env: { + browser: false, + node: true, + es6: true + }, + // https://github.com/standard/standard/blob/master/docs/RULES-en.md + extends: 'standard', + // required to lint *.vue files + plugins: [ + 'html' + ], + // add your custom rules here + 'rules': { + // allow paren-less arrow functions + 'arrow-parens': 0, + // allow async-await + 'generator-star-spacing': 0, + // allow debugger during development + 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0 + }, + globals: { + App: true, + Page: true, + wx: true, + getApp: true, + getPage: true, + requirePlugin: true + } +} diff --git a/index.js b/index.js index 20cd398..221e400 100644 --- a/index.js +++ b/index.js @@ -1,39 +1,29 @@ +const path = require('path'); +const relative = require('relative'); + function MpvuePlugin() {} MpvuePlugin.prototype.apply = function(compiler) { - const {options: {entry, plugins}} = compiler; compiler.plugin('emit', function(compilation, callback) { - let commonsChunkNames = []; - // 获取所有的 chunk name - plugins.forEach(item => { - let { chunkNames } = item; - if (item.constructor.name === 'CommonsChunkPlugin' && chunkNames) { - commonsChunkNames = commonsChunkNames.concat(chunkNames); - } - }) - let pages = Object.keys(entry); - compilation.chunks.forEach(commonChunk => { - const { files, chunks: childChunks, name } = commonChunk; - let commonWxssFile = files.find(item => item.endsWith('.wxss')); - - if (commonsChunkNames.indexOf(name) > -1 && commonWxssFile) { - childChunks.forEach(item => { - let wxssFile = item.files.find(item => item.endsWith('.wxss')); - if (item.name === 'app' && wxssFile) { // 过滤 app - return; - } - try { - if (compilation.assets[wxssFile]) { - let wxss = compilation.assets[wxssFile].source(); - wxss = `@import "/${commonWxssFile}";\n${wxss}`; - compilation.assets[wxssFile].source = () => wxss; + Object.keys(compilation.entrypoints).forEach(key => { + const entry = compilation.entrypoints[key]; + const { chunks } = entry; + const entryChunk = chunks.pop(); + entryChunk.files.forEach(filePath => { + const extname = path.extname(filePath); + let content = compilation.assets[filePath].source(); + chunks.reverse().forEach(chunk => { + chunk.files.forEach(childFile => { + if (path.extname(childFile) === extname && compilation.assets[filePath]) { + content = extname === '.wxss' ? + `@import "${relative(filePath, childFile)}";\n${content}` + : `require("${relative(filePath, childFile)}");\n${content}`; } - } catch (error) { - console.error(error, wxssFile) - } + }) + compilation.assets[filePath].source = () => content; }) - } - }); + }) + }) callback(); }); }; diff --git a/package.json b/package.json index 8e00ba1..65028fe 100644 --- a/package.json +++ b/package.json @@ -22,5 +22,8 @@ "url": "https://github.com/mpvue/webpack-mpvue-asset-plugin/issues" }, "homepage": "https://github.com/mpvue/webpack-mpvue-asset-plugin#readme", - "description": "" + "description": "", + "dependencies": { + "relative": "^3.0.2" + } } diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..dffca42 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,19 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +isarray@1.0.0: + version "1.0.0" + resolved "http://r.npm.sankuai.com/isarray/download/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" + +isobject@^2.0.0: + version "2.1.0" + resolved "http://r.npm.sankuai.com/isobject/download/isobject-2.1.0.tgz#f065561096a3f1da2ef46272f815c840d87e0c89" + dependencies: + isarray "1.0.0" + +relative@^3.0.2: + version "3.0.2" + resolved "http://r.npm.sankuai.com/relative/download/relative-3.0.2.tgz#0dcd8ec54a5d35a3c15e104503d65375b5a5367f" + dependencies: + isobject "^2.0.0" From 44730927efb30f0c3bc17ca7cedf51fcd8cb8f45 Mon Sep 17 00:00:00 2001 From: aorz Date: Tue, 17 Jul 2018 13:34:30 +0800 Subject: [PATCH 2/8] add: readme --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..5506715 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# webpack-mpvue-plugin + +> mpvue 资源路径解析插件 + +## 使用示例: + +```js +const MpvuePlugin = require('webpack-mpvue-asset-plugin') +// webpack config +{ + entry: [], + output: { + path: path.resolve(__dirname, 'dist'), + filename: 'foo.bundle.js' + }, + plugins: [ + new MpvuePlugin() + ] +}; +``` From ba14a0eff47ba9eb5706206725126fee6c1fafbc Mon Sep 17 00:00:00 2001 From: aorz Date: Fri, 20 Jul 2018 20:23:57 +0800 Subject: [PATCH 3/8] =?UTF-8?q?fix:=20win=20=E4=B8=8B=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 10 ++++++---- package.json | 5 +++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 221e400..c7f8bd0 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const path = require('path'); +const upath = require('upath'); const relative = require('relative'); function MpvuePlugin() {} @@ -15,9 +16,10 @@ MpvuePlugin.prototype.apply = function(compiler) { chunks.reverse().forEach(chunk => { chunk.files.forEach(childFile => { if (path.extname(childFile) === extname && compilation.assets[filePath]) { - content = extname === '.wxss' ? - `@import "${relative(filePath, childFile)}";\n${content}` - : `require("${relative(filePath, childFile)}");\n${content}`; + const relativePath = upath.normalize(relative(filePath, childFile)) + content = extname === '.wxss' ? + `@import "${relativePath}";\n${content}` + : `require("${relativePath}");\n${content}`; } }) compilation.assets[filePath].source = () => content; @@ -28,4 +30,4 @@ MpvuePlugin.prototype.apply = function(compiler) { }); }; -module.exports = MpvuePlugin; \ No newline at end of file +module.exports = MpvuePlugin; diff --git a/package.json b/package.json index 65028fe..3fc1b27 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack-mpvue-asset-plugin", - "version": "0.0.2", + "version": "0.1.0", "main": "index.js", "directories": { "lib": "lib" @@ -24,6 +24,7 @@ "homepage": "https://github.com/mpvue/webpack-mpvue-asset-plugin#readme", "description": "", "dependencies": { - "relative": "^3.0.2" + "relative": "^3.0.2", + "upath": "^1.1.0" } } From c1ac8712570358bf1dedd0da81e560acc798d585 Mon Sep 17 00:00:00 2001 From: aorz Date: Fri, 20 Jul 2018 20:29:22 +0800 Subject: [PATCH 4/8] =?UTF-8?q?docs:=20=E6=B7=BB=E5=8A=A0=20issue=20?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5506715..6a807d8 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,5 @@ const MpvuePlugin = require('webpack-mpvue-asset-plugin') ] }; ``` + +bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。 From abc8b1e47fc4aa8bc05c32de968e960fe17f613c Mon Sep 17 00:00:00 2001 From: aorz Date: Tue, 17 Jul 2018 10:29:13 +0800 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=88=86?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .babelrc | 18 ++++++++++++++++++ .editorconfig | 9 +++++++++ .eslintrc.js | 37 +++++++++++++++++++++++++++++++++++++ index.js | 12 +++++++----- package.json | 5 ++++- yarn.lock | 19 +++++++++++++++++++ 6 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 .babelrc create mode 100644 .editorconfig create mode 100644 .eslintrc.js create mode 100644 yarn.lock diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..c06df4d --- /dev/null +++ b/.babelrc @@ -0,0 +1,18 @@ +{ + "presets": [ + ["env", { + "modules": false, + "targets": { + "browsers": ["> 1%", "last 2 versions", "not ie <= 8"] + } + }], + "stage-2" + ], + "plugins": ["transform-runtime"], + "env": { + "test": { + "presets": ["env", "stage-2"], + "plugins": ["istanbul"] + } + } +} diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9d08a1a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 2 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..aafb909 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,37 @@ +// http://eslint.org/docs/user-guide/configuring + +module.exports = { + root: true, + parser: 'babel-eslint', + parserOptions: { + sourceType: 'module' + }, + env: { + browser: false, + node: true, + es6: true + }, + // https://github.com/standard/standard/blob/master/docs/RULES-en.md + extends: 'standard', + // required to lint *.vue files + plugins: [ + 'html' + ], + // add your custom rules here + 'rules': { + // allow paren-less arrow functions + 'arrow-parens': 0, + // allow async-await + 'generator-star-spacing': 0, + // allow debugger during development + 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0 + }, + globals: { + App: true, + Page: true, + wx: true, + getApp: true, + getPage: true, + requirePlugin: true + } +} diff --git a/index.js b/index.js index bb20cd8..236a4e8 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,9 @@ +const path = require('path'); +const relative = require('relative'); + function MpvuePlugin() {} MpvuePlugin.prototype.apply = function(compiler) { - const {options: {entry, plugins}} = compiler; compiler.plugin('emit', function(compilation, callback) { let commonsChunkNames = []; // 获取所有的 chunk name @@ -15,7 +17,7 @@ MpvuePlugin.prototype.apply = function(compiler) { compilation.chunks.forEach(commonChunk => { const { files, chunks: childChunks, name } = commonChunk; let commonWxssFile = files.find(item => item.endsWith('.wxss')); - + if (commonsChunkNames.indexOf(name) > -1 && commonWxssFile) { childChunks.forEach(item => { let wxssFile = item.files.find(item => item.endsWith('.wxss')); @@ -30,10 +32,10 @@ MpvuePlugin.prototype.apply = function(compiler) { console.error(error, wxssFile) } }) - } - }); + }) + }) callback(); }); }; -module.exports = MpvuePlugin; \ No newline at end of file +module.exports = MpvuePlugin; diff --git a/package.json b/package.json index 505580c..386d620 100644 --- a/package.json +++ b/package.json @@ -22,5 +22,8 @@ "url": "https://github.com/mpvue/webpack-mpvue-asset-plugin/issues" }, "homepage": "https://github.com/mpvue/webpack-mpvue-asset-plugin#readme", - "description": "" + "description": "", + "dependencies": { + "relative": "^3.0.2" + } } diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..dffca42 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,19 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +isarray@1.0.0: + version "1.0.0" + resolved "http://r.npm.sankuai.com/isarray/download/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" + +isobject@^2.0.0: + version "2.1.0" + resolved "http://r.npm.sankuai.com/isobject/download/isobject-2.1.0.tgz#f065561096a3f1da2ef46272f815c840d87e0c89" + dependencies: + isarray "1.0.0" + +relative@^3.0.2: + version "3.0.2" + resolved "http://r.npm.sankuai.com/relative/download/relative-3.0.2.tgz#0dcd8ec54a5d35a3c15e104503d65375b5a5367f" + dependencies: + isobject "^2.0.0" From f6098620bf756cf01fa338c47ce8e1f1a1f47425 Mon Sep 17 00:00:00 2001 From: aorz Date: Tue, 17 Jul 2018 13:34:30 +0800 Subject: [PATCH 6/8] add: readme --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..5506715 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# webpack-mpvue-plugin + +> mpvue 资源路径解析插件 + +## 使用示例: + +```js +const MpvuePlugin = require('webpack-mpvue-asset-plugin') +// webpack config +{ + entry: [], + output: { + path: path.resolve(__dirname, 'dist'), + filename: 'foo.bundle.js' + }, + plugins: [ + new MpvuePlugin() + ] +}; +``` From ea8e10558984f1de8290c796494af4b5d6581559 Mon Sep 17 00:00:00 2001 From: aorz Date: Fri, 20 Jul 2018 20:23:57 +0800 Subject: [PATCH 7/8] =?UTF-8?q?fix:=20win=20=E4=B8=8B=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 44 ++++++++++++++++++-------------------------- package.json | 5 +++-- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index 236a4e8..c7f8bd0 100644 --- a/index.js +++ b/index.js @@ -1,36 +1,28 @@ const path = require('path'); +const upath = require('upath'); const relative = require('relative'); function MpvuePlugin() {} MpvuePlugin.prototype.apply = function(compiler) { compiler.plugin('emit', function(compilation, callback) { - let commonsChunkNames = []; - // 获取所有的 chunk name - plugins.forEach(item => { - let { chunkNames } = item; - if (item.constructor.name === 'CommonsChunkPlugin' && chunkNames) { - commonsChunkNames = commonsChunkNames.concat(chunkNames); - } - }) - let pages = Object.keys(entry); - compilation.chunks.forEach(commonChunk => { - const { files, chunks: childChunks, name } = commonChunk; - let commonWxssFile = files.find(item => item.endsWith('.wxss')); - - if (commonsChunkNames.indexOf(name) > -1 && commonWxssFile) { - childChunks.forEach(item => { - let wxssFile = item.files.find(item => item.endsWith('.wxss')); - if (item.name === 'app' && wxssFile) { // 过滤 app - return; - } - try { - let wxss = compilation.assets[wxssFile].source(); - wxss = `@import "/${commonWxssFile}";\n${wxss}`; - compilation.assets[wxssFile].source = () => wxss; - } catch (error) { - console.error(error, wxssFile) - } + Object.keys(compilation.entrypoints).forEach(key => { + const entry = compilation.entrypoints[key]; + const { chunks } = entry; + const entryChunk = chunks.pop(); + entryChunk.files.forEach(filePath => { + const extname = path.extname(filePath); + let content = compilation.assets[filePath].source(); + chunks.reverse().forEach(chunk => { + chunk.files.forEach(childFile => { + if (path.extname(childFile) === extname && compilation.assets[filePath]) { + const relativePath = upath.normalize(relative(filePath, childFile)) + content = extname === '.wxss' ? + `@import "${relativePath}";\n${content}` + : `require("${relativePath}");\n${content}`; + } + }) + compilation.assets[filePath].source = () => content; }) }) }) diff --git a/package.json b/package.json index 386d620..3fc1b27 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack-mpvue-asset-plugin", - "version": "0.0.1", + "version": "0.1.0", "main": "index.js", "directories": { "lib": "lib" @@ -24,6 +24,7 @@ "homepage": "https://github.com/mpvue/webpack-mpvue-asset-plugin#readme", "description": "", "dependencies": { - "relative": "^3.0.2" + "relative": "^3.0.2", + "upath": "^1.1.0" } } From c4dd0d2e257ca5fa7af488acc846e63f41982531 Mon Sep 17 00:00:00 2001 From: aorz Date: Fri, 20 Jul 2018 20:29:22 +0800 Subject: [PATCH 8/8] =?UTF-8?q?docs:=20=E6=B7=BB=E5=8A=A0=20issue=20?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5506715..6a807d8 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,5 @@ const MpvuePlugin = require('webpack-mpvue-asset-plugin') ] }; ``` + +bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。