Skip to content

Commit

Permalink
fix(tests): fixed integration/mjs crashing
Browse files Browse the repository at this point in the history
[bump]
  • Loading branch information
DuCanhGH committed Jan 11, 2024
1 parent aeb0dc9 commit 1d8b44d
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/next-pwa/__tests__/e2e/app-dir/index.test.ts
Original file line number Diff line number Diff line change
@@ -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!");
Expand Down
2 changes: 1 addition & 1 deletion packages/next-pwa/__tests__/integration/mjs/index.test.ts
Original file line number Diff line number Diff line change
@@ -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!");
Expand Down
10 changes: 5 additions & 5 deletions packages/next-pwa/__tests__/test-utils/create-describe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 2 additions & 0 deletions packages/next-pwa/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type PublicPath = NonNullable<NonNullable<WebpackConfig["output"]>["publicPath"]
export type NextPwaContext = {
publicPath: PublicPath | undefined;
nextConfig: NextConfigComplete;
webpack: typeof Webpack;
webpackContext: WebpackConfigContext;
webpackConfig: WebpackConfig;
tsConfig: TsConfigJson | undefined;
Expand Down Expand Up @@ -233,6 +234,7 @@ export const createContext = (
return {
publicPath,
nextConfig: resolvedNextConfig,
webpack,
webpackContext: options,
webpackConfig,
tsConfig,
Expand Down
7 changes: 3 additions & 4 deletions packages/next-pwa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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.");
Expand All @@ -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] : []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const buildCustomWorker = (ctx: NextPwaContext) => {
pluginInstance: new ChildCompilationPlugin({
src: customWorkerEntry,
dest: path.join(ctx.options.customWorkerDest, name),
webpack: ctx.webpack,
}),
};
};
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
}),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const buildSWEntryWorker = (ctx: NextPwaContext) => {
pluginInstance: new ChildCompilationPlugin({
src: swEntryWorkerEntry,
dest: path.join(ctx.options.dest, name),
webpack: ctx.webpack,
}),
};
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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";

export interface ChildCompilationPluginOptions {
src: string;
dest: string;
plugins?: WebpackPluginInstance[];
webpack: typeof Webpack;
}

/**
Expand All @@ -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<void> {
async performChildCompilation(compilation: Webpack.Compilation, parentCompiler: Webpack.Compiler): Promise<void> {
const resolvedDest = relativeToOutputPath(compilation, this.dest);
const outputOptions: Parameters<Compilation["createChildCompiler"]>["1"] = {
filename: resolvedDest,
Expand All @@ -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<void>((resolve, reject) => {
childCompiler.runAsChild((error, _entries, childCompilation) => {
Expand Down

0 comments on commit 1d8b44d

Please sign in to comment.