-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
224 lines (207 loc) · 5.84 KB
/
webpack.config.js
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
const path = require("path");
const webpack = require("webpack");
// const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require("html-webpack-plugin");
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
// const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// const PreloadWebpackPlugin = require('preload-webpack-plugin');
const ConcatSource = require("webpack-sources").ConcatSource;
const ModuleFilenameHelpers = require("webpack/lib/ModuleFilenameHelpers");
class WrapperPlugin {
/**
* @param {Object} args
* @param {string | Function} [args.header] Text that will be prepended to an output file.
* @param {string | Function} [args.footer] Text that will be appended to an output file.
* @param {string | RegExp} [args.test] Tested against output file names to check if they should be affected by this
* plugin.
* @param {boolean} [args.afterOptimizations=false] Indicating whether this plugin should be activated before
* (`false`) or after (`true`) the optimization stage. Example use case: Set this to true if you want to avoid
* minification from affecting the text added by this plugin.
*/
constructor(args) {
if (typeof args !== 'object') {
throw new TypeError('Argument "args" must be an object.');
}
this.header = args.hasOwnProperty('header') ? args.header : '';
this.footer = args.hasOwnProperty('footer') ? args.footer : '';
this.afterOptimizations = args.hasOwnProperty('afterOptimizations') ? !!args.afterOptimizations : false;
this.test = args.hasOwnProperty('test') ? args.test : '';
}
apply(compiler) {
const header = this.header;
const footer = this.footer;
const tester = {test: this.test};
compiler.hooks.compilation.tap('WrapperPlugin', (compilation) => {
if (this.afterOptimizations) {
compilation.hooks.afterOptimizeChunkAssets.tap('WrapperPlugin', (chunks) => {
wrapChunks(compilation, chunks, footer, header);
});
} else {
compilation.hooks.optimizeChunkAssets.tapAsync('WrapperPlugin', (chunks, done) => {
wrapChunks(compilation, chunks, footer, header);
done();
})
}
});
function wrapFile(compilation, fileName, chunkHash) {
const headerContent = (typeof header === 'function') ? header(fileName, chunkHash) : header;
const footerContent = (typeof footer === 'function') ? footer(fileName, chunkHash) : footer;
compilation.assets[fileName] = new ConcatSource(
String(headerContent),
compilation.assets[fileName],
String(footerContent));
}
function wrapChunks(compilation, chunks) {
chunks.forEach(chunk => {
const args = {
hash: compilation.hash,
chunkhash: chunk.hash
};
chunk.files.forEach(fileName => {
if (ModuleFilenameHelpers.matchObject(tester, fileName)) {
wrapFile(compilation, fileName, args);
}
});
});
}
}
}
//
//
//
let babelOptionIndex = {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"browsers": ["last 2 versions", "ie >= 11"]
}
}
]
]
,"plugins": [
"@babel/plugin-transform-classes"
// "@babel/plugin-transform-async-to-generator",
// "@babel/plugin-transform-arrow-functions"
]
}
const config = {
mode: "production", //"devtool",//"production",// "none"
//"@babel/polyfill",
entry: [ "@babel/polyfill", "./src/scripts/index.ts" ],
resolve: {
extensions: [".js", ".ts"]
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: babelOptionIndex
},
{
loader: "ts-loader"
}
]
}
]
},
cache: true,
devtool: "source-map"
};
const testConfig = {
mode: "development",
//"@babel/polyfill",
entry: [ "@babel/polyfill", "./src/scripts/test.ts" ],
resolve: {
extensions: [".js", ".ts"]
},
devServer: {
contentBase: './dist',
port: 9001,
open: 'chrome'
// host: '0.0.0.0'
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: babelOptionIndex
},
{
loader: "ts-loader"
}
]
}
]
},
cache: true,
devtool: "source-map",
output: {
path: path.join(__dirname, "dist"),
filename: "./[name].js"
},
plugins: [
new HtmlWebpackPlugin({
// excludeChunks: ["Util", "BiscuitViewer"],
// chunks: ['app'],
template: path.join(__dirname, "src/test.html")
})
]
};
// module.exports = config;
//
let libraryName = "GDB";
let pfh = `(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory().default;
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports['MyLibrary'] = factory().default;
else{
root['MyLibrary'] = factory().default;
}
})(typeof self !== 'undefined' ? self : this, function() {
return `.replace(/MyLibrary/g, libraryName);
let pff = `\n})`
// let umdCfg = Object.assign({}, config);
// umdCfg.output = {
// path: path.join(__dirname, "dist"),
// library: libraryName,
// libraryTarget: "umd",
// // globalObject: 'this',
// filename: "./gdb.umd.js"
// }
let globalCfg = Object.assign({}, config);
globalCfg.output = {
path: path.join(__dirname, "dist"),
library: libraryName,
libraryTarget: "window",
filename: "./gdb.js"
}
globalCfg.plugins = [
new WrapperPlugin({
test: /\.js$/,
header: pfh,
footer: pff
}),
]
module.exports = (env, argv)=>{
let list = [];
let doBuildInDevelopmentMode = false;
if (doBuildInDevelopmentMode || argv.mode === 'production') {
list.push(globalCfg);
}else{
list.push(testConfig, globalCfg);
}
return list;
};