Vue component transform, compiler only with no subcompilers
npm install @paulcbetts/vueify html
// app.vue
{{msg}}
`
You can also mix preprocessor languages in the component file:
` vue
// app.vue
h1(class="red") {{msg}}
`
And you can import using the src attribute:
` html
`
Under the hood, the transform will:
- extract the styles, compile them and insert them with the insert-css module.
- extract the template, compile it and add it to your exported options.
You can require() other stuff in the
`
vueify
If you are using in Node:
`
js
`
var fs = require("fs")
var browserify = require('browserify')
var vueify = require('vueify')
browserify('./main.js')
.transform(vueify)
.bundle()
.pipe(fs.createWriteStream("bundle.js"))
NODE_ENV
Building for Production
Make sure to have the environment variable set to "production" when building for production! This strips away unnecessary code (e.g. hot-reload) for smaller bundle size.
gulp --production
If you are using Gulp, note that does not affect vueify; you still need to explicitly set NODE_ENV=production.
`
ES2015 with Babel
Vueify is pre-configured to work with Babel. Simply install Babel-related dependencies:
bash
`
npm install\
babel-core\
babel-preset-es2015\
--save-dev
.babelrc
Then create a :
`
json
`
{
"presets": ["es2015"]
}
.vue
And voila! You can now write ES2015 in your files. Note if you want to use ES2015 on normal .js files, you will also need babelify.
babel
You can also configure babel with the field in vue.config.js, which will take the highest priority.
npm install stylus --save-dev
Enabling Other Pre-Processors
For other pre-processors, you also need to install the corresponding node modules to enable the compilation. e.g. to get stylus compiled in your Vue components, do .
node-sass
These are the preprocessors supported by vueify out of the box:
- stylus
- less
- scss (via , use sass in config section)
coffee
- jade
- pug
- coffee-script (use in config section)
vue.config.js
PostCSS
Vueify uses PostCSS for scoped CSS rewrite. You can also provide your own PostCSS plugins! See config section below for an example.
Configuring Options
Create a file at where your build command is run (usually the root level of your project):
`
js
`
module.exports = {
// configure a built-in compiler
sass: {
includePaths: [...]
},
// provide your own postcss plugins
postcss: [...],
// register custom compilers
customCompilers: {
// for tags with lang="ts"
ts: function (content, cb, compiler, filePath) {
// content: content extracted from lang="ts" blocks
// cb: the callback to call when you're done compiling
// compiler: the vueify compiler instance
// filePath: the path for the file being compiled
//
// compile some TypeScript... and when you're done:
cb(null, result)
}
}
}
`
Example using custom PostCSS plugin:
js
`
var cssnext = require('cssnext')
module.exports = {
postcss: [cssnext()]
}
vueify
Alternatively, if you are using in Node and don't want to create a vue.config.js file:
`
js
`
var fs = require("fs")
var browserify = require('browserify')
var vueify = require('vueify')
// apply custom config
vueify.compiler.applyConfig({
// ...same as in vue.config.js
})
browserify('./main.js')
.transform(vueify)
.bundle()
.pipe(fs.createWriteStream("bundle.js"))
vueify
Or simply pass configuration object to (in Node) (for instance to set sass search paths as in the following example):
`
js
`
var fs = require("fs")
var browserify = require('browserify')
var vueify = require('vueify')
browserify('./main.js')
.transform(vueify, {
sass: {
includePaths: [...]
},
// ...same as in vue.config.js
})
.bundle()
.pipe(fs.createWriteStream("bundle.js"))
Scoped CSS
When a
hi
`
`
Into the following:
html
`
hi
$3
1. You can include both scoped and non-scoped styles in the same component.
2. The following will be affected by both the parent's scoped CSS and the child's scoped CSS:
- A child component's root node
- Content inserted to a child component via
`
Hot Reload
To enable hot component reloading, you need to install the browserify-hmr plugin:
bash
`
npm install browserify-hmr --save-dev
watchify -p browserify-hmr index.js -o bundle.js
vue-cli
You can scaffold a hot-reload enabled project easily using and the this template.
CSS Extraction
By default, the CSS in each component is injected into the page using a