@astrojs/cloudflare 10.1.0 → 10.2.1
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
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
import { createReadStream } from 'node:fs';
|
|
2
|
-
import { appendFile, rename, stat } from 'node:fs/promises';
|
|
2
|
+
import { appendFile, rename, stat, unlink } from 'node:fs/promises';
|
|
3
3
|
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';
|
|
13
|
+
import { NonServerChunkDetector } from './utils/non-server-chunk-detector.js';
|
|
10
14
|
import { wasmModuleLoader } from './utils/wasm-module-loader.js';
|
|
11
15
|
export default function createIntegration(args) {
|
|
12
16
|
let _config;
|
|
17
|
+
// Initialize the unused chunk analyzer as a shared state between hooks.
|
|
18
|
+
// The analyzer is used on earlier hooks to collect information about used hooks on a Vite plugin
|
|
19
|
+
// and then later after the full build to clean up unused chunks, so it has to be shared between them.
|
|
20
|
+
const chunkAnalyzer = new NonServerChunkDetector();
|
|
13
21
|
return {
|
|
14
22
|
name: '@astrojs/cloudflare',
|
|
15
23
|
hooks: {
|
|
@@ -27,6 +35,51 @@ export default function createIntegration(args) {
|
|
|
27
35
|
wasmModuleLoader({
|
|
28
36
|
disabled: !args?.wasmModuleImports,
|
|
29
37
|
}),
|
|
38
|
+
chunkAnalyzer.getPlugin(),
|
|
39
|
+
{
|
|
40
|
+
name: 'dynamic-imports-analyzer',
|
|
41
|
+
enforce: 'post',
|
|
42
|
+
generateBundle(_, bundle) {
|
|
43
|
+
let astrojsSSRVirtualEntryAST;
|
|
44
|
+
const prerenderImports = [];
|
|
45
|
+
let entryChunk;
|
|
46
|
+
// Find all pages (ignore the ssr entrypoint) which are prerendered based on the dynamic imports of the prerender chunk
|
|
47
|
+
for (const chunk of Object.values(bundle)) {
|
|
48
|
+
if (chunk.type !== 'chunk')
|
|
49
|
+
continue;
|
|
50
|
+
if (chunk.name === '_@astrojs-ssr-virtual-entry') {
|
|
51
|
+
astrojsSSRVirtualEntryAST = this.parse(chunk.code);
|
|
52
|
+
entryChunk = chunk;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const isPrerendered = chunk.dynamicImports.some((entry) => entry.includes('prerender'));
|
|
56
|
+
if (isPrerendered) {
|
|
57
|
+
prerenderImports.push(chunk.fileName);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
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();
|
|
81
|
+
},
|
|
82
|
+
},
|
|
30
83
|
],
|
|
31
84
|
},
|
|
32
85
|
image: setImageConfig(args?.imageService ?? 'DEFAULT', config.image, command, logger),
|
|
@@ -217,6 +270,13 @@ export default function createIntegration(args) {
|
|
|
217
270
|
logger.error('Failed to write _redirects file');
|
|
218
271
|
}
|
|
219
272
|
}
|
|
273
|
+
// Get chunks from the bundle that are not needed on the server and delete them
|
|
274
|
+
// Those modules are build only for prerendering routes.
|
|
275
|
+
const chunksToDelete = chunkAnalyzer.getNonServerChunks();
|
|
276
|
+
for (const chunk of chunksToDelete) {
|
|
277
|
+
// Chunks are located on `./_worker.js` directory inside of the output directory
|
|
278
|
+
await unlink(new URL(`./_worker.js/${chunk}`, _config.outDir));
|
|
279
|
+
}
|
|
220
280
|
},
|
|
221
281
|
},
|
|
222
282
|
};
|
|
@@ -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,49 @@
|
|
|
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
|
+
console.log(arrayExpression);
|
|
18
|
+
// @ts-expect-error - @types/estree seem to be wrong
|
|
19
|
+
if (arrayExpression.start && arrayExpression.end) {
|
|
20
|
+
// @ts-expect-error - @types/estree seem to be wrong
|
|
21
|
+
s.remove(arrayExpression.start, arrayExpression.end);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function mutateDynamicPageImportsInPlace(node, prerenderImports, constsToRemove, s) {
|
|
27
|
+
const declarator = node.declarations[0];
|
|
28
|
+
if (declarator.id.type !== 'Identifier')
|
|
29
|
+
return;
|
|
30
|
+
if (!declarator.id.name.startsWith('_page'))
|
|
31
|
+
return;
|
|
32
|
+
if (!declarator.init || declarator.init.type !== 'ArrowFunctionExpression')
|
|
33
|
+
return;
|
|
34
|
+
if (!declarator.init.body || declarator.init.body.type !== 'ImportExpression')
|
|
35
|
+
return;
|
|
36
|
+
if (!declarator.init.body.source || declarator.init.body.source.type !== 'Literal')
|
|
37
|
+
return;
|
|
38
|
+
const sourceValue = declarator.init.body.source.value;
|
|
39
|
+
if (typeof sourceValue !== 'string')
|
|
40
|
+
return;
|
|
41
|
+
if (prerenderImports.some((importItem) => sourceValue.includes(importItem))) {
|
|
42
|
+
// @ts-expect-error - @types/estree seem to be wrong
|
|
43
|
+
if (node.start && node.end) {
|
|
44
|
+
constsToRemove.push(declarator.id.name);
|
|
45
|
+
// @ts-expect-error - @types/estree seem to be wrong
|
|
46
|
+
s.remove(node.start, node.end);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
/**
|
|
3
|
+
* A Vite bundle analyzer that identifies chunks that are not used for server rendering.
|
|
4
|
+
*
|
|
5
|
+
* The chunks injected by Astro for prerendering are flagged as non-server chunks.
|
|
6
|
+
* Any chunks that is only used by a non-server chunk are also flagged as non-server chunks.
|
|
7
|
+
* This continues transitively until all non-server chunks are found.
|
|
8
|
+
*/
|
|
9
|
+
export declare class NonServerChunkDetector {
|
|
10
|
+
private nonServerChunks?;
|
|
11
|
+
getPlugin(): Plugin;
|
|
12
|
+
private processBundle;
|
|
13
|
+
getNonServerChunks(): string[];
|
|
14
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Vite bundle analyzer that identifies chunks that are not used for server rendering.
|
|
3
|
+
*
|
|
4
|
+
* The chunks injected by Astro for prerendering are flagged as non-server chunks.
|
|
5
|
+
* Any chunks that is only used by a non-server chunk are also flagged as non-server chunks.
|
|
6
|
+
* This continues transitively until all non-server chunks are found.
|
|
7
|
+
*/
|
|
8
|
+
export class NonServerChunkDetector {
|
|
9
|
+
nonServerChunks;
|
|
10
|
+
getPlugin() {
|
|
11
|
+
return {
|
|
12
|
+
name: 'non-server-chunk-detector',
|
|
13
|
+
generateBundle: (_, bundle) => {
|
|
14
|
+
this.processBundle(bundle);
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
processBundle(bundle) {
|
|
19
|
+
const chunkNamesToFiles = new Map();
|
|
20
|
+
const entryChunks = [];
|
|
21
|
+
const chunkToDependencies = new Map();
|
|
22
|
+
for (const chunk of Object.values(bundle)) {
|
|
23
|
+
if (chunk.type !== 'chunk')
|
|
24
|
+
continue;
|
|
25
|
+
// Construct a mapping from a chunk name to its file name
|
|
26
|
+
chunkNamesToFiles.set(chunk.name, chunk.fileName);
|
|
27
|
+
// Construct a mapping from a chunk file to all the modules it imports
|
|
28
|
+
chunkToDependencies.set(chunk.fileName, [...chunk.imports, ...chunk.dynamicImports]);
|
|
29
|
+
if (chunk.isEntry) {
|
|
30
|
+
// Entry chunks should always be kept around since they are to be imported by the runtime
|
|
31
|
+
entryChunks.push(chunk.fileName);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const chunkDecisions = new Map();
|
|
35
|
+
for (const entry of entryChunks) {
|
|
36
|
+
// Entry chunks are used on the server
|
|
37
|
+
chunkDecisions.set(entry, true);
|
|
38
|
+
}
|
|
39
|
+
for (const chunk of ['prerender', 'prerender@_@astro']) {
|
|
40
|
+
// Prerender chunks are not used on the server
|
|
41
|
+
const fileName = chunkNamesToFiles.get(chunk);
|
|
42
|
+
if (fileName) {
|
|
43
|
+
chunkDecisions.set(fileName, false);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// Start a stack of chunks that are used on the server
|
|
47
|
+
const chunksToWalk = [...entryChunks];
|
|
48
|
+
// Iterate over the chunks, traversing the transitive dependencies of the chunks used on the server
|
|
49
|
+
for (let chunk = chunksToWalk.pop(); chunk; chunk = chunksToWalk.pop()) {
|
|
50
|
+
for (const dep of chunkToDependencies.get(chunk) ?? []) {
|
|
51
|
+
// Skip dependencies already flagged, dependencies may be repeated and/or circular
|
|
52
|
+
if (chunkDecisions.has(dep))
|
|
53
|
+
continue;
|
|
54
|
+
// A dependency of a module used on the server is also used on the server
|
|
55
|
+
chunkDecisions.set(dep, true);
|
|
56
|
+
// Add the dependency to the stack so its own dependencies are also flagged
|
|
57
|
+
chunksToWalk.push(dep);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Any chunk not flagged as used on the server is a non-server chunk
|
|
61
|
+
this.nonServerChunks = Array.from(chunkToDependencies.keys()).filter((chunk) => !chunkDecisions.get(chunk));
|
|
62
|
+
}
|
|
63
|
+
getNonServerChunks() {
|
|
64
|
+
return this.nonServerChunks ?? [];
|
|
65
|
+
}
|
|
66
|
+
}
|
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.1
|
|
4
|
+
"version": "10.2.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -28,11 +28,13 @@
|
|
|
28
28
|
"dist"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@astrojs/underscore-redirects": "^0.3.3",
|
|
32
31
|
"@astrojs/internal-helpers": "0.3.0",
|
|
32
|
+
"@astrojs/underscore-redirects": "^0.3.3",
|
|
33
33
|
"@cloudflare/workers-types": "^4.20240320.1",
|
|
34
|
-
"miniflare": "^3.20240320.0",
|
|
35
34
|
"esbuild": "^0.19.5",
|
|
35
|
+
"estree-walker": "^3.0.3",
|
|
36
|
+
"magic-string": "^0.30.10",
|
|
37
|
+
"miniflare": "^3.20240320.0",
|
|
36
38
|
"tiny-glob": "^0.2.9",
|
|
37
39
|
"wrangler": "^3.39.0"
|
|
38
40
|
},
|
|
@@ -40,11 +42,13 @@
|
|
|
40
42
|
"astro": "^4.2.0"
|
|
41
43
|
},
|
|
42
44
|
"devDependencies": {
|
|
45
|
+
"astro": "^4.5.8",
|
|
46
|
+
"cheerio": "1.0.0-rc.12",
|
|
43
47
|
"execa": "^8.0.1",
|
|
44
48
|
"fast-glob": "^3.3.2",
|
|
49
|
+
"rollup": "^4.14.0",
|
|
45
50
|
"strip-ansi": "^7.1.0",
|
|
46
|
-
"
|
|
47
|
-
"cheerio": "1.0.0-rc.12",
|
|
51
|
+
"vite": "^5.2.6",
|
|
48
52
|
"@astrojs/test-utils": "0.0.1",
|
|
49
53
|
"astro-scripts": "0.0.14"
|
|
50
54
|
},
|