cncjs
/
webpack.config.production.js
204 строки · 5.6 Кб
1const crypto = require('crypto');2const path = require('path');3const boolean = require('boolean');4const dotenv = require('dotenv');5const CSSSplitWebpackPlugin = require('css-split-webpack-plugin').default;6const findImports = require('find-imports');7const HtmlWebpackPlugin = require('html-webpack-plugin');8const without = require('lodash/without');9const MiniCssExtractPlugin = require('mini-css-extract-plugin');10const nib = require('nib');11const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');12const stylusLoader = require('stylus-loader');13const TerserPlugin = require('terser-webpack-plugin');14const webpack = require('webpack');15const ManifestPlugin = require('webpack-manifest-plugin');16const babelConfig = require('./babel.config');17const buildConfig = require('./build.config');18const pkg = require('./src/package.json');19
20dotenv.config({21path: path.resolve('webpack.config.app.production.env')22});23
24const USE_ESLINT_LOADER = boolean(process.env.USE_ESLINT_LOADER);25const USE_TERSER_PLUGIN = boolean(process.env.USE_TERSER_PLUGIN);26const USE_OPTIMIZE_CSS_ASSETS_PLUGIN = boolean(process.env.USE_OPTIMIZE_CSS_ASSETS_PLUGIN);27
28// Use publicPath for production
29const publicPath = ((payload) => {30const algorithm = 'sha1';31const buf = String(payload);32const hash = crypto.createHash(algorithm).update(buf).digest('hex');33return '/' + hash.substr(0, 8) + '/'; // 8 digits34})(pkg.version);35const buildVersion = pkg.version;36const timestamp = new Date().getTime();37
38module.exports = {39mode: 'production',40cache: true,41target: 'web',42context: path.resolve(__dirname, 'src/app'),43devtool: 'cheap-module-source-map',44entry: {45polyfill: [46path.resolve(__dirname, 'src/app/polyfill/index.js')47],48vendor: findImports([49'src/app/**/*.{js,jsx}',50'!src/app/polyfill/**/*.js',51'!src/app/**/*.development.js'52], { flatten: true }),53app: [54path.resolve(__dirname, 'src/app/index.jsx')55]56},57output: {58path: path.resolve(__dirname, 'dist/cncjs/app'),59chunkFilename: `[name].[chunkhash].bundle.js?_=${timestamp}`,60filename: `[name].[chunkhash].bundle.js?_=${timestamp}`,61publicPath: publicPath62},63module: {64rules: [65USE_ESLINT_LOADER && {66test: /\.jsx?$/,67loader: 'eslint-loader',68enforce: 'pre',69exclude: /node_modules/70},71{72test: /\.jsx?$/,73loader: 'babel-loader',74options: babelConfig,75exclude: /node_modules/76},77{78test: /\.styl$/,79use: [80MiniCssExtractPlugin.loader,81{82loader: 'css-loader',83options: {84modules: true,85localIdentName: '[path][name]__[local]--[hash:base64:5]',86camelCase: true,87importLoaders: 1,88}89},90'stylus-loader'91],92exclude: [93path.resolve(__dirname, 'src/app/styles')94]95},96{97test: /\.styl$/,98use: [99MiniCssExtractPlugin.loader,100{101loader: 'css-loader',102options: {103modules: false,104camelCase: true,105}106},107'stylus-loader'108],109include: [110path.resolve(__dirname, 'src/app/styles')111]112},113{114test: /\.css$/,115use: [116'style-loader',117'css-loader'118]119},120{121test: /\.(png|jpg|svg)$/,122loader: 'url-loader',123options: {124limit: 8192125}126},127{128test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,129loader: 'url-loader',130options: {131limit: 10000,132mimetype: 'application/font-woff'133}134},135{136test: /\.(ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/,137loader: 'file-loader'138}139].filter(Boolean)140},141node: {142fs: 'empty',143net: 'empty',144tls: 'empty'145},146optimization: {147minimizer: [148USE_TERSER_PLUGIN && (149new TerserPlugin()150),151USE_OPTIMIZE_CSS_ASSETS_PLUGIN && (152new OptimizeCSSAssetsPlugin()153),154].filter(Boolean)155},156plugins: [157new webpack.DefinePlugin({158'process.env': {159NODE_ENV: JSON.stringify('production'),160BUILD_VERSION: JSON.stringify(buildVersion),161LANGUAGES: JSON.stringify(buildConfig.languages),162TRACKING_ID: JSON.stringify(buildConfig.analytics.trackingId)163}164}),165new stylusLoader.OptionsPlugin({166default: {167// nib - CSS3 extensions for Stylus168use: [nib()],169// no need to have a '@import "nib"' in the stylesheet170import: ['~nib/lib/nib/index.styl']171}172}),173new webpack.ContextReplacementPlugin(174/moment[\/\\]locale$/,175new RegExp('^\./(' + without(buildConfig.languages, 'en').join('|') + ')$')176),177// Generates a manifest.json file in your root output directory with a mapping of all source file names to their corresponding output file.178new ManifestPlugin({179fileName: 'manifest.json'180}),181new MiniCssExtractPlugin({182filename: `[name].css?_=${timestamp}`,183chunkFilename: `[id].css?_=${timestamp}`184}),185new CSSSplitWebpackPlugin({186size: 4000,187imports: '[name].[ext]?[hash]',188filename: '[name]-[part].[ext]?[hash]',189preserve: false190}),191new HtmlWebpackPlugin({192filename: 'index.hbs',193template: path.resolve(__dirname, 'index.hbs'),194chunksSortMode: 'dependency' // Sort chunks by dependency195})196],197resolve: {198modules: [199path.resolve(__dirname, 'src'),200'node_modules'201],202extensions: ['.js', '.jsx']203}204};205