Skip to content

Commit ca21dca

Browse files
committed
Splits webview configs to allow for flexibility
Switches webviews to SWC for minification (smaller sizes)
1 parent a7436a3 commit ca21dca

File tree

1 file changed

+104
-40
lines changed

1 file changed

+104
-40
lines changed

webpack.config.mjs

Lines changed: 104 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export default function (env, argv) {
6464
return [
6565
getExtensionConfig('node', mode, env),
6666
getExtensionConfig('webworker', mode, env),
67-
getWebviewsConfig(mode, env),
67+
getWebviewsCommonConfig(mode, env),
68+
...getWebviewsConfigs(mode, env),
6869
];
6970
}
7071

@@ -200,6 +201,7 @@ function getExtensionConfig(target, mode, env) {
200201
terserOptions: {
201202
compress: {
202203
drop_debugger: true,
204+
drop_console: true,
203205
ecma: 2020,
204206
// Keep the class names otherwise @log won't provide a useful name
205207
keep_classnames: true,
@@ -299,14 +301,36 @@ function getExtensionConfig(target, mode, env) {
299301
};
300302
}
301303

304+
/**
305+
* @param { 'production' | 'development' | 'none' } mode
306+
* @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; esbuild?: boolean; skipLint?: boolean }} env
307+
* @returns { WebpackConfig[] }
308+
*/
309+
function getWebviewsConfigs(mode, env) {
310+
return [
311+
getWebviewConfig(
312+
{
313+
commitDetails: { entry: './commitDetails/commitDetails.ts' },
314+
graph: { entry: './plus/graph/graph.tsx', plus: true },
315+
home: { entry: './home/home.ts' },
316+
rebase: { entry: './rebase/rebase.ts' },
317+
settings: { entry: './settings/settings.ts' },
318+
timeline: { entry: './plus/timeline/timeline.ts', plus: true },
319+
patchDetails: { entry: './plus/patchDetails/patchDetails.ts', plus: true },
320+
},
321+
mode,
322+
env,
323+
),
324+
];
325+
}
326+
302327
/**
303328
* @param { 'production' | 'development' | 'none' } mode
304329
* @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; esbuild?: boolean; skipLint?: boolean }} env
305330
* @returns { WebpackConfig }
306331
*/
307-
function getWebviewsConfig(mode, env) {
332+
function getWebviewsCommonConfig(mode, env) {
308333
const basePath = path.join(__dirname, 'src', 'webviews', 'apps');
309-
const tsConfigPath = path.join(basePath, 'tsconfig.json');
310334

311335
/** @type WebpackConfig['plugins'] | any */
312336
const plugins = [
@@ -321,29 +345,6 @@ function getWebviewsConfig(mode, env) {
321345
}
322346
: undefined,
323347
),
324-
new DefinePlugin({
325-
DEBUG: mode === 'development',
326-
}),
327-
new ForkTsCheckerPlugin({
328-
async: false,
329-
formatter: 'basic',
330-
typescript: {
331-
configFile: tsConfigPath,
332-
},
333-
}),
334-
new WebpackRequireFromPlugin({
335-
variableName: 'webpackResourceBasePath',
336-
}),
337-
new MiniCssExtractPlugin({ filename: '[name].css' }),
338-
getHtmlPlugin('commitDetails', false, mode, env),
339-
getHtmlPlugin('graph', true, mode, env),
340-
getHtmlPlugin('home', false, mode, env),
341-
getHtmlPlugin('rebase', false, mode, env),
342-
getHtmlPlugin('settings', false, mode, env),
343-
getHtmlPlugin('timeline', true, mode, env),
344-
getHtmlPlugin('patchDetails', true, mode, env),
345-
getCspHtmlPlugin(mode, env),
346-
// new InlineChunkHtmlPlugin(HtmlPlugin, mode === 'production' ? ['\\.css$'] : []),
347348
new CopyPlugin({
348349
patterns: [
349350
{
@@ -379,6 +380,68 @@ function getWebviewsConfig(mode, env) {
379380
);
380381
}
381382

383+
return {
384+
name: 'webviews:common',
385+
context: basePath,
386+
entry: {},
387+
mode: mode,
388+
target: 'web',
389+
output: {
390+
path: path.join(__dirname, 'dist', 'webviews'),
391+
publicPath: '#{root}/dist/webviews/',
392+
},
393+
plugins: plugins,
394+
infrastructureLogging:
395+
mode === 'production'
396+
? undefined
397+
: {
398+
level: 'log', // enables logging required for problem matchers
399+
},
400+
stats: {
401+
preset: 'errors-warnings',
402+
assets: true,
403+
assetsSort: 'name',
404+
assetsSpace: 100,
405+
colors: true,
406+
env: true,
407+
errorsCount: true,
408+
excludeAssets: [/\.(ttf|webp)/],
409+
warningsCount: true,
410+
timings: true,
411+
},
412+
};
413+
}
414+
415+
/**
416+
* @param {{ [key:string]: {entry: string; plus?: boolean }}} webviews
417+
* @param { 'production' | 'development' | 'none' } mode
418+
* @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; esbuild?: boolean; skipLint?: boolean }} env
419+
* @returns { WebpackConfig }
420+
*/
421+
function getWebviewConfig(webviews, mode, env) {
422+
const basePath = path.join(__dirname, 'src', 'webviews', 'apps');
423+
const tsConfigPath = path.join(basePath, 'tsconfig.json');
424+
425+
/** @type WebpackConfig['plugins'] | any */
426+
const plugins = [
427+
new DefinePlugin({
428+
DEBUG: mode === 'development',
429+
}),
430+
new ForkTsCheckerPlugin({
431+
async: false,
432+
formatter: 'basic',
433+
typescript: {
434+
configFile: tsConfigPath,
435+
},
436+
}),
437+
new WebpackRequireFromPlugin({
438+
variableName: 'webpackResourceBasePath',
439+
}),
440+
new MiniCssExtractPlugin({ filename: '[name].css' }),
441+
...Object.entries(webviews).map(([name, config]) => getHtmlPlugin(name, Boolean(config.plus), mode, env)),
442+
getCspHtmlPlugin(mode, env),
443+
];
444+
382445
const imageGeneratorConfig = getImageMinimizerConfig(mode, env);
383446

384447
if (mode !== 'production') {
@@ -390,6 +453,16 @@ function getWebviewsConfig(mode, env) {
390453
);
391454
}
392455

456+
let name = '';
457+
let filePrefix = '';
458+
if (Object.keys(webviews).length > 1) {
459+
name = 'webviews';
460+
filePrefix = 'webviews';
461+
} else {
462+
name = `webviews:${Object.keys(webviews)[0]}`;
463+
filePrefix = `webviews-${Object.keys(webviews)[0]}`;
464+
}
465+
393466
if (env.analyzeBundle) {
394467
const out = path.join(__dirname, 'out');
395468
if (!fs.existsSync(out)) {
@@ -401,24 +474,16 @@ function getWebviewsConfig(mode, env) {
401474
analyzerMode: 'static',
402475
generateStatsFile: true,
403476
openAnalyzer: false,
404-
reportFilename: path.join(out, 'webview-bundle-report.html'),
405-
statsFilename: path.join(out, 'stats.json'),
477+
reportFilename: path.join(out, `${filePrefix}-bundle-report.html`),
478+
statsFilename: path.join(out, `${filePrefix}-stats.json`),
406479
}),
407480
);
408481
}
409482

410483
return {
411-
name: 'webviews',
484+
name: name,
412485
context: basePath,
413-
entry: {
414-
commitDetails: './commitDetails/commitDetails.ts',
415-
graph: './plus/graph/graph.tsx',
416-
home: './home/home.ts',
417-
rebase: './rebase/rebase.ts',
418-
settings: './settings/settings.ts',
419-
timeline: './plus/timeline/timeline.ts',
420-
patchDetails: './plus/patchDetails/patchDetails.ts',
421-
},
486+
entry: Object.fromEntries(Object.entries(webviews).map(([name, { entry }]) => [name, entry])),
422487
mode: mode,
423488
target: 'web',
424489
devtool: mode === 'production' && !env.analyzeBundle ? false : 'source-map',
@@ -437,8 +502,7 @@ function getWebviewsConfig(mode, env) {
437502
mode === 'production'
438503
? [
439504
new TerserPlugin({
440-
// Terser seems better than SWC for minifying the webviews (esm?)
441-
// minify: TerserPlugin.swcMinify,
505+
minify: TerserPlugin.swcMinify,
442506
extractComments: false,
443507
parallel: true,
444508
terserOptions: {

0 commit comments

Comments
 (0)