Skip to content

Commit 8717867

Browse files
authored
fix(plugin-webpack): ESM-compatible imports (#4206)
1 parent 2cf3b7b commit 8717867

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

packages/plugin/webpack/src/WebpackConfig.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import path from 'node:path';
22

33
import debug from 'debug';
44
import HtmlWebpackPlugin from 'html-webpack-plugin';
5-
import * as webpack from 'webpack';
6-
import { DefinePlugin, ExternalsPlugin, WebpackPluginInstance } from 'webpack';
5+
import type * as webpack from 'webpack';
6+
import webpackPkg from 'webpack';
7+
8+
const { DefinePlugin, ExternalsPlugin } = webpackPkg;
79
import { merge as webpackMerge } from 'webpack-merge';
810

911
import {
@@ -366,7 +368,7 @@ export default class WebpackConfigGenerator {
366368
template: entryPoint.html,
367369
filename: `${entryPoint.name}/index.html`,
368370
chunks: [entryPoint.name].concat(entryPoint.additionalChunks || []),
369-
}) as WebpackPluginInstance,
371+
}) as webpack.WebpackPluginInstance,
370372
);
371373
}
372374
}

packages/plugin/webpack/src/WebpackPlugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import debug from 'debug';
2020
import glob from 'fast-glob';
2121
import fs from 'fs-extra';
2222
import { PRESET_TIMER } from 'listr2';
23-
import * as webpack from 'webpack';
23+
import webpack from 'webpack';
2424
import WebpackDevServer from 'webpack-dev-server';
2525
import { merge } from 'webpack-merge';
2626

@@ -145,7 +145,7 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
145145
rendererOptions: WebpackPluginRendererConfig | null,
146146
): Promise<webpack.MultiStats | undefined> =>
147147
new Promise((resolve, reject) => {
148-
webpack.webpack(options).run(async (err, stats) => {
148+
webpack(options).run(async (err, stats) => {
149149
if (rendererOptions && rendererOptions.jsonStats) {
150150
for (const [index, entryStats] of (stats?.stats ?? []).entries()) {
151151
const name = rendererOptions.entryPoints[index].name;
@@ -639,7 +639,7 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`);
639639

640640
const mainConfig = await this.configGenerator.getMainConfig();
641641
await new Promise((resolve, reject) => {
642-
const compiler = webpack.webpack(mainConfig);
642+
const compiler = webpack(mainConfig);
643643
const [onceResolve, onceReject] = once(resolve, reject);
644644
const cb: WebpackWatchHandler = async (err, stats) => {
645645
if (tab && stats) {
@@ -741,7 +741,7 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`);
741741
entryConfig.stats = 'none';
742742
}
743743

744-
const compiler = webpack.webpack(configs);
744+
const compiler = webpack(configs);
745745

746746
const promises = preloadPlugins.map((preloadPlugin) => {
747747
return new Promise((resolve, reject) => {

tools/test-dist.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import * as path from 'node:path';
2+
import { pathToFileURL } from 'node:url';
23

34
import chalk from 'chalk';
45
import fs from 'fs-extra';
56

67
const BASE_DIR = path.resolve(import.meta.dirname, '..');
78
const PACKAGES_DIR = path.resolve(BASE_DIR, 'packages');
89

10+
// Packages whose entry points have top-level side effects (CLI scripts)
11+
// that cannot be safely imported in a test context.
12+
const SKIP_IMPORT = new Set(['create-electron-app']);
13+
914
(async () => {
1015
const dirsToCheck: string[] = [];
1116

@@ -46,6 +51,17 @@ const PACKAGES_DIR = path.resolve(BASE_DIR, 'packages');
4651
chalk.red(`Main entry not found (${main})`),
4752
);
4853
bad = true;
54+
} else if (!SKIP_IMPORT.has(pj.name)) {
55+
try {
56+
await import(pathToFileURL(path.resolve(dir, main)).href);
57+
} catch (err: unknown) {
58+
const message = err instanceof Error ? err.message : String(err);
59+
console.error(
60+
`${chalk.cyan(`[${pj.name}]`)}:`,
61+
chalk.red(`Failed to import main entry (${main}): ${message}`),
62+
);
63+
bad = true;
64+
}
4965
}
5066
if (!typings || !(await fs.pathExists(path.resolve(dir, typings)))) {
5167
console.error(

0 commit comments

Comments
 (0)