UNPKG

4.31 kBJavaScriptView Raw
1const path = require('path');
2const entryProvider = require('../utilities/entry-provider');
3const {ProvidePlugin, IgnorePlugin} = require('webpack');
4const DeadCodePlugin = require('webpack-deadcode-plugin');
5
6const MiniCssExtractPlugin = require('mini-css-extract-plugin');
7const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
8const TerserPlugin = require('terser-webpack-plugin');
9const {WebSitesManifestPlugin} = require("../plugins/WebSitesManifestPlugin");
10const {RemoveAssetsPlugin} = require("../plugins/RemoveAssetsPlugin");
11
12
13const commonChunkName = 'webpack-common-chunk';
14
15
16const cssLoader = {
17 loader: 'css-loader',
18 options: {
19 importLoaders: 1,
20 sourceMap: true
21 }
22};
23
24const cssLoaderWithModules = {
25 loader: 'css-loader',
26 options: {
27 modules: true,
28 localIdentName: '[local]--[md5:contenthash:hex:8]',
29 sourceMap: true
30 }
31};
32
33const lessLoader = {
34 loader: 'less-loader',
35 options: {
36 sourceMap: true
37 }
38};
39
40const babelLoader = {
41 loader: 'babel-loader',
42 options: {
43 presets: ["@babel/env", "@babel/react"],
44 plugins: [
45 ["@babel/proposal-decorators", {legacy: true}],
46 ["@babel/proposal-class-properties", {loose: true}]
47 ],
48 }
49};
50
51const cssModuleRegex = /\.module\.css$/;
52
53module.exports = (env, options) => {
54 const isProduction = options.mode === 'production';
55 return ({
56 devtool: isProduction ? 'none' : 'cheap-module-eval-source-map',
57
58 entry: entryProvider.getEntries('./front/entry/', '**/*.js'),
59 output: {
60 library: 'returnValue',
61 libraryExport: 'default',
62 globalObject: 'this',
63 path: path.resolve('./front/build'),
64 filename: '[name].[contenthash].js'
65 },
66
67 resolve: {
68 modules: ['.', 'node_modules'],
69 extensions: ['*', '.js', '.jsx']
70 },
71 module: {
72 rules: [
73 {
74 test: /\.(js|jsx)$/,
75 use: [babelLoader]
76 },
77 {
78 test: cssModuleRegex,
79 use: [MiniCssExtractPlugin.loader, cssLoaderWithModules]
80 },
81 {
82 test: /\.(css|less)$/,
83 exclude: cssModuleRegex,
84 sideEffects: true,
85 use: [MiniCssExtractPlugin.loader, cssLoader, lessLoader],
86
87 },
88 {
89 test: /\.(png|jpg|jpeg|gif|svg|eot|woff|woff2|ttf|swf)$/,
90 loader: 'file-loader',
91 options: {
92 name: 'assets/[name].[contenthash].[ext]',
93 },
94 },
95 ]
96 },
97 stats: {children: false},
98 plugins: [
99 !isProduction && new DeadCodePlugin({
100 detectUnusedExport: false,
101 patterns: [
102 'front/src/**/*.js',
103 'theme/**/*.js',
104 ],
105 exclude: [
106 '**/theme/layout/**',
107 ],
108 }),
109 new ProvidePlugin({
110 $: "jquery",
111 jQuery: "jquery"
112 }),
113 new IgnorePlugin(/^\.\/locale$/, /moment$/),
114 new MiniCssExtractPlugin({
115 filename: '[name].[contenthash].css',
116 }),
117 new RemoveAssetsPlugin({
118 shouldDelete: /css\\.*\.js(\.map)?$/
119 }),
120 new WebSitesManifestPlugin()
121 ,
122 ].filter(Boolean),
123 optimization: {
124 runtimeChunk: {
125 name: commonChunkName
126 },
127 splitChunks: {
128 cacheGroups: {
129 [commonChunkName]: {
130 name: commonChunkName,
131 chunks: 'initial',
132 minChunks: 2
133 }
134 }
135 },
136 minimizer: [
137 new TerserPlugin(),
138 new OptimizeCssAssetsPlugin()
139 ]
140 }
141 });
142};