Skip to content

Commit 57ad0de

Browse files
authored
Merge pull request #41 from mpvue/develop
1.1.2
2 parents 1e71389 + a5c40f6 commit 57ad0de

File tree

5 files changed

+27
-30
lines changed

5 files changed

+27
-30
lines changed

lib/mp-compiler/index.js

+12-13
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')
@@ -27,10 +26,9 @@ let slotsHookAdded = false
2726

2827
// 调用 compiler 生成 wxml
2928
function genComponentWxml (compiled, options, emitFile, emitError, emitWarning) {
30-
options.components['slots'] = { src: 'slots', name: 'slots' }
29+
options.components['slots'] = { src: '/components/slots', name: 'slots' }
3130
const { code: wxmlCodeStr, compiled: cp, slots, importCode } = compiler.compileToWxml(compiled, options)
3231
const { mpErrors, mpTips } = cp
33-
3432
// 缓存 slots,延迟编译
3533
cacheSlots(slots, importCode)
3634

@@ -51,7 +49,7 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning)
5149
function createAppWxml (emitFile, resourcePath, rootComponent, context) {
5250
const { src } = getFileInfo(resourcePath) || {}
5351
const { name: componentName, filePath: wxmlSrc } = getCompNameAndSrc(context, rootComponent)
54-
const wxmlContent = genPageWxml(componentName, relative(`/${src}.wxml`, `/${wxmlSrc}`))
52+
const wxmlContent = genPageWxml(componentName, wxmlSrc)
5553
emitFile(`${src}.wxml`, wxmlContent)
5654
}
5755
// 更新全局组件时,需要重新生成wxml,用这个字段保存所有需要更新的页面及其参数
@@ -118,12 +116,13 @@ function compileWxml (compiled, html) {
118116
// 针对 .vue 单文件的脚本逻辑的处理
119117
// 处理出当前单文件组件的子组件依赖
120118
function compileMPScript (script, mpOptioins, moduleId) {
119+
const { resourcePath, options, resolve, context } = this
121120
const babelrc = getBabelrc(mpOptioins.globalBabelrc)
122121
let result, metadata
123122
let scriptContent = script.content
124123
const babelOptions = { extends: babelrc, plugins: [parseComponentsDeps] }
125124
if (script.src) { // 处理src
126-
const scriptpath = path.join(path.dirname(this.resourcePath), script.src)
125+
const scriptpath = path.join(path.dirname(resourcePath), script.src)
127126
scriptContent = fs.readFileSync(scriptpath).toString()
128127
}
129128
if (script.lang === 'ts') { // 处理ts
@@ -138,16 +137,16 @@ function compileMPScript (script, mpOptioins, moduleId) {
138137

139138
// 处理子组件的信息
140139
const components = {}
141-
const fileInfo = resolveTarget(this.resourcePath, this.options.entry)
140+
const fileInfo = resolveTarget(resourcePath, options.entry)
142141
if (originComponents) {
143-
resolveSrc(originComponents, components, this.resolve, this.context).then(() => {
144-
resolveComponent(this.resourcePath, fileInfo, importsMap, components, moduleId)
142+
resolveSrc(originComponents, components, resolve, context, options.context).then(() => {
143+
resolveComponent(resourcePath, fileInfo, importsMap, components, moduleId)
145144
}).catch(err => {
146145
console.error(err)
147-
resolveComponent(this.resourcePath, fileInfo, importsMap, components, moduleId)
146+
resolveComponent(resourcePath, fileInfo, importsMap, components, moduleId)
148147
})
149148
} else {
150-
resolveComponent(this.resourcePath, fileInfo, importsMap, components, moduleId)
149+
resolveComponent(resourcePath, fileInfo, importsMap, components, moduleId)
151150
}
152151

153152
return script
@@ -183,7 +182,7 @@ function compileMP (content, mpOptioins) {
183182

184183
// 解析全局组件的路径
185184
const components = {}
186-
resolveSrc(globalComps, components, resolve, context).then(() => {
185+
resolveSrc(globalComps, components, resolve, context, options.context).then(() => {
187186
handleResult(components)
188187
}).catch(err => {
189188
console.error(err)
@@ -220,13 +219,13 @@ function compileMP (content, mpOptioins) {
220219
return content
221220
}
222221

223-
function resolveSrc (originComponents, components, resolveFn, context) {
222+
function resolveSrc (originComponents, components, resolveFn, context, projectRoot) {
224223
return Promise.all(Object.keys(originComponents).map(k => {
225224
return new Promise((resolve, reject) => {
226225
resolveFn(context, originComponents[k], (err, realSrc) => {
227226
if (err) return reject(err)
228227
const com = covertCCVar(k)
229-
const { filePath, name } = getCompNameAndSrc(context, realSrc)
228+
const { filePath, name } = getCompNameAndSrc(projectRoot, realSrc)
230229
components[com] = { src: filePath, name }
231230
resolve()
232231
})

lib/mp-compiler/util.js

+3-11
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
}
@@ -88,7 +86,7 @@ function cacheSlots (slots, importCode) {
8886
importCodeCache[importCode] = importCode
8987
}
9088
function getSlots () {
91-
const allImportCode = Object.keys(importCodeCache).map(v => importCodeCache[v]).join('\n').replace('<import src="slots" />', '')
89+
const allImportCode = Object.keys(importCodeCache).map(v => importCodeCache[v]).join('\n').replace('<import src="/components/slots" />', '')
9290
const allSlots = Object.keys(slotsCache).map(v => slotsCache[v].code).join('\n')
9391
return allImportCode + allSlots
9492
}
@@ -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

+4-5
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

+7
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

+1-1
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.5",
44
"description": "mpvue single-file component loader for Webpack",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)