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

增加对~开头资源路径处理 #32

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/template-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function (html) {
var defaultModules = [transformRequire(options.transformToRequire, {
outputPath: this.options.output.path,
resourcePath: this.resourcePath
})]
}, this.emitError)]

var userModules = vueOptions.compilerModules || options.compilerModules
// for HappyPack cross-process use cases
Expand Down
25 changes: 14 additions & 11 deletions lib/template-compiler/modules/transform-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,53 @@ var defaultOptions = {
image: 'xlink:href'
}

module.exports = (userOptions, fileOptions) => {
module.exports = (userOptions, fileOptions, emitError) => {
var options = userOptions
? Object.assign({}, defaultOptions, userOptions)
: defaultOptions

return {
postTransformNode: node => {
transform(node, options, fileOptions)
transform(node, options, fileOptions, emitError)
}
}
}

function transform (node, options, fileOptions) {
function transform (node, options, fileOptions, emitError) {
for (var tag in options) {
if (node.tag === tag && node.attrs) {
var attributes = options[tag]
if (typeof attributes === 'string') {
rewrite(node.attrsMap, attributes, fileOptions)
rewrite(node.attrsMap, attributes, fileOptions, emitError)
} else if (Array.isArray(attributes)) {
attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions))
attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions, emitError))
}
}
}
}

function rewrite (attrsMap, name, fileOptions) {
function rewrite (attrsMap, name, fileOptions, emitError) {
var value = attrsMap[name]
if (value) {
var firstChar = value.charAt(0)
if (firstChar === '.') {
if (firstChar === '.' || firstChar === '~') {
// 资源路径
var assetPath = path.resolve(path.dirname(fileOptions.resourcePath), value)
var assetPath = firstChar === '.'
? path.resolve(path.dirname(fileOptions.resourcePath), value)
: path.resolve(process.cwd(), 'node_modules', value.slice(1))
// 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里
var assetOutputPath = path.join('copy-asset', path.relative(process.cwd(), assetPath).replace(/^src/, ''))
attrsMap[name] = `/${assetOutputPath.split(path.sep).join('/')}`
copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath))
copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath), emitError)
}
}
}

function copyAsset (from, to) {
function copyAsset (from, to, emitError) {
var readStream = fs.createReadStream(from)
readStream.on('error', emitError)
mkdirp(path.dirname(to), err => {
if (err) console.error(err)
if (err) emitError(err)
var writeStream = fs.createWriteStream(to)
readStream.pipe(writeStream)
})
Expand Down