Skip to content

Commit 11cfa86

Browse files
committed
fix: use node_modules as no external list
Reading package.json is not a viable solution because it lists only direct dependencies. Get the list from node_modules instead.
1 parent 14239f6 commit 11cfa86

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

vite.config.api.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import glob from "glob";
55
import { build } from "vite";
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8-
const pj = JSON.parse(
9-
fs.readFileSync(path.join(__dirname, "package.json"), "utf-8")
10-
);
118

129
const files = glob.sync("src/api/**/*.ts").map((file: string) => ({
1310
entry: `./${file}`,
@@ -17,9 +14,14 @@ const files = glob.sync("src/api/**/*.ts").map((file: string) => ({
1714
files.forEach(async (file: { entry: string; distFileName: string }) => {
1815
await build({
1916
ssr: {
20-
noExternal: Object.keys(pj.dependencies || {}).concat(
21-
Object.keys(pj.devDependencies || {})
22-
),
17+
noExternal: fs
18+
.readdirSync(path.join(__dirname, "node_modules"), {
19+
withFileTypes: true,
20+
})
21+
.filter(
22+
(dirent) => dirent.isDirectory() && !dirent.name.startsWith(".")
23+
)
24+
.map((dirent) => new RegExp(dirent.name)),
2325
},
2426
configFile: false,
2527
resolve: {

vite.config.ssr.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { defineConfig } from "vite";
22
import { viteStaticCopy } from "vite-plugin-static-copy";
3+
import { fileURLToPath } from "node:url";
34
import path from "node:path";
5+
import fs from "node:fs";
46
import react from "@vitejs/plugin-react";
5-
import pj from "./package.json";
67

78
process.env.NODE_ENV = "production";
9+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
810

911
// https://vitejs.dev/config/
1012
export default defineConfig({
1113
ssr: {
12-
noExternal: Object.keys(pj.dependencies || {}).concat(
13-
Object.keys(pj.devDependencies || {})
14-
),
14+
noExternal: fs
15+
.readdirSync(path.join(__dirname, "node_modules"), {
16+
withFileTypes: true,
17+
})
18+
.filter((dirent) => dirent.isDirectory() && !dirent.name.startsWith("."))
19+
.map((dirent) => new RegExp(dirent.name)),
1520
},
1621
resolve: {
1722
alias: [

0 commit comments

Comments
 (0)