Skip to content

1.1.2 #41

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

Merged
merged 8 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
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
25 changes: 12 additions & 13 deletions lib/mp-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const babel = require('babel-core')
const path = require('path')
const fs = require('fs')
const deepEqual = require('deep-equal')
const relative = require('../utils/relative')

const { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents } = require('./parse')
const { parseComponentsDeps: parseComponentsDepsTs } = require('./parse-ts')
Expand All @@ -27,10 +26,9 @@ let slotsHookAdded = false

// 调用 compiler 生成 wxml
function genComponentWxml (compiled, options, emitFile, emitError, emitWarning) {
options.components['slots'] = { src: 'slots', name: 'slots' }
options.components['slots'] = { src: '/components/slots', name: 'slots' }
const { code: wxmlCodeStr, compiled: cp, slots, importCode } = compiler.compileToWxml(compiled, options)
const { mpErrors, mpTips } = cp

// 缓存 slots,延迟编译
cacheSlots(slots, importCode)

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

// 处理子组件的信息
const components = {}
const fileInfo = resolveTarget(this.resourcePath, this.options.entry)
const fileInfo = resolveTarget(resourcePath, options.entry)
if (originComponents) {
resolveSrc(originComponents, components, this.resolve, this.context).then(() => {
resolveComponent(this.resourcePath, fileInfo, importsMap, components, moduleId)
resolveSrc(originComponents, components, resolve, context, options.context).then(() => {
resolveComponent(resourcePath, fileInfo, importsMap, components, moduleId)
}).catch(err => {
console.error(err)
resolveComponent(this.resourcePath, fileInfo, importsMap, components, moduleId)
resolveComponent(resourcePath, fileInfo, importsMap, components, moduleId)
})
} else {
resolveComponent(this.resourcePath, fileInfo, importsMap, components, moduleId)
resolveComponent(resourcePath, fileInfo, importsMap, components, moduleId)
}

return script
Expand Down Expand Up @@ -183,7 +182,7 @@ function compileMP (content, mpOptioins) {

// 解析全局组件的路径
const components = {}
resolveSrc(globalComps, components, resolve, context).then(() => {
resolveSrc(globalComps, components, resolve, context, options.context).then(() => {
handleResult(components)
}).catch(err => {
console.error(err)
Expand Down Expand Up @@ -220,13 +219,13 @@ function compileMP (content, mpOptioins) {
return content
}

function resolveSrc (originComponents, components, resolveFn, context) {
function resolveSrc (originComponents, components, resolveFn, context, projectRoot) {
return Promise.all(Object.keys(originComponents).map(k => {
return new Promise((resolve, reject) => {
resolveFn(context, originComponents[k], (err, realSrc) => {
if (err) return reject(err)
const com = covertCCVar(k)
const { filePath, name } = getCompNameAndSrc(context, realSrc)
const { filePath, name } = getCompNameAndSrc(projectRoot, realSrc)
components[com] = { src: filePath, name }
resolve()
})
Expand Down
14 changes: 3 additions & 11 deletions lib/mp-compiler/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require('path')
const fs = require('fs')
const relative = require('../utils/relative')

const resolveSrc = require('../utils/resolve-src')
const pagesNameMap = Object.create(null)

function cacheFileInfo (resourcePath, ...arg) {
Expand All @@ -17,7 +16,7 @@ function getFileInfo (resourcePath) {
var hash = require('hash-sum')
const cache = Object.create(null)
function getCompNameAndSrc (context, file) {
const filePath = `${relative(context, file).replace(/^src\//, '')}.wxml`
const filePath = `/${resolveSrc(context, file)}.wxml`
if (!cache[file]) {
cache[file] = hash(file)
}
Expand All @@ -31,7 +30,6 @@ function getCompNameAndSrc (context, file) {
function getNameByFile (dir) {
// const arr = dir.match(/[pages?/components?]\/(.*?)(\/)/)
const arr = dir.match(/pages\/(.*?)\//)
// 兼容 win 下的路径格式不统一的问题
if (arr && arr[1]) {
return arr[1]
}
Expand Down Expand Up @@ -88,7 +86,7 @@ function cacheSlots (slots, importCode) {
importCodeCache[importCode] = importCode
}
function getSlots () {
const allImportCode = Object.keys(importCodeCache).map(v => importCodeCache[v]).join('\n').replace('<import src="slots" />', '')
const allImportCode = Object.keys(importCodeCache).map(v => importCodeCache[v]).join('\n').replace('<import src="/components/slots" />', '')
const allSlots = Object.keys(slotsCache).map(v => slotsCache[v].code).join('\n')
return allImportCode + allSlots
}
Expand Down Expand Up @@ -123,11 +121,6 @@ function getBabelrc (src) {
return ''
}

function getPathPrefix (src) {
const length = src.split('/').length - 1
return `${'../'.repeat(length)}`
}

const defaultStylePart = {
type: 'style',
content: '\n',
Expand All @@ -154,6 +147,5 @@ module.exports = {
getSlots,
htmlBeautify,
getBabelrc,
getPathPrefix,
getPageSrc
}
9 changes: 4 additions & 5 deletions lib/template-compiler/modules/transform-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')
var relative = require('../../utils/relative')
var resolveSrc = require('../../utils/resolve-src')

var defaultOptions = {
img: 'src',
Expand Down Expand Up @@ -42,10 +42,9 @@ function rewrite (attrsMap, name, fileOptions) {
if (firstChar === '.') {
var { resourcePath, outputPath, context } = fileOptions
var assetPath = path.resolve(resourcePath, '..', value)
// 资源路径, 为了分包,去掉了 src 目录
var toPath = relative(context, assetPath).replace(/^\/src\//, '')
attrsMap[name] = path.join(outputPath, toPath)
copyAsset(assetPath, attrsMap[name])
var toPath = resolveSrc(context, assetPath)
attrsMap[name] = `/${toPath}`
copyAsset(assetPath, path.join(outputPath, toPath))
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions lib/utils/resolve-src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const relative = require('relative')
const upath = require('upath')

// 获取文件路径,去掉 src 和 node_modules 目录
module.exports = function (...arv) {
return upath.normalize(relative(...arv)).replace(/^src\//, '').replace(/node_modules\//g, 'modules/')
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mpvue-loader",
"version": "1.1.0",
"version": "1.1.2-rc.5",
"description": "mpvue single-file component loader for Webpack",
"main": "index.js",
"repository": {
Expand Down