-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.build.js
194 lines (177 loc) · 5.14 KB
/
webpack.build.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
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const fs = require('fs');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('css-minimizer-webpack-plugin');
const { Compilation, sources } = require('webpack');
const { buildHelper } = require('./helpers/processVariables');
const distDir = 'output';
const basePath = path.resolve(__dirname);
const fileList = glob.sync('themes/**/*.scss', { posix: true, dotRelative: true });
const nodeModules = `./node_modules/`;
const fontAwesomeCssBasePath = `${nodeModules}@fortawesome/fontawesome-free/css`;
const fontAwesomeWebFontBasePath = `${nodeModules}@fortawesome/fontawesome-free`;
const materialIconsFilePath = `${nodeModules}@material-icons/font/css`;
const materialIconsFontFilePath = `${nodeModules}@material-icons/font`;
const sgIconsPath = "./sg-icons";
const processArgs = () => {
const args = process.argv.slice(2);
const output = {};
let arrayKey = null;
args.forEach((val) => {
if (String(val).indexOf('--') === 0 || String(val).indexOf('-') === 0) {
if (String(val).indexOf('--') === 0) {
arrayKey = String(val).substring(2);
} else {
arrayKey = String(val).substring(1);
}
} else {
if (arrayKey != null) {
output[arrayKey] = val;
arrayKey = null;
}
}
});
return output;
}
const inputArgs = processArgs();
const manifestSourceFilePath = "./app.manifest.json";
const themeList = {};
themeList.themeName = glob.sync(distDir + '/themes/css/*-theme.css');
const jsonData = JSON.stringify(themeList, null, 4);
fs.writeFileSync(manifestSourceFilePath, jsonData); // TODO - write manifest file
const outputPathVariable = 'output-path';
class Without {
constructor(patterns) {
this.patterns = patterns;
}
apply(compiler) {
compiler.hooks.emit.tapAsync("MiniCssExtractPluginCleanup", (compilation, callback) => {
Object.keys(compilation.assets)
.filter(asset => {
let match = false, i = this.patterns.length;
while (i--) {
if (this.patterns[i].test(asset)) {
match = true;
}
}
return match;
}).forEach(asset => {
delete compilation.assets[asset];
});
callback();
});
}
}
class Replace {
apply(compiler) {
compiler.hooks.thisCompilation.tap('Replace', (compilation) => {
compilation.hooks.processAssets.tap(
{
name: 'Replace',
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
},
() => {
if ((inputArgs["mode"] && inputArgs["mode"] !== "" && inputArgs["mode"] === "production")) {
const themes = ['light-theme.css', 'dark-theme.css', 'high-contrast-theme.css', 'zoom-dark-theme.css', 'zoom-light-theme.css'];
themes.forEach((theme) => {
const themeFile = compilation.getAsset(theme);
compilation.updateAsset(theme, new sources.RawSource(buildHelper(themeFile.source.source())));
})
}
}
);
});
}
}
const createEntryList = function () {
let filesObj = {};
fileList.map((item) => {
let itemArr = item.split("/");
let fileName = itemArr[itemArr.length - 1].split('.')[0];
let keyName = `${fileName}`;
filesObj[keyName] = path.resolve(basePath, item);
});
return filesObj;
};
const entryList = createEntryList();
entryList['external'] = [
path.resolve(basePath, `${fontAwesomeCssBasePath}/all.min.css`),
path.resolve(basePath, `${materialIconsFilePath}/all.css`),
path.resolve(basePath, `${sgIconsPath}/css/all.css`)
];
if (inputArgs[outputPathVariable] !== "") {
destinationFilePath = inputArgs[outputPathVariable];
} else {
// Throw error
}
module.exports = {
entry: entryList,
resolve: {
extensions: ['.scss', '.css']
},
optimization: {
// css minify only production mode
minimizer: [new OptimizeCSSAssetsPlugin()]
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
'css-loader?url=false'
]
},
{
test: /\.(png|jpg|svg|woff|woff2|eot|ttf)$/,
loader: 'url-loader',
options: {
limit: 3000000,
name: 'images/[name].[ext]'
}
},
{
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
'css-loader',
'sass-loader',
]
}
],
},
mode: (inputArgs["mode"] && inputArgs["mode"] !== "" && inputArgs["mode"] === "production") ? 'production' : 'development',
performance: {
maxEntrypointSize: 2048000000,
maxAssetSize: 2048000000
},
plugins: [
new MiniCssExtractPlugin({
filename: ({ chunk }) => (chunk.name === 'ch5-theme' || chunk.name === 'external') ? "css/[name].css" : "[name].css"
}),
new Replace(),
new Without([/.js?$/]), // just give a list with regex patterns that should be excluded like /\.css\.js(\.map)?$
new CopyPlugin({
patterns: [
{
from: path.resolve(basePath, fontAwesomeWebFontBasePath + "/webfonts/"),
to: path.resolve(destinationFilePath + "/webfonts/")
},
{
from: path.resolve(basePath, materialIconsFontFilePath + "/font/"),
to: path.resolve(destinationFilePath + "/font/")
},
{
from: path.resolve(basePath, sgIconsPath + "/svgs/"),
to: path.resolve(destinationFilePath + "/svgs/")
}
]
})
]
};