首先建立一個文件夾webpack-demo(名字自取),而後在本地安裝webpack,接着安裝webpack-clicss
npm init -y npm install webpack webpack-cli --save-dev
而後在主文件夾下新建一個src和public子文件夾和webpack.config.js配置文件html
webpack-demo |- /public |- index.html |- /src |- main.js |- package-lock.json |- package.json |- webpack.config.js
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>webpack-demo</title> </head> <body> <div id="app"></div> </body> </html>
webpack-config.js
const path = require('path'); module.exports = { entry: './src/main.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist') } };
安裝webpack-dev-server
,可以爲你提供了一個簡單的 web 服務器,而且可以實時從新加載等等
安裝html-webpack-plugin
和clean-webpack-plugin
插件vue
npm install --save-dev webpack-dev-server npm install --save-dev html-webpack-plugin npm install --save-dev clean-webpack-plugin
package.json
{ "name": "development", "version": "1.0.0", "description": "", "main": "webpack.config.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", + "start": "webpack-dev-server --open --hot" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "html-webpack-plugin": "^2.29.0", "webpack": "^3.0.0", "webpack-cli": "^3.3.7", "clean-webpack-plugin": "^3.0.0", } }
webpack.config.js
const path = require('path'); + const HtmlWebpackPlugin = require('html-webpack-plugin'); + const { CleanWebpackPlugin } = require('clean-webpack-plugin'); module.exports = { entry: { app: './src/main.js' }, + plugins: [ + new HtmlWebpackPlugin({ + template:'./public/index.html', + }), + new CleanWebpackPlugin(), + ], + devServer: { + contentBase: './dist', + port: 8000 + }, output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist') } };
爲了從 JavaScript 模塊中 import 一個 CSS 文件,你須要在 module 配置中 安裝並添加 style-loader 和 css-loader
爲了能夠在項目中引入圖片、字體文件,你須要在 module 配置中 安裝並添加 file-loadernode
npm install --save-dev style-loader css-loader npm install --save-dev file-loader
webpack.config.js
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); module.exports = { entry: './src/main.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, plugins: [ new HtmlWebpackPlugin({ template:'./public/index.html', }), new CleanWebpackPlugin(), ], devServer: { contentBase: './dist', port: 8000 }, + module: { + rules: [ + { + test: /\.css$/, + use: [ + 'style-loader', + 'css-loader' + ] + }, + { + test: /\.(jpg|png|svg|gif)$/, + use: [ + 'file-loader' + ] + }, + { + test: /\.(woff|woff2|eot|ttf|otf)$/, + use: [ + 'file-loader' + ] + } + ] + } }
因爲好多好用的代碼是ECMAScript 2015+ 版本的語法,瀏覽器沒法識別,好比箭頭函數等等,所以須要安裝babel相關的模塊和插件來轉換爲向後兼容的 JavaScript 語法(我只安裝一下經常使用的,若是你使用其餘的,須要本身安裝和配置)webpack
npm install --save-dev babel-loader npm install --save-dev @babel/core @babel/cli @babel/preset-env npm install --save-dev @babel/plugin-transform-arrow-functions npm install --save-dev @babel/runtime @babel/plugin-transform-runtime
在主文件夾下面新建子文件夾babel.config.jsweb
webpack.config.js
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); module.exports = { entry: './src/main.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, plugins: [ new HtmlWebpackPlugin({ template:'./public/index.html', }), new CleanWebpackPlugin(), ], devServer: { contentBase: './dist', port: 8000 }, module: { rules: [ { test: /\.css$/, use: [ 'style-loader', 'css-loader' ] }, { test: /\.(jpg|png|svg|gif)$/, use: [ 'file-loader' ] }, { test: /\.(woff|woff2|eot|ttf|otf)$/, use: [ 'file-loader' ] }, + { + test: /\.js$/, + use: [ + 'babel-loader' + ], + exclude: /node_modules/ + }, ] } }
babel.config.js
module.exports = function (api) { api.cache(true); const presets = [ '@babel/preset-env', ]; const plugins = [ '@babel/plugin-transform-arrow-functions', '@babel/plugin-transform-runtime' ]; return { presets, plugins }; }
安裝vue相關的包npm
npm install vue --save npm install --save-dev vue-loader vue-template-compiler
webpack.config.js
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); + const VueLoaderPlugin = require('vue-loader/lib/plugin'); module.exports = { entry: './src/main.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, plugins: [ new HtmlWebpackPlugin({ template:'./public/index.html', }), new CleanWebpackPlugin(), + new VueLoaderPlugin(), ], devServer: { contentBase: './dist', port: 8000 }, module: { rules: [ { test: /\.css$/, use: [ 'style-loader', 'css-loader' ] }, { test: /\.(jpg|png|svg|gif)$/, use: [ 'file-loader' ] }, { test: /\.(woff|woff2|eot|ttf|otf)$/, use: [ 'file-loader' ] }, { test: /\.js$/, use: [ 'babel-loader' ], exclude: /node_modules/ }, + { + test: /\.vue$/, + use: [ + 'vue-loader' + ], + }, ] } }
安裝sass(sass是一個CSS預處理器,還挺好用,能夠根據我的愛好安裝)json
npm install --save-dev sass-loader npm install --save-dev node-sass
webpack.config.js
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const VueLoaderPlugin = require('vue-loader/lib/plugin'); module.exports = { entry: './src/main.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, plugins: [ new HtmlWebpackPlugin({ template:'./public/index.html', }), new CleanWebpackPlugin(), new VueLoaderPlugin(), ], devServer: { contentBase: './dist', port: 8000 }, module: { rules: [ { test: /\.css$/, use: [ 'style-loader', 'css-loader' ] }, { test: /\.(jpg|png|svg|gif)$/, use: [ 'file-loader' ] }, { test: /\.(woff|woff2|eot|ttf|otf)$/, use: [ 'file-loader' ] }, { test: /\.js$/, use: [ 'babel-loader' ], exclude: /node_modules/ }, { test: /\.vue$/, use: [ 'vue-loader' ], }, + { + test:/\.scss$/, + use:[ + 'style-loader', + 'css-loader', + 'sass-loader' + ] + }, ] } }
sass的使用以下:api
引入外部樣式使用瀏覽器
import '../css/test.scss'
在.vue中使用
<style lang="scss"> //sass語法樣式 </style>
在子文件夾src下新建App.vue文件
main.js
import Vue from 'vue' import App from './App.vue' new Vue({ el: '#app', render: h => h(App), });
webpack-demo |- /public |- index.html |- /src |- main.js |-App.vue |- babel.config.js |- package-lock.json |- package.json |- webpack.config.js
使用npm start
啓動項目