webpack中文文档css
yarn add webpack@3.10.1 --dev
yarn add webpack-dev-server@2.9.7 --dev
建立webpack.config.js文件html
const path = require('path'); module.exports = { entry: './src/app.js', //入口文件 output: { path: path.resolve(__dirname, 'dist'), filename: 'app.js' } };
执行命令node
node_module/.bin/webpack
htmlWebpackPluginreact
// 安装html-webpakc-plugin yarn add html-webpack-plugin --dev
自定义html模版(title,mate等信息)webpack
配置连接git
// webpack.config.js文件 plugins: [ new HtmlWebpackPlugin({ template: './src/index.html' }) ]
参考连接github
// 安装 // 多个插件之间空格分隔 yarn add babel-core@6.26.0 babel-present-env@1.6.1 babel-loader@7.1.2 --dev // webpack.config.js配置 module: { rules: [ { test: /\.js$/, exclude: /(node_modules|bower_components)/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'], plugins: [require('@babel/plugin-transform-object-rest-spread')] } } } ] }
babel-preset-react
web
//babel-preset-react yarn add babel-preset-react@6.24.1 --dev
// 安装react react-dom yarn add react react-dom
style-loader
与css-loader
json
module:{ rules: [ { test: /\.css$/, use: [ 'style-loader', 'css-loader' ] } ] }
ExtractTextWebpackPlugin
后端
ExtractTextWebpackPlugin: 将包或包中的文本提取到单独的文件中。
配置
const ExtractTextPlugin = require('extract-text-webpack-plugin'); module:{ rules: [ { test: /\.css$/, use: ExtractTextPlugin.extract({ fallback: "style-loader", use: "css-loader" }) } ] }, plugins: [ new ExtractTextPlugin("styles.css"), ]
sass-loader
,sass-loader
依赖node-sass
与webpack
yarn add sass-loader node-sass
用
file-loader
与url-loader
处理图片资源,url-loader
依赖file-loader
// 安装 yarn add url-loader file-loader --dev // 配置 module: { rules: [ { test: /\.(png|jpg|gif)$/, use: [ { loader: 'url-loader', options: { limit: 8192 } } ] } ] }
yarn add font-awesome // jsx中引入css import 'font-awesome/css/font-awesome.min.css';
new webpack.optimize.CommonsChunkPlugin({ name: 'common', filename: 'js/base.js' })
// 安装 yarn webpack-dev-server@2.9.7 // webpack.config.js中 配置 devServer: { contentBase: './dist' port: 8086 } // 更改启动方式 package.json "scripts": { "dev" : "node_modulse/.bin/webpack-dev-server", "dist": "node_modules/.bin/webpack -p" //添加-p为线上打包 }
object
配置模块如何解析,例如,挡在ES2015中调用import "loadsh"
, resolve
选项可以对webpack查找"lodash"
的方式去作修改。
object
建立import
或require
的别名,来确保模块引入变得更简单,例如一些位于src/
文件夹下的经常使用模块
// webpack.config.js 配置 module.exports = { // ... resolve: { alias: { Utilities: path.resolve(__dirname, 'src/utilities/'), Templates: path.resolve(__dirname, 'src/templates/') } } }
如今,替换【在导入时使用相对路径】这种方式,就像这样:
import Utility from '../../utilities/utility';
能够这样使用别名
import Utility from 'Utilities/utility';
当使用 HTML5 History API
时,任意的 404 响应均可能须要被替代为 index.html
。经过传入如下启用:
module.exports = { // ... devServer = { historyApiFallback: { index: '/dist/index.html' } } }
devServer.proxy
若是你有单独的后端开发服务器 API,而且但愿在同域名下发送 API 请求 ,那么代理某些 URL 会颇有用(能够避免浏览器跨域报错)。
在 localhost:3000 上有后端服务的话,你能够这样启用代理:
// webpack.config.js配置 module.exports = { // ... devServer: { proxy: { '/api': 'http://localhost:3000' } } }
请求到 /api/users
如今会被代理到请求 http://localhost:3000/api/users
。
module.exports = { // ... devServer: { proxy: { '/manage': { target: 'http://admintest.happymmall.com', changeOrigin: true }, '/user/logout.do': { target: 'http://admintest.happymmall.com', changeOrigin: true } } } }