1 of 26

Webpack + Vue Loader : Recipe for supercharged Vue

Hemant Rai

2 of 26

A good tool improves the way you work.

A great tool improves the way you think.

Jeff Duntemann

3 of 26

Hemant Rai

@hemantisme

thisDOTname

4 of 26

Recap

5 of 26

Script tags

Big .js file

IIFE

Modules

ESM

01

02

03

04

05

6 of 26

Dependency Managers [npm, bower]

Minimization [uglifyjs, jsmin]

Module Systems [CommonJs, AMD, ES6 Modules]

Preprocessing [PostCSS]

Assets Handling [DataURL]

Transpilers [coffee-script, babel]

Module Loaders [require.js, jspm]

Image Compressing [imagemin]

Task Runner [grunt, gulp, make]

Templating [jade, handlebars]

Module Bundler [browserify, webpack]

7 of 26

Webpack, Loaders & Plugins

8 of 26

Webpack

9 of 26

10 of 26

Vue CLI

11 of 26

configureWebpack

// vue.config.js

module.exports = {

configureWebpack: {

plugins: [

new MyAwesomeWebpackPlugin(),

new StyleLintPlugin({

files:

['**/*.{vue,htm,html,css,stylus}']

})

]

}

}

12 of 26

configureWebpack

// vue.config.js

module.exports = {

configureWebpack: config => {

if (process.env.NODE_ENV === 'production')

{

// mutate config for production...

} else {

// mutate for development...

}

}

}

13 of 26

chainWebpack

Modify Options of a Loader

// vue.config.js

module.exports = {

chainWebpack: config => {

config.module

.rule('vue')

.use('vue-loader')

.loader('vue-loader')

.tap(options => {

// modify the options...

return options

})

}

}

14 of 26

chainWebpack

Adding a New Loader

// vue.config.js

module.exports = {

chainWebpack: config => {

// Pug Loader

config.module

.rule('pug')

.test(/\.pug$/)

.use('pug-plain-loader')

.loader('pug-plain-loader')

.end()

}

}

15 of 26

chainWebpack

Replacing Loaders of a Rule

// vue.config.js

module.exports = {

chainWebpack: config => {

const svgRule = config.module.rule('svg')

svgRule.uses.clear()

svgRule

.use('vue-svg-loader')

.loader('vue-svg-loader')

}

}

16 of 26

chainWebpack

Modifying Options of a Plugin

// vue.config.js

module.exports = {

chainWebpack: config => {

config

.plugin('html')

.tap(args => {

args[0].template =

'/path/to/index.html'

return args

})

}

}

17 of 26

Vue Loader

18 of 26

Asset URL Handling

<img src="../image.png">

// will be compiled into

createElement('img', {

attrs: {

src: require('../image.png')

// this is now a module request

}

})

// Absolute path

/images/foo.png

// Relative module request

./../images/foo.png

// Module request

~some-npm-package/foo.png

19 of 26

Pre-Processors

SASS/SCSS, LESS, Stylus

Babel, TypeScript

Pug, Mustache

01

02

03

20 of 26

Scoped CSS

<style scoped>

.example {

color: red;

}

</style>

<template>

<div class="example">hi</div>

</template>

<style>

.example[data-v-f3f3eg9] {

color: red;

}

</style>

<template>

<div class="example" data-v-f3f3eg9>hi</div>

</template>

21 of 26

CSS Modules

<style module>

.red { color: red; }

.bold { font-weight: bold; }

</style>

<template>

<div>

<p :class="[$style.red, $style.bold]">

Red and bold

</p>

</div>

</template>

<script>

export default {

created () { console.log(this.$style.red) }

}

</script>

22 of 26

Custom Blocks

Custom Language Blocks

Custom Loaders

01

02

23 of 26

Linting

ESLint

stylelint

01

02

24 of 26

Takeaways

25 of 26

Browsers understand HTML, JS & CSS

Tools allow us to go beyond

Tools have to be managed and maintained

Webpack, Vue CLI and Vue Loader

Future is modular

26 of 26

Fin.