webpack 2的react開發配置執行個體代碼,webpackreact
基於webpack 2.3的標準文法,包含了less變數替換、React組件熱載入、第三庫單獨輸出、區分生產與開發環境等常用配置。
'use strict'module.exports = function( env ) { // 產生環境下webpack使用-p參數開啟代碼壓縮 // webpack[-dev-server]使用--env dev參數指定編譯環境 var isDev = env == 'dev'; var path = require( 'path' ); var webpack = require( 'webpack' ); var CleanWebpackPlugin = require( 'clean-webpack-plugin' ); var CopyWebpackPlugin = require( 'copy-webpack-plugin' ); var HtmlWebpackPlugin = require( 'html-webpack-plugin' ); var WebkitPrefixer = require( 'autoprefixer' ); var WebpackMd5Hash = require( 'webpack-md5-hash' ); var BundleAnalyzerPlugin = require( 'webpack-bundle-analyzer' ).BundleAnalyzerPlugin; var sourcedir = path.resolve( __dirname, 'src' );// 源碼和資源檔的置放位置 var outputdir = path.resolve( __dirname, 'build' );// 編譯結果的置放位置 var webContextRoot = '/myreact/';// 應用的實際訪問路徑,預設是'/' // antd的表徵圖字型檔的實際訪問路徑,利用less-load的變數替換功能 var antd_fonticon = webContextRoot + 'assets/antd_fonticon/iconfont'; var hasValue = function( item ) { return item != null; }; return { //context: path.resolve( __dirname ), devtool: 'source-map', devServer: { host: '0.0.0.0', port: 8082, historyApiFallback: true }, resolve: { // 讓less-loader等外掛程式能找到以~相對定位的資源 modules: [sourcedir, 'node_modules'] }, entry: { main: [ // 編譯新版本js的新api(如Promise),主要是讓IE11能夠執行編譯後的代碼 'babel-polyfill', //使用react-hot-loader@3.0.0-beta.6, // 搭配webpack-dev-server --hot命令實現react組件的hot reload isDev ? 'react-hot-loader/patch' : null, path.resolve( sourcedir, 'main.jsx' )].filter( hasValue ), // 第三方庫匯總輸出 vendor: ['bootstrap/dist/css/bootstrap.min.css', 'react', 'react-dom', 'react-router', 'redux', 'react-redux', 'react-router-redux', 'moment', 'lodash', 'immutable', 'whatwg-fetch', // 只含antd的js部分 'antd', // 各控制項還需引入各自的樣式檔案 'antd/lib/style/index.less'] }, output: { path: outputdir, filename: isDev ? 'js/[name].js' : 'js/[name]_[chunkhash:8].js', // 使用require.ensure造成的消極式載入的代碼檔案 chunkFilename: isDev ? 'js/chunk_[id]_[name].js' : 'js/chunk_[name]_[chunkhash:8].js', publicPath: webContextRoot }, module: { rules: [{ test: /\.jsx?$/, exclude: /(node_modules|bower_components)/, use: [{ // 編譯新版本js文法為低版本js文法 loader: 'babel-loader', options: { presets: [ // 編譯es2015版本的js ['babel-preset-es2015', { modules: false }], 'babel-preset-stage-2', // 編譯jsx 'babel-preset-react'], plugins: [// 支援熱載入react組件 isDev ? 'react-hot-loader/babel' : null, // 減少重複的編譯後的輔助方法 'babel-plugin-transform-runtime', // 按需載入組件的代碼和樣式 ['babel-plugin-import', { libraryName: 'antd', style: true }]].filter( hasValue ) } }] }, { test: /\.css$/, use: ['style-loader', { loader: 'css-loader', options: { // 第三方組件未以module方式引入css,所以不能在全域開啟css module modules: false } }, { loader: 'postcss-loader', options: { plugins: [WebkitPrefixer] } }] }, { test: /\.less$/, use: ['style-loader', { loader: 'css-loader', options: { modules: false } }, { loader: 'postcss-loader', options: { plugins: [WebkitPrefixer] } }, { loader: 'less-loader', options: { modules: false, modifyVars: { // 替換antd用到的字型檔路徑 "icon-url": JSON.stringify( antd_fonticon ) } } }] }, { test: /\.(jpg|png|gif)$/, use: { loader: 'url-loader', options: { // 編碼為dataUrl的最大尺寸 limit: 10000, // 輸出路徑,相對於publicPath outputPath: 'assets/', name: isDev ? '[name].[ext]' : '[name]_[hash:8].[ext]' } } }, { test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/, use: { loader: 'url-loader', options: { limit: 10000, mimetype: 'application/font-woff', outputPath: 'assets/', name: isDev ? '[name].[ext]' : '[name]_[hash:8].[ext]' } } }, { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, use: { loader: 'url-loader', options: { limit: 10000, mimetype: 'application/octet-stream', outputPath: 'assets/', name: isDev ? '[name].[ext]' : '[name]_[hash:8].[ext]' } } }, { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use: { loader: 'url-loader', options: { limit: 10000, mimetype: 'application/vnd.ms-fontobject', outputPath: 'assets/', name: isDev ? '[name].[ext]' : '[name]_[hash:8].[ext]' } } }, { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, use: { loader: 'url-loader', options: { limit: 10000, mimetype: 'application/svg+xml', outputPath: 'assets/', name: isDev ? '[name].[ext]' : '[name]_[hash:8].[ext]' } } }] }, plugins: [ // momentjs包含大量本地化代碼,需篩選 new webpack.ContextReplacementPlugin( /moment[\/\\]locale$/, /en-ca|zh-cn/ ), new webpack.optimize.OccurrenceOrderPlugin( true ), // 複製無需編譯的檔案至輸出目錄 new CopyWebpackPlugin( [{ from: path.resolve( sourcedir, 'assets' ), to: 'assets' }] ), // 修複webpack的chunkhash不以chunk檔案實際內容為準的問題 new WebpackMd5Hash(), // 單獨打包輸出第三方組件,和webpack產生的運行時代碼 new webpack.optimize.CommonsChunkPlugin( { name: ['vendor', 'manifest'] }), // 自動填滿js、css引用進首頁 new HtmlWebpackPlugin( { title: 'wzp react', template: path.resolve( sourcedir, 'index.html' ), inject: 'body' // Inject all scripts into the body }), // 設定環境變數 new webpack.DefinePlugin( { process: { env: { // process.env.NODE_ENV==="production" // 應用代碼裡,可憑此判斷是否運行在生產環境 NODE_ENV: isDev ? JSON.stringify( 'development' ) : JSON.stringify( 'production' ) } } }), // print more readable module names on HMR updates isDev ? new webpack.NamedModulesPlugin() : null, // 先清理輸出目錄 isDev ? null : new CleanWebpackPlugin( [outputdir] ), // 排除特定庫 isDev ? null : new webpack.IgnorePlugin( /.*/, /react-hot-loader$/ ), // 輸出報告,查看第三方庫的大小 isDev ? null : new BundleAnalyzerPlugin( { analyzerMode: 'static', reportFilename: 'report.html', openAnalyzer: true, generateStatsFile: false }) ].filter( hasValue ) }};
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。