From 1d8b44d5b3fca789b77b0ead239663e068482178 Mon Sep 17 00:00:00 2001 From: DuCanhGH <75556609+DuCanhGH@users.noreply.github.com> Date: Thu, 11 Jan 2024 17:37:41 +0700 Subject: [PATCH] fix(tests): fixed integration/mjs crashing [bump] --- .../next-pwa/__tests__/e2e/app-dir/index.test.ts | 2 +- .../__tests__/integration/mjs/index.test.ts | 2 +- .../__tests__/test-utils/create-describe.ts | 10 +++++----- .../__tests__/test-utils/next-instance-start.ts | 2 +- packages/next-pwa/package.json | 2 +- packages/next-pwa/src/context.ts | 2 ++ packages/next-pwa/src/index.ts | 7 +++---- .../src/webpack/builders/build-custom-worker.ts | 1 + .../src/webpack/builders/build-fallback-worker.ts | 5 ++--- .../src/webpack/builders/build-sw-entry-worker.ts | 1 + .../webpack/plugins/child-compilation-plugins.ts | 15 +++++++++------ 11 files changed, 27 insertions(+), 22 deletions(-) diff --git a/packages/next-pwa/__tests__/e2e/app-dir/index.test.ts b/packages/next-pwa/__tests__/e2e/app-dir/index.test.ts index 729c9a10..873735ba 100644 --- a/packages/next-pwa/__tests__/e2e/app-dir/index.test.ts +++ b/packages/next-pwa/__tests__/e2e/app-dir/index.test.ts @@ -1,6 +1,6 @@ import { createDescribe } from "../../test-utils/index.ts"; -createDescribe("integration mjs", { sourceDir: __dirname, skipInstall: false }, ({ next }) => { +createDescribe("e2e app-dir", { sourceDir: __dirname, skipInstall: false }, ({ next }) => { it("should render", async () => { const $ = await next.render("/"); expect($("#welcome-text").text()).toBe("This is a Next.js PWA!"); diff --git a/packages/next-pwa/__tests__/integration/mjs/index.test.ts b/packages/next-pwa/__tests__/integration/mjs/index.test.ts index 873735ba..729c9a10 100644 --- a/packages/next-pwa/__tests__/integration/mjs/index.test.ts +++ b/packages/next-pwa/__tests__/integration/mjs/index.test.ts @@ -1,6 +1,6 @@ import { createDescribe } from "../../test-utils/index.ts"; -createDescribe("e2e app-dir", { sourceDir: __dirname, skipInstall: false }, ({ next }) => { +createDescribe("integration mjs", { sourceDir: __dirname, skipInstall: false }, ({ next }) => { it("should render", async () => { const $ = await next.render("/"); expect($("#welcome-text").text()).toBe("This is a Next.js PWA!"); diff --git a/packages/next-pwa/__tests__/test-utils/create-describe.ts b/packages/next-pwa/__tests__/test-utils/create-describe.ts index bea12981..313e4452 100644 --- a/packages/next-pwa/__tests__/test-utils/create-describe.ts +++ b/packages/next-pwa/__tests__/test-utils/create-describe.ts @@ -35,13 +35,13 @@ const createNext = async (opts: NextTestOpts) => { await nextInstance.spawn(); return nextInstance; } catch (err) { - console.error(`failed to create next instance: ${err}`); + console.error(`failed to create next instance: ${err}, cliOutput: ${nextInstance?.cliOutput ?? "N/A"}`); try { - await nextInstance?.destroy(); - } catch { - // do nothing + void nextInstance?.destroy(); + } catch (err) { + console.error("failed to clean up after failure", err); } - process.exit(1); + throw new Error("failed to create next instance."); } }; diff --git a/packages/next-pwa/__tests__/test-utils/next-instance-start.ts b/packages/next-pwa/__tests__/test-utils/next-instance-start.ts index cbfcdeb7..2c4de006 100644 --- a/packages/next-pwa/__tests__/test-utils/next-instance-start.ts +++ b/packages/next-pwa/__tests__/test-utils/next-instance-start.ts @@ -72,7 +72,7 @@ export class NextInstanceStart extends NextInstance { console.error(msg); }); } catch (err) { - console.error(`next spawn failed: ${JSON.stringify(err, null, 2)}`); + console.error(`next spawn failed: ${err}`); } }); } diff --git a/packages/next-pwa/package.json b/packages/next-pwa/package.json index 3b79eec6..220fdec6 100644 --- a/packages/next-pwa/package.json +++ b/packages/next-pwa/package.json @@ -53,7 +53,7 @@ "dev": "rimraf dist && rollup --config --watch", "build": "rimraf dist && cross-env NODE_ENV=production rollup --config --compact", "lint": "eslint src --ext ts,tsx,js,jsx,cjs,mjs", - "test": "NODE_OPTIONS=--experimental-vm-modules npx jest", + "test": "NODE_OPTIONS=--experimental-vm-modules jest", "typecheck": "tsc" }, "dependencies": { diff --git a/packages/next-pwa/src/context.ts b/packages/next-pwa/src/context.ts index 3badbc19..d0a4019d 100644 --- a/packages/next-pwa/src/context.ts +++ b/packages/next-pwa/src/context.ts @@ -21,6 +21,7 @@ type PublicPath = NonNullable["publicPath"] export type NextPwaContext = { publicPath: PublicPath | undefined; nextConfig: NextConfigComplete; + webpack: typeof Webpack; webpackContext: WebpackConfigContext; webpackConfig: WebpackConfig; tsConfig: TsConfigJson | undefined; @@ -233,6 +234,7 @@ export const createContext = ( return { publicPath, nextConfig: resolvedNextConfig, + webpack, webpackContext: options, webpackConfig, tsConfig, diff --git a/packages/next-pwa/src/index.ts b/packages/next-pwa/src/index.ts index 282d9706..cefeaaa7 100644 --- a/packages/next-pwa/src/index.ts +++ b/packages/next-pwa/src/index.ts @@ -2,7 +2,7 @@ import path from "node:path"; import { CleanWebpackPlugin } from "clean-webpack-plugin"; import type { NextConfig } from "next"; -import type { Configuration, default as Webpack } from "webpack"; +import type { Configuration } from "webpack"; import { logger } from "$utils/index.js"; @@ -16,8 +16,7 @@ const withPWAInit = (pluginOptions: PluginOptions = {}): ((nextConfig?: NextConf return (nextConfig = {}) => ({ ...nextConfig, webpack(config: Configuration, options) { - const webpack: typeof Webpack = options.webpack; - const ctx = createContext(webpack, options, nextConfig, config, pluginOptions); + const ctx = createContext(options.webpack, options, nextConfig, config, pluginOptions); if (ctx.options.disable) { options.isServer && logger.info("PWA support is disabled."); @@ -34,7 +33,7 @@ const withPWAInit = (pluginOptions: PluginOptions = {}): ((nextConfig?: NextConf const sweWorker = buildSWEntryWorker(ctx); ctx.webpackConfig.plugins.push( - new webpack.DefinePlugin({ + new ctx.webpack.DefinePlugin({ __PWA_SW_ENTRY_WORKER__: sweWorker?.name && `'${sweWorker.name}'`, }), ...(sweWorker ? [sweWorker.pluginInstance] : []), diff --git a/packages/next-pwa/src/webpack/builders/build-custom-worker.ts b/packages/next-pwa/src/webpack/builders/build-custom-worker.ts index c8bbfa24..18f3c318 100644 --- a/packages/next-pwa/src/webpack/builders/build-custom-worker.ts +++ b/packages/next-pwa/src/webpack/builders/build-custom-worker.ts @@ -33,6 +33,7 @@ export const buildCustomWorker = (ctx: NextPwaContext) => { pluginInstance: new ChildCompilationPlugin({ src: customWorkerEntry, dest: path.join(ctx.options.customWorkerDest, name), + webpack: ctx.webpack, }), }; }; diff --git a/packages/next-pwa/src/webpack/builders/build-fallback-worker.ts b/packages/next-pwa/src/webpack/builders/build-fallback-worker.ts index 6a2e51d4..bc8aa7f9 100644 --- a/packages/next-pwa/src/webpack/builders/build-fallback-worker.ts +++ b/packages/next-pwa/src/webpack/builders/build-fallback-worker.ts @@ -1,8 +1,6 @@ import path from "node:path"; import { fileURLToPath } from "node:url"; -import webpack from "webpack"; - import { NextPwaContext } from "../../context.js"; import type { FallbackRoutes } from "../../types.js"; import { getContentHash } from "../../utils.js"; @@ -42,7 +40,8 @@ export const buildFallbackWorker = (ctx: NextPwaContext) => { pluginInstance: new ChildCompilationPlugin({ src: fallbackJs, dest: path.join(ctx.options.dest, name), - plugins: [new webpack.EnvironmentPlugin(envs)], + plugins: [new ctx.webpack.EnvironmentPlugin(envs)], + webpack: ctx.webpack, }), }; }; diff --git a/packages/next-pwa/src/webpack/builders/build-sw-entry-worker.ts b/packages/next-pwa/src/webpack/builders/build-sw-entry-worker.ts index da34005d..7f3591e8 100644 --- a/packages/next-pwa/src/webpack/builders/build-sw-entry-worker.ts +++ b/packages/next-pwa/src/webpack/builders/build-sw-entry-worker.ts @@ -24,6 +24,7 @@ export const buildSWEntryWorker = (ctx: NextPwaContext) => { pluginInstance: new ChildCompilationPlugin({ src: swEntryWorkerEntry, dest: path.join(ctx.options.dest, name), + webpack: ctx.webpack, }), }; }; diff --git a/packages/next-pwa/src/webpack/plugins/child-compilation-plugins.ts b/packages/next-pwa/src/webpack/plugins/child-compilation-plugins.ts index 2b451480..04fec3d4 100644 --- a/packages/next-pwa/src/webpack/plugins/child-compilation-plugins.ts +++ b/packages/next-pwa/src/webpack/plugins/child-compilation-plugins.ts @@ -1,5 +1,5 @@ import type { Compilation, WebpackPluginInstance } from "webpack"; -import webpack from "webpack"; +import type { default as Webpack } from "webpack"; import { relativeToOutputPath } from "./relative-to-output-path.js"; @@ -7,6 +7,7 @@ export interface ChildCompilationPluginOptions { src: string; dest: string; plugins?: WebpackPluginInstance[]; + webpack: typeof Webpack; } /** @@ -18,19 +19,21 @@ export class ChildCompilationPlugin implements WebpackPluginInstance { src: string; dest: string; plugins: WebpackPluginInstance[] | undefined; - constructor({ src, dest, plugins }: ChildCompilationPluginOptions) { + webpack: typeof Webpack; + constructor({ src, dest, plugins, webpack }: ChildCompilationPluginOptions) { this.src = src; this.dest = dest; this.plugins = plugins; + this.webpack = webpack; } - apply(compiler: webpack.Compiler) { + apply(compiler: Webpack.Compiler) { compiler.hooks.make.tapPromise(this.constructor.name, (compilation) => - this.performChildCompilation(compilation, compiler).catch((error: webpack.WebpackError) => { + this.performChildCompilation(compilation, compiler).catch((error: Webpack.WebpackError) => { compilation.errors.push(error); }), ); } - async performChildCompilation(compilation: webpack.Compilation, parentCompiler: webpack.Compiler): Promise { + async performChildCompilation(compilation: Webpack.Compilation, parentCompiler: Webpack.Compiler): Promise { const resolvedDest = relativeToOutputPath(compilation, this.dest); const outputOptions: Parameters["1"] = { filename: resolvedDest, @@ -48,7 +51,7 @@ export class ChildCompilationPlugin implements WebpackPluginInstance { } } - new webpack.EntryPlugin(parentCompiler.context, this.src, this.constructor.name).apply(childCompiler); + new this.webpack.EntryPlugin(parentCompiler.context, this.src, this.constructor.name).apply(childCompiler); await new Promise((resolve, reject) => { childCompiler.runAsChild((error, _entries, childCompilation) => {