Vue.js and Webpack 4 From Scratch, Part 1

Part 1: Up and?running

My previous?article?described how to create a Vue.js application using the command line interface. This is the easiest way to get started and will be suitable for the majority of needs. However, it certainly doesn’t hurt to try taking the opposite road and build an application entirely from scratch. With the release of?Webpack 4?now is a good time to take this ambitious route and learn a bit more about the workings of the build and bundling process by crafting it entirely by hand.

In this article I will start with an empty folder and create a webpack build for a Vue.js application including:

Hot Module loading with webpack-dev-server

Linting using eslint

CSS pre-processing with stylus

Testing using @vue/test-utils and Jest

The only pre-requisite is having node and npm installed, although I will be using VSCode as my editor.

The final code for this article is available on?github?for reference, the official webpack templates will undoubtedly be updated to Webpack 4 shortly.

As this is quite a long process I will divide the article into parts, this first part will take us from an empty folder up to a simple page containing a Vue component bundled with webpack and opened in a browser window.

Part 2?will cover hot module reloading, styling and transpilation using babel.

The?final part?covers linting, testing, static assets and CSS extraction.

The first thing to do is create a new folder, cd into it and initialise NPM:

npm init

You will be asked to provide some information about the project, it’s safe to accept defaults at this stage, and once completed you will have a package.json file entirely empty of anything other than meta-information. Now we can begin adding our packages.

npm install --save vue vue-router

This installs our application dependencies.

npm install --save-dev webpack webpack-cli

This installs webpack to allow us to begin building our application bundle.

Application Structure

I like to put all the application code, our javascript and vue components, in a subfolder called src. In here I will add:

app.js?: The application entrypoint.

App.vue: The root component

pages: A folder containing all top-level components, each of these will have a route entry associated with it.

components: A folder containing our building block components. Components will be organised into sub-folders based on feature.

router: A folder for all our vue-router configuration.

If I was using vuex I would create another folder called store to contain all our actions/mutations etc.

When I create these, the folder structure should look like this:

We’ll start our app.js with possibly the simplest entrypoint possible:

import Vue from 'vue'

import App from './App.vue'

new Vue({

el: '#app',

render: h => h(App)

})

This will pull in our App component and render it into the DOM element with id “app”. Our App component is just as simple for the moment:

Hello World!

Now we have some Vue code, we need to bundle it using Webpack so it can be pulled into our html.

Webpack

Webpack is a module bundler for Javascript applications, when we run the webpack command we are asking it to start at our entrypoint and then build a dependency graph of the whole application, pulling those dependencies into one or more bundles that can be included in our application. It supports multiple different file types through?loaders, loaders will take files that have no concept of modules (e.g. css) and process them in a way that allows them to participate in the overall dependency graph that webpack is building.

Webpack 4 was just released with a ton of new features including:

Built-in chunking support with the optimization API.

Support for 5 different module types, including Web Assembly modules.

A “mode” configuration setting that applies a sensible set of defaults, simple projects can be built relying on this alone to determine how to build your application.

Zero-config setups (#0CJS). If you supply the mode then Webpack will use default entry and output settings to build your application without the need for a config file.

All this plus smaller bundle sizes and faster build times!

Let’s try out a simple webpack build.

In the root of our project create a folder called build and add a file called webpack.config.dev.js. We will use this to configure a loader for our Vue single-file components.

'use strict'

const { VueLoaderPlugin } = require('vue-loader')

module.exports = {

mode: 'development',

entry: [

'./src/app.js'

],

module: {

rules: [

{

test: /\.vue$/,

use: 'vue-loader'

}

]

},

plugins: [

new VueLoaderPlugin()

]

}

The module section will contain all our loaders, each loader declaration consists of a minimum of 2 properties, test and loader. Test is a regular expression that webpack will use to identify which file types to be processed by this loader and loader is the name of the loader itself.

For this to work we need to install some more dependencies:

npm install --save-dev vue-loader vue-template-compiler vue-style-loader css-loader

And add an npm build script:

"build": "webpack --config build/webpack.config.dev.js"

We also need to add an index.html file to pull in our built bundle, place this in the root of the project:

My Vue app with webpack 4

If you now run the build script you should see something like this:

And if you open index.html in your browser you should see:

As an aside, there is an extension for VSCode, “Live Server”, that allows you to right-click on html files and launch them in an on-demand web server as opposed to opening them from your file system. Eventually we will serve our application using webpack-dev-server with hot module reloading but for the meantime this is an excellent stopgap.

Next Step

In the next part of the article I will create a component with some script to illustrate babel-compilation and configure stylus for our css. We will install webpack-dev-server and configure hot module reloading including support for static assets. To complete the application we will create a production build including splitting out of vendor modules and show how to test our application using @vue/test-utils and jest.

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末隘竭,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子僚焦,更是在濱河造成了極大的恐慌靖避,老刑警劉巖驻民,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡昨稼,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門拳锚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來假栓,“玉大人,你說我怎么就攤上這事霍掺∝揖#” “怎么了?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵杆烁,是天一觀的道長牙丽。 經常有香客問我,道長兔魂,這世上最難降的妖魔是什么烤芦? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮析校,結果婚禮上拍棕,老公的妹妹穿的比我還像新娘。我一直安慰自己勺良,他們只是感情好绰播,可當我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著尚困,像睡著了一般蠢箩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上事甜,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天谬泌,我揣著相機與錄音,去河邊找鬼逻谦。 笑死掌实,一個胖子當著我的面吹牛,可吹牛的內容都是我干的邦马。 我是一名探鬼主播贱鼻,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼宴卖,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了邻悬?” 一聲冷哼從身側響起症昏,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎父丰,沒想到半個月后肝谭,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡蛾扇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年攘烛,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片镀首。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡医寿,死狀恐怖,靈堂內的尸體忽然破棺而出蘑斧,到底是詐尸還是另有隱情,我是刑警寧澤须眷,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布竖瘾,位于F島的核電站,受9級特大地震影響花颗,放射性物質發(fā)生泄漏捕传。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一扩劝、第九天 我趴在偏房一處隱蔽的房頂上張望庸论。 院中可真熱鬧,春花似錦棒呛、人聲如沸聂示。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鱼喉。三九已至,卻和暖如春趋观,著一層夾襖步出監(jiān)牢的瞬間扛禽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工皱坛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留编曼,地道東北人。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓剩辟,卻偏偏與公主長得像掐场,于是被迫代替她去往敵國和親往扔。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內容