diff --git a/README.md b/README.md index 6a807d8..284f641 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,4 @@ const MpvuePlugin = require('webpack-mpvue-asset-plugin') }; ``` -bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。 +bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。 \ No newline at end of file diff --git a/index.js b/index.js index 3f210b1..86078ac 100644 --- a/index.js +++ b/index.js @@ -9,38 +9,47 @@ const getRelativePath = (filePath) => { return filePath } -const emitHandle = (compilation, callback) => { - Object.keys(compilation.entrypoints).forEach(key => { - const { chunks } = compilation.entrypoints[key] - 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)) + }else { + Object.keys(compilation.entrypoints).forEach(key => { + const { chunks } = compilation.entrypoints[key] + chunksHandle(chunks, compilation) + }) + } + callback() }