Skip to content

Commit

Permalink
fix(mjs): fixed the ESM build crashing
Browse files Browse the repository at this point in the history
[bump]
  • Loading branch information
DuCanhGH committed Jan 11, 2024
1 parent feccbaf commit aeb0dc9
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .changeset/tiny-crabs-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@ducanh2912/next-pwa": patch
---

fix(mjs): fixed the ESM build crashing

- This was due to us referencing `__dirname`, which was `undefined` in the ESM build...
28 changes: 14 additions & 14 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ jobs:
version: latest
- name: Run Biome
run: biome ci .
# test-integration-dev:
# name: ⚫️ Run Jest (development mode) integration tests
# uses: ./.github/workflows/build-reusable.yml
# with:
# afterBuild: |
# NEXT_TEST_MODE=dev pnpm test -- -- __tests__/integration --ci --runInBand
# secrets: inherit
# test-integration-prod:
# name: ⚫️ Run Jest (production mode) integration tests
# uses: ./.github/workflows/build-reusable.yml
# with:
# afterBuild: |
# NEXT_TEST_MODE=start pnpm test -- -- __tests__/integration --ci --runInBand
# secrets: inherit
test-integration-dev:
name: ⚫️ Run Jest (development mode) integration tests
uses: ./.github/workflows/build-reusable.yml
with:
afterBuild: |
NEXT_TEST_MODE=dev pnpm test -- -- __tests__/integration --ci --runInBand
secrets: inherit
test-integration-prod:
name: ⚫️ Run Jest (production mode) integration tests
uses: ./.github/workflows/build-reusable.yml
with:
afterBuild: |
NEXT_TEST_MODE=start pnpm test -- -- __tests__/integration --ci --runInBand
secrets: inherit
test-e2e-dev:
name: ⚫️ Run Jest (development mode) E2E tests
uses: ./.github/workflows/build-reusable.yml
Expand Down
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("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
Binary file not shown.
8 changes: 8 additions & 0 deletions packages/next-pwa/__tests__/integration/mjs/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const RootLayout = ({ children }: { children: React.ReactNode }) => (
<html lang="en">
<head />
<body>{children}</body>
</html>
);

export default RootLayout;
10 changes: 10 additions & 0 deletions packages/next-pwa/__tests__/integration/mjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Image from "next/image";

const Page = () => (
<main>
<p id="welcome-text">This is a Next.js PWA!</p>
<Image src="/next.svg" alt="Next.js Logo" width={180} height={37} priority />
</main>
);

export default Page;
21 changes: 21 additions & 0 deletions packages/next-pwa/__tests__/integration/mjs/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createDescribe } from "../../test-utils/index.ts";

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!");
});

it("should fetch image", async () => {
const image = await next.fetch("/next.svg");
expect(image.status).toBe(200);
const favicon = await next.fetch("/favicon.ico");
expect(favicon.status).toBe(200);
});

it("should be able to fetch service worker", async () => {
const sw = await next.fetch("/sw.js");
expect(sw.status).toBe(200);
expect(sw.headers.get("Content-Type")?.includes("application/javascript")).toBe(true);
});
});
11 changes: 11 additions & 0 deletions packages/next-pwa/__tests__/integration/mjs/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-check
import withPWAInit from "@ducanh2912/next-pwa";

const withPWA = withPWAInit({
dest: "public",
});

/** @type {import('next').NextConfig} */
const nextConfig = {};

export default withPWA(nextConfig);
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions packages/next-pwa/__tests__/integration/mjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"allowImportingTsExtensions": true,
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
8 changes: 6 additions & 2 deletions packages/next-pwa/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "node:path";
import { fileURLToPath } from "node:url";

import fg from "fast-glob";
import type { NextConfig, NextConfigComplete, WebpackConfigContext } from "next/dist/server/config-shared.js";
Expand All @@ -11,6 +12,8 @@ import type { PluginOptions } from "./types.js";
import { getFileHash } from "./utils.js";
import { getDefaultDocumentPage } from "./webpack/builders/get-default-document-page.js";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

export type PluginOptionsComplete = Required<PluginOptions>;

type PublicPath = NonNullable<NonNullable<WebpackConfig["output"]>["publicPath"]>;
Expand Down Expand Up @@ -59,6 +62,7 @@ export const parseOptions = (
}: PluginOptions,
): PluginOptionsComplete => {
if (!additionalManifestEntries) {
const publicDir = path.resolve(webpackContext.dir, "public");
additionalManifestEntries = fg
.sync(
[
Expand All @@ -74,12 +78,12 @@ export const parseOptions = (
...publicExcludes,
],
{
cwd: path.resolve(webpackContext.dir, "public"),
cwd: publicDir,
},
)
.map((f) => ({
url: path.posix.join(nextConfig.basePath, f),
revision: getFileHash(`public/${f}`),
revision: getFileHash(path.resolve(publicDir, f)),
}));
}

Expand Down
2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ packages:
- "docs"
- "packages/**"
- "examples/**"
- "!examples/**/.next"
- "!**/.next"
- "next-pwa-e2e-test"

0 comments on commit aeb0dc9

Please sign in to comment.