Skip to content

Commit

Permalink
Support CJS and ESM properly
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Aug 8, 2024
1 parent f4883d3 commit c3db041
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"exports": {
".": {
"types": "./dist/index.d.ts",
"browser": "./dist/index.js",
"node": "./dist/index.cjs"
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./browser/mocks/cache": "./dist/plugins/next-mocks/alias/cache/index.js",
"./browser/mocks/navigation": "./dist/plugins/next-mocks/alias/navigation/index.js",
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { vitePluginNextFont } from "./plugins/next-font/plugin";
import { vitePluginNextSwc } from "./plugins/next-swc/plugin";

import "./polyfills/promise-with-resolvers";
import loadConfig from "next/dist/server/config.js";
import nextServerConfig from "next/dist/server/config.js";
import {
PHASE_DEVELOPMENT_SERVER,
PHASE_PRODUCTION_BUILD,
Expand All @@ -20,6 +20,9 @@ import { vitePluginNextMocks } from "./plugins/next-mocks/plugin";
import { getExecutionEnvironment, isVitestEnv } from "./utils";

const require = createRequire(import.meta.url);
const loadConfig: typeof nextServerConfig =
// biome-ignore lint/suspicious/noExplicitAny: CJS support
(nextServerConfig as any).default || nextServerConfig;

type VitePluginOptions = {
/**
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/next-swc/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from "node:path";
import loadJsConfig from "next/dist/build/load-jsconfig.js";
import nextLoadJsConfig from "next/dist/build/load-jsconfig.js";
import { transform } from "next/dist/build/swc/index.js";
import { findPagesDir } from "next/dist/lib/find-pages-dir.js";
import type { NextConfigComplete } from "next/dist/server/config-shared.js";
Expand All @@ -14,6 +14,10 @@ const excluded = /[\\/]((cache[\\/][^\\/]+\.zip[\\/])|virtual:)[\\/]/;

const included = /\.((c|m)?(j|t)sx?)$/;

const loadJsConfig: typeof nextLoadJsConfig =
// biome-ignore lint/suspicious/noExplicitAny: CJS support
(nextLoadJsConfig as any).default || nextLoadJsConfig;

export function vitePluginNextSwc(
rootDir: string,
nextConfigResolver: PromiseWithResolvers<NextConfigComplete>,
Expand Down
6 changes: 5 additions & 1 deletion src/utils/nextjs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "node:fs";
import { join } from "node:path";
import { loadEnvConfig } from "@next/env";
import * as nextEnv from "@next/env";
import Log from "next/dist/build/output/log.js";
import {
loadBindings,
Expand All @@ -11,6 +11,10 @@ import type { NextConfigComplete } from "next/dist/server/config-shared.js";
const nextDistPath =
/(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/;

// biome-ignore lint/suspicious/noExplicitAny: Support for CJS
const { loadEnvConfig } = ((nextEnv as any).default ||
nextEnv) as typeof nextEnv;

/**
* Set up the environment variables for the Next.js project
*/
Expand Down

0 comments on commit c3db041

Please sign in to comment.