UNPKG

4.03 kBJavaScriptView Raw
1/* eslint no-console: off */
2const path = require("path");
3const webpack = require("webpack");
4const CompressionPlugin = require('compression-webpack-plugin');
5const fs = require('fs');
6const utils = require('./cli/utils');
7
8
9/* Webpack config generator */
10
11const generateConfig = ({extensionPath, devMode=false, customOutputPath, analyzeBundle=false}) => {
12 utils.verbose(`Generating webpack config. Extensions? ${!!extensionPath}. devMode: ${devMode}`);
13
14 /* which directories should be parsed by babel and other loaders? */
15 const directoriesToTransform = [path.join(__dirname, 'src')];
16
17 /* webpack alias' used in code import / require statements */
18 const aliasesToResolve = {
19 "@extensions": '.', /* must provide a default, else it won't compile */
20 "@auspice": path.join(__dirname, 'src'),
21 "@libraries": path.join(__dirname, 'node_modules')
22 };
23
24 let extensionData;
25 if (extensionPath) {
26 // console.log("BUILDING WITH EXTENSIONS");
27 const dir = path.resolve(__dirname, path.dirname(extensionPath));
28 aliasesToResolve["@extensions"] = dir;
29 directoriesToTransform.push(dir);
30 // console.log("directoriesToTransform", directoriesToTransform);
31 extensionData = JSON.parse(fs.readFileSync(extensionPath, {encoding: 'utf8'}));
32 // console.log("extensionData", extensionData);
33 }
34
35 /* plugins */
36 /* inject strings into the client-accessible process.env */
37 const pluginProcessEnvData = new webpack.DefinePlugin({
38 "process.env": {
39 NODE_ENV: devMode ? JSON.stringify("development") : JSON.stringify("production"),
40 EXTENSION_DATA: JSON.stringify(extensionData)
41 }
42 });
43 /* gzip everything - https://github.com/webpack-contrib/compression-webpack-plugin */
44 const pluginCompress = new CompressionPlugin({
45 filename: "[path].gz[query]",
46 algorithm: "gzip",
47 test: /\.js$|\.css$|\.html$/,
48 threshold: 10240,
49 minRatio: 0.8
50 });
51 const plugins = devMode ? [
52 new webpack.HotModuleReplacementPlugin(),
53 pluginProcessEnvData,
54 new webpack.NoEmitOnErrorsPlugin()
55 ] : [
56 pluginProcessEnvData,
57 new webpack.optimize.AggressiveMergingPlugin(), // merge chunks - https://github.com/webpack/docs/wiki/list-of-plugins#aggressivemergingplugin
58 pluginCompress
59 ];
60
61 if (analyzeBundle) {
62 const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; // eslint-disable-line
63 plugins.push(new BundleAnalyzerPlugin());
64 }
65
66 const entry = [
67 "babel-polyfill",
68 "./src/index"
69 ];
70 if (devMode) {
71 entry.splice(1, 0, "webpack-hot-middleware/client");
72 }
73
74 /* Where do we want the output to be saved?
75 * For development we use the (virtual) "devel" directory
76 * Else we must choose to save it in the CWD or the source
77 */
78 const outputPath = devMode ?
79 path.resolve(__dirname, "devel") : // development: use the (virtual) "devel" directory
80 customOutputPath ?
81 path.resolve(customOutputPath, "dist") :
82 path.resolve(__dirname, "dist");
83 utils.verbose(`Webpack writing output to: ${outputPath}`);
84
85 const config = {
86 mode: devMode ? 'development' : 'production',
87 context: __dirname,
88 entry,
89 output: {
90 path: outputPath,
91 filename: "auspice.bundle.js",
92 chunkFilename: 'auspice.chunk.[name].bundle.js',
93 publicPath: "/dist/"
94 },
95 resolve: {
96 alias: aliasesToResolve
97 },
98 node: {
99 fs: 'empty'
100 },
101 plugins,
102 optimization: {
103 minimize: !devMode
104 },
105 module: {
106 rules: [
107 {
108 test: /\.js$/,
109 loader: 'babel-loader',
110 include: directoriesToTransform,
111 options: {
112 cwd: path.resolve(__dirname)
113 }
114 },
115 {
116 test: /\.css$/,
117 use: ["style-loader", "css-loader"]
118 },
119 {
120 test: /\.(gif|png|jpe?g|svg|woff2?|eot|otf|ttf)$/i,
121 use: "file-loader"
122 }
123 ]
124 }
125 };
126
127 config.devtool = 'cheap-module-source-map';
128
129 return config;
130};
131
132module.exports = {default: generateConfig};