vite-plugin-react-server 0.3.0 → 0.3.4
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/README.md +73 -40
- package/dist/assertServerCondition.d.ts +2 -0
- package/dist/assertServerCondition.d.ts.map +1 -0
- package/dist/bin/patch.js +51 -0
- package/dist/bin/patch.js.map +1 -0
- package/dist/build/createBuildConfig.d.ts +7 -6
- package/dist/build/createBuildConfig.d.ts.map +1 -1
- package/dist/build/mergeInputs.d.ts +5 -0
- package/dist/build/mergeInputs.d.ts.map +1 -0
- package/dist/helpers/inputNormalizer.d.ts +6 -0
- package/dist/helpers/inputNormalizer.d.ts.map +1 -0
- package/dist/helpers/normalizedRelativePath.d.ts +3 -1
- package/dist/helpers/normalizedRelativePath.d.ts.map +1 -1
- package/dist/helpers/tryManifest.d.ts +10 -3
- package/dist/helpers/tryManifest.d.ts.map +1 -1
- package/dist/html/createPageLoader.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/options.d.ts +13 -8
- package/dist/options.d.ts.map +1 -1
- package/dist/plugin.d.ts +2 -7
- package/dist/plugin.d.ts.map +1 -1
- package/dist/react-client/plugin.js +1 -1
- package/dist/react-server/plugin.d.ts.map +1 -1
- package/dist/react-server/plugin.js +108 -61
- package/dist/react-server/plugin.js.map +1 -1
- package/dist/scripts/check-react-version.js +34 -0
- package/dist/scripts/check-react-version.js.map +1 -0
- package/dist/src/build/createBuildConfig.js +44 -0
- package/dist/src/build/createBuildConfig.js.map +1 -0
- package/dist/src/build/mergeInputs.js +16 -0
- package/dist/src/build/mergeInputs.js.map +1 -0
- package/dist/src/checkFilesExist.js.map +1 -0
- package/dist/src/collect-css-manifest.js.map +1 -0
- package/dist/src/components.js.map +1 -0
- package/dist/src/getEnv.js.map +1 -0
- package/dist/src/helpers/inputNormalizer.js +11 -0
- package/dist/src/helpers/inputNormalizer.js.map +1 -0
- package/dist/src/helpers/normalizedRelativePath.js +34 -0
- package/dist/src/helpers/normalizedRelativePath.js.map +1 -0
- package/dist/src/helpers/tryManifest.js +27 -0
- package/dist/src/helpers/tryManifest.js.map +1 -0
- package/dist/{html → src/html}/createPageLoader.js +3 -1
- package/dist/src/html/createPageLoader.js.map +1 -0
- package/dist/{options.js → src/options.js} +80 -24
- package/dist/src/options.js.map +1 -0
- package/dist/src/react-server/createHandler.js.map +1 -0
- package/dist/src/react-server/createRscStream.js.map +1 -0
- package/dist/src/resolvePage.js.map +1 -0
- package/dist/src/resolveProps.js.map +1 -0
- package/dist/src/worker/createHtmlStream.js +62 -0
- package/dist/src/worker/createHtmlStream.js.map +1 -0
- package/dist/{worker → src/worker}/createWorker.js +9 -8
- package/dist/src/worker/createWorker.js.map +1 -0
- package/dist/src/worker/renderPages.js.map +1 -0
- package/dist/types.d.ts +7 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/worker/createWorker.d.ts +8 -1
- package/dist/worker/createWorker.d.ts.map +1 -1
- package/dist/worker/loader.js +7 -0
- package/dist/worker/loader.js.map +1 -0
- package/dist/worker/worker.js +112 -0
- package/dist/worker/worker.js.map +1 -0
- package/package.json +109 -111
- package/patches/react-server-dom-esm+0.0.1.patch +13424 -0
- package/scripts/bump-version.mjs +68 -0
- package/scripts/check-react-version.mjs +48 -0
- package/src/assertServerCondition.ts +13 -0
- package/src/build/createBuildConfig.ts +27 -44
- package/src/build/mergeInputs.ts +42 -0
- package/src/helpers/inputNormalizer.ts +22 -0
- package/src/helpers/normalizedRelativePath.ts +25 -25
- package/src/helpers/tryManifest.ts +19 -5
- package/src/html/createPageLoader.ts +2 -0
- package/src/options.ts +119 -47
- package/src/plugin.ts +4 -30
- package/src/react-server/createSsrHandler.ts +1 -1
- package/src/react-server/plugin.ts +116 -56
- package/src/types.ts +19 -1
- package/src/worker/createWorker.ts +12 -7
- package/tsconfig.json +2 -0
- package/dist/build/createBuildConfig.js +0 -55
- package/dist/build/createBuildConfig.js.map +0 -1
- package/dist/checkFilesExist.js.map +0 -1
- package/dist/collect-css-manifest.js.map +0 -1
- package/dist/components.js.map +0 -1
- package/dist/getEnv.js.map +0 -1
- package/dist/helpers/normalizedRelativePath.js +0 -31
- package/dist/helpers/normalizedRelativePath.js.map +0 -1
- package/dist/html/createPageLoader.js.map +0 -1
- package/dist/options.js.map +0 -1
- package/dist/plugin.js +0 -31
- package/dist/plugin.js.map +0 -1
- package/dist/react-server/createHandler.js.map +0 -1
- package/dist/react-server/createRscStream.js.map +0 -1
- package/dist/resolvePage.js.map +0 -1
- package/dist/resolveProps.js.map +0 -1
- package/dist/transformer/index.js +0 -54
- package/dist/transformer/index.js.map +0 -1
- package/dist/transformer/preserveDirectives.js +0 -72
- package/dist/transformer/preserveDirectives.js.map +0 -1
- package/dist/transformer/transformer.js +0 -80
- package/dist/transformer/transformer.js.map +0 -1
- package/dist/worker/createWorker.js.map +0 -1
- package/dist/worker/renderPages.js.map +0 -1
- /package/dist/{checkFilesExist.js → src/checkFilesExist.js} +0 -0
- /package/dist/{collect-css-manifest.js → src/collect-css-manifest.js} +0 -0
- /package/dist/{components.js → src/components.js} +0 -0
- /package/dist/{getEnv.js → src/getEnv.js} +0 -0
- /package/dist/{react-server → src/react-server}/createHandler.js +0 -0
- /package/dist/{react-server → src/react-server}/createRscStream.js +0 -0
- /package/dist/{resolvePage.js → src/resolvePage.js} +0 -0
- /package/dist/{resolveProps.js → src/resolveProps.js} +0 -0
- /package/dist/{worker → src/worker}/renderPages.js +0 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import fs from 'node:fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Bumps the version of the package.
|
|
7
|
+
* @param {'major' | 'minor' | 'patch'} type - The type of version to bump.
|
|
8
|
+
*/
|
|
9
|
+
async function bumpVersion(type) {
|
|
10
|
+
// Read package.json
|
|
11
|
+
const packageJson = JSON.parse(
|
|
12
|
+
await fs.readFile(path.resolve('package.json'), 'utf-8')
|
|
13
|
+
)
|
|
14
|
+
const packageLockJson = JSON.parse(
|
|
15
|
+
await fs.readFile(path.resolve('package-lock.json'), 'utf-8')
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
// Parse current version
|
|
19
|
+
const [major, minor, patch] = packageJson.version.split('.').map(Number)
|
|
20
|
+
|
|
21
|
+
// Calculate new version
|
|
22
|
+
/**
|
|
23
|
+
* The new version of the package.
|
|
24
|
+
* @type {string}
|
|
25
|
+
*/
|
|
26
|
+
let newVersion
|
|
27
|
+
switch (type) {
|
|
28
|
+
case 'major':
|
|
29
|
+
newVersion = `${major + 1}.0.0`
|
|
30
|
+
break
|
|
31
|
+
case 'minor':
|
|
32
|
+
newVersion = `${major}.${minor + 1}.0`
|
|
33
|
+
break
|
|
34
|
+
case 'patch':
|
|
35
|
+
newVersion = `${major}.${minor}.${patch + 1}`
|
|
36
|
+
break
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Update both files
|
|
40
|
+
packageJson.version = newVersion
|
|
41
|
+
packageLockJson.version = newVersion
|
|
42
|
+
packageLockJson.packages[''].version = newVersion
|
|
43
|
+
|
|
44
|
+
// Write back
|
|
45
|
+
await fs.writeFile(
|
|
46
|
+
path.resolve('package.json'),
|
|
47
|
+
JSON.stringify(packageJson, null, 2) + '\n'
|
|
48
|
+
)
|
|
49
|
+
await fs.writeFile(
|
|
50
|
+
path.resolve('package-lock.json'),
|
|
51
|
+
JSON.stringify(packageLockJson, null, 2) + '\n'
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
console.log(`Version bumped to ${newVersion}`)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Get version type from command line argument
|
|
58
|
+
/**
|
|
59
|
+
* The type of version to bump.
|
|
60
|
+
* @type {'major' | 'minor' | 'patch'}
|
|
61
|
+
*/
|
|
62
|
+
const type = process.argv[2]
|
|
63
|
+
if (!type || !['major', 'minor', 'patch'].includes(type)) {
|
|
64
|
+
console.error('Please specify version type: major, minor, or patch')
|
|
65
|
+
process.exit(1)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
bumpVersion(type).catch(console.error)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'node:fs/promises'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { fileURLToPath } from 'node:url'
|
|
5
|
+
|
|
6
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
7
|
+
|
|
8
|
+
const PATCH_RECONCILER_VERSION = '19.1.0-experimental-b3a95caf-20250113'
|
|
9
|
+
const ALTERNATIVE_REACT_VERSION = '0.0.0-experimental-b3a95caf-20250113'
|
|
10
|
+
const STUB_ESM_VERSION = `0.0.1`
|
|
11
|
+
const PATCH_FILE = `react-server-dom-esm+${STUB_ESM_VERSION}.patch`
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
try {
|
|
15
|
+
// Read installed React version
|
|
16
|
+
const reactPkgPath = path.resolve(process.cwd(), 'node_modules/react/package.json')
|
|
17
|
+
const reactPkg = JSON.parse(await fs.readFile(reactPkgPath, 'utf-8'))
|
|
18
|
+
const installedVersion = reactPkg.version
|
|
19
|
+
|
|
20
|
+
// Get our patch file from our package
|
|
21
|
+
const ourPatchPath = path.resolve(__dirname, `../patches/${PATCH_FILE}`)
|
|
22
|
+
let patchContent = await fs.readFile(ourPatchPath, 'utf-8')
|
|
23
|
+
|
|
24
|
+
// Replace the version in the patch content
|
|
25
|
+
patchContent = patchContent.replace(
|
|
26
|
+
new RegExp(PATCH_RECONCILER_VERSION, 'g'),
|
|
27
|
+
installedVersion
|
|
28
|
+
).replace(
|
|
29
|
+
new RegExp(ALTERNATIVE_REACT_VERSION, 'g'),
|
|
30
|
+
installedVersion
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
// Create patches dir in user's project
|
|
34
|
+
const userPatchesDir = path.resolve(process.cwd(), 'patches')
|
|
35
|
+
await fs.mkdir(userPatchesDir, { recursive: true })
|
|
36
|
+
|
|
37
|
+
// Write the patch file - use stub version for filename
|
|
38
|
+
const newFileName = `react-server-dom-esm+${STUB_ESM_VERSION}.patch`
|
|
39
|
+
const newPatchPath = path.resolve(userPatchesDir, newFileName)
|
|
40
|
+
await fs.writeFile(newPatchPath, patchContent)
|
|
41
|
+
|
|
42
|
+
console.log(`Updated patch to match React version ${installedVersion}`)
|
|
43
|
+
} catch (error) {
|
|
44
|
+
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
main()
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function assertServerCondition<P extends typeof process>(p: P): asserts p is P & { env: { NODE_OPTIONS: string } } {
|
|
2
|
+
const nodeOptions = p.env['NODE_OPTIONS'];
|
|
3
|
+
if (!nodeOptions?.match(/--conditions[= ]react-server/)) {
|
|
4
|
+
if(!nodeOptions?.match(/--conditions/)) {
|
|
5
|
+
console.warn(`process.env['NODE_OPTIONS'] is missing \`--conditions\` flag, it should be like \`--conditions=react-server\` or \`--conditions react-server\`, but was ${JSON.stringify(process.env['NODE_OPTIONS'])}`);
|
|
6
|
+
}
|
|
7
|
+
if(!nodeOptions?.match(/react-server/)) {
|
|
8
|
+
throw new Error(`process.env['NODE_OPTIONS'] is missing \`react-server\` flag, it should be like \`--conditions=react-server\` or \`--conditions react-server\`, but was ${JSON.stringify(process.env['NODE_OPTIONS'])}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return undefined;
|
|
12
|
+
};
|
|
13
|
+
assertServerCondition(process);
|
|
@@ -1,62 +1,43 @@
|
|
|
1
|
-
import { dirname, resolve } from "node:path";
|
|
2
|
-
import { fileURLToPath } from "node:url";
|
|
3
1
|
import type { InlineConfig } from "vite";
|
|
4
|
-
import {
|
|
5
|
-
import type {
|
|
6
|
-
|
|
2
|
+
import type { ResolvedUserConfig, ResolvedUserOptions } from "../types.js";
|
|
3
|
+
import type { InputOption } from "rollup";
|
|
4
|
+
import { mergeInputs } from "./mergeInputs.js";
|
|
5
|
+
import { createInputNormalizer } from "../helpers/inputNormalizer.js";
|
|
7
6
|
|
|
8
7
|
type CreateBuildConfigOptions = {
|
|
8
|
+
input: InputOption;
|
|
9
|
+
userOptions: ResolvedUserOptions;
|
|
10
|
+
userConfig: ResolvedUserConfig;
|
|
9
11
|
root: string;
|
|
10
|
-
|
|
11
|
-
outDir: string;
|
|
12
|
-
entries: string[];
|
|
13
|
-
options?: StreamPluginOptions;
|
|
12
|
+
moduleBaseExceptions: string[];
|
|
14
13
|
};
|
|
15
14
|
|
|
15
|
+
|
|
16
16
|
export function createBuildConfig({
|
|
17
17
|
root,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
input,
|
|
19
|
+
userOptions,
|
|
20
|
+
userConfig,
|
|
21
|
+
moduleBaseExceptions
|
|
22
22
|
}: CreateBuildConfigOptions) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
entries.map((entry) => {
|
|
26
|
-
// Get the path relative to root
|
|
27
|
-
const relativePath = entry.replace(root + "/", "");
|
|
28
|
-
// Create a unique key based on the full path
|
|
29
|
-
const key = relativePath.replace(/\.[^/.]+$/, ""); // Remove extension
|
|
30
|
-
|
|
31
|
-
return [key, entry];
|
|
32
|
-
})
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
const workerPath = options?.workerPath
|
|
36
|
-
? resolve(root, options.workerPath)
|
|
37
|
-
: resolve(__dirname, "..", DEFAULT_CONFIG.WORKER_PATH);
|
|
23
|
+
const { output, input: inputConfig, ...restRollupOptions } =
|
|
24
|
+
userConfig.build.rollupOptions ?? {};
|
|
38
25
|
|
|
39
|
-
|
|
40
|
-
? resolve(root, options.loaderPath)
|
|
41
|
-
: resolve(__dirname, "..", DEFAULT_CONFIG.LOADER_PATH);
|
|
26
|
+
let mergedInputs = mergeInputs(input, inputConfig);
|
|
42
27
|
|
|
28
|
+
let inputNormalizer = createInputNormalizer({
|
|
29
|
+
root,
|
|
30
|
+
});
|
|
31
|
+
if(typeof mergedInputs === 'object' && mergedInputs != null) {
|
|
32
|
+
mergedInputs = Object.fromEntries(Object.entries(mergedInputs).map(inputNormalizer));
|
|
33
|
+
}
|
|
43
34
|
const config: InlineConfig = {
|
|
44
35
|
configFile: false,
|
|
45
|
-
|
|
46
|
-
base,
|
|
36
|
+
...userConfig,
|
|
47
37
|
build: {
|
|
48
|
-
|
|
49
|
-
ssr: true,
|
|
50
|
-
ssrEmitAssets: false,
|
|
51
|
-
manifest: true,
|
|
52
|
-
ssrManifest: true,
|
|
53
|
-
outDir,
|
|
38
|
+
...userConfig.build,
|
|
54
39
|
rollupOptions: {
|
|
55
|
-
input:
|
|
56
|
-
...entryInputs,
|
|
57
|
-
worker: workerPath,
|
|
58
|
-
loader: loaderPath,
|
|
59
|
-
},
|
|
40
|
+
input: mergedInputs,
|
|
60
41
|
output: {
|
|
61
42
|
format: "esm",
|
|
62
43
|
preserveModules: true,
|
|
@@ -65,7 +46,9 @@ export function createBuildConfig({
|
|
|
65
46
|
entryFileNames: "[name].js",
|
|
66
47
|
chunkFileNames: "[name].js",
|
|
67
48
|
assetFileNames: "[name][extname]",
|
|
49
|
+
...output,
|
|
68
50
|
},
|
|
51
|
+
...restRollupOptions,
|
|
69
52
|
},
|
|
70
53
|
},
|
|
71
54
|
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { InputOption } from "rollup";
|
|
2
|
+
|
|
3
|
+
export const mergeAsArray = (entries: InputOption) => {
|
|
4
|
+
return Array.isArray(entries)
|
|
5
|
+
? entries
|
|
6
|
+
: typeof entries === "object" && entries != null
|
|
7
|
+
? Object.values(entries)
|
|
8
|
+
: typeof entries === "string"
|
|
9
|
+
? [entries]
|
|
10
|
+
: [];
|
|
11
|
+
};
|
|
12
|
+
export const mergeAsObject = (entries: InputOption) => {
|
|
13
|
+
return Array.isArray(entries)
|
|
14
|
+
? Object.fromEntries(entries.map((entry) => [entry, entry]))
|
|
15
|
+
: typeof entries === "object" && entries != null
|
|
16
|
+
? entries
|
|
17
|
+
: typeof entries === "string"
|
|
18
|
+
? { [entries]: entries }
|
|
19
|
+
: {};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const mergeInputs = (
|
|
23
|
+
input: InputOption,
|
|
24
|
+
input2: InputOption | undefined
|
|
25
|
+
) => {
|
|
26
|
+
if (!input2) return input;
|
|
27
|
+
return Array.isArray(input)
|
|
28
|
+
? [...input, ...mergeAsArray(input2)]
|
|
29
|
+
: typeof input === "string"
|
|
30
|
+
? [input, ...mergeAsArray(input2)]
|
|
31
|
+
: input != null && typeof input2 === "object" && input2 != null
|
|
32
|
+
? { ...input, ...mergeAsObject(input2) }
|
|
33
|
+
: input != null
|
|
34
|
+
? input
|
|
35
|
+
: Array.isArray(input2)
|
|
36
|
+
? input2
|
|
37
|
+
: typeof input2 === "object" && input2 != null
|
|
38
|
+
? input2
|
|
39
|
+
: typeof input2 === "string"
|
|
40
|
+
? input2
|
|
41
|
+
: [];
|
|
42
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
|
|
3
|
+
type NormalizedInputOptions = {
|
|
4
|
+
// will automatically remove this part
|
|
5
|
+
root?: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const createInputNormalizer =
|
|
9
|
+
(
|
|
10
|
+
{
|
|
11
|
+
root = process.cwd(),
|
|
12
|
+
}: NormalizedInputOptions = {} as NormalizedInputOptions
|
|
13
|
+
) =>
|
|
14
|
+
([key, path]: [string, string]) =>
|
|
15
|
+
[
|
|
16
|
+
key,
|
|
17
|
+
path.startsWith(root)
|
|
18
|
+
? relative(root, path)
|
|
19
|
+
: path.startsWith("/")
|
|
20
|
+
? path
|
|
21
|
+
: `/${path}`,
|
|
22
|
+
];
|
|
@@ -4,56 +4,56 @@ type NormalizedRelativePathOptions = {
|
|
|
4
4
|
// will automatically remove this part
|
|
5
5
|
root: string;
|
|
6
6
|
// will automatically see this as a optional extra part of the rootDir that will be removed
|
|
7
|
-
outDir: string
|
|
7
|
+
outDir: string;
|
|
8
8
|
// will ensure it always starts with this path, if it does not it will be added
|
|
9
9
|
moduleBase: string;
|
|
10
10
|
// will ensure it never starts with a leading /, which in some cases is needed (vite entry), other cases it is not for example from project root /
|
|
11
11
|
noLeadingSlash: boolean;
|
|
12
|
+
// will ensure it never ends with a trailing /
|
|
13
|
+
noTrailingSlash: boolean;
|
|
14
|
+
// allowed exception to moduleBase rules
|
|
15
|
+
moduleBaseExceptions: string[];
|
|
12
16
|
};
|
|
13
17
|
|
|
14
18
|
export const createNormalizedRelativePath = (
|
|
15
19
|
options: NormalizedRelativePathOptions = {
|
|
16
20
|
root: process.cwd(),
|
|
17
|
-
outDir:
|
|
21
|
+
outDir: "dist",
|
|
18
22
|
moduleBase: "src",
|
|
19
23
|
noLeadingSlash: false,
|
|
24
|
+
noTrailingSlash: false,
|
|
25
|
+
moduleBaseExceptions: [],
|
|
20
26
|
}
|
|
21
27
|
) => {
|
|
22
|
-
|
|
28
|
+
let base =
|
|
23
29
|
options.noLeadingSlash && options.moduleBase.startsWith("/")
|
|
24
30
|
? options.moduleBase.slice(1)
|
|
25
31
|
: options.moduleBase;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
: (path: string) =>
|
|
34
|
-
(options.outDir as string) === path
|
|
35
|
-
? path.slice(options.outDir.length)
|
|
36
|
-
: path;
|
|
32
|
+
if (options.noTrailingSlash && base.endsWith("/")) {
|
|
33
|
+
base = base.slice(0, -1);
|
|
34
|
+
}
|
|
35
|
+
const removeOutDir = (path: string) =>
|
|
36
|
+
(options.outDir as string) === path
|
|
37
|
+
? path.slice(options.outDir.length)
|
|
38
|
+
: path;
|
|
37
39
|
|
|
38
40
|
const removeRoot = (path: string) => {
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
: normalized;
|
|
41
|
+
const relative = path.startsWith(options.root)
|
|
42
|
+
? path.slice(options.root.length)
|
|
43
|
+
: path;
|
|
43
44
|
return relative;
|
|
44
45
|
};
|
|
45
46
|
|
|
46
47
|
const ensureModuleBase = (path: string) => {
|
|
48
|
+
let transformed = path;
|
|
47
49
|
if (options.noLeadingSlash && path.startsWith("/")) {
|
|
48
|
-
|
|
50
|
+
transformed = path.slice(1);
|
|
49
51
|
}
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
`Path ${path} does not start with module base ${base}, this will not work down the line.`
|
|
53
|
-
);
|
|
52
|
+
if (options.noTrailingSlash && transformed.endsWith("/")) {
|
|
53
|
+
transformed = transformed.slice(0, -1);
|
|
54
54
|
}
|
|
55
|
-
return
|
|
55
|
+
return transformed;
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
-
return (path: string) => ensureModuleBase(removeOutDir(removeRoot(path)));
|
|
58
|
+
return (path: string) => ensureModuleBase(removeOutDir(removeRoot(normalizePath(path))));
|
|
59
59
|
};
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
|
+
import type { Manifest } from "vite";
|
|
3
4
|
|
|
4
|
-
type TryManifestOptions = {
|
|
5
|
+
type TryManifestOptions<SSR extends boolean> = {
|
|
5
6
|
root: string;
|
|
6
7
|
outDir: string;
|
|
7
|
-
ssrManifest:
|
|
8
|
+
ssrManifest: SSR;
|
|
8
9
|
};
|
|
9
10
|
|
|
10
|
-
export function tryManifest(options: TryManifestOptions) {
|
|
11
|
+
export function tryManifest<SSR extends boolean>(options: TryManifestOptions<SSR>): {
|
|
12
|
+
type: "success";
|
|
13
|
+
manifest: SSR extends true ? Record<string, string[]> : Manifest;
|
|
14
|
+
} | {
|
|
15
|
+
type: "error";
|
|
16
|
+
error: Error;
|
|
17
|
+
} {
|
|
11
18
|
const manifestPath = resolve(
|
|
12
19
|
options.root,
|
|
13
20
|
options.outDir,
|
|
@@ -15,9 +22,16 @@ export function tryManifest(options: TryManifestOptions) {
|
|
|
15
22
|
options.ssrManifest ? "ssr-manifest.json" : "manifest.json"
|
|
16
23
|
);
|
|
17
24
|
try {
|
|
18
|
-
|
|
25
|
+
const result= JSON.parse(readFileSync(manifestPath, "utf-8"));
|
|
26
|
+
return {
|
|
27
|
+
type: "success",
|
|
28
|
+
manifest: result,
|
|
29
|
+
}
|
|
19
30
|
} catch (e) {
|
|
20
31
|
console.log("No manifest found", manifestPath);
|
|
21
|
-
return
|
|
32
|
+
return {
|
|
33
|
+
type: "error",
|
|
34
|
+
error: e as Error,
|
|
35
|
+
}
|
|
22
36
|
}
|
|
23
37
|
}
|