Skip to content

Commit 66f7d73

Browse files
authored
Merge pull request #38 from mpvue/wz/fix-img-path-error
过滤 node_modules 路径
2 parents cd718a1 + 0b88b47 commit 66f7d73

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

lib/mp-compiler/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const babel = require('babel-core')
55
const path = require('path')
66
const fs = require('fs')
77
const deepEqual = require('deep-equal')
8-
const relative = require('../utils/relative')
98

109
const { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents } = require('./parse')
1110
const { parseComponentsDeps: parseComponentsDepsTs } = require('./parse-ts')
@@ -51,7 +50,7 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning)
5150
function createAppWxml (emitFile, resourcePath, rootComponent, context) {
5251
const { src } = getFileInfo(resourcePath) || {}
5352
const { name: componentName, filePath: wxmlSrc } = getCompNameAndSrc(context, rootComponent)
54-
const wxmlContent = genPageWxml(componentName, relative(`/${src}.wxml`, `/${wxmlSrc}`))
53+
const wxmlContent = genPageWxml(componentName, wxmlSrc)
5554
emitFile(`${src}.wxml`, wxmlContent)
5655
}
5756
// 更新全局组件时,需要重新生成wxml,用这个字段保存所有需要更新的页面及其参数
@@ -118,12 +117,13 @@ function compileWxml (compiled, html) {
118117
// 针对 .vue 单文件的脚本逻辑的处理
119118
// 处理出当前单文件组件的子组件依赖
120119
function compileMPScript (script, mpOptioins, moduleId) {
120+
const { resourcePath, options, resolve, context } = this
121121
const babelrc = getBabelrc(mpOptioins.globalBabelrc)
122122
let result, metadata
123123
let scriptContent = script.content
124124
const babelOptions = { extends: babelrc, plugins: [parseComponentsDeps] }
125125
if (script.src) { // 处理src
126-
const scriptpath = path.join(path.dirname(this.resourcePath), script.src)
126+
const scriptpath = path.join(path.dirname(resourcePath), script.src)
127127
scriptContent = fs.readFileSync(scriptpath).toString()
128128
}
129129
if (script.lang === 'ts') { // 处理ts
@@ -138,16 +138,16 @@ function compileMPScript (script, mpOptioins, moduleId) {
138138

139139
// 处理子组件的信息
140140
const components = {}
141-
const fileInfo = resolveTarget(this.resourcePath, this.options.entry)
141+
const fileInfo = resolveTarget(resourcePath, options.entry)
142142
if (originComponents) {
143-
resolveSrc(originComponents, components, this.resolve, this.context).then(() => {
144-
resolveComponent(this.resourcePath, fileInfo, importsMap, components, moduleId)
143+
resolveSrc(originComponents, components, resolve, context, options.context).then(() => {
144+
resolveComponent(resourcePath, fileInfo, importsMap, components, moduleId)
145145
}).catch(err => {
146146
console.error(err)
147-
resolveComponent(this.resourcePath, fileInfo, importsMap, components, moduleId)
147+
resolveComponent(resourcePath, fileInfo, importsMap, components, moduleId)
148148
})
149149
} else {
150-
resolveComponent(this.resourcePath, fileInfo, importsMap, components, moduleId)
150+
resolveComponent(resourcePath, fileInfo, importsMap, components, moduleId)
151151
}
152152

153153
return script
@@ -183,7 +183,7 @@ function compileMP (content, mpOptioins) {
183183

184184
// 解析全局组件的路径
185185
const components = {}
186-
resolveSrc(globalComps, components, resolve, context).then(() => {
186+
resolveSrc(globalComps, components, resolve, context, options.context).then(() => {
187187
handleResult(components)
188188
}).catch(err => {
189189
console.error(err)
@@ -220,13 +220,13 @@ function compileMP (content, mpOptioins) {
220220
return content
221221
}
222222

223-
function resolveSrc (originComponents, components, resolveFn, context) {
223+
function resolveSrc (originComponents, components, resolveFn, context, projectRoot) {
224224
return Promise.all(Object.keys(originComponents).map(k => {
225225
return new Promise((resolve, reject) => {
226226
resolveFn(context, originComponents[k], (err, realSrc) => {
227227
if (err) return reject(err)
228228
const com = covertCCVar(k)
229-
const { filePath, name } = getCompNameAndSrc(context, realSrc)
229+
const { filePath, name } = getCompNameAndSrc(projectRoot, realSrc)
230230
components[com] = { src: filePath, name }
231231
resolve()
232232
})

lib/mp-compiler/util.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const path = require('path')
22
const fs = require('fs')
3-
const relative = require('../utils/relative')
4-
3+
const resolveSrc = require('../utils/resolve-src')
54
const pagesNameMap = Object.create(null)
65

76
function cacheFileInfo (resourcePath, ...arg) {
@@ -17,7 +16,7 @@ function getFileInfo (resourcePath) {
1716
var hash = require('hash-sum')
1817
const cache = Object.create(null)
1918
function getCompNameAndSrc (context, file) {
20-
const filePath = `${relative(context, file).replace(/^src\//, '')}.wxml`
19+
const filePath = `/${resolveSrc(context, file)}.wxml`
2120
if (!cache[file]) {
2221
cache[file] = hash(file)
2322
}
@@ -31,7 +30,6 @@ function getCompNameAndSrc (context, file) {
3130
function getNameByFile (dir) {
3231
// const arr = dir.match(/[pages?/components?]\/(.*?)(\/)/)
3332
const arr = dir.match(/pages\/(.*?)\//)
34-
// 兼容 win 下的路径格式不统一的问题
3533
if (arr && arr[1]) {
3634
return arr[1]
3735
}
@@ -123,11 +121,6 @@ function getBabelrc (src) {
123121
return ''
124122
}
125123

126-
function getPathPrefix (src) {
127-
const length = src.split('/').length - 1
128-
return `${'../'.repeat(length)}`
129-
}
130-
131124
const defaultStylePart = {
132125
type: 'style',
133126
content: '\n',
@@ -154,6 +147,5 @@ module.exports = {
154147
getSlots,
155148
htmlBeautify,
156149
getBabelrc,
157-
getPathPrefix,
158150
getPageSrc
159151
}

lib/template-compiler/modules/transform-require.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var fs = require('fs')
44
var path = require('path')
55
var mkdirp = require('mkdirp')
6-
var relative = require('../../utils/relative')
6+
var resolveSrc = require('../../utils/resolve-src')
77

88
var defaultOptions = {
99
img: 'src',
@@ -42,10 +42,9 @@ function rewrite (attrsMap, name, fileOptions) {
4242
if (firstChar === '.') {
4343
var { resourcePath, outputPath, context } = fileOptions
4444
var assetPath = path.resolve(resourcePath, '..', value)
45-
// 资源路径, 为了分包,去掉了 src 目录
46-
var toPath = relative(context, assetPath).replace(/^\/src\//, '')
47-
attrsMap[name] = path.join(outputPath, toPath)
48-
copyAsset(assetPath, attrsMap[name])
45+
var toPath = resolveSrc(context, assetPath)
46+
attrsMap[name] = `/${toPath}`
47+
copyAsset(assetPath, path.join(outputPath, toPath))
4948
}
5049
}
5150
}

lib/utils/resolve-src.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const relative = require('relative')
2+
const upath = require('upath')
3+
4+
// 获取文件路径,去掉 src 和 node_modules 目录
5+
module.exports = function (...arv) {
6+
return upath.normalize(relative(...arv)).replace(/^src\//, '').replace(/node_modules\//g, 'modules/')
7+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpvue-loader",
3-
"version": "1.1.0",
3+
"version": "1.1.2-rc.3",
44
"description": "mpvue single-file component loader for Webpack",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)