From ec131ab8b3d5796ea02b53437c878374591f7b56 Mon Sep 17 00:00:00 2001 From: aorz Date: Fri, 20 Apr 2018 19:24:04 +0800 Subject: [PATCH 1/4] update mpvue-template-compiler version --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index dc82f46..37ec5ab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mpvue-loader", - "version": "1.0.11", + "version": "1.0.12", "description": "mpvue single-file component loader for Webpack", "main": "index.js", "repository": { @@ -67,7 +67,7 @@ }, "peerDependencies": { "css-loader": "*", - "mpvue-template-compiler": "^1.0.7" + "mpvue-template-compiler": "^1.0.9" }, "devDependencies": { "babel-core": "^6.25.0", @@ -90,7 +90,7 @@ "marked": "^0.3.6", "memory-fs": "^0.4.1", "mkdirp": "^0.5.1", - "mpvue-template-compiler": "^1.0.7", + "mpvue-template-compiler": "^1.0.9", "mocha": "^3.4.2", "node-libs-browser": "^2.0.0", "normalize-newline": "^3.0.0", From f3387e07d377b1f0349f7b4096435504a5af5992 Mon Sep 17 00:00:00 2001 From: aorz Date: Fri, 27 Apr 2018 22:03:07 +0800 Subject: [PATCH 2/4] new version 1.0.13 --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 37ec5ab..af1fc8d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mpvue-loader", - "version": "1.0.12", + "version": "1.0.13", "description": "mpvue single-file component loader for Webpack", "main": "index.js", "repository": { @@ -67,7 +67,7 @@ }, "peerDependencies": { "css-loader": "*", - "mpvue-template-compiler": "^1.0.9" + "mpvue-template-compiler": "^1.0.10" }, "devDependencies": { "babel-core": "^6.25.0", @@ -90,7 +90,7 @@ "marked": "^0.3.6", "memory-fs": "^0.4.1", "mkdirp": "^0.5.1", - "mpvue-template-compiler": "^1.0.9", + "mpvue-template-compiler": "^1.0.10", "mocha": "^3.4.2", "node-libs-browser": "^2.0.0", "normalize-newline": "^3.0.0", From 033f7d47b849a8d9800736771f1737caf802220d Mon Sep 17 00:00:00 2001 From: linrui Date: Sun, 29 Apr 2018 20:29:16 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=BC=95=E5=85=A5=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/template-compiler/index.js | 5 ++- .../modules/transform-require.js | 39 +++++++++++++------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/template-compiler/index.js b/lib/template-compiler/index.js index 9dfacd8..3f0ba06 100644 --- a/lib/template-compiler/index.js +++ b/lib/template-compiler/index.js @@ -16,7 +16,10 @@ module.exports = function (html) { var vueOptions = this.options.__vueOptions__ || {} var options = loaderUtils.getOptions(this) || {} - var defaultModules = [transformRequire(options.transformToRequire)] + var defaultModules = [transformRequire(options.transformToRequire, { + outputPath: this.options.output.path, + resourcePath: this.resourcePath + })] var userModules = vueOptions.compilerModules || options.compilerModules // for HappyPack cross-process use cases if (typeof userModules === 'string') { diff --git a/lib/template-compiler/modules/transform-require.js b/lib/template-compiler/modules/transform-require.js index 7d9be15..788f3a1 100644 --- a/lib/template-compiler/modules/transform-require.js +++ b/lib/template-compiler/modules/transform-require.js @@ -1,36 +1,40 @@ // vue compiler module for transforming `:` to `require` +var fs = require('fs') +var path = require('path') +var mkdirp = require('mkdirp') + var defaultOptions = { img: 'src', image: 'xlink:href' } -module.exports = userOptions => { +module.exports = (userOptions, fileOptions) => { var options = userOptions ? Object.assign({}, defaultOptions, userOptions) : defaultOptions return { postTransformNode: node => { - transform(node, options) + transform(node, options, fileOptions) } } } -function transform (node, options) { +function transform (node, options, fileOptions) { for (var tag in options) { if (node.tag === tag && node.attrs) { var attributes = options[tag] if (typeof attributes === 'string') { - node.attrs.some(attr => rewrite(attr, attributes)) + node.attrs.some(attr => rewrite(attr, node.attrsMap, attributes, fileOptions)) } else if (Array.isArray(attributes)) { - attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item))) + attributes.forEach(item => node.attrs.some(attr => rewrite(attr, node.attrsMap, item, fileOptions))) } } } } -function rewrite (attr, name) { +function rewrite (attr, attrsMap, name, fileOptions) { if (attr.name === name) { var value = attr.value var isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"' @@ -38,13 +42,24 @@ function rewrite (attr, name) { return } var firstChar = value.charAt(1) - if (firstChar === '.' || firstChar === '~') { - if (firstChar === '~') { - var secondChar = value.charAt(2) - value = '"' + value.slice(secondChar === '/' ? 3 : 2) - } - attr.value = `require(${value})` + if (firstChar === '.') { + // 资源路径 + var assetPath = path.join(path.dirname(fileOptions.resourcePath), value.slice(1, -1)) + // 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里 + var assetOutputPath = path.join('copy-asset', path.relative(process.cwd(), assetPath).replace(/^src/, '')) + attrsMap[name] = `/${assetOutputPath}` + // 拷贝资源 + copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath)) } return true } } + +function copyAsset (from, to) { + var readStream = fs.createReadStream(from) + mkdirp(path.dirname(to), err => { + if (err) console.error(err) + var writeStream = fs.createWriteStream(to) + readStream.pipe(writeStream) + }) +} From 9409b863320e40182d5fc53f97c385c87d53ebbd Mon Sep 17 00:00:00 2001 From: linrui Date: Mon, 30 Apr 2018 12:18:54 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9base64=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/transform-require.js | 49 ++++++++++++------- package.json | 5 +- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/lib/template-compiler/modules/transform-require.js b/lib/template-compiler/modules/transform-require.js index 788f3a1..1059211 100644 --- a/lib/template-compiler/modules/transform-require.js +++ b/lib/template-compiler/modules/transform-require.js @@ -3,10 +3,12 @@ var fs = require('fs') var path = require('path') var mkdirp = require('mkdirp') +var mime = require('mime') var defaultOptions = { img: 'src', - image: 'xlink:href' + image: 'xlink:href', + limit: 10 * 1024 } module.exports = (userOptions, fileOptions) => { @@ -26,32 +28,43 @@ function transform (node, options, fileOptions) { if (node.tag === tag && node.attrs) { var attributes = options[tag] if (typeof attributes === 'string') { - node.attrs.some(attr => rewrite(attr, node.attrsMap, attributes, fileOptions)) + rewrite(node.attrsMap, attributes, fileOptions, options.limit) } else if (Array.isArray(attributes)) { - attributes.forEach(item => node.attrs.some(attr => rewrite(attr, node.attrsMap, item, fileOptions))) + attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions, options.limit)) } } } } -function rewrite (attr, attrsMap, name, fileOptions) { - if (attr.name === name) { - var value = attr.value - var isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"' - if (!isStatic) { - return - } - var firstChar = value.charAt(1) +function rewrite (attrsMap, name, fileOptions, limit) { + var value = attrsMap[name] + if (value) { + var firstChar = value.charAt(0) if (firstChar === '.') { // 资源路径 - var assetPath = path.join(path.dirname(fileOptions.resourcePath), value.slice(1, -1)) - // 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里 - var assetOutputPath = path.join('copy-asset', path.relative(process.cwd(), assetPath).replace(/^src/, '')) - attrsMap[name] = `/${assetOutputPath}` - // 拷贝资源 - copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath)) + var assetPath = path.join(path.dirname(fileOptions.resourcePath), value) + // 小于limit的资源转base64 + var str = assetToBase64(assetPath, limit) + if (str) { + attrsMap[name] = `data:${mime.getType(assetPath) || ''};base64,${str}` + } else { + // 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里 + var assetOutputPath = path.join('copy-asset', path.relative(process.cwd(), assetPath).replace(/^src/, '')) + attrsMap[name] = `/${assetOutputPath}` + copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath)) + } + } + } +} + +function assetToBase64 (assetPath, limit) { + try { + var buffer = fs.readFileSync(assetPath) + if (buffer.length <= limit) { + return buffer.toString('base64') } - return true + } catch (err) { + console.error('ReadFile Error:' + err) } } diff --git a/package.json b/package.json index af1fc8d..d8890bf 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,8 @@ "js-beautify": "^1.6.14", "loader-utils": "^1.1.0", "lru-cache": "^4.1.1", + "mime": "^2.3.1", + "mkdirp": "^0.5.1", "postcss": "^6.0.6", "postcss-load-config": "^1.1.0", "postcss-selector-parser": "^2.0.0", @@ -89,9 +91,8 @@ "lint-staged": "^4.0.2", "marked": "^0.3.6", "memory-fs": "^0.4.1", - "mkdirp": "^0.5.1", - "mpvue-template-compiler": "^1.0.10", "mocha": "^3.4.2", + "mpvue-template-compiler": "^1.0.10", "node-libs-browser": "^2.0.0", "normalize-newline": "^3.0.0", "null-loader": "^0.1.1",