-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: reimplement treeshaking non-dynamic prerendered remote functions #15447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
teemingc
wants to merge
12
commits into
main
Choose a base branch
from
treeshake-prerendered
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−5
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4f690c7
reimplement treeshaking
teemingc 854a82b
changeset
teemingc 9592805
whoops
teemingc 3415251
Update packages/kit/src/exports/vite/build/remote.js
teemingc 065e6f5
fix test
teemingc 44064a6
Merge branch 'treeshake-prerendered' of https://github.com/sveltejs/k…
teemingc ff8a7cb
llm hallucination
teemingc 733fd0f
oops
teemingc 58289aa
respect sourcemap option
teemingc 3ad1ead
Merge branch 'main' into treeshake-prerendered
teemingc 7e63740
accidental removal
teemingc 25a685f
Merge branch 'main' into treeshake-prerendered
teemingc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/kit': patch | ||
| --- | ||
|
|
||
| fix: reimplement treeshaking non-dynamic prerendered remote functions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /** @import { ServerMetadata } from 'types' */ | ||
| /** @import { OutputBundle } from 'rollup' */ | ||
|
|
||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { Parser } from 'acorn'; | ||
| import MagicString from 'magic-string'; | ||
| import { posixify } from '../../../utils/filesystem.js'; | ||
| import { import_peer } from '../../../utils/import.js'; | ||
|
|
||
| /** | ||
| * @param {string} out | ||
| * @param {Array<{ hash: string, file: string }>} remotes | ||
| * @param {ServerMetadata} metadata | ||
| * @param {string} cwd | ||
| * @param {OutputBundle} server_bundle | ||
| * @param {NonNullable<import('vitest/config').ViteUserConfig['build']>['sourcemap']} sourcemap | ||
| */ | ||
| export async function treeshake_prerendered_remotes( | ||
| out, | ||
| remotes, | ||
| metadata, | ||
| cwd, | ||
| server_bundle, | ||
| sourcemap | ||
| ) { | ||
| if (remotes.length === 0) return; | ||
|
|
||
| const vite = /** @type {typeof import('vite')} */ (await import_peer('vite')); | ||
|
|
||
| for (const remote of remotes) { | ||
| const exports_map = metadata.remotes.get(remote.hash); | ||
| if (!exports_map) continue; | ||
|
|
||
| /** @type {string[]} */ | ||
| const dynamic = []; | ||
| /** @type {string[]} */ | ||
| const prerendered = []; | ||
|
|
||
| for (const [name, value] of exports_map) { | ||
| (value.dynamic ? dynamic : prerendered).push(name); | ||
| } | ||
|
|
||
| if (prerendered.length === 0) continue; // nothing to treeshake | ||
|
|
||
| // remove file extension | ||
| const remote_filename = path.basename(remote.file).split('.').slice(0, -1).join('.'); | ||
|
|
||
| const remote_chunk = Object.values(server_bundle).find((chunk) => { | ||
| return chunk.name === remote_filename; | ||
| }); | ||
|
|
||
| if (!remote_chunk) continue; | ||
|
|
||
| const chunk_path = posixify(path.relative(cwd, `${out}/server/${remote_chunk.fileName}`)); | ||
|
|
||
| const code = fs.readFileSync(chunk_path, 'utf-8'); | ||
| const parsed = Parser.parse(code, { sourceType: 'module', ecmaVersion: 'latest' }); | ||
| const modified_code = new MagicString(code); | ||
|
|
||
| for (const fn of prerendered) { | ||
| for (const node of parsed.body) { | ||
| const declaration = | ||
| node.type === 'ExportNamedDeclaration' | ||
| ? node.declaration | ||
| : node.type === 'VariableDeclaration' | ||
| ? node | ||
| : null; | ||
|
|
||
| if (!declaration || declaration.type !== 'VariableDeclaration') continue; | ||
|
|
||
| for (const declarator of declaration.declarations) { | ||
| if (declarator.id.type === 'Identifier' && declarator.id.name === fn) { | ||
| modified_code.overwrite( | ||
| node.start, | ||
| node.end, | ||
| `const ${fn} = prerender('unchecked', () => { throw new Error('Unexpectedly called prerender function. Did you forget to set { dynamic: true } ?') });` | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const node of parsed.body) { | ||
| if (node.type === 'ExportDefaultDeclaration') { | ||
| modified_code.remove(node.start, node.end); | ||
| } | ||
| } | ||
|
|
||
| const stubbed = modified_code.toString(); | ||
| fs.writeFileSync(chunk_path, stubbed); | ||
|
|
||
| const bundle = /** @type {import('vite').Rollup.RollupOutput} */ ( | ||
| await vite.build({ | ||
| configFile: false, | ||
| build: { | ||
| write: false, | ||
| ssr: true, | ||
| target: 'esnext', | ||
| sourcemap, | ||
| rollupOptions: { | ||
| // avoid resolving imports | ||
| external: (id) => !id.endsWith(chunk_path), | ||
| input: { | ||
| treeshaken: chunk_path | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| ); | ||
|
|
||
| const chunk = bundle.output.find( | ||
| (output) => output.type === 'chunk' && output.name === 'treeshaken' | ||
| ); | ||
| if (chunk && chunk.type === 'chunk') { | ||
| fs.writeFileSync(chunk_path, chunk.code); | ||
|
|
||
| const chunk_sourcemap = bundle.output.find( | ||
| (output) => output.type === 'asset' && output.fileName === chunk.fileName + '.map' | ||
| ); | ||
| if (chunk_sourcemap && chunk_sourcemap.type === 'asset') { | ||
| fs.writeFileSync(chunk_path + '.map', chunk_sourcemap.source); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,25 @@ | ||
| import process from 'node:process'; | ||
| import { expect } from '@playwright/test'; | ||
| import { test } from '../../../utils.js'; | ||
| import fs from 'node:fs'; | ||
| import process from 'node:process'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { expect } from '@playwright/test'; | ||
| import { test } from '../../../utils.js'; | ||
|
|
||
| test.skip(({ javaScriptEnabled }) => javaScriptEnabled); | ||
|
|
||
| const root = path.resolve(fileURLToPath(import.meta.url), '..', '..'); | ||
|
|
||
| test.describe('remote functions', () => { | ||
| test("doesn't write bundle to disk when treeshaking prerendered remote functions", () => { | ||
| test.skip(!!process.env.DEV, 'skip when in dev mode'); | ||
| test.skip(!!process.env.DEV, 'only applicable after build'); | ||
| expect(fs.existsSync(path.join(root, 'dist'))).toBe(false); | ||
| }); | ||
|
|
||
| test('non-dynamic prerendered remote functions are treeshaken', () => { | ||
| test.skip(!!process.env.DEV, 'only applicable after build'); | ||
| const code = fs.readFileSync( | ||
| path.join(root, '.svelte-kit', 'output', 'server', 'chunks', 'prerender.remote.js') | ||
| ); | ||
| expect(code.includes('const with_read = prerender(')).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.