UNPKG

4.82 kBJavaScriptView Raw
1'use strict'
2
3const path = require('path')
4const webpack = require('webpack')
5const merge = require('webpack-merge')
6const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
7const StatsPlugin = require('stats-webpack-plugin')
8const TerserPlugin = require('terser-webpack-plugin')
9const { fromRoot, pkg, paths, getLibraryName } = require('../utils')
10const userConfig = require('./user')()
11const isProduction = process.env.NODE_ENV === 'production'
12
13const base = (env, argv) => {
14 const filename = [
15 'index',
16 isProduction ? '.min' : null,
17 '.js'
18 ]
19 .filter(Boolean)
20 .join('')
21
22 return {
23 bail: Boolean(isProduction),
24 mode: isProduction ? 'production' : 'development',
25 entry: [userConfig.entry],
26 output: {
27 path: fromRoot(paths.dist),
28 filename: filename,
29 sourceMapFilename: filename + '.map',
30 library: getLibraryName(pkg.name),
31 libraryTarget: 'umd',
32 devtoolModuleFilenameTemplate: info => 'file:' + encodeURI(info.absoluteResourcePath)
33 },
34 module: {
35 rules: [
36 {
37 oneOf: [
38 {
39 test: /\.js$/,
40 include: fromRoot(paths.src),
41 use: {
42 loader: require.resolve('babel-loader'),
43 options: {
44 presets: [require('./babelrc')()],
45 babelrc: false,
46 cacheDirectory: true
47 }
48 }
49 },
50 {
51 test: /\.js$/,
52 exclude: /@babel(?:\/|\\{1,2})runtime/,
53 use: {
54 loader: require.resolve('babel-loader'),
55 options: {
56 presets: [require('./babelrc')()],
57 babelrc: false,
58 cacheDirectory: true,
59 sourceMaps: false
60 }
61 }
62 }
63 ]
64 }
65 ]
66 },
67 resolve: {
68 alias: {
69 '@babel/runtime': path.dirname(
70 require.resolve('@babel/runtime/package.json')
71 )
72 }
73 },
74 optimization: {
75 minimize: isProduction,
76 minimizer: [
77 // This is only used in production mode
78 new TerserPlugin({
79 terserOptions: {
80 parse: {
81 // we want terser to parse ecma 8 code. However, we don't want it
82 // to apply any minfication steps that turns valid ecma 5 code
83 // into invalid ecma 5 code. This is why the 'compress' and 'output'
84 // sections only apply transformations that are ecma 5 safe
85 // https://github.com/facebook/create-react-app/pull/4234
86 ecma: 8
87 },
88 compress: {
89 ecma: 5,
90 warnings: false
91 },
92 mangle: {
93 safari10: true
94 },
95 output: {
96 ecma: 5,
97 comments: false
98 }
99 },
100 // Use multi-process parallel running to improve the build speed
101 // Default number of concurrent runs: os.cpus().length - 1
102 parallel: true,
103 // Enable file caching
104 cache: true,
105 sourceMap: true
106 })
107 ]
108 },
109 plugins: [
110 new webpack.DefinePlugin({
111 'process.env': JSON.stringify({
112 DEBUG: process.env.DEBUG,
113 NODE_ENV: process.env.NODE_ENV
114 })
115 })
116 ],
117 target: 'web',
118 node: process.env.AEGIR_NODE === 'false' ? {
119 global: true,
120 __filename: 'mock',
121 __dirname: 'mock',
122 dgram: false,
123 fs: false,
124 net: false,
125 tls: false,
126 child_process: false,
127 console: false,
128 process: true, // TODO remove this once readable-stream is fixed
129 Buffer: false,
130 setImmediate: false,
131 os: false,
132 assert: false,
133 constants: false,
134 events: false,
135 http: false,
136 path: false,
137 querystring: false,
138 stream: false,
139 string_decoder: false,
140 timers: false,
141 url: false,
142 util: false,
143 crypto: false
144 } : {
145 dgram: 'empty',
146 fs: 'empty',
147 net: 'empty',
148 tls: 'empty',
149 child_process: 'empty',
150 console: false,
151 global: true,
152 process: true,
153 __filename: 'mock',
154 __dirname: 'mock',
155 Buffer: true,
156 setImmediate: true
157 }
158 }
159}
160
161module.exports = (env, argv) => {
162 const external = typeof userConfig.webpack === 'function'
163 ? userConfig.webpack(env, argv)
164 : userConfig.webpack
165 if (process.env.AEGIR_BUILD_ANALYZE === 'true') {
166 return merge(
167 base(env, argv),
168 {
169 plugins: [
170 new BundleAnalyzerPlugin(),
171 new StatsPlugin('stats.json')
172 ],
173 profile: true
174 },
175 external
176 )
177 }
178
179 return merge(
180 base(env, argv),
181 external
182 )
183}