1 | const path = require('path');
|
2 | const fs = require('fs');
|
3 | const webpack = require('webpack');
|
4 | const HtmlWebpackPlugin = require('html-webpack-plugin');
|
5 | const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
|
6 | const TerserPlugin = require('terser-webpack-plugin');
|
7 | const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
|
8 | const CopyWebpackPlugin = require('copy-webpack-plugin');
|
9 | const loaders = require('@availity/workflow-settings/webpack');
|
10 | const html = require('./html');
|
11 |
|
12 | process.noDeprecation = true;
|
13 |
|
14 | const 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 |
|
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 |
|
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 |
|
83 |
|
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 |
|
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`,
|
140 | from: '**/*',
|
141 | to: 'static'
|
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 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | ecma: 8
|
166 | },
|
167 | compress: {
|
168 | ecma: 5,
|
169 | warnings: false,
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | comparisons: false
|
175 | },
|
176 | mangle: {
|
177 | safari10: true
|
178 | },
|
179 | output: {
|
180 | ecma: 5,
|
181 | comments: false,
|
182 |
|
183 |
|
184 | ascii_only: true
|
185 | }
|
186 | },
|
187 |
|
188 |
|
189 | parallel: true,
|
190 |
|
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 |
|
203 | module.exports = plugin;
|