前言
webpack的相关内容
vue cli中的vue.config.js配置
参考:https://cli.vuejs.org/zh/config/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| 'use strict'
const path = require('path')
function resolve(dir) { return path.join(__dirname, dir) }
module.exports = {
publicPath: process.env.NODE_ENV === 'production' ? './' : '/', outputDir: 'dist', assetsDir: 'static', indexPath: 'index.html', filenameHashing: true, lintOnSave: false, productionSourceMap: false, devServer: { port: 8080, open: false, https: false, host: ip, overlay: { warnings: false, errors: true }, proxy: { '/api': { target: process.env.VUE_APP_BASE_API, ws: true, secure: false, changeOrigin: true, pathRewrite: { '^/api': '' } } } }, configureWebpack: { name: '网页标题', resolve: { alias: { '@': resolve('src') } } }, chainWebpack: (config) => {} }
|
CommonJS
module的exports属性(module.exports)是对外的接口。require方法用于加载模块。
webpack
webpack --config可以配置webpack的加载那个文件
webpack项目的创建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| npm init npm install webpack --save--dev npm install webpack-dev-server --save-dev
var config = {}
module.exports = config
{ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "webpack-dev-server --open --config webpack.config.js" }, }
var path = require('path') var config = { entry: { main: './main' }, output:{ path: path.join(__dirname,'dist'), publicPath: '/dist', filename: 'main.js' } } module.exports = config
npm run dev
npm install css-loader --save-dev npm install style-loader --save-dev
var config = { module: { rules: [ { test: /\.css$/, use: ['style-loader','css-loader'] } ] } };
npm install extract-text-webpack-plugin --save-dev
var ExtractTextPlugin = require('extract-text-webpack-plugin'); var config = { module: { rules: [ { test: /\.css$/, use: ExtractTextPlugin.extract({ use: 'css-loader', fallback: 'style-loader' }) } ] }, plugins: [ new ExtractTextPlugin("main.css") ] };
|
vue-loader
使用.vue文件需要先安装vue-loader、vue-style-loader等加载器并做配置。因为要使用ES6语法,还需要安装babel和babel-loader等加载器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| npm install --save vue npm install --save-dev vue-loader npm install --save-dev vue-style-loader npm install --save-dev vue-template-compiler npm install --save-dev vue-hot-reload-api npm install --save-dev babel npm install --save-dev babel-loader npm install --save-dev babel-core npm install --save-dev babel-plugin-transform-runtime npm install --save-dev babel-preset-es2015 npm install --save-dev babel-runtime
var config = { var ExtractTextPlugin = require('extract-text-webpack-plugin'); module: { rules: [ { test: /\.vue$/, loader: 'vue-loader', options: { loaders: { css: ExtractTextPlugin.extract({ use: 'css-loader', fallback: 'vue-style-loader' }) } }, { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ }, } ] }
|
babel配置
1 2 3 4 5 6
| { "presets": ["es2015"], "plugins": ["transform-runtime"], "comments": false }
|
webpack进一步配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
var config = { module: { rules: [ { test: /\.(gif|jpg|png|woff|svg|eot|ttf)\? ? .*$/, loader: 'url-loader? limit=1024' } ] } };
|
webpack的打包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
"scripts": { "dev": "webpack-dev-server --open --config webpack.config.js", "build": "webpack --progress --hide-modules --config webpack.prod.config.js" }
var webpack = require('webpack'); var HtmlwebpackPlugin = require('html-webpack-plugin'); var ExtractTextPlugin = require('extract-text-webpack-plugin'); var merge = require('webpack-merge'); var webpackBaseConfig = require('./webpack.config.js');
webpackBaseConfig.plugins = [];
module.exports = merge(webpackBaseConfig, { output: { publicPath: '/dist/', filename: '[name].[hash].js' }, plugins: [ new ExtractTextPlugin({ filename: '[name].[hash].css', allChunks: true }), new webpack.DefinePlugin({ 'process.env': { NODE_ENV: '"production"' } }), new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }), new HtmlWebpackPlugin({ filename: '../index_prod.html', template: './index.ejs', inject: false }) ] });
|