diff --git a/index.js b/index.js index 358f774..3f210b1 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,13 @@ const path = require('path') const upath = require('upath') const relative = require('relative') +const getRelativePath = (filePath) => { + if (!/^\.(\.)?\//.test(filePath)) { + filePath = `./${filePath}` + } + return filePath +} + const emitHandle = (compilation, callback) => { Object.keys(compilation.entrypoints).forEach(key => { const { chunks } = compilation.entrypoints[key] @@ -16,14 +23,16 @@ const emitHandle = (compilation, callback) => { 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 (extname === '.js') { + relativePath = getRelativePath(relativePath) } if (/^(\.wxss)|(\.ttss)|(\.acss)|(\.css)$/.test(extname)) { - content = `@import "${relativePath}"\n${content}` - } else { + relativePath = getRelativePath(relativePath) + content = `@import "${relativePath}";\n${content}` + } else if (!(/^\.map$/.test(extname))) { content = `require("${relativePath}")\n${content}` } } diff --git a/package.json b/package.json index 2936fa1..3eebea9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack-mpvue-asset-plugin", - "version": "0.1.3", + "version": "2.0.0", "main": "index.js", "directories": { "lib": "lib"