-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpack.config.js
200 lines (176 loc) Β· 4.89 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
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
//@ts-check
// Heavily inspired and copied from Gitlens, special thanks to @eamodio for amazing community support.
'use strict';
const path = require('path');
const { spawnSync } = require('child_process');
const { DefinePlugin } = require('webpack');
const { generateFonts } = require('@twbs/fantasticon');
const { validate } = require('schema-utils');
function getExtensionConfig(mode) {
const plugins = [
new DefinePlugin({ 'global.GENTLY': false }),
new FantasticonPlugin({
configPath: '.fantasticonrc.js',
onBefore:
mode !== 'production'
? undefined
: () =>
spawnSync('yarn', ['run', 'icons:svgo'], {
cwd: __dirname,
encoding: 'utf8',
shell: true,
}),
onComplete: () =>
spawnSync('yarn', ['run', 'icons:apply'], {
cwd: __dirname,
encoding: 'utf8',
shell: true,
}),
})
]
return {
target: 'node', // vscode extensions run in a Node.js-context π -> https://webpack.js.org/configuration/node/
entry: './src/extension.ts', // the entry point of this extension, π -> https://webpack.js.org/configuration/entry-context/
output: {
// the bundle is stored in the 'dist' folder (check package.json), π -> https://webpack.js.org/configuration/output/
path: path.resolve(__dirname, 'dist'),
filename: 'extension.js',
libraryTarget: 'commonjs2',
devtoolModuleFilenameTemplate: '../[resource-path]',
},
plugins,
devtool: 'source-map',
externals: {
vscode: 'commonjs vscode', // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, π -> https://webpack.js.org/configuration/externals/
},
resolve: {
// support reading TypeScript and JavaScript files, π -> https://github.com/TypeStrong/ts-loader
extensions: ['.ts', '.js'],
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
},
],
},
],
},
};
}
const schema = {
type: 'object',
properties: {
config: {
type: 'object',
},
configPath: {
type: 'string',
},
onBefore: {
instanceof: 'Function',
},
onComplete: {
instanceof: 'Function',
},
},
};
class FantasticonPlugin {
alreadyRun = false;
constructor(options = {}) {
this.pluginName = 'fantasticon';
this.options = options;
validate(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
schema,
options,
{
name: this.pluginName,
baseDataPath: 'options',
},
);
}
/**
* @param {import("webpack").Compiler} compiler
*/
apply(compiler) {
const {
config = undefined,
configPath = undefined,
onBefore = undefined,
onComplete = undefined,
} = this.options;
let loadedConfig;
if (configPath) {
try {
loadedConfig = require(path.join(__dirname, configPath));
} catch (ex) {
console.error(`[${this.pluginName}] Error loading configuration: ${ex}`);
}
}
if (!loadedConfig && !config) {
console.error(`[${this.pluginName}] Error loading configuration: no configuration found`);
return;
}
const fontConfig = { ...loadedConfig, ...config };
// TODO@eamodio: Figure out how to add watching for the fontConfig.inputDir
// Maybe something like: https://github.com/Fridus/webpack-watch-files-plugin
/**
* @this {FantasticonPlugin}
* @param {import("webpack").Compiler} compiler
*/
async function generate(compiler) {
if (compiler.watchMode) {
if (this.alreadyRun) return;
this.alreadyRun = true;
}
const logger = compiler.getInfrastructureLogger(this.pluginName);
logger.log(`Generating '${compiler.name}' icon font...`);
const start = Date.now();
let onBeforeDuration = 0;
if (onBefore != null) {
const start = Date.now();
await onBefore(fontConfig);
onBeforeDuration = Date.now() - start;
}
await generateFonts(fontConfig);
let onCompleteDuration = 0;
if (onComplete != null) {
const start = Date.now();
await onComplete(fontConfig);
onCompleteDuration = Date.now() - start;
}
let suffix = '';
if (onBeforeDuration > 0 || onCompleteDuration > 0) {
suffix = ` (${onBeforeDuration > 0 ? `onBefore: ${onBeforeDuration}ms` : ''}${
onCompleteDuration > 0
? `${onBeforeDuration > 0 ? ', ' : ''}onComplete: ${onCompleteDuration}ms`
: ''
})`;
}
logger.log(`Generated '${compiler.name}' icon font in \x1b[32m${Date.now() - start}ms\x1b[0m${suffix}`);
}
const generateFn = generate.bind(this);
compiler.hooks.beforeRun.tapPromise(this.pluginName, generateFn);
compiler.hooks.watchRun.tapPromise(this.pluginName, generateFn);
}
}
module.exports = function (env, argv) {
const mode = argv.mode || 'none';
// env = {
// analyzeBundle: false,
// analyzeDeps: false,
// esbuild: true,
// ...env,
// };
return [
getExtensionConfig(mode),
]
}