Comparing version 1.0.2 to 1.0.3
52
index.js
@@ -10,9 +10,10 @@ 'use strict' | ||
const through = require('through2') | ||
const transpile = require('vue-template-es2015-compiler') | ||
var transpile = require('vue-template-es2015-compiler') | ||
let defaults = { | ||
sep: os.EOL, | ||
es2015: true, | ||
namespace: 'window.templates', | ||
extension: '.vue', | ||
commonjs: false, | ||
prefixStart: '', | ||
@@ -25,12 +26,25 @@ prefixIgnore: [], | ||
function toFunction(code) { | ||
return transpile('function render() {' + code + '}') | ||
} | ||
function gulpVue(name, config) { | ||
let options = extend({}, defaults, config || {}) | ||
let concat, fileName, firstFile | ||
let templateNamespace = `${options.namespace} = {}` | ||
/** | ||
* Adds a render function to the render output code. | ||
* When the es2015 option is set, it will produce cleaner | ||
* output that can be used with `is strict`. | ||
* @param {String} code - The compiled code. | ||
* @returns {String} - The final javascript template code. | ||
*/ | ||
function toFunction(code) { | ||
let output = `function r(){${code}}` | ||
if (options.es2015) return transpile(output) | ||
return output | ||
} | ||
let concat, fileName, firstFile, templateNamespace | ||
if (options.commonjs) { | ||
templateNamespace = '' | ||
} else templateNamespace = `${options.namespace}={}` | ||
function combineFiles(file, encoding, next) { | ||
@@ -45,3 +59,4 @@ if (!firstFile) { | ||
let baseName = path.basename(file.path, options.extension) | ||
// Replace hyphens with underscores in order to keep dot-notation. | ||
let baseName = path.basename(file.path, options.extension).replace('-', '_') | ||
let templateData = file.contents.toString('utf8') | ||
@@ -54,5 +69,7 @@ | ||
let templatePrefix = _path.slice(pathPrefixIndex + 1, _path.length - 1) | ||
// Ignore path components from prefixIgnore. | ||
templatePrefix = templatePrefix.filter((i) => { | ||
return !options.prefixIgnore.includes(i) | ||
}) | ||
let templateName = `${templatePrefix.join('_')}_${baseName}` | ||
@@ -65,3 +82,18 @@ let compiled = compiler.compile(templateData, options.vue) | ||
let jsTemplate = `${options.namespace}.${templateName}={render: ${toFunction(compiled.render)}, staticRenderFns: [${compiled.staticRenderFns.map(toFunction).join(',')}]}` | ||
// Generate a javascript file for direct usage in the browser, | ||
// using a global namespace. | ||
let jsTemplate | ||
if (options.commonjs) { | ||
jsTemplate = `module.exports.${templateName}={render:${toFunction(compiled.render, options)}` | ||
if (compiled.staticRenderFns.length) { | ||
jsTemplate += `,staticRenderFns:[${compiled.staticRenderFns.map(toFunction).join(',')}]}` | ||
} else jsTemplate += '}' | ||
} else { | ||
// Generate a commonjs module. | ||
jsTemplate = `${options.namespace}.${templateName}={r:${toFunction(compiled.render, options)}` | ||
if (compiled.staticRenderFns.length) { | ||
jsTemplate += `,s:[${compiled.staticRenderFns.map(toFunction).join(',')}]}` | ||
} else jsTemplate += '}' | ||
} | ||
concat.add(file.relative, jsTemplate, file.sourceMap) | ||
@@ -68,0 +100,0 @@ } catch (_error) { |
{ | ||
"name": "gulp-vuejs", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Fast and uncomplicated .vue file compiler for Gulp.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
12663
98