-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
117 lines (105 loc) · 3.74 KB
/
webpack.config.js
File metadata and controls
117 lines (105 loc) · 3.74 KB
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
'use strict';
const path = require('path');
const fs = require('fs');
const { TerserPlugin, CopyPlugin, BannerPlugin, ReplaceCodePlugin } = require('webpack');
/**
* @typedef {import("webpack").Configuration} Configuration
* @typedef {{from: string, to?: (string|Function), context?: string, transform?: Function, transformAll?: Function}} ObjectPattern
*/
/**
* @param {string} name
* @param {Configuration} config
* @returns {Configuration}
*/
const webpackConfig = (name, config, clean = name.charAt(0) !== '/') => ({
...config,
mode: 'production',
name: clean ? name : name.substring(1),
output: {
path: path.join(__dirname, 'dist', clean ? name : ''),
clean,
...(config.output || {}),
},
context: __dirname,
target: config.target || 'node10',
node: { __filename: false, __dirname: false },
cache: { type: 'filesystem' },
stats: { modulesSpace: Infinity, nestedModules: true, nestedModulesSpace: Infinity },
optimization: {
nodeEnv: false,
// minimize: false,
...(config.optimization || {}),
minimizer: ((config.optimization || {}).minimizer || [null]).map(newTerserPlugin),
},
});
const newTerserPlugin = (opt) =>
new TerserPlugin({
// cache: true,
parallel: true,
extractComments: { condition: 'some', banner: false },
...(opt || {}),
terserOptions: {
mangle: false,
format: { beautify: true, indent_level: 2, comments: false, ...((opt || {}).terserOptions || {}) },
compress: { passes: 1 },
},
});
/** @param {Array<(ObjectPattern|string)>} patterns */
const newCopyPlugin = (patterns) => new CopyPlugin({ patterns });
/** @param {Array<{data: Buffer, absoluteFilename: string}>} assets */
const combineDTS = (assets) => {
const baseDir = path.join(__dirname, 'node_modules');
const getModuleName = (file) =>
path
.relative(baseDir, file)
.replace(/\\+/g, '/')
.replace(/(\/index)?\.d\.ts$/i, '');
return assets.reduce((result, asset) => {
const moduleName = getModuleName(asset.absoluteFilename);
const assetDir = path.dirname(asset.absoluteFilename);
const content = String(asset.data)
.replace(/(^|\s)declare\s/gm, '$1')
.replace(
/^(\s*(?:im|ex)port .* from ['"])([./][^'"]*)/gm,
(_, m1, m2) => `${m1}${getModuleName(path.resolve(assetDir, m2))}`
);
return `${result}\ndeclare module '${moduleName}' {\n${content.trim()}\n}\n`;
}, '');
};
String.prototype.camelize = function () {
return this.replace(/[^A-Za-z0-9]+(.)/g, (_, c) => c.toUpperCase());
};
String.prototype.replaceBulk = function (...arr) {
return arr.reduce((s, i) => String.prototype.replace.apply(s, i), this);
};
const resolveDepPath = (rel, alt = '', base = 'node_modules') =>
fs.existsSync((rel = path.resolve(__dirname, base, rel))) ? rel : alt && path.resolve(__dirname, base, alt);
module.exports = [
/*
webpackConfig('import-local', {
entry: { index: './node_modules/import-local/index' },
output: { libraryTarget: 'commonjs2' },
target: 'node8',
externals: { originalRequire: 'commonjs2 ./originalRequire' },
module: {
rules: [
{
test: /node_modules.import-local.index\.js$/i,
loader: 'webpack/lib/replace-loader',
options: { search: /\brequire(\(\w+)/g, replace: 'require("originalRequire")$1' },
},
],
},
plugins: [
newCopyPlugin([
{ from: '{license*,readme*,*.d.ts}', context: 'node_modules/import-local' },
{
from: 'node_modules/import-local/package.json',
transform: (content) => String(content).replace(/,\s*"(d(evD)?ependencies|scripts|xo)": *\{[^{}]*\}/g, ''),
},
]),
new ReplaceCodePlugin({ search: ' require("./originalRequire")', replace: ' require' }),
],
}),
*/
];