-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
86 lines (80 loc) · 2.67 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
/* eslint-env node, es6 */
'use strict';
const
BASE_NAME = 'm-class-list',
OBJECT_NAME = 'mClassList',
BUILD_MODE = process.env.NODE_ENV === 'production',
BUILD_BASE_NAME = BASE_NAME,
PREPROC_REMOVE_TAGS = BUILD_MODE ? ['DEBUG'] : [],
webpack = require('webpack'),
preProc = require('pre-proc'),
path = require('path'),
fs = require('fs'),
PKG = require('./package'),
SRC_DIR_PATH = path.resolve(__dirname, 'src'),
BUILD_DIR_PATH = BUILD_MODE ? __dirname : path.resolve(__dirname, 'test'),
ESM_DIR_PATH = __dirname,
ENTRY_PATH = path.join(SRC_DIR_PATH, `${BASE_NAME}.js`);
function writeFile(filePath, content, messageClass) {
const HL = '='.repeat(48);
fs.writeFileSync(filePath,
`/* ${HL}\n DON'T MANUALLY EDIT THIS FILE\n${HL} */\n\n${content}`);
console.log(`Output (${messageClass}): ${filePath}`);
}
module.exports = {
mode: BUILD_MODE ? 'production' : 'development',
entry: ENTRY_PATH,
output: {
path: BUILD_DIR_PATH,
filename: `${BUILD_BASE_NAME}${BUILD_MODE ? '.min' : ''}.js`,
library: OBJECT_NAME,
libraryTarget: 'var',
libraryExport: 'default'
},
module: {
rules: [
{
resource: {and: [SRC_DIR_PATH, /\.js$/]},
use: [
// ================================ Save ESM file
{
loader: 'skeleton-loader',
options: {
procedure(content) {
if (this.resourcePath === ENTRY_PATH) {
writeFile(
path.join(ESM_DIR_PATH, `${BUILD_BASE_NAME}${BUILD_MODE ? '' : '-debug'}.esm.js`),
content, 'ESM');
}
return content;
}
}
},
// ================================ Babel
{
loader: 'babel-loader',
options: {presets: [['@babel/preset-env', {targets: 'defaults', modules: false}]]}
},
// ================================ Preprocess
PREPROC_REMOVE_TAGS.length ? {
loader: 'skeleton-loader',
options: {
procedure(content) {
content = preProc.removeTag(PREPROC_REMOVE_TAGS, content);
if (BUILD_MODE && this.resourcePath === ENTRY_PATH) {
writeFile(path.join(SRC_DIR_PATH, `${BUILD_BASE_NAME}.proc.js`), content, 'PROC');
}
return content;
}
}
} : null
].filter(loader => !!loader)
}
]
},
devtool: BUILD_MODE ? false : 'source-map',
plugins: BUILD_MODE ? [
new webpack.BannerPlugin(
`${PKG.title || PKG.name} v${PKG.version} (c) ${PKG.author.name} ${PKG.homepage}`)
] : []
};