Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加script src对于webpack别名的支持 #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/mp-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down