-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.config.mjs
157 lines (146 loc) · 4.55 KB
/
webpack.config.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright 2022 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import extension from './extension.json' assert { type: 'json' };
import camelCase from 'camelcase';
import capitalize from 'capitalize';
import createEntryFile from './createEntryFile.mjs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const entries = {};
const plugins = [];
export default (env, argv) => {
// Each view becomes its own "app". These are automatically generated based on naming convention.
['action'].forEach((type) => {
const typePluralized = type + 's';
const delegates =
type === 'configuration'
? [extension['configuration']]
: extension[typePluralized];
delegates.forEach((itemDescriptor) => {
let itemNameCapitalized;
let chunkName;
if (itemDescriptor.viewPath) {
if (type === 'configuration') {
itemNameCapitalized = 'Configuration';
chunkName = 'configuration/configuration';
} else {
const itemName = itemDescriptor.name;
const itemNameCamelized = camelCase(itemName);
itemNameCapitalized = capitalize(itemNameCamelized);
chunkName = `${typePluralized}/${itemNameCamelized}`;
}
const entryPath = `./.entries/${chunkName}.js`;
createEntryFile(entryPath, itemNameCapitalized, chunkName);
entries[chunkName] = entryPath;
plugins.push(
new HtmlWebpackPlugin({
title: itemDescriptor.displayName || 'Configuration',
reactDevHook:
argv.mode === 'development'
? '<script> if (window.parent !== window) {' +
' window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = ' +
'window.parent.__REACT_DEVTOOLS_GLOBAL_HOOK__; ' +
'} </script>'
: '',
filename: `${chunkName}.html`,
template: 'src/view/template.html',
chunks: ['common', chunkName]
})
);
}
});
});
let minChunks = Math.round(Object.keys(entries).length / 4);
if (minChunks < 2) {
minChunks = 2;
}
return {
optimization: {
runtimeChunk: false,
splitChunks: {
cacheGroups: {
default: false,
commons: {
name: 'common',
chunks: 'all',
minChunks: minChunks
}
}
}
},
entry: entries,
devtool: argv.mode === 'development' ? 'source-map' : false,
plugins: plugins,
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
chunkFilename: '[name].js'
},
module: {
rules: [
{
test: /\.jsx?$/,
include: /src\/view/,
exclude: /__tests__/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/react', '@babel/env'],
plugins: ['@babel/plugin-proposal-class-properties']
}
}
]
},
{
test: /\.js$/,
include: /\.entries/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/env']
}
}
]
},
{
test: /\.styl/,
include: /src\/view/,
use: ['style-loader', 'css-loader', 'stylus-loader']
},
{
test: /\.css/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(jpe?g|png|gif)$/,
use: 'file-loader'
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: 'url-loader?limit=10000&mimetype=application/font-woff'
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: 'file-loader'
}
]
},
resolve: {
extensions: ['.js', '.jsx']
}
};
};