@rpcbase/vite 0.74.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 +53 -92
- 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,100 +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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (segments.length < 2) return { pkgName: specifier, subpath: "" }
|
|
30
|
-
return {
|
|
31
|
-
pkgName: `${segments[0]}/${segments[1]}`,
|
|
32
|
-
subpath: segments.slice(2).join("/"),
|
|
27
|
+
fs.mkdirSync(injectDir, { recursive: true })
|
|
28
|
+
try {
|
|
29
|
+
const existing = fs.readFileSync(injectPath, "utf8")
|
|
30
|
+
if (existing === injectContents) {
|
|
31
|
+
return injectPath
|
|
33
32
|
}
|
|
33
|
+
} catch {
|
|
34
|
+
// file does not exist yet
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
return
|
|
37
|
+
fs.writeFileSync(injectPath, injectContents, "utf8")
|
|
38
|
+
return injectPath
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// 2) fallback: résoudre l'entrée du package puis remonter jusqu'à sa racine
|
|
50
|
-
const entry = require.resolve(pkgName) // ex: .../node_modules/<pkg>/dist/...
|
|
51
|
-
let dir = path.dirname(entry)
|
|
52
|
-
|
|
53
|
-
// remonter jusqu'à un package.json dont "name" === pkg
|
|
54
|
-
while (true) {
|
|
55
|
-
const pkgJsonPath = path.join(dir, "package.json")
|
|
56
|
-
if (existsSync(pkgJsonPath)) {
|
|
57
|
-
try {
|
|
58
|
-
const json = JSON.parse(readFileSync(pkgJsonPath, "utf8"))
|
|
59
|
-
if (json && json.name === pkgName) return dir
|
|
60
|
-
} catch {
|
|
61
|
-
// ignore parsing errors
|
|
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" }
|
|
62
50
|
}
|
|
63
|
-
}
|
|
64
|
-
const parent = path.dirname(dir)
|
|
65
|
-
if (parent === dir) break
|
|
66
|
-
dir = parent
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// 3) dernier recours: reconstruire depuis le segment node_modules (ok avec pnpm)
|
|
70
|
-
const nmMarker = `${path.sep}node_modules${path.sep}`
|
|
71
|
-
const idx = entry.lastIndexOf(nmMarker)
|
|
72
|
-
if (idx !== -1) {
|
|
73
|
-
const nmRoot = entry.slice(0, idx + nmMarker.length)
|
|
74
|
-
return path.join(nmRoot, pkgName)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// si vraiment rien trouvé, relancer l'erreur d'origine
|
|
78
|
-
throw err
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const withTrailingSep = p => (p.endsWith(path.sep) ? p : p + path.sep)
|
|
83
|
-
|
|
84
|
-
const resolveIntegratedPackagePath = (specifier) => {
|
|
85
|
-
const { pkgName, subpath } = splitPackageSpecifier(specifier)
|
|
86
|
-
if (!pkgName) return specifier
|
|
87
|
-
|
|
88
|
-
const root = resolvePackageRoot(pkgName)
|
|
89
|
-
if (!subpath) {
|
|
90
|
-
return withTrailingSep(root)
|
|
91
|
-
}
|
|
92
51
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const entry = pkgJson.module || pkgJson.main
|
|
99
|
-
if (entry) {
|
|
100
|
-
return path.join(subDir, entry)
|
|
101
|
-
}
|
|
102
|
-
} catch {
|
|
103
|
-
// ignore parsing errors
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
return require.resolve(specifier)
|
|
109
|
-
} catch {
|
|
110
|
-
return withTrailingSep(subDir)
|
|
111
|
-
}
|
|
112
|
-
}
|
|
52
|
+
return undefined
|
|
53
|
+
})
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
]
|
|
113
57
|
|
|
114
58
|
|
|
115
59
|
// const isCI = process.env.CI === "true"
|
|
@@ -163,6 +107,15 @@ integratedPackages.forEach(pkg => {
|
|
|
163
107
|
|
|
164
108
|
export {resolveAliases}
|
|
165
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
|
+
]
|
|
166
119
|
|
|
167
120
|
// https://vite.dev/config/
|
|
168
121
|
const getBaseConfig = ({ command, mode, isSsrBuild, isPreview }) => {
|
|
@@ -185,9 +138,9 @@ const getBaseConfig = ({ command, mode, isSsrBuild, isPreview }) => {
|
|
|
185
138
|
isProduction && posthogSourcemapsPlugin({}),
|
|
186
139
|
].filter(Boolean),
|
|
187
140
|
define: {
|
|
188
|
-
|
|
141
|
+
"globalThis.__rb_env__": JSON.stringify({
|
|
189
142
|
...env,
|
|
190
|
-
},
|
|
143
|
+
}),
|
|
191
144
|
},
|
|
192
145
|
envPrefix: ALLOWED_ENV_PREFIXES,
|
|
193
146
|
publicDir: path.join(process.cwd(), "./src/client/public"),
|
|
@@ -225,25 +178,33 @@ const getBaseConfig = ({ command, mode, isSsrBuild, isPreview }) => {
|
|
|
225
178
|
// "react", "react-dom",
|
|
226
179
|
"cookie",
|
|
227
180
|
],
|
|
228
|
-
|
|
181
|
+
noExternal: [
|
|
182
|
+
...rbPackagesNeedingEnvPolyfill,
|
|
183
|
+
"react-use",
|
|
184
|
+
],
|
|
229
185
|
},
|
|
230
186
|
optimizeDeps: {
|
|
231
187
|
include: [
|
|
188
|
+
// Force RPC Base packages through esbuild so rb-env-polyfill injects before their dist code touches globalThis.__rb_env__
|
|
189
|
+
...rbPackagesNeedingEnvPolyfill,
|
|
232
190
|
// "react", "react-dom", "react-dom/server"
|
|
233
191
|
// "cookie"
|
|
234
192
|
],
|
|
235
193
|
exclude: [
|
|
236
|
-
"@rpcbase/auth",
|
|
237
|
-
"@rpcbase/env",
|
|
238
|
-
"@rpcbase/form",
|
|
239
|
-
"@rpcbase/router",
|
|
240
194
|
"@radix-ui/*",
|
|
241
195
|
// TMP only in sample app?
|
|
242
196
|
// "react", "react-dom/server",
|
|
243
197
|
// "react-hook-form",
|
|
244
198
|
// "react-dom"
|
|
245
199
|
],
|
|
200
|
+
esbuildOptions: {
|
|
201
|
+
inject: esbuildInject,
|
|
202
|
+
plugins: esbuildInjectPlugins,
|
|
203
|
+
},
|
|
246
204
|
},
|
|
205
|
+
// future: {
|
|
206
|
+
// removeSsrLoadModule: true
|
|
207
|
+
// },
|
|
247
208
|
}
|
|
248
209
|
}
|
|
249
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
|
+
}
|