Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wise-parsers-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

chore: parse page options and remote modules with Vite's `parseSync` instead of `acorn`
2 changes: 0 additions & 2 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
"dependencies": {
"@rolldown/pluginutils": "^1.0.1",
"@standard-schema/spec": "^1.1.0",
"@sveltejs/acorn-typescript": "^1.0.12",
"acorn": "^8.18.0",
"cookie": "^2.0.1",
"devalue": "^5.9.0",
"esm-env": "^1.2.2",
Expand Down
8 changes: 4 additions & 4 deletions packages/kit/src/exports/vite/build/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import fs from 'node:fs';
import path from 'node:path';
import { Parser } from 'acorn';
import MagicString from 'magic-string';
import { posixify } from '../../../utils/os.js';

Expand Down Expand Up @@ -59,11 +58,12 @@ export async function treeshake_prerendered_remotes(
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 parsed = vite.parseSync(chunk_path, code);
if (parsed.errors.length) throw new Error(parsed.errors[0].message);
const modified_code = new MagicString(code);

for (const fn of prerendered) {
for (const node of parsed.body) {
for (const node of parsed.program.body) {
const declaration =
node.type === 'ExportNamedDeclaration'
? node.declaration
Expand All @@ -85,7 +85,7 @@ export async function treeshake_prerendered_remotes(
}
}

for (const node of parsed.body) {
for (const node of parsed.program.body) {
if (node.type === 'ExportDefaultDeclaration') {
modified_code.remove(node.start, node.end);
}
Expand Down
30 changes: 15 additions & 15 deletions packages/kit/src/exports/vite/static_analysis/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @import { PageOptions } from './types.js' */
/** @import { ESTree } from 'vite' */
import path from 'node:path';
import { tsPlugin } from '@sveltejs/acorn-typescript';
import { Parser } from 'acorn';
import { parseSync } from 'vite';
import { read } from '../../../utils/filesystem.js';

export const valid_page_options_array = /** @type {const} */ ([
Expand All @@ -21,8 +21,6 @@ const skip_parsing_regex = new RegExp(
`${Array.from(valid_page_options).join('|')}|(?:export[\\s\\n]+\\*[\\s\\n]+from)`
);

const parser = Parser.extend(tsPlugin());

/**
* Collects page options from a +page.js/+layout.js file, ignoring reassignments
* and using the declared value (except for load functions, for which the value is `true`).
Expand All @@ -39,15 +37,13 @@ export function statically_analyse_page_options(filename, input) {
}

try {
const source = parser.parse(input, {
sourceType: 'module',
ecmaVersion: 'latest'
});
const source = parseSync(filename, input, { sourceType: 'module' });
if (source.errors.length) throw new Error(source.errors[0].message);

/** @type {Map<string, import('acorn').Literal['value']>} */
/** @type {Map<string, Extract<ESTree.Expression, { type: 'Literal' }>['value']>} */
const page_options = new Map();

for (const statement of source.body) {
for (const statement of source.program.body) {
// ignore export all declarations with aliases that are not page options
if (
statement.type === 'ExportAllDeclaration' &&
Expand Down Expand Up @@ -82,7 +78,7 @@ export function statically_analyse_page_options(filename, input) {
export_specifiers.set(get_name(specifier.local), exported_name);
}

for (const statement of source.body) {
for (const statement of source.program.body) {
switch (statement.type) {
case 'ImportDeclaration': {
for (const import_specifier of statement.specifiers) {
Expand All @@ -105,7 +101,10 @@ export function statically_analyse_page_options(filename, input) {

// class and function declarations
if (declaration.type !== 'VariableDeclaration') {
if (export_specifiers.has(declaration.id.name)) {
if (
declaration.id?.type === 'Identifier' &&
export_specifiers.has(declaration.id.name)
) {
return null;
}
break;
Expand Down Expand Up @@ -156,9 +155,10 @@ export function statically_analyse_page_options(filename, input) {

// class and function declarations
if (statement.declaration.type !== 'VariableDeclaration') {
if (valid_page_options.has(statement.declaration.id.name)) {
const { id } = statement.declaration;
if (id?.type === 'Identifier' && valid_page_options.has(id.name)) {
// Special case: We only want to know that 'load' is exported (in a way that doesn't cause truthy checks in other places to trigger)
if (statement.declaration.id.name === 'load') {
if (id.name === 'load') {
page_options.set('load', null);
} else {
return null;
Expand Down Expand Up @@ -202,7 +202,7 @@ export function statically_analyse_page_options(filename, input) {
}

/**
* @param {import('acorn').Identifier | import('acorn').Literal} node
* @param {ESTree.ModuleExportName} node
* @returns {string}
*/
function get_name(node) {
Expand Down
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading