diff --git a/index.js b/index.js index 858f55b..358f774 100644 --- a/index.js +++ b/index.js @@ -1,41 +1,41 @@ -const path = require('path'); -const upath = require('upath'); -const relative = require('relative'); +const path = require('path') +const upath = require('upath') +const relative = require('relative') -function MpvuePlugin() {} +const emitHandle = (compilation, callback) => { + Object.keys(compilation.entrypoints).forEach(key => { + const { chunks } = compilation.entrypoints[key] + const entryChunk = chunks.pop() -MpvuePlugin.prototype.apply = function(compiler) { - compiler.plugin('emit', function(compilation, callback) { - 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]) { - let relativePath = upath.normalize(relative(filePath, childFile)) + entryChunk.files.forEach(filePath => { + const assetFile = compilation.assets[filePath] + const extname = path.extname(filePath) + let content = assetFile.source() - // 百度小程序js引用不支持绝对路径,改为相对路径 - if (extname === '.js' && !/^\.(\.)?\//.test(relativePath)) { - relativePath = `./${relativePath}`; - } + 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' && !/^\.(\.)?\//.test(relativePath)) { + relativePath = `./${relativePath}` + } - if (/^(\.wxss)|(\.css)$/.test(extname)) { - content = `@import "${relativePath}";\n${content}`; - } else { - content = `require("${relativePath}");\n${content}`; - } + if (/^(\.wxss)|(\.ttss)|(\.acss)|(\.css)$/.test(extname)) { + content = `@import "${relativePath}"\n${content}` + } else { + content = `require("${relativePath}")\n${content}` } - }) - compilation.assets[filePath].source = () => content; + } }) + assetFile.source = () => content }) }) - callback(); - }); -}; + }) + callback() +} + +function MpvuePlugin() {} +MpvuePlugin.prototype.apply = compiler => compiler.plugin('emit', emitHandle) -module.exports = MpvuePlugin; +module.exports = MpvuePlugin diff --git a/package.json b/package.json index 38ec498..2936fa1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack-mpvue-asset-plugin", - "version": "0.1.2", + "version": "0.1.3", "main": "index.js", "directories": { "lib": "lib"