From 509c430e294dc2c45da7ae0701350be4228c2bf1 Mon Sep 17 00:00:00 2001 From: nopast <490964186@qq.com> Date: Thu, 7 Mar 2019 11:46:53 +0800 Subject: [PATCH 1/5] webpack4 support --- README.md | 10 ++++++++++ index.js | 3 +-- package.json | 13 +++++++------ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6a807d8..55e372e 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,13 @@ const MpvuePlugin = require('webpack-mpvue-asset-plugin') ``` bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。 + +本仓库是 fork 自 [webpack-mpvue-asset-plugin](https://github.com/mpvue/webpack-mpvue-asset-plugin) 修改而来,主要更新到支持 webpack4。 + +bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。 + +## 安装 + +```bash +npm install webpack4-mpvue-asset-plugin --save-dev +``` \ No newline at end of file diff --git a/index.js b/index.js index 3f210b1..5d17d6b 100644 --- a/index.js +++ b/index.js @@ -10,8 +10,7 @@ const getRelativePath = (filePath) => { } const emitHandle = (compilation, callback) => { - Object.keys(compilation.entrypoints).forEach(key => { - const { chunks } = compilation.entrypoints[key] + compilation.entrypoints.forEach(({chunks}) => { const entryChunk = chunks.pop() entryChunk.files.forEach(filePath => { diff --git a/package.json b/package.json index 3eebea9..16e9741 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "webpack-mpvue-asset-plugin", - "version": "2.0.0", + "name": "webpack4-mpvue-asset-plugin", + "version": "2.0.1", "main": "index.js", "directories": { "lib": "lib" @@ -8,20 +8,21 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "author": "rememberwz ", + "author": "nopast", "license": "MIT", "devDependencies": {}, "repository": { "type": "git", - "url": "git+https://github.com/mpvue/webpack-mpvue-asset-plugin.git" + "url": "https://github.com/BugKun/webpack-mpvue-asset-plugin/tree/feature/webpack4" }, "keywords": [ - "mpvue" + "mpvue", + "webpack4" ], "bugs": { "url": "https://github.com/mpvue/webpack-mpvue-asset-plugin/issues" }, - "homepage": "https://github.com/mpvue/webpack-mpvue-asset-plugin#readme", + "homepage": "https://github.com/BugKun/webpack-mpvue-asset-plugin/tree/feature/webpack4#readme", "description": "", "dependencies": { "relative": "^3.0.2", From b4635309294b3a7a1160b857b7391586a3bca459 Mon Sep 17 00:00:00 2001 From: nopast <490964186@qq.com> Date: Fri, 8 Mar 2019 14:38:21 +0800 Subject: [PATCH 2/5] fallback readme and package.json --- README.md | 12 +----------- package.json | 13 ++++++------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 55e372e..284f641 100644 --- a/README.md +++ b/README.md @@ -19,14 +19,4 @@ const MpvuePlugin = require('webpack-mpvue-asset-plugin') }; ``` -bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。 - -本仓库是 fork 自 [webpack-mpvue-asset-plugin](https://github.com/mpvue/webpack-mpvue-asset-plugin) 修改而来,主要更新到支持 webpack4。 - -bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。 - -## 安装 - -```bash -npm install webpack4-mpvue-asset-plugin --save-dev -``` \ No newline at end of file +bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。 \ No newline at end of file diff --git a/package.json b/package.json index 16e9741..3eebea9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "webpack4-mpvue-asset-plugin", - "version": "2.0.1", + "name": "webpack-mpvue-asset-plugin", + "version": "2.0.0", "main": "index.js", "directories": { "lib": "lib" @@ -8,21 +8,20 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "author": "nopast", + "author": "rememberwz ", "license": "MIT", "devDependencies": {}, "repository": { "type": "git", - "url": "https://github.com/BugKun/webpack-mpvue-asset-plugin/tree/feature/webpack4" + "url": "git+https://github.com/mpvue/webpack-mpvue-asset-plugin.git" }, "keywords": [ - "mpvue", - "webpack4" + "mpvue" ], "bugs": { "url": "https://github.com/mpvue/webpack-mpvue-asset-plugin/issues" }, - "homepage": "https://github.com/BugKun/webpack-mpvue-asset-plugin/tree/feature/webpack4#readme", + "homepage": "https://github.com/mpvue/webpack-mpvue-asset-plugin#readme", "description": "", "dependencies": { "relative": "^3.0.2", From 08209672fbd7f38b3eb4367d1c2773b6b100a1db Mon Sep 17 00:00:00 2001 From: nopast <490964186@qq.com> Date: Fri, 8 Mar 2019 16:56:21 +0800 Subject: [PATCH 3/5] webpack3 support --- index.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5d17d6b..145510c 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ const getRelativePath = (filePath) => { } const emitHandle = (compilation, callback) => { - compilation.entrypoints.forEach(({chunks}) => { + function chunksHandle(chunks) { const entryChunk = chunks.pop() entryChunk.files.forEach(filePath => { @@ -39,7 +39,17 @@ const emitHandle = (compilation, callback) => { assetFile.source = () => content }) }) - }) + } + + if(compilation.entrypoints instanceof Map) { + compilation.entrypoints.forEach(({chunks}) => chunksHandle(chunks)) + }else { + Object.keys(compilation.entrypoints).forEach(key => { + const { chunks } = compilation.entrypoints[key] + chunksHandle(chunks) + }) + } + callback() } From cab72f4b83e1b916dd2fa79653a5eb8521c5a0f8 Mon Sep 17 00:00:00 2001 From: nopast <490964186@qq.com> Date: Fri, 8 Mar 2019 17:02:05 +0800 Subject: [PATCH 4/5] format --- index.js | 60 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index 145510c..f93e435 100644 --- a/index.js +++ b/index.js @@ -9,44 +9,44 @@ const getRelativePath = (filePath) => { return filePath } -const emitHandle = (compilation, callback) => { - function chunksHandle(chunks) { - const entryChunk = chunks.pop() - - entryChunk.files.forEach(filePath => { - const assetFile = compilation.assets[filePath] - const extname = path.extname(filePath) - let content = assetFile.source() - - chunks.reverse().forEach(chunk => { - chunk.files.forEach(subFile => { - if (path.extname(subFile) === extname && assetFile) { - let relativePath = upath.normalize(relative(filePath, subFile)) - - // 百度小程序 js 引用不支持绝对路径,改为相对路径 - if (extname === '.js') { - relativePath = getRelativePath(relativePath) - } - - if (/^(\.wxss)|(\.ttss)|(\.acss)|(\.css)$/.test(extname)) { - relativePath = getRelativePath(relativePath) - content = `@import "${relativePath}";\n${content}` - } else if (!(/^\.map$/.test(extname))) { - content = `require("${relativePath}")\n${content}` - } +const chunksHandle = (chunks, compilation) => { + const entryChunk = chunks.pop() + + entryChunk.files.forEach(filePath => { + const assetFile = compilation.assets[filePath] + const extname = path.extname(filePath) + let content = assetFile.source() + + chunks.reverse().forEach(chunk => { + chunk.files.forEach(subFile => { + if (path.extname(subFile) === extname && assetFile) { + let relativePath = upath.normalize(relative(filePath, subFile)) + + // 百度小程序 js 引用不支持绝对路径,改为相对路径 + if (extname === '.js') { + relativePath = getRelativePath(relativePath) + } + + if (/^(\.wxss)|(\.ttss)|(\.acss)|(\.css)$/.test(extname)) { + relativePath = getRelativePath(relativePath) + content = `@import "${relativePath}";\n${content}` + } else if (!(/^\.map$/.test(extname))) { + content = `require("${relativePath}")\n${content}` } - }) - assetFile.source = () => content + } }) + assetFile.source = () => content }) - } + }) +} +const emitHandle = (compilation, callback) => { if(compilation.entrypoints instanceof Map) { - compilation.entrypoints.forEach(({chunks}) => chunksHandle(chunks)) + compilation.entrypoints.forEach(({chunks}) => chunksHandle(chunks, compilation)) }else { Object.keys(compilation.entrypoints).forEach(key => { const { chunks } = compilation.entrypoints[key] - chunksHandle(chunks) + chunksHandle(chunks, compilation) }) } From 5485b84fd0315efee7a8e7acbe5e09cbfe0334e5 Mon Sep 17 00:00:00 2001 From: nopast <490964186@qq.com> Date: Fri, 10 May 2019 11:17:33 +0800 Subject: [PATCH 5/5] bug fixed --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index f93e435..86078ac 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,7 @@ const chunksHandle = (chunks, compilation) => { relativePath = getRelativePath(relativePath) content = `@import "${relativePath}";\n${content}` } else if (!(/^\.map$/.test(extname))) { - content = `require("${relativePath}")\n${content}` + content = `require("${relativePath}");\n${content}` } } })