diff --git a/.eslintrc b/.eslintrc index d0dd64c..0a918b9 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,7 @@ { "root": true, - "extends": "vue" + "extends": "vue", + "env": { + "mocha": true + } } diff --git a/README.md b/README.md index 66fa388..9a42c15 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ -# vueify [![Build Status](https://circleci.com/gh/vuejs/vueify.svg?style=shield)](https://circleci.com/gh/vuejs/vueify) [![npm version](https://badge.fury.io/js/vueify.svg)](http://badge.fury.io/js/vueify) +# THIS REPOSITORY IS DEPRECATED + +> Note: We are concentrating our efforts on supporting webpack and rollup. + +## vueify [![Build Status](https://circleci.com/gh/vuejs/vueify.svg?style=shield)](https://circleci.com/gh/vuejs/vueify) [![npm version](https://badge.fury.io/js/vueify.svg)](http://badge.fury.io/js/vueify) > [Browserify](http://browserify.org/) transform for [Vue.js](http://vuejs.org/) components, with scoped CSS and component hot-reloading. +**NOTE: master branch now hosts version ^9.0, which only works with Vue ^2.0. Vueify 8.x which works with Vue 1.x is in the [8.x branch](https://github.com/vuejs/vueify/tree/8.x).** + This transform allows you to write your components in this format: ``` html @@ -269,7 +275,7 @@ npm install browserify-hmr --save-dev watchify -p browserify-hmr index.js -o bundle.js ``` -You can scaffold a hot-reload enabled project easily using `vue-cli` and the [this template](https://github.com/vuejs-templates/browserify-simple-2.0). +You can scaffold a hot-reload enabled project easily using `vue-cli` and the [this template](https://github.com/vuejs-templates/browserify-simple). ## CSS Extraction @@ -294,6 +300,14 @@ browserify('./main.js') This only works for vueify 9+. For Vue 1.x / vueify 8.x you can use [vueify-extract-css](https://github.com/rawcreative/vueify-extract-css). +## Building for Production + +When building for production, follow these steps to ensure smaller bundle size: + +1. Make sure `process.env.NODE_ENV === "production"`. This tells `vueify` to avoid including hot-reload related code. + +2. Apply a global [envify](https://github.com/hughsk/envify) transform to your bundle. This allows the minifier to strip out all the warnings in Vue's source code wrapped in env variable conditional blocks. + ## Compiler API The compiler API (originally `vue-component-compiler`) is also exposed: diff --git a/index.js b/index.js index 1bb2933..d789a83 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ module.exports = function vueify (file, options) { return through() } + compiler.loadConfig() compiler.applyConfig(options) compiler.applyConfig({ sourceMap: options._flags.debug diff --git a/lib/compiler.js b/lib/compiler.js index 4bf8f50..cde3d99 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -60,6 +60,10 @@ compiler.applyConfig = function (config) { } compiler.compile = function (content, filePath, cb) { + var isProduction = process.env.NODE_ENV === 'production' + var isServer = process.env.VUE_ENV === 'server' + var isTest = !!process.env.VUEIFY_TEST + // generate css scope id var id = 'data-v-' + genId(filePath) // parse the component into parts @@ -96,7 +100,7 @@ compiler.compile = function (content, filePath, cb) { var map = null // styles var style = resolvedParts.styles.join('\n') - if (style) { + if (style && !isServer) { // emit style compiler.emit('style', { file: filePath, @@ -126,16 +130,30 @@ compiler.compile = function (content, filePath, cb) { // template var template = resolvedParts.template if (template) { + if (!isProduction && !isServer) { + output += + 'if (__vue__options__.functional) {console.error("' + + '[vueify] functional components are not supported and ' + + 'should be defined in plain js files using render functions.' + + '")}\n' + } + var beforeLines + if (map) { + beforeLines = output.split(splitRE).length + } output += '__vue__options__.render = ' + template.render + '\n' + '__vue__options__.staticRenderFns = ' + template.staticRenderFns + '\n' + if (map) { + addTemplateMapping(content, parts, output, map, beforeLines) + } } // scoped CSS id if (hasScopedStyle) { output += '__vue__options__._scopeId = "' + id + '"\n' } // hot reload - if (process.env.NODE_ENV !== 'production' && !process.env.VUEIFY_TEST) { + if (!isProduction && !isTest && !isServer) { output += 'if (module.hot) {(function () {' + ' var hotAPI = require("' + hotReloadAPIPath + '")\n' + @@ -194,10 +212,29 @@ compiler.compile = function (content, filePath, cb) { }) } }) + map._hashedFilename = hashedFilename return map } } +function addTemplateMapping (content, parts, output, map, beforeLines) { + var afterLines = output.split(splitRE).length + var templateLine = content.slice(0, parts.template.start).split(splitRE).length + for (; beforeLines < afterLines; beforeLines++) { + map.addMapping({ + source: map._hashedFilename, + generated: { + line: beforeLines, + column: 0 + }, + original: { + line: templateLine, + column: 0 + } + }) + } +} + function processTemplate (part, filePath, parts) { if (!part) return Promise.resolve() var template = getContent(part, filePath) diff --git a/lib/compilers/babel.js b/lib/compilers/babel.js index d999bfa..be317b4 100644 --- a/lib/compilers/babel.js +++ b/lib/compilers/babel.js @@ -1,5 +1,6 @@ var fs = require('fs') var path = require('path') +var json = require('json5') var assign = require('object-assign') var ensureRequire = require('../ensure-require') @@ -16,27 +17,27 @@ var babelOptions = fs.existsSync(babelRcPath) function getBabelRc () { var rc try { - rc = JSON.parse(fs.readFileSync(babelRcPath, 'utf-8')) + rc = json.parse(fs.readFileSync(babelRcPath, 'utf-8')) } catch (e) { throw new Error('[vueify] Your .babelrc seems to be incorrectly formatted.') } return rc } -if (babelOptions === defaultBabelOptions) { - try { - ensureRequire('babel', ['babel-preset-es2015', 'babel-runtime', 'babel-plugin-transform-runtime']) - } catch (e) { - console.error(e.message) - console.error( - '\n^^^ You are seeing this because you are using Vueify\'s default babel ' + - 'configuration. You can override this with .babelrc or the babel option ' + - 'in vue.config.js.' - ) +module.exports = function (raw, cb, compiler, filePath) { + if ((compiler.options.babel || babelOptions) === defaultBabelOptions) { + try { + ensureRequire('babel', ['babel-preset-es2015', 'babel-runtime', 'babel-plugin-transform-runtime']) + } catch (e) { + console.error(e.message) + console.error( + '\n^^^ You are seeing this because you are using Vueify\'s default babel ' + + 'configuration. You can override this with .babelrc or the babel option ' + + 'in vue.config.js.' + ) + } } -} -module.exports = function (raw, cb, compiler, filePath) { try { var babel = require('babel-core') var options = assign({ diff --git a/lib/gen-id.js b/lib/gen-id.js index 25281ae..8c9621d 100644 --- a/lib/gen-id.js +++ b/lib/gen-id.js @@ -1,8 +1,8 @@ // utility for generating a uid for each component file // used in scoped CSS rewriting -var fileUid = 1 -var fileRegistry = Object.create(null) +var hash = require('hash-sum') +var cache = Object.create(null) module.exports = function genId (file) { - return fileRegistry[file] || (fileRegistry[file] = fileUid++) + return cache[file] || (cache[file] = hash(file)) } diff --git a/lib/style-rewriter.js b/lib/style-rewriter.js index 9881524..1195799 100644 --- a/lib/style-rewriter.js +++ b/lib/style-rewriter.js @@ -40,18 +40,32 @@ var addId = postcss.plugin('add-id', function () { */ module.exports = function (id, css, scoped, options) { - var key = id + '!!' + css + var key = id + '!!' + scoped + '!!' + css var val = cache.get(key) if (val) { return Promise.resolve(val) } else { - var plugins = options.postcss - ? options.postcss.slice() - : [] + var plugins = [] + var opts = {} + + if (options.postcss instanceof Array) { + plugins = options.postcss.slice() + } else if (options.postcss instanceof Object) { + plugins = options.postcss.plugins || [] + opts = options.postcss.options + } + // scoped css rewrite - if (scoped) { + // make sure the addId plugin is only pushed once + if (scoped && plugins.indexOf(addId) === -1) { plugins.push(addId) } + + // remove the addId plugin if the style block is not scoped + if (!scoped && plugins.indexOf(addId) !== -1) { + plugins.splice(plugins.indexOf(addId), 1) + } + // minification if (process.env.NODE_ENV === 'production') { plugins.push(require('cssnano')(assign({ @@ -60,10 +74,11 @@ module.exports = function (id, css, scoped, options) { } currentId = id return postcss(plugins) - .process(css) + .process(css, opts) .then(function (res) { cache.set(key, res.css) return res.css }) } } + diff --git a/lib/template-compiler.js b/lib/template-compiler.js index 3824c37..51314b9 100644 --- a/lib/template-compiler.js +++ b/lib/template-compiler.js @@ -1,5 +1,6 @@ var chalk = require('chalk') var vueCompiler = require('vue-template-compiler') +var transpile = require('vue-template-es2015-compiler') module.exports = function compileTemplate (template, compiler) { var compiled = vueCompiler.compile(template) @@ -17,5 +18,5 @@ module.exports = function compileTemplate (template, compiler) { } function toFunction (code) { - return 'function(){' + code + '}' + return transpile('function render () {' + code + '}') } diff --git a/package.json b/package.json index ecd6cb9..c1caee7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vueify", - "version": "9.1.0", + "version": "9.4.1", "description": "Vue component transform for Browserify", "main": "index.js", "repository": { @@ -31,8 +31,10 @@ "postcss-selector-parser": "^2.0.0", "source-map": "^0.5.6", "through": "^2.3.6", + "json5": "^0.5.1", "vue-hot-reload-api": "^2.0.1", - "vue-template-compiler": "^2.0.0-alpha.3" + "vue-template-compiler": "^2.0.0-alpha.8", + "vue-template-es2015-compiler": "^1.2.2" }, "devDependencies": { "babel-core": "^6.0.0", @@ -44,6 +46,7 @@ "coffee-script": "^1.10.0", "eslint": "^2.13.0", "eslint-config-vue": "^1.0.3", + "eslint-plugin-html": "^1.5.3", "jade": "^1.11.0", "jsdom": "^9.2.1", "less": "^2.5.1", diff --git a/plugins/extract-css.js b/plugins/extract-css.js index 740bb80..e3331e9 100644 --- a/plugins/extract-css.js +++ b/plugins/extract-css.js @@ -18,21 +18,16 @@ module.exports = function (b, opts) { outPath.write(css) outPath.end() } else if (typeof outPath === 'string') { - fs.writeFile(outPath, css) + fs.writeFile(outPath, css, function () {}) } }) }) - b.on('reset', listen) - listen() - - function listen () { - b.on('transform', function (tr, file) { - if (tr.vueify) { - tr.on('vueify-style', function (e) { - styles[e.file] = e.style - }) - } - }) - } + b.on('transform', function (tr, file) { + if (tr.vueify) { + tr.on('vueify-style', function (e) { + styles[e.file] = e.style + }) + } + }) } diff --git a/test/fixtures/style-export.vue b/test/fixtures/style-export.vue new file mode 100644 index 0000000..c3311de --- /dev/null +++ b/test/fixtures/style-export.vue @@ -0,0 +1,3 @@ + diff --git a/test/test.js b/test/test.js index 5081c87..dfcf92a 100644 --- a/test/test.js +++ b/test/test.js @@ -3,13 +3,13 @@ process.env.VUEIFY_TEST = true const fs = require('fs') const path = require('path') const expect = require('chai').expect -const hash = require('hash-sum') const rimraf = require('rimraf') const mkdirp = require('mkdirp') const browserify = require('browserify') const vueify = require('../index') const jsdom = require('jsdom') const vueCompiler = require('vue-template-compiler') +const transpile = require('vue-template-es2015-compiler') const genId = require('../lib/gen-id') const tempDir = path.resolve(__dirname, './temp') @@ -37,9 +37,21 @@ function test (file, assert) { }) } +function testCssExtract (file, assert) { + it(file, done => { + fs.writeFileSync(mockEntry, 'window.vueModule = require("../fixtures/' + file + '.vue")') + browserify(mockEntry) + .transform(vueify) + .plugin('./plugins/extract-css', { out: { write: assert, end: done }}) + .bundle((err, buf) => { + if (err) return done(err) + }) + }) +} + function assertRenderFn (options, template) { const compiled = vueCompiler.compile(template) - expect(options.render.toString()).to.equal('function (){' + compiled.render + '}') + expect(options.render.toString()).to.equal(transpile('function render() {' + compiled.render + '}')) } describe('vueify', () => { @@ -122,4 +134,8 @@ describe('vueify', () => { var id = 'data-v-' + genId(require.resolve('./fixtures/media-query.vue')) expect(style).to.contain('@media print {\n .foo[' + id + '] {\n color: #000;\n }\n}') }) + + testCssExtract('style-export', css => { + expect(css).to.equal('h2 {color: red;}') + }) })