diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index 2938960..e7aae85 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -108,11 +108,24 @@ function compileWxml (compiled, html) { resourcePath: this.resourcePath, context: this.options.context, rootComponent: null, - compiled, html + compiled, + html }) }) } +function hadlerSrc (alias, src) { + const keys = Object.keys(alias) + + for (const key of keys) { + if (src.indexOf(key) === 0) { + const scriptSrc = src.slice(key.length) + return path.join(alias[key], scriptSrc) + } + } + return src +} + // 针对 .vue 单文件的脚本逻辑的处理 // 处理出当前单文件组件的子组件依赖 function compileMPScript (script, mpOptioins, moduleId) { @@ -122,7 +135,12 @@ function compileMPScript (script, mpOptioins, moduleId) { let scriptContent = script.content const babelOptions = { extends: babelrc, plugins: [parseComponentsDeps] } if (script.src) { // 处理src - const scriptpath = path.join(path.dirname(resourcePath), script.src) + var scriptSrc = script.src + if (options.resolve && options.resolve.alias) { + scriptSrc = hadlerSrc(options.resolve.alias, script.src) + } + const scriptpath = path.resolve(path.dirname(resourcePath), scriptSrc) + scriptContent = fs.readFileSync(scriptpath).toString() } if (script.lang === 'ts') { // 处理ts