|
| 1 | +import path from 'path'; |
| 2 | +import chalk from 'chalk'; |
| 3 | +import globby from 'globby'; |
| 4 | +import webpack from 'webpack'; |
| 5 | +import CopyWebpackPlugin from 'copy-webpack-plugin'; |
| 6 | +import ExtractTextPlugin from 'extract-text-webpack-plugin'; |
| 7 | +import WebpackAssetsManifest from 'webpack-assets-manifest'; |
| 8 | +import minimist from 'minimist'; |
| 9 | +import {stripIndents} from 'common-tags'; |
| 10 | + |
| 11 | +import {settings, useHandlebars} from './config'; |
| 12 | +const configFile = require('../baumeister.json'); |
| 13 | + |
| 14 | +const cliFlags = minimist(process.argv.slice(2)); |
| 15 | +const isDevMode = process.env.NODE_ENV === 'development'; |
| 16 | +const buildTarget = isDevMode ? ' Development ' : ' Production '; |
| 17 | + |
| 18 | +const manifest = new WebpackAssetsManifest({ |
| 19 | + output: path.resolve('.webpack-assets.json') |
| 20 | +}); |
| 21 | + |
| 22 | +const generateCssFile = new ExtractTextPlugin({ |
| 23 | + filename: configFile.cacheBusting && !isDevMode ? 'assets/css/[name].[chunkhash].bundle.css' : 'assets/css/[name].bundle.css' |
| 24 | +}); |
| 25 | + |
| 26 | +const copyVendorFiles = configFile.vendor.includeStaticFiles.map(glob => { |
| 27 | + return { |
| 28 | + from: glob, |
| 29 | + context: 'node_modules', |
| 30 | + to: settings.destinations.vendorFiles |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +const getVendorCSS = function () { |
| 35 | + // Return flattened array of resolved globs from baumeister.json |
| 36 | + const vendorCSS = [].concat(...configFile.vendor.bundleCSS.map(glob => globby.sync(`./node_modules/${glob}`))); |
| 37 | + if (!vendorCSS.length) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + return {vendor: vendorCSS}; |
| 41 | +}; |
| 42 | + |
| 43 | +if (!cliFlags.json) { |
| 44 | + console.log(chalk.yellow(stripIndents`Build target: ${chalk.bold.inverse(buildTarget)} |
| 45 | + ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`)); |
| 46 | +} |
| 47 | + |
| 48 | +module.exports = (options) => ({ |
| 49 | + devServer: options.devServer, |
| 50 | + entry: { |
| 51 | + app: `${settings.sources.app}index.js`, |
| 52 | + ...getVendorCSS() |
| 53 | + }, |
| 54 | + module: { |
| 55 | + rules: [ |
| 56 | + { |
| 57 | + test: /\.(js|jsx)$/, |
| 58 | + include: path.resolve(__dirname, '../', settings.sources.app), |
| 59 | + loader: 'babel-loader', options: {sourceMap: true} |
| 60 | + }, |
| 61 | + { |
| 62 | + test: /\.css$/, |
| 63 | + use: generateCssFile.extract({ |
| 64 | + use: [ |
| 65 | + {loader: 'css-loader', options: {sourceMap: true}} |
| 66 | + ] |
| 67 | + }) |
| 68 | + }, |
| 69 | + { |
| 70 | + test: /\.scss$/, |
| 71 | + use: generateCssFile.extract({ |
| 72 | + use: [ |
| 73 | + {loader: 'css-loader', options: {sourceMap: true}}, |
| 74 | + {loader: 'postcss-loader', options: |
| 75 | + { |
| 76 | + sourceMap: true, |
| 77 | + config: { |
| 78 | + ctx: { |
| 79 | + usePurifyCSS: configFile.purifyCSS.usePurifyCSS, |
| 80 | + cssnano: { |
| 81 | + discardComments: { |
| 82 | + removeAll: true |
| 83 | + } |
| 84 | + }, |
| 85 | + autoprefixer: { |
| 86 | + browsers: [ |
| 87 | + '> 1%', |
| 88 | + 'last 3 version', |
| 89 | + 'ie 8', |
| 90 | + 'ie 9', |
| 91 | + 'Firefox ESR', |
| 92 | + 'Opera 12.1' |
| 93 | + ] |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + {loader: 'sass-loader', options: {sourceMap: true}} |
| 100 | + ] |
| 101 | + }) |
| 102 | + } |
| 103 | + ] |
| 104 | + }, |
| 105 | + output: options.output, |
| 106 | + plugins: [ |
| 107 | + manifest, |
| 108 | + generateCssFile, |
| 109 | + new webpack.optimize.CommonsChunkPlugin({ |
| 110 | + name: 'vendor', |
| 111 | + minChunks: module => /node_modules/.test(module.resource) |
| 112 | + }), |
| 113 | + new webpack.ProvidePlugin({...configFile.webpack.ProvidePlugin}), |
| 114 | + new CopyWebpackPlugin([ |
| 115 | + { |
| 116 | + from: '**/*.html', |
| 117 | + context: useHandlebars ? settings.destinations.handlebars : './src', |
| 118 | + transform(content) { |
| 119 | + return content.toString().replace(/@@(.*\.css|.*\.js)/g, (match, $1) => { |
| 120 | + if (!($1 in manifest.assets)) { |
| 121 | + return `<!-- No ${$1} to be bundled -->`; |
| 122 | + } |
| 123 | + return /\.css/g.test($1) ? `<link href="${manifest.assets[$1]}" rel="stylesheet">` : `<script src="${manifest.assets[$1]}"></script>`; |
| 124 | + }); |
| 125 | + } |
| 126 | + }, |
| 127 | + { |
| 128 | + from: '**/*', |
| 129 | + context: settings.sources.assets, |
| 130 | + to: settings.destinations.assets, |
| 131 | + ignore: ['scss/**'] |
| 132 | + }, |
| 133 | + ...copyVendorFiles |
| 134 | + ]), |
| 135 | + ...options.plugins |
| 136 | + ], |
| 137 | + stats: { |
| 138 | + timings: true, |
| 139 | + version: false, |
| 140 | + hash: false |
| 141 | + } |
| 142 | +}); |
0 commit comments