1 |
|
2 |
|
3 | const webpack = require('webpack');
|
4 | const { resolve } = require('path');
|
5 | const Merge = require('webpack-merge');
|
6 | const { environment } = require('@rails/webpacker');
|
7 | const config = require('@rails/webpacker/package/config');
|
8 | const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
9 | var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
|
10 | const ImageminWebpWebpackPlugin = require('imagemin-webp-webpack-plugin');
|
11 | const WebpackAssetsManifest = require('webpack-assets-manifest');
|
12 | const { env } = require('process');
|
13 | const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
|
14 | const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
|
15 | const { getIfUtils, removeEmpty } = require('webpack-config-utils');
|
16 |
|
17 | const { ifProduction } = getIfUtils(env.NODE_ENV);
|
18 |
|
19 | const context = resolve(config.source_path);
|
20 | const loaderPath = resolve(__dirname + '/loaders');
|
21 |
|
22 | const assets = require(resolve(loaderPath, 'assets'));
|
23 | const styles = require(resolve(loaderPath, 'styles'));
|
24 |
|
25 | const uglifierSetting = {
|
26 | parallel: true,
|
27 | cache: true,
|
28 | sourceMap: true,
|
29 | uglifyOptions: {
|
30 | ie8: false,
|
31 | ecma: 7,
|
32 | warnings: false,
|
33 | mangle: false,
|
34 | compress: {
|
35 | warnings: false,
|
36 | comparisons: false,
|
37 | drop_console: true,
|
38 | },
|
39 | output: {
|
40 | ascii_only: true,
|
41 | },
|
42 | },
|
43 | };
|
44 |
|
45 | const entry = require('./entries');
|
46 | const source = resolve(config.source_path);
|
47 |
|
48 | const SetPath = (s, e) => (e ? resolve(s, e) : undefined);
|
49 |
|
50 | const modules = removeEmpty([
|
51 | resolve(source, config.source_entry_path),
|
52 | SetPath(source, config.image_entry),
|
53 | SetPath(source, config.stylesheets_entry),
|
54 | resolve('node_modules'),
|
55 | ]);
|
56 |
|
57 | environment.plugins.append(
|
58 | 'Manifest',
|
59 | new WebpackAssetsManifest({
|
60 | integrity: false,
|
61 | entrypoints: true,
|
62 | writeToDisk: true,
|
63 | publicPath: true,
|
64 | transform(imgs, manifest) {
|
65 | const regx = /.(jpg|png|jpeg)/;
|
66 | Object.entries(imgs).forEach(([k, v]) => {
|
67 | console.log('Testing', regx.test(k));
|
68 | if (regx.test(k)) {
|
69 | const { key, value } = manifest.hooks.customize.call({
|
70 | key: k.replace(regx, '.webp'),
|
71 | value: v.replace(regx, '.webp'),
|
72 | });
|
73 |
|
74 | imgs[key] = value;
|
75 | }
|
76 | });
|
77 |
|
78 |
|
79 | },
|
80 | })
|
81 | );
|
82 |
|
83 | environment.plugins.prepend(
|
84 | 'Lodash',
|
85 | new LodashModuleReplacementPlugin({
|
86 | collections: true,
|
87 | currying: true,
|
88 | shorthands: true,
|
89 |
|
90 |
|
91 | })
|
92 | );
|
93 | console.log(resolve(source, config.image_entry) + '/*.(png|jpg)');
|
94 | environment.plugins.prepend('CreateWebp', new ImageminWebpWebpackPlugin());
|
95 |
|
96 | environment.plugins.prepend(
|
97 | 'ExtractText',
|
98 | new MiniCssExtractPlugin({
|
99 | filename: ifProduction('[name]-[contenthash:8].css', '[name].css'),
|
100 | chunkFilename: '[name]-[contenthash:8].chunk.css',
|
101 | })
|
102 | );
|
103 |
|
104 | environment.plugins.prepend('Moment', new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en-gb/));
|
105 |
|
106 | environment.plugins.prepend(
|
107 | 'LoaderOptions',
|
108 | new webpack.LoaderOptionsPlugin(
|
109 | ifProduction(
|
110 | {
|
111 | minimize: true,
|
112 | debug: false,
|
113 | },
|
114 | {
|
115 | debug: true,
|
116 | }
|
117 | )
|
118 | )
|
119 | );
|
120 |
|
121 | if (ifProduction()) {
|
122 | environment.config.set('optimization.minimizer', [new UglifyJsPlugin(uglifierSetting)]);
|
123 |
|
124 | environment.plugins.prepend(
|
125 | 'optimization.css',
|
126 | new OptimizeCssAssetsPlugin({
|
127 | cssProcessor: require('cssnano'),
|
128 | cssProcessorPluginOptions: {
|
129 | preset: ['default', { discardComments: { removeAll: true } }],
|
130 | },
|
131 | canPrint: true,
|
132 | })
|
133 | );
|
134 | }
|
135 |
|
136 | console.log(resolve(config.source_path, config.image_entry || 'images'));
|
137 |
|
138 | environment.loaders.insert('file', assets(env.NODE_ENV));
|
139 | environment.config.set('context', context);
|
140 | environment.config.set('resolve.alias', {
|
141 | images: resolve(config.source_path, config.image_entry || 'images'),
|
142 | Images: resolve(config.source_path, config.image_entry || 'images'),
|
143 | });
|
144 |
|
145 |
|
146 |
|
147 | module.exports = (cssHotReloading = false) => {
|
148 | environment.loaders.insert('css', styles(env.NODE_ENV, cssHotReloading));
|
149 | environment.loaders.insert('sass', styles(env.NODE_ENV, cssHotReloading, true));
|
150 | return { environment, entry };
|
151 | };
|