@rpcbase/vite 0.73.0 → 0.75.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +58 -47
- package/package.json +1 -1
- package/resolveIntegratedPackagePath.js +96 -0
package/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import "./dotEnvExpand.js"
|
|
2
2
|
|
|
3
3
|
import path from "path"
|
|
4
|
-
import
|
|
5
|
-
import { existsSync, readFileSync } from "node:fs"
|
|
4
|
+
import fs from "node:fs"
|
|
6
5
|
|
|
7
6
|
import nocache from "nocache"
|
|
8
7
|
import {
|
|
@@ -16,51 +15,45 @@ import react from "@vitejs/plugin-react"
|
|
|
16
15
|
import { createHtmlPlugin } from "vite-plugin-html"
|
|
17
16
|
import { glob } from "glob"
|
|
18
17
|
|
|
18
|
+
import { resolveIntegratedPackagePath } from "./resolveIntegratedPackagePath.js"
|
|
19
19
|
import { posthogSourcemapsPlugin } from "./posthogSourcemapsPlugin.js"
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
const
|
|
22
|
+
const ensureEnvPolyfillInjectPath = () => {
|
|
23
|
+
const injectDir = path.resolve(process.cwd(), "node_modules/.rpcbase")
|
|
24
|
+
const injectPath = path.join(injectDir, "rb-env-polyfill-inject.js")
|
|
25
|
+
const injectContents = "import \"@rpcbase/env/polyfill\"\n"
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
// 1) chemin rapide: si le package exporte son package.json
|
|
27
|
+
fs.mkdirSync(injectDir, { recursive: true })
|
|
26
28
|
try {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// 2) fallback: résoudre l'entrée du package puis remonter jusqu'à sa racine
|
|
31
|
-
const entry = require.resolve(pkg) // ex: .../node_modules/<pkg>/dist/...
|
|
32
|
-
let dir = path.dirname(entry)
|
|
33
|
-
|
|
34
|
-
// remonter jusqu'à un package.json dont "name" === pkg
|
|
35
|
-
while (true) {
|
|
36
|
-
const pkgJsonPath = path.join(dir, "package.json")
|
|
37
|
-
if (existsSync(pkgJsonPath)) {
|
|
38
|
-
try {
|
|
39
|
-
const json = JSON.parse(readFileSync(pkgJsonPath, "utf8"))
|
|
40
|
-
if (json && json.name === pkg) return dir
|
|
41
|
-
} catch {
|
|
42
|
-
// ignore parsing errors
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const parent = path.dirname(dir)
|
|
46
|
-
if (parent === dir) break
|
|
47
|
-
dir = parent
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// 3) dernier recours: reconstruire depuis le segment node_modules (ok avec pnpm)
|
|
51
|
-
const nmMarker = `${path.sep}node_modules${path.sep}`
|
|
52
|
-
const idx = entry.lastIndexOf(nmMarker)
|
|
53
|
-
if (idx !== -1) {
|
|
54
|
-
const nmRoot = entry.slice(0, idx + nmMarker.length)
|
|
55
|
-
return path.join(nmRoot, pkg)
|
|
29
|
+
const existing = fs.readFileSync(injectPath, "utf8")
|
|
30
|
+
if (existing === injectContents) {
|
|
31
|
+
return injectPath
|
|
56
32
|
}
|
|
57
|
-
|
|
58
|
-
//
|
|
59
|
-
throw err
|
|
33
|
+
} catch {
|
|
34
|
+
// file does not exist yet
|
|
60
35
|
}
|
|
36
|
+
|
|
37
|
+
fs.writeFileSync(injectPath, injectContents, "utf8")
|
|
38
|
+
return injectPath
|
|
61
39
|
}
|
|
62
40
|
|
|
63
|
-
const
|
|
41
|
+
const esbuildInjectPath = ensureEnvPolyfillInjectPath()
|
|
42
|
+
const esbuildInject = [esbuildInjectPath]
|
|
43
|
+
const esbuildInjectPlugins = [
|
|
44
|
+
{
|
|
45
|
+
name: "rb-env-polyfill-inject",
|
|
46
|
+
setup(build) {
|
|
47
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
48
|
+
if (args.path === esbuildInjectPath) {
|
|
49
|
+
return { path: esbuildInjectPath, namespace: "file" }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return undefined
|
|
53
|
+
})
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
]
|
|
64
57
|
|
|
65
58
|
|
|
66
59
|
// const isCI = process.env.CI === "true"
|
|
@@ -99,6 +92,7 @@ const integratedPackages = [
|
|
|
99
92
|
"axios",
|
|
100
93
|
"libphonenumber-js",
|
|
101
94
|
"posthog-js",
|
|
95
|
+
"posthog-js/react",
|
|
102
96
|
"posthog-node",
|
|
103
97
|
"react-router",
|
|
104
98
|
"react-hook-form",
|
|
@@ -107,12 +101,21 @@ const integratedPackages = [
|
|
|
107
101
|
|
|
108
102
|
// Add each integrated package to resolveAliases
|
|
109
103
|
integratedPackages.forEach(pkg => {
|
|
110
|
-
const
|
|
111
|
-
resolveAliases[pkg] =
|
|
104
|
+
const resolved = resolveIntegratedPackagePath(pkg)
|
|
105
|
+
resolveAliases[pkg] = resolved
|
|
112
106
|
})
|
|
113
107
|
|
|
114
108
|
export {resolveAliases}
|
|
115
109
|
|
|
110
|
+
const rbPackagesNeedingEnvPolyfill = [
|
|
111
|
+
"@rpcbase/auth",
|
|
112
|
+
"@rpcbase/env",
|
|
113
|
+
"@rpcbase/env/polyfill",
|
|
114
|
+
"@rpcbase/server",
|
|
115
|
+
"@rpcbase/client",
|
|
116
|
+
"@rpcbase/form",
|
|
117
|
+
"@rpcbase/router",
|
|
118
|
+
]
|
|
116
119
|
|
|
117
120
|
// https://vite.dev/config/
|
|
118
121
|
const getBaseConfig = ({ command, mode, isSsrBuild, isPreview }) => {
|
|
@@ -135,9 +138,9 @@ const getBaseConfig = ({ command, mode, isSsrBuild, isPreview }) => {
|
|
|
135
138
|
isProduction && posthogSourcemapsPlugin({}),
|
|
136
139
|
].filter(Boolean),
|
|
137
140
|
define: {
|
|
138
|
-
|
|
141
|
+
"globalThis.__rb_env__": JSON.stringify({
|
|
139
142
|
...env,
|
|
140
|
-
},
|
|
143
|
+
}),
|
|
141
144
|
},
|
|
142
145
|
envPrefix: ALLOWED_ENV_PREFIXES,
|
|
143
146
|
publicDir: path.join(process.cwd(), "./src/client/public"),
|
|
@@ -175,25 +178,33 @@ const getBaseConfig = ({ command, mode, isSsrBuild, isPreview }) => {
|
|
|
175
178
|
// "react", "react-dom",
|
|
176
179
|
"cookie",
|
|
177
180
|
],
|
|
178
|
-
|
|
181
|
+
noExternal: [
|
|
182
|
+
...rbPackagesNeedingEnvPolyfill,
|
|
183
|
+
"react-use",
|
|
184
|
+
],
|
|
179
185
|
},
|
|
180
186
|
optimizeDeps: {
|
|
181
187
|
include: [
|
|
188
|
+
// Force RPC Base packages through esbuild so rb-env-polyfill injects before their dist code touches globalThis.__rb_env__
|
|
189
|
+
...rbPackagesNeedingEnvPolyfill,
|
|
182
190
|
// "react", "react-dom", "react-dom/server"
|
|
183
191
|
// "cookie"
|
|
184
192
|
],
|
|
185
193
|
exclude: [
|
|
186
|
-
"@rpcbase/auth",
|
|
187
|
-
"@rpcbase/env",
|
|
188
|
-
"@rpcbase/form",
|
|
189
|
-
"@rpcbase/router",
|
|
190
194
|
"@radix-ui/*",
|
|
191
195
|
// TMP only in sample app?
|
|
192
196
|
// "react", "react-dom/server",
|
|
193
197
|
// "react-hook-form",
|
|
194
198
|
// "react-dom"
|
|
195
199
|
],
|
|
200
|
+
esbuildOptions: {
|
|
201
|
+
inject: esbuildInject,
|
|
202
|
+
plugins: esbuildInjectPlugins,
|
|
203
|
+
},
|
|
196
204
|
},
|
|
205
|
+
// future: {
|
|
206
|
+
// removeSsrLoadModule: true
|
|
207
|
+
// },
|
|
197
208
|
}
|
|
198
209
|
}
|
|
199
210
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import path from "path"
|
|
2
|
+
import { createRequire } from "module"
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const require = createRequire(import.meta.url)
|
|
7
|
+
|
|
8
|
+
const splitPackageSpecifier = (specifier) => {
|
|
9
|
+
if (!specifier) return { pkgName: specifier, subpath: "" }
|
|
10
|
+
|
|
11
|
+
if (specifier.startsWith("@")) {
|
|
12
|
+
const segments = specifier.split("/")
|
|
13
|
+
if (segments.length < 2) return { pkgName: specifier, subpath: "" }
|
|
14
|
+
return {
|
|
15
|
+
pkgName: `${segments[0]}/${segments[1]}`,
|
|
16
|
+
subpath: segments.slice(2).join("/"),
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const [pkgName, ...rest] = specifier.split("/")
|
|
21
|
+
return { pkgName, subpath: rest.join("/") }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const withTrailingSep = p => (p.endsWith(path.sep) ? p : p + path.sep)
|
|
25
|
+
|
|
26
|
+
const resolvePackageRoot = (pkg) => {
|
|
27
|
+
const { pkgName } = splitPackageSpecifier(pkg)
|
|
28
|
+
if (!pkgName) return pkg
|
|
29
|
+
|
|
30
|
+
// 1) chemin rapide: si le package exporte son package.json
|
|
31
|
+
try {
|
|
32
|
+
const pkgJson = require.resolve(`${pkgName}/package.json`)
|
|
33
|
+
return path.dirname(pkgJson)
|
|
34
|
+
} catch (err) {
|
|
35
|
+
// 2) fallback: résoudre l'entrée du package puis remonter jusqu'à sa racine
|
|
36
|
+
const entry = require.resolve(pkgName) // ex: .../node_modules/<pkg>/dist/...
|
|
37
|
+
let dir = path.dirname(entry)
|
|
38
|
+
|
|
39
|
+
// remonter jusqu'à un package.json dont "name" === pkg
|
|
40
|
+
while (true) {
|
|
41
|
+
const pkgJsonPath = path.join(dir, "package.json")
|
|
42
|
+
if (existsSync(pkgJsonPath)) {
|
|
43
|
+
try {
|
|
44
|
+
const json = JSON.parse(readFileSync(pkgJsonPath, "utf8"))
|
|
45
|
+
if (json && json.name === pkgName) return dir
|
|
46
|
+
} catch {
|
|
47
|
+
// ignore parsing errors
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const parent = path.dirname(dir)
|
|
51
|
+
if (parent === dir) break
|
|
52
|
+
dir = parent
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 3) dernier recours: reconstruire depuis le segment node_modules (ok avec pnpm)
|
|
56
|
+
const nmMarker = `${path.sep}node_modules${path.sep}`
|
|
57
|
+
const idx = entry.lastIndexOf(nmMarker)
|
|
58
|
+
if (idx !== -1) {
|
|
59
|
+
const nmRoot = entry.slice(0, idx + nmMarker.length)
|
|
60
|
+
return path.join(nmRoot, pkgName)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// si vraiment rien trouvé, relancer l'erreur d'origine
|
|
64
|
+
throw err
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const resolveIntegratedPackagePath = (specifier) => {
|
|
69
|
+
const { pkgName, subpath } = splitPackageSpecifier(specifier)
|
|
70
|
+
if (!pkgName) return specifier
|
|
71
|
+
|
|
72
|
+
const root = resolvePackageRoot(pkgName)
|
|
73
|
+
if (!subpath) {
|
|
74
|
+
return withTrailingSep(root)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const subDir = path.join(root, subpath)
|
|
78
|
+
const pkgJsonPath = path.join(subDir, "package.json")
|
|
79
|
+
if (existsSync(pkgJsonPath)) {
|
|
80
|
+
try {
|
|
81
|
+
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, "utf8"))
|
|
82
|
+
const entry = pkgJson.module || pkgJson.main
|
|
83
|
+
if (entry) {
|
|
84
|
+
return path.join(subDir, entry)
|
|
85
|
+
}
|
|
86
|
+
} catch {
|
|
87
|
+
// ignore parsing errors
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
return require.resolve(specifier)
|
|
93
|
+
} catch {
|
|
94
|
+
return withTrailingSep(subDir)
|
|
95
|
+
}
|
|
96
|
+
}
|