UNPKG

4.36 kBJavaScriptView Raw
1/* eslint-env node */
2// process.env.NODE_ENV = 'production';
3const webpack = require('webpack');
4const { resolve } = require('path');
5const Merge = require('webpack-merge');
6const { environment } = require('@rails/webpacker');
7const config = require('@rails/webpacker/package/config');
8const MiniCssExtractPlugin = require('mini-css-extract-plugin');
9var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
10const ImageminWebpWebpackPlugin = require('imagemin-webp-webpack-plugin');
11const WebpackAssetsManifest = require('webpack-assets-manifest');
12const { env } = require('process');
13const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
14const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
15const { getIfUtils, removeEmpty } = require('webpack-config-utils');
16
17const { ifProduction } = getIfUtils(env.NODE_ENV);
18
19const context = resolve(config.source_path);
20const loaderPath = resolve(__dirname + '/loaders');
21
22const assets = require(resolve(loaderPath, 'assets'));
23const styles = require(resolve(loaderPath, 'styles'));
24
25const 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
45const entry = require('./entries');
46const source = resolve(config.source_path);
47
48const SetPath = (s, e) => (e ? resolve(s, e) : undefined);
49
50const 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
57environment.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 // return assets;
79 },
80 })
81);
82
83environment.plugins.prepend(
84 'Lodash',
85 new LodashModuleReplacementPlugin({
86 collections: true,
87 currying: true,
88 shorthands: true,
89 // , exotics: true
90 // , paths: true
91 })
92);
93console.log(resolve(source, config.image_entry) + '/*.(png|jpg)');
94environment.plugins.prepend('CreateWebp', new ImageminWebpWebpackPlugin());
95
96environment.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
104environment.plugins.prepend('Moment', new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en-gb/));
105
106environment.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
121if (ifProduction()) {
122 environment.config.set('optimization.minimizer', [new UglifyJsPlugin(uglifierSetting)]);
123 // environment.plugins.prepend('UglifyJs', new UglifyJsPlugin(uglifierSetting));
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
136console.log(resolve(config.source_path, config.image_entry || 'images'));
137
138environment.loaders.insert('file', assets(env.NODE_ENV));
139environment.config.set('context', context);
140environment.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// webpackConfig.entry = entry; // Overwrite the webpacker entries
146
147module.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};