From 2fd2e0984a6a3096b8c24656483e7e728c11508a Mon Sep 17 00:00:00 2001 From: linrui Date: Thu, 5 Jul 2018 14:11:32 +0800 Subject: [PATCH 1/2] support asset from node_modules --- lib/template-compiler/modules/transform-require.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/template-compiler/modules/transform-require.js b/lib/template-compiler/modules/transform-require.js index b144265..567420c 100644 --- a/lib/template-compiler/modules/transform-require.js +++ b/lib/template-compiler/modules/transform-require.js @@ -38,9 +38,11 @@ function rewrite (attrsMap, name, fileOptions) { 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('/')}` From 2bc09f52d214f7236532a0a1e9fb58a4aac346f4 Mon Sep 17 00:00:00 2001 From: linrui Date: Tue, 10 Jul 2018 17:19:35 +0800 Subject: [PATCH 2/2] add error handler --- lib/template-compiler/index.js | 2 +- .../modules/transform-require.js | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/template-compiler/index.js b/lib/template-compiler/index.js index f45bd47..9544881 100644 --- a/lib/template-compiler/index.js +++ b/lib/template-compiler/index.js @@ -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 diff --git a/lib/template-compiler/modules/transform-require.js b/lib/template-compiler/modules/transform-require.js index 567420c..64c7b86 100644 --- a/lib/template-compiler/modules/transform-require.js +++ b/lib/template-compiler/modules/transform-require.js @@ -9,32 +9,32 @@ 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) @@ -46,15 +46,16 @@ function rewrite (attrsMap, name, fileOptions) { // 重写路径,为了避免重名,在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) })