UNPKG

5.85 kBJavaScriptView Raw
1const path = require('path');
2const fs = require('fs');
3const webpack = require('webpack');
4const HtmlWebpackPlugin = require('html-webpack-plugin');
5const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
6const TerserPlugin = require('terser-webpack-plugin');
7const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
8const CopyWebpackPlugin = require('copy-webpack-plugin');
9const loaders = require('@availity/workflow-settings/webpack');
10const html = require('./html');
11
12process.noDeprecation = true;
13
14const plugin = settings => {
15 const babelrcPath = path.join(settings.project(), '.babelrc');
16 const babelrcExists = fs.existsSync(babelrcPath);
17
18 function getVersion() {
19 return settings.pkg().version || 'N/A';
20 }
21
22 const config = {
23 mode: 'production',
24
25 context: settings.app(),
26
27 entry: {
28 index: ['./index.js']
29 },
30
31 optimization: {
32 splitChunks: {
33 cacheGroups: {
34 styles: {
35 name: 'styles',
36 test: /\.css$/,
37 chunks: 'all',
38 enforce: true
39 },
40 commons: {
41 chunks: 'initial',
42 minChunks: 2
43 },
44 vendor: {
45 test: /node_modules/,
46 chunks: 'initial',
47 name: 'vendor',
48 priority: 10,
49 enforce: true
50 }
51 }
52 },
53 minimizer: []
54 },
55
56 output: {
57 path: settings.output(),
58 filename: settings.fileName(),
59 chunkFilename: settings.chunkFileName(),
60 // TODO: remove this when upgrading to webpack 5
61 futureEmitAssets: true,
62 devtoolModuleFilenameTemplate: info =>
63 `webpack:///${path.relative(settings.project(), info.absoluteResourcePath)}${
64 info.loaders ? `?${info.loaders}` : ''
65 }`
66 },
67
68 devtool: settings.sourceMap(),
69
70 resolve: {
71 // Tell webpack what directories should be searched when resolving modules
72 modules: [
73 settings.app(),
74 'node_modules',
75 path.join(settings.project(), 'node_modules'),
76 path.join(__dirname, 'node_modules')
77 ],
78 symlinks: true,
79 extensions: ['.js', '.jsx', '.json', '.css', 'scss']
80 },
81
82 // This set of options is identical to the resolve property set above,
83 // but is used only to resolve webpack's loader packages.
84 resolveLoader: {
85 modules: [path.join(settings.project(), 'node_modules'), path.join(__dirname, 'node_modules')],
86 symlinks: true
87 },
88
89 module: {
90 rules: [
91 {
92 test: /\.jsx?$/,
93 include: settings.include(),
94 use: [
95 {
96 loader: 'babel-loader',
97 options: {
98 presets: [require.resolve('@availity/workflow-babel-preset')],
99 cacheDirectory: settings.isDevelopment(),
100 babelrc: babelrcExists
101 }
102 }
103 ]
104 },
105 loaders.css.production,
106 loaders.scss.production,
107 loaders.fonts,
108 loaders.images
109 ]
110 },
111 plugins: [
112 new webpack.DefinePlugin(settings.globals()),
113
114 new webpack.BannerPlugin({
115 banner: `APP_VERSION=${JSON.stringify(getVersion())};`,
116 test: /\.jsx?/,
117 raw: true,
118 entryOnly: true
119 }),
120
121 new webpack.BannerPlugin({
122 banner: `v${getVersion()} - ${new Date().toJSON()}`
123 }),
124
125 new HtmlWebpackPlugin(html(settings)),
126
127 // Ignore all the moment local files
128 new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
129
130 new CaseSensitivePathsPlugin(),
131
132 new loaders.MiniCssExtractPlugin({
133 filename: 'css/[name]-[contenthash].css'
134 }),
135
136 new CopyWebpackPlugin(
137 [
138 {
139 context: `${settings.project()}/project/static`, // copy from this directory
140 from: '**/*', // copy all files
141 to: 'static' // copy into {output}/static folder
142 }
143 ],
144 {
145 debug: 'warning'
146 }
147 ),
148 new webpack.ProvidePlugin({
149 Promise: 'es6-promise-promise',
150 Symbol: 'es6-symbol'
151 })
152 ]
153 };
154
155 if (settings.isProduction()) {
156 config.optimization.minimizer.push(
157 new TerserPlugin({
158 terserOptions: {
159 parse: {
160 // we want terser to parse ecma 8 code. However, we don't want it
161 // to apply any minfication steps that turns valid ecma 5 code
162 // into invalid ecma 5 code. This is why the 'compress' and 'output'
163 // sections only apply transformations that are ecma 5 safe
164 // https://github.com/facebook/create-react-app/pull/4234
165 ecma: 8
166 },
167 compress: {
168 ecma: 5,
169 warnings: false,
170 // Disabled because of an issue with Uglify breaking seemingly valid code:
171 // https://github.com/facebook/create-react-app/issues/2376
172 // Pending further investigation:
173 // https://github.com/mishoo/UglifyJS2/issues/2011
174 comparisons: false
175 },
176 mangle: {
177 safari10: true
178 },
179 output: {
180 ecma: 5,
181 comments: false,
182 // Turned on because emoji and regex is not minified properly using default
183 // https://github.com/facebook/create-react-app/issues/2488
184 ascii_only: true
185 }
186 },
187 // Use multi-process parallel running to improve the build speed
188 // Default number of concurrent runs: os.cpus().length - 1
189 parallel: true,
190 // Enable file caching
191 cache: true,
192 sourceMap: true
193 }),
194 new OptimizeCSSAssetsPlugin({
195 cssProcessorOptions: { zindex: false, reduceIdents: false }
196 })
197 );
198 }
199
200 return config;
201};
202
203module.exports = plugin;