-
Notifications
You must be signed in to change notification settings - Fork 17
/
base.config.ts
269 lines (257 loc) · 7.94 KB
/
base.config.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import CssModulePlugin from '@dojo/webpack-contrib/css-module-plugin/CssModulePlugin';
import { existsSync } from 'fs';
import * as loaderUtils from 'loader-utils';
import * as MiniCssExtractPlugin from 'mini-css-extract-plugin';
import * as path from 'path';
import * as webpack from 'webpack';
const postcssPresetEnv = require('postcss-preset-env');
const slash = require('slash');
const IgnorePlugin = require('webpack/lib/IgnorePlugin');
const basePath = process.cwd();
const srcPath = path.join(basePath, 'src');
const testPath = path.join(basePath, 'tests');
const allPaths = [srcPath, testPath];
const packageJsonPath = path.join(basePath, 'package.json');
const packageJson = existsSync(packageJsonPath) ? require(packageJsonPath) : {};
const packageName = packageJson.name || '';
const tsLintPath = path.join(basePath, 'tslint.json');
const tsLint = existsSync(tsLintPath) ? require(tsLintPath) : false;
function getJsonpFunctionName(name: string) {
name = name
.replace(/[^a-z0-9_]/g, ' ')
.trim()
.replace(/\s+/g, '_');
return `dojoWebpackJsonp${name}`;
}
function getUMDCompatLoader(options: { bundles?: { [key: string]: string[] } }) {
const { bundles = {} } = options;
return {
loader: 'umd-compat-loader',
options: {
imports(module: string, context: string) {
const filePath = path.relative(basePath, path.join(context, module));
let chunkName = slash(filePath);
Object.keys(bundles).some(name => {
if (bundles[name].indexOf(slash(filePath)) > -1) {
chunkName = name;
return true;
}
return false;
});
return `@dojo/webpack-contrib/promise-loader?global,${chunkName}!${module}`;
}
}
};
}
function getLocalIdent(
loaderContext: webpack.loader.LoaderContext,
localIdentName: string,
localName: string,
options: any
) {
if (!options.context) {
const { context, rootContext } = loaderContext;
options.context = typeof rootContext === 'string' ? rootContext : context;
}
const request = slash(path.relative(options.context, loaderContext.resourcePath));
options.content = `${options.hashPrefix}${request}+${localName}`;
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
const hash = loaderUtils.interpolateName(loaderContext, localIdentName, options);
return hash.replace(new RegExp('[^a-zA-Z0-9\\-_\u00A0-\uFFFF]', 'g'), '-').replace(/^((-?[0-9])|--)/, '_$1');
}
const removeEmpty = (items: any[]) => items.filter(item => item);
const banner = `
[Dojo](https://dojo.io/)
Copyright [JS Foundation](https://js.foundation/) & contributors
[New BSD license](https://github.com/dojo/meta/blob/master/LICENSE)
All rights reserved
`;
interface CssStyle {
walkDecls(processor: (decl: { value: string }) => void): void;
}
function colorToColorMod(style: CssStyle) {
style.walkDecls(decl => {
decl.value = decl.value.replace('color(', 'color-mod(');
});
}
export default function webpackConfigFactory(args: any): webpack.Configuration {
const elements = args.element ? [args.element] : args.elements;
const jsonpIdent = args.element ? args.element.name : 'custom-elements';
const extensions = args.legacy ? ['.ts', '.tsx', '.js'] : ['.ts', '.tsx', '.mjs', '.js'];
const compilerOptions = args.legacy ? {} : { target: 'es6', module: 'esnext' };
const features = args.legacy ? args.features : ['chrome'];
const postcssPresetConfig = {
browsers: args.legacy ? ['last 2 versions', 'ie >= 10'] : ['last 2 versions'],
insertBefore: {
'color-mod-function': colorToColorMod
},
features: {
'color-mod-function': true,
'nesting-rules': true
},
autoprefixer: {
grid: args.legacy
}
};
const config: webpack.Configuration = {
mode: 'development',
entry: elements.reduce((entry: any, element: any) => {
entry[element.name] = [
`imports-loader?widgetFactory=${element.path}!${path.join(__dirname, 'template', 'custom-element.js')}`
];
return entry;
}, {}),
node: { dgram: 'empty', net: 'empty', tls: 'empty', fs: 'empty' },
output: {
chunkFilename: `[name]-${packageJson.version}.js`,
filename: `[name]-${packageJson.version}.js`,
jsonpFunction: getJsonpFunctionName(`-${packageName}-${jsonpIdent}`),
libraryTarget: 'jsonp',
path: path.resolve('./output'),
pathinfo: false
},
resolve: {
modules: [basePath, path.join(basePath, 'node_modules')],
extensions
},
optimization: {
splitChunks: {
cacheGroups: elements.reduce((groups: { [key: string]: webpack.Options.CacheGroupsOptions }, element: any) => {
function recursiveIssuer(m: any): string | boolean {
return m.issuer ? recursiveIssuer(m.issuer) : m.name ? m.name : false;
}
groups[`${element.name}Styles`] = {
name: element.name,
test: (m: any, c: any, entry = element.name) =>
m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
chunks: 'all',
enforce: true
};
return groups;
}, {})
}
},
watchOptions: { ignored: /node_modules/ },
plugins: removeEmpty([
new CssModulePlugin(basePath),
new webpack.BannerPlugin(banner),
new IgnorePlugin(/request\/providers\/node/),
new MiniCssExtractPlugin({
filename: `[name]-${packageJson.version}.css`,
sourceMap: true
} as any)
]),
module: {
rules: removeEmpty([
tsLint && {
test: /\.ts$/,
enforce: 'pre',
loader: 'tslint-loader',
options: { configuration: tsLint, emitErrors: true, failOnHint: true }
},
{
test: /@dojo\/.*\.js$/,
enforce: 'pre',
loader: 'source-map-loader-cli',
options: { includeModulePaths: true }
},
{
include: allPaths,
test: /.*\.ts?$/,
enforce: 'pre',
loader: `@dojo/webpack-contrib/css-module-dts-loader?type=ts&instanceName=0_${jsonpIdent}`
},
{
include: allPaths,
test: /.*\.m\.css?$/,
enforce: 'pre',
loader: '@dojo/webpack-contrib/css-module-dts-loader?type=css'
},
{
include: allPaths,
test: /.*\.ts(x)?$/,
use: removeEmpty([
features && {
loader: '@dojo/webpack-contrib/static-build-loader',
options: { features }
},
getUMDCompatLoader({ bundles: args.bundles }),
{
loader: 'ts-loader',
options: { onlyCompileBundledFiles: true, instance: jsonpIdent, transpileOnly: true, compilerOptions }
}
])
},
{
// We cannot trust that all `mjs` modules use the correct import format for all dependencies
// (e.g., do not use `import from` for cjs modules). Setting the type to `javascript/auto` allows
// incorrect imports to continue working.
type: 'javascript/auto',
test: /\.mjs$/,
use: removeEmpty([
{
loader: '@dojo/webpack-contrib/static-build-loader',
options: {
features
}
}
])
},
{
test: /\.js?$/,
use: removeEmpty([
features && {
loader: '@dojo/webpack-contrib/static-build-loader',
options: { features }
},
'umd-compat-loader'
])
},
{
test: new RegExp(`globalize(\\${path.sep}|$)`),
loader: 'imports-loader?define=>false'
},
{
test: /.*\.(gif|png|jpe?g|svg|eot|ttf|woff|woff2)$/i,
loader: 'file-loader?hash=sha512&digest=hex&name=[hash:base64:8].[ext]'
},
{
test: /\.css$/,
exclude: allPaths,
use: [MiniCssExtractPlugin.loader, 'css-loader?sourceMap']
},
{
test: /\.m\.css.js$/,
exclude: allPaths,
use: ['json-css-module-loader']
},
{
include: allPaths,
test: /.*\.css?$/,
use: [
MiniCssExtractPlugin.loader,
'@dojo/webpack-contrib/css-module-decorator-loader',
{
loader: 'css-loader',
options: {
modules: true,
sourceMap: true,
importLoaders: 1,
localIdentName: '[name]__[local]__[hash:base64:5]',
getLocalIdent
}
},
{
loader: 'postcss-loader?sourceMap',
options: {
ident: 'postcss',
plugins: [require('postcss-import')(), postcssPresetEnv(postcssPresetConfig)]
}
}
]
}
])
}
};
return config;
}