@astrojs/cloudflare 10.2.0 → 10.2.2
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/dist/index.js +34 -32
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.js +48 -0
- package/package.json +3 -1
package/dist/index.js
CHANGED
|
@@ -4,9 +4,12 @@ import { createInterface } from 'node:readline/promises';
|
|
|
4
4
|
import { appendForwardSlash, prependForwardSlash, removeLeadingForwardSlash, } from '@astrojs/internal-helpers/path';
|
|
5
5
|
import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
|
|
6
6
|
import { AstroError } from 'astro/errors';
|
|
7
|
+
import { walk } from 'estree-walker';
|
|
8
|
+
import MagicString from 'magic-string';
|
|
7
9
|
import { getPlatformProxy } from 'wrangler';
|
|
8
10
|
import { createRoutesFile, getParts } from './utils/generate-routes-json.js';
|
|
9
11
|
import { setImageConfig } from './utils/image-config.js';
|
|
12
|
+
import { mutateDynamicPageImportsInPlace, mutatePageMapInPlace } from './utils/index.js';
|
|
10
13
|
import { NonServerChunkDetector } from './utils/non-server-chunk-detector.js';
|
|
11
14
|
import { wasmModuleLoader } from './utils/wasm-module-loader.js';
|
|
12
15
|
export default function createIntegration(args) {
|
|
@@ -15,7 +18,6 @@ export default function createIntegration(args) {
|
|
|
15
18
|
// The analyzer is used on earlier hooks to collect information about used hooks on a Vite plugin
|
|
16
19
|
// and then later after the full build to clean up unused chunks, so it has to be shared between them.
|
|
17
20
|
const chunkAnalyzer = new NonServerChunkDetector();
|
|
18
|
-
const prerenderImports = [];
|
|
19
21
|
return {
|
|
20
22
|
name: '@astrojs/cloudflare',
|
|
21
23
|
hooks: {
|
|
@@ -38,44 +40,44 @@ export default function createIntegration(args) {
|
|
|
38
40
|
name: 'dynamic-imports-analyzer',
|
|
39
41
|
enforce: 'post',
|
|
40
42
|
generateBundle(_, bundle) {
|
|
43
|
+
let astrojsSSRVirtualEntryAST;
|
|
44
|
+
const prerenderImports = [];
|
|
45
|
+
let entryChunk;
|
|
41
46
|
// Find all pages (ignore the ssr entrypoint) which are prerendered based on the dynamic imports of the prerender chunk
|
|
42
47
|
for (const chunk of Object.values(bundle)) {
|
|
43
48
|
if (chunk.type !== 'chunk')
|
|
44
49
|
continue;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
if (chunk.name === '_@astrojs-ssr-virtual-entry') {
|
|
51
|
+
astrojsSSRVirtualEntryAST = this.parse(chunk.code);
|
|
52
|
+
entryChunk = chunk;
|
|
53
|
+
continue;
|
|
48
54
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
entryChunk.type === 'chunk' &&
|
|
53
|
-
entryChunk.name === '_@astrojs-ssr-virtual-entry') {
|
|
54
|
-
// Update dynamicImports information, so that there are no imports listed which we remove later
|
|
55
|
-
entryChunk.dynamicImports = entryChunk.dynamicImports.filter((entry) => !prerenderImports.map((e) => e[1]).includes(entry));
|
|
56
|
-
// Clean the ssr entry file from prerendered imporst, since Astro adds them, which it shouldn't. But this is a current limitation in core, because the prerender meta info gets added later in the chain
|
|
57
|
-
for (const page of prerenderImports) {
|
|
58
|
-
// Find the dynamic import inside of the ssr entry file, which get generated by Astro: https://github.com/withastro/astro/blob/08cdd0919d3249a762822e4bba9e0c5d3966916c/packages/astro/src/core/build/plugins/plugin-ssr.ts#L56
|
|
59
|
-
const importRegex = new RegExp(`^const (_page\\d) = \\(\\) => import\\('.\\/${page[1]}'\\);$\\n`, 'gm');
|
|
60
|
-
let pageId;
|
|
61
|
-
const matches = entryChunk.code.matchAll(importRegex);
|
|
62
|
-
for (const match of matches) {
|
|
63
|
-
if (match[1]) {
|
|
64
|
-
pageId = match[1];
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const pageSource = page[0].split(':')[1].replace('@_@', '.');
|
|
68
|
-
entryChunk.code = entryChunk.code.replace(importRegex, '');
|
|
69
|
-
if (pageId) {
|
|
70
|
-
// Find the page in the pageMap of the ssr entry file, which get generated by Astro: https://github.com/withastro/astro/blob/08cdd0919d3249a762822e4bba9e0c5d3966916c/packages/astro/src/core/build/plugins/plugin-ssr.ts#L65
|
|
71
|
-
const arrayRegex = new RegExp(`\\["${pageSource}", ?${pageId}\\],?`, 'gm');
|
|
72
|
-
entryChunk.code = entryChunk.code.replace(arrayRegex, '');
|
|
73
|
-
}
|
|
55
|
+
const isPrerendered = chunk.dynamicImports.some((entry) => entry.includes('prerender'));
|
|
56
|
+
if (isPrerendered) {
|
|
57
|
+
prerenderImports.push(chunk.fileName);
|
|
74
58
|
}
|
|
75
59
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
60
|
+
if (!astrojsSSRVirtualEntryAST)
|
|
61
|
+
return;
|
|
62
|
+
if (!entryChunk)
|
|
63
|
+
return;
|
|
64
|
+
const s = new MagicString(entryChunk.code);
|
|
65
|
+
const constsToRemove = [];
|
|
66
|
+
walk(astrojsSSRVirtualEntryAST, {
|
|
67
|
+
leave(node) {
|
|
68
|
+
// We are only looking for VariableDeclarations, since both (dynamic imports and pageMap) are declared as constants in the code
|
|
69
|
+
if (node.type !== 'VariableDeclaration')
|
|
70
|
+
return;
|
|
71
|
+
if (!node.declarations[0] ||
|
|
72
|
+
node.declarations[0].type !== 'VariableDeclarator')
|
|
73
|
+
return;
|
|
74
|
+
// This function will remove the dynamic imports from the entrypoint
|
|
75
|
+
mutateDynamicPageImportsInPlace(node, prerenderImports, constsToRemove, s);
|
|
76
|
+
// This function will remove the pageMap entries which are invalid now
|
|
77
|
+
mutatePageMapInPlace(node, constsToRemove, s);
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
entryChunk.code = s.toString();
|
|
79
81
|
},
|
|
80
82
|
},
|
|
81
83
|
],
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Node } from 'estree-walker';
|
|
2
|
+
import type MagicString from 'magic-string';
|
|
3
|
+
export declare function mutatePageMapInPlace(node: Extract<Node, {
|
|
4
|
+
type: 'VariableDeclaration';
|
|
5
|
+
}>, constsToRemove: string[], s: MagicString): void;
|
|
6
|
+
export declare function mutateDynamicPageImportsInPlace(node: Extract<Node, {
|
|
7
|
+
type: 'VariableDeclaration';
|
|
8
|
+
}>, prerenderImports: string[], constsToRemove: string[], s: MagicString): void;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export function mutatePageMapInPlace(node, constsToRemove, s) {
|
|
2
|
+
const declarator = node.declarations[0];
|
|
3
|
+
if (declarator.id.type !== 'Identifier')
|
|
4
|
+
return;
|
|
5
|
+
if (declarator.id.name !== 'pageMap')
|
|
6
|
+
return;
|
|
7
|
+
if (!declarator.init || declarator.init.type !== 'NewExpression')
|
|
8
|
+
return;
|
|
9
|
+
if (!declarator.init.arguments[0] || declarator.init.arguments[0].type !== 'ArrayExpression')
|
|
10
|
+
return;
|
|
11
|
+
for (const arrayExpression of declarator.init.arguments[0].elements) {
|
|
12
|
+
if (!arrayExpression || arrayExpression.type !== 'ArrayExpression')
|
|
13
|
+
continue;
|
|
14
|
+
if (!arrayExpression.elements[1] || arrayExpression.elements[1].type !== 'Identifier')
|
|
15
|
+
continue;
|
|
16
|
+
if (constsToRemove.includes(arrayExpression.elements[1].name)) {
|
|
17
|
+
// @ts-expect-error - @types/estree seem to be wrong
|
|
18
|
+
if (arrayExpression.start && arrayExpression.end) {
|
|
19
|
+
// @ts-expect-error - @types/estree seem to be wrong
|
|
20
|
+
s.remove(arrayExpression.start, arrayExpression.end);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function mutateDynamicPageImportsInPlace(node, prerenderImports, constsToRemove, s) {
|
|
26
|
+
const declarator = node.declarations[0];
|
|
27
|
+
if (declarator.id.type !== 'Identifier')
|
|
28
|
+
return;
|
|
29
|
+
if (!declarator.id.name.startsWith('_page'))
|
|
30
|
+
return;
|
|
31
|
+
if (!declarator.init || declarator.init.type !== 'ArrowFunctionExpression')
|
|
32
|
+
return;
|
|
33
|
+
if (!declarator.init.body || declarator.init.body.type !== 'ImportExpression')
|
|
34
|
+
return;
|
|
35
|
+
if (!declarator.init.body.source || declarator.init.body.source.type !== 'Literal')
|
|
36
|
+
return;
|
|
37
|
+
const sourceValue = declarator.init.body.source.value;
|
|
38
|
+
if (typeof sourceValue !== 'string')
|
|
39
|
+
return;
|
|
40
|
+
if (prerenderImports.some((importItem) => sourceValue.includes(importItem))) {
|
|
41
|
+
// @ts-expect-error - @types/estree seem to be wrong
|
|
42
|
+
if (node.start && node.end) {
|
|
43
|
+
constsToRemove.push(declarator.id.name);
|
|
44
|
+
// @ts-expect-error - @types/estree seem to be wrong
|
|
45
|
+
s.remove(node.start, node.end);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/cloudflare",
|
|
3
3
|
"description": "Deploy your site to Cloudflare Workers/Pages",
|
|
4
|
-
"version": "10.2.
|
|
4
|
+
"version": "10.2.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
"@astrojs/underscore-redirects": "^0.3.3",
|
|
33
33
|
"@cloudflare/workers-types": "^4.20240320.1",
|
|
34
34
|
"esbuild": "^0.19.5",
|
|
35
|
+
"estree-walker": "^3.0.3",
|
|
36
|
+
"magic-string": "^0.30.10",
|
|
35
37
|
"miniflare": "^3.20240320.0",
|
|
36
38
|
"tiny-glob": "^0.2.9",
|
|
37
39
|
"wrangler": "^3.39.0"
|