Skip to content
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

upgrade to svelte5 #454

Merged
merged 46 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d3dd010
upgrade to svelte5
ryanatkn Mar 24, 2024
2653ca7
exposes internals
ryanatkn Mar 24, 2024
f8fbed7
Merge branch 'main' into svelte5
ryanatkn Mar 24, 2024
d2d5853
Merge branch 'main' into svelte5
ryanatkn Mar 26, 2024
827eae3
Merge branch 'main' into svelte5
ryanatkn Apr 4, 2024
585a5e1
Merge branch 'main' into svelte5
ryanatkn Apr 13, 2024
6bf3482
wip
ryanatkn Apr 13, 2024
f502b81
wip
ryanatkn Apr 13, 2024
085231b
wip
ryanatkn Apr 13, 2024
a9ad3d4
wip
ryanatkn Apr 13, 2024
dcf17c8
wip
ryanatkn Apr 13, 2024
7f20e10
wip
ryanatkn Apr 13, 2024
4390c98
wip
ryanatkn Apr 14, 2024
256862d
Merge branch 'main' into svelte5
ryanatkn Apr 14, 2024
0438b9d
Merge branch 'main' into svelte5
ryanatkn Apr 14, 2024
8bd036e
wip
ryanatkn Apr 30, 2024
c7e699e
wip
ryanatkn Apr 30, 2024
31255e9
wip
ryanatkn Apr 30, 2024
e413e88
dep
ryanatkn Apr 30, 2024
8287307
dep
ryanatkn Apr 30, 2024
bf61351
esnext
ryanatkn Apr 30, 2024
3b31ccb
wip
ryanatkn May 1, 2024
53fb6ea
wip
ryanatkn May 1, 2024
f60729d
Merge branch 'main' into svelte5
ryanatkn May 1, 2024
401bae3
wip
ryanatkn May 1, 2024
a673978
wip
ryanatkn May 1, 2024
f085117
wip
ryanatkn May 1, 2024
8c1bd2e
wip
ryanatkn May 1, 2024
1f9dde3
wip
ryanatkn May 19, 2024
44a7e3b
wip
ryanatkn May 19, 2024
437670b
wip
ryanatkn May 20, 2024
2d11e82
wip
ryanatkn May 20, 2024
2cc359c
wip
ryanatkn May 20, 2024
03d8955
wip
ryanatkn May 20, 2024
fa3d214
wip
ryanatkn May 20, 2024
e01a551
wip
ryanatkn May 20, 2024
db38951
wip
ryanatkn May 20, 2024
a5d0fb8
wip
ryanatkn May 25, 2024
7d5f09b
wip
ryanatkn May 25, 2024
1824666
wip
ryanatkn May 25, 2024
7dd31a9
wip
ryanatkn May 26, 2024
ed9ce77
wip
ryanatkn May 26, 2024
300a911
wip
ryanatkn May 26, 2024
e7dde4d
wip
ryanatkn May 26, 2024
af7d025
wip
ryanatkn May 26, 2024
e0409b7
wip
ryanatkn May 26, 2024
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/eleven-spiders-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ryanatkn/gro": minor
---

upgrade to svelte@5 and esbuild@0.20
887 changes: 447 additions & 440 deletions package-lock.json

Large diffs are not rendered by default.

43 changes: 24 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"node": ">=20.10"
},
"scripts": {
"build": "rm -rf .gro dist && svelte-package && chmod +x ./dist/gro.js && npm link -f",
"build": "rm -rf .gro dist && svelte-kit sync && svelte-package && chmod +x ./dist/gro.js && npm link -f",
"start": "gro dev",
"test": "gro test"
},
Expand All @@ -48,39 +48,40 @@
"@ryanatkn/belt": "^0.20.10",
"chokidar": "^3.6.0",
"dotenv": "^16.4.5",
"es-module-lexer": "^1.5.0",
"es-module-lexer": "^1.5.3",
"kleur": "^4.1.5",
"mri": "^1.2.0",
"prettier": "^3.2.5",
"prettier-plugin-svelte": "^3.2.2",
"prettier-plugin-svelte": "^3.2.3",
"tiny-glob": "^0.2.9",
"ts-morph": "^22.0.0",
"tslib": "^2.6.2",
"zod": "^3.22.4"
"zod": "^3.23.8"
},
"peerDependencies": {
"esbuild": "^0.19",
"svelte": "*"
"esbuild": "^0.20",
"svelte": "^5.0.0-next.0"
},
"devDependencies": {
"@changesets/changelog-git": "^0.2.0",
"@changesets/types": "^6.0.0",
"@ryanatkn/eslint-config": "^0.1.0",
"@ryanatkn/fuz": "^0.92.0",
"@ryanatkn/eslint-config": "^0.1.2",
"@ryanatkn/fuz": "^0.101.2",
"@ryanatkn/moss": "^0.3.0",
"@sveltejs/adapter-static": "^3.0.1",
"@sveltejs/kit": "^2.5.4",
"@sveltejs/package": "^2.3.0",
"@sveltejs/vite-plugin-svelte": "^3.0.2",
"@sveltejs/kit": "^2.5.10",
"@sveltejs/package": "^2.3.1",
"@sveltejs/vite-plugin-svelte": "^3.1.0",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.11.30",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"esbuild": "^0.19.0",
"@types/node": "^20.12.12",
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/parser": "^7.10.0",
"esbuild": "^0.20.2",
"eslint": "^8.57.0",
"eslint-plugin-svelte": "^2.35.1",
"svelte": "^4.2.12",
"svelte-check": "^3.6.8",
"typescript": "^5.4.3",
"eslint-plugin-svelte": "^2.39.0",
"svelte": "^5.0.0-next.141",
"svelte-check": "^3.7.1",
"typescript": "^5.4.5",
"uvu": "^0.5.6"
},
"eslintConfig": {
Expand Down Expand Up @@ -357,6 +358,10 @@
"default": "./dist/src_json.js",
"types": "./dist/src_json.d.ts"
},
"./svelte_helpers.js": {
"default": "./dist/svelte_helpers.js",
"types": "./dist/svelte_helpers.d.ts"
},
"./sveltekit_config.js": {
"default": "./dist/sveltekit_config.js",
"types": "./dist/sveltekit_config.d.ts"
Expand Down
8 changes: 8 additions & 0 deletions src/fixtures/modules/Some_Test_Svelte.svelte
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<script lang="ts" context="module">
export const a = 'ok';
</script>

<script lang="ts">
// ...
</script>

Some_Test_Svelte.svelte contents
1 change: 1 addition & 0 deletions src/fixtures/modules/some_test_css.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* some_test_css */
.some_test_css {
--a: ok;
--some_test_property: red;
}
9 changes: 9 additions & 0 deletions src/fixtures/modules/some_test_js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const a = 'ok';

export const some_test_js = '.js';

export const some_test_fn = () => true;

export class Some_Test_Class {
a = 1;
}
1 change: 1 addition & 0 deletions src/fixtures/modules/some_test_json.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"a": "ok",
"some_test_json": ".json"
}
5 changes: 5 additions & 0 deletions src/fixtures/modules/some_test_server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Some_Test_Svelte_Ts} from './some_test_svelte_ts.svelte.js';

export const some_test_server = 'some_test_server';

export const Rexported_Some_Test_Svelte_Ts = Some_Test_Svelte_Ts;
3 changes: 3 additions & 0 deletions src/fixtures/modules/some_test_svelte_js.svelte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Some_Test_Svelte_Js {
a = $state('ok');
}
3 changes: 3 additions & 0 deletions src/fixtures/modules/some_test_svelte_ts.svelte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Some_Test_Svelte_Ts {
a = $state('ok');
}
2 changes: 2 additions & 0 deletions src/fixtures/modules/some_test_ts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const a = 'ok';

export const some_test_ts = '.ts';

export const some_test_fn = (): boolean => true;
Expand Down
88 changes: 88 additions & 0 deletions src/lib/esbuild_plugin_svelte.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {test} from 'uvu';
import * as assert from 'uvu/assert';
import * as esbuild from 'esbuild';
import {readFile, rm} from 'node:fs/promises';

import {esbuild_plugin_svelte} from './esbuild_plugin_svelte.js';

test('build for the client', async () => {
const outfile = './src/fixtures/modules/some_test_server_bundle_DELETEME.js';
const built = await esbuild.build({
entryPoints: ['./src/fixtures/modules/some_test_server.ts'],
plugins: [esbuild_plugin_svelte()],
outfile,
format: 'esm',
platform: 'node',
packages: 'external',
bundle: true,
target: 'esnext',
});
assert.is(built.errors.length, 0);
assert.is(built.warnings.length, 0);

const built_output = await readFile(outfile, 'utf8');
assert.is(
built_output,
`// src/fixtures/modules/some_test_svelte_ts.svelte.ts
import * as $ from "svelte/internal/client";
var Some_Test_Svelte_Ts = class {
#a = $.source("ok");
get a() {
return $.get(this.#a);
}
set a(value) {
$.set(this.#a, $.proxy(value));
}
};

// src/fixtures/modules/some_test_server.ts
var some_test_server = "some_test_server";
var Rexported_Some_Test_Svelte_Ts = Some_Test_Svelte_Ts;
export {
Rexported_Some_Test_Svelte_Ts,
some_test_server
};
`,
);

await rm(outfile); // TODO could be cleaner
});

test('build for the server', async () => {
const outfile = './src/fixtures/modules/some_test_client_bundle_DELETEME.js';
const built = await esbuild.build({
entryPoints: ['./src/fixtures/modules/some_test_server.ts'],
plugins: [esbuild_plugin_svelte({svelte_compile_module_options: {generate: 'server'}})],
outfile,
format: 'esm',
platform: 'node',
packages: 'external',
bundle: true,
target: 'esnext',
});
assert.is(built.errors.length, 0);
assert.is(built.warnings.length, 0);

const built_output = await readFile(outfile, 'utf8');
assert.is(
built_output,
`// src/fixtures/modules/some_test_svelte_ts.svelte.ts
import * as $ from "svelte/internal/server";
var Some_Test_Svelte_Ts = class {
a = "ok";
};

// src/fixtures/modules/some_test_server.ts
var some_test_server = "some_test_server";
var Rexported_Some_Test_Svelte_Ts = Some_Test_Svelte_Ts;
export {
Rexported_Some_Test_Svelte_Ts,
some_test_server
};
`,
);

await rm(outfile); // TODO could be cleaner
});

test.run();
97 changes: 64 additions & 33 deletions src/lib/esbuild_plugin_svelte.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,83 @@
import type * as esbuild from 'esbuild';
import {compile, preprocess, type CompileOptions, type PreprocessorGroup} from 'svelte/compiler';
import {
compile,
compileModule,
preprocess,
type CompileOptions,
type ModuleCompileOptions,
type PreprocessorGroup,
} from 'svelte/compiler';
import {readFile} from 'node:fs/promises';
import {relative} from 'node:path';
import {cwd} from 'node:process';

import {SVELTE_MATCHER, SVELTE_RUNES_MATCHER} from './svelte_helpers.js';

export interface Options {
dir?: string;
// These `svelte_` prefixes are unnecessary and verbose
// but they align with the upstream APIs,
// so it's simpler overall like for project-wide searching.
svelte_compile_options?: CompileOptions;
svelte_compile_module_options?: ModuleCompileOptions;
svelte_preprocessors?: PreprocessorGroup | PreprocessorGroup[];
}

export const esbuild_plugin_svelte = ({
dir = cwd(),
svelte_compile_options,
svelte_preprocessors,
}: Options): esbuild.Plugin => ({
name: 'svelte',
setup: (build) => {
build.onLoad({filter: /\.svelte$/u}, async ({path}) => {
let source = await readFile(path, 'utf8');
try {
const filename = relative(dir, path);
const preprocessed = svelte_preprocessors
? await preprocess(source, svelte_preprocessors, {filename})
: null;
// TODO handle preprocessor sourcemaps, same as in loader - merge?
if (preprocessed?.code) source = preprocessed?.code;
const {js, warnings} = compile(source, svelte_compile_options);
const contents = js.map ? js.code + '//# sourceMappingURL=' + js.map.toUrl() : js.code;
return {
contents,
warnings: warnings.map((w) => to_sveltekit_message(filename, source, w)),
};
} catch (err) {
return {errors: [to_sveltekit_message(path, source, err)]};
}
});
},
});
export const esbuild_plugin_svelte = (options: Options = {}): esbuild.Plugin => {
const {
dir = cwd(),
svelte_compile_options = {},
svelte_compile_module_options = {},
svelte_preprocessors,
} = options;
return {
name: 'svelte',
setup: (build) => {
build.onLoad({filter: SVELTE_RUNES_MATCHER}, async ({path}) => {
const source = await readFile(path, 'utf8');
try {
const filename = relative(dir, path);
const {js, warnings} = compileModule(source, {
filename,
...svelte_compile_module_options,
});
const contents = js.map ? js.code + '//# sourceMappingURL=' + js.map.toUrl() : js.code;
return {
contents,
warnings: warnings.map((w) => convert_svelte_message_to_esbuild(filename, source, w)),
};
} catch (err) {
return {errors: [convert_svelte_message_to_esbuild(path, source, err)]};
}
});
build.onLoad({filter: SVELTE_MATCHER}, async ({path}) => {
let source = await readFile(path, 'utf8');
try {
const filename = relative(dir, path);
const preprocessed = svelte_preprocessors
? await preprocess(source, svelte_preprocessors, {filename})
: null;
// TODO handle preprocessor sourcemaps, same as in loader - merge?
if (preprocessed?.code) source = preprocessed?.code;
const {js, warnings} = compile(source, {
filename,
...svelte_compile_options,
});
const contents = js.map ? js.code + '//# sourceMappingURL=' + js.map.toUrl() : js.code;
return {
contents,
warnings: warnings.map((w) => convert_svelte_message_to_esbuild(filename, source, w)),
};
} catch (err) {
return {errors: [convert_svelte_message_to_esbuild(path, source, err)]};
}
});
},
};
};

/**
* Following the example in the esbuild docs:
* https://esbuild.github.io/plugins/#svelte-plugin
*/
const to_sveltekit_message = (
const convert_svelte_message_to_esbuild = (
path: string,
source: string,
{message, start, end}: SvelteError,
Expand Down
1 change: 0 additions & 1 deletion src/lib/esbuild_plugin_sveltekit_local_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const esbuild_plugin_sveltekit_local_imports = (): esbuild.Plugin => ({
}));
build.onLoad({filter: /.*/u, namespace: 'sveltekit_local_imports_js'}, async ({path}) => ({
contents: await readFile(path),
loader: 'js',
resolveDir: dirname(path),
}));
},
Expand Down
2 changes: 0 additions & 2 deletions src/lib/esbuild_plugin_sveltekit_shim_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ export const esbuild_plugin_sveltekit_shim_app = ({
namespace: 'sveltekit_shim_app_paths',
}));
build.onLoad({filter: /.*/u, namespace: 'sveltekit_shim_app_paths'}, () => ({
loader: 'js',
contents: render_sveltekit_shim_app_paths(base_url, assets_url),
}));
build.onResolve({filter: /^\$app\/environment$/u}, ({path}) => ({
path: sveltekit_shim_app_specifiers.get(path)!,
namespace: 'sveltekit_shim_app_environment',
}));
build.onLoad({filter: /.*/u, namespace: 'sveltekit_shim_app_environment'}, () => ({
loader: 'js',
contents: render_sveltekit_shim_app_environment(dev),
}));
},
Expand Down
5 changes: 5 additions & 0 deletions src/lib/gro_plugin_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ export const gro_plugin_server = ({
svelte_preprocessors,
} = await init_sveltekit_config(sveltekit_config ?? dir);

// TODO feels hacky
if (svelte_compile_options.generate === undefined) {
svelte_compile_options.generate = 'server';
}

const {outbase, outdir, outname} = outpaths(dev);

const server_outpath = join(outdir, outname);
Expand Down
Loading
Loading