diff --git a/.changeset/dirty-doors-hide.md b/.changeset/dirty-doors-hide.md new file mode 100644 index 000000000000..da40c8d476a1 --- /dev/null +++ b/.changeset/dirty-doors-hide.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/package': patch +--- + +chore: bump `svelte2tsx` dependency for more up-to-date `d.ts` output diff --git a/.changeset/pretty-lizards-attend.md b/.changeset/pretty-lizards-attend.md new file mode 100644 index 000000000000..51d8dbc4bc7a --- /dev/null +++ b/.changeset/pretty-lizards-attend.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/package': patch +--- + +fix: adjust declaration map paths diff --git a/documentation/docs/30-advanced/70-packaging.md b/documentation/docs/30-advanced/70-packaging.md index 04d9b4dace04..93294e3f2616 100644 --- a/documentation/docs/30-advanced/70-packaging.md +++ b/documentation/docs/30-advanced/70-packaging.md @@ -218,6 +218,23 @@ You should think carefully about whether or not the changes you make to your pac } ``` +## Source maps + +You can create so-called declaration maps (`d.ts.map` files) by setting `"declarationMap": true` in your `tsconfig.json`. This will allow editors such as VS Code to go to the original `.ts` or `.svelte` file when using features like _Go to Definition_. This means you also need to publish your source files alongside your dist folder in a way that the relative path inside the declaration files leads to a file on disk. Assuming that you have all your library code inside `src/lib` as suggested by Svelte's CLI, this is as simple as adding `src/lib` to `files` in your `package.json`: + +```json +{ + "files": [ + "dist", + "!dist/**/*.test.*", + "!dist/**/*.spec.*", + +++"src/lib", + "!src/lib/**/*.test.*", + "!src/lib/**/*.spec.*"+++ + ] +} +``` + ## Options `svelte-package` accepts the following options: diff --git a/packages/package/package.json b/packages/package/package.json index 1ab629b29654..2d8a3d1a7e19 100644 --- a/packages/package/package.json +++ b/packages/package/package.json @@ -24,7 +24,7 @@ "kleur": "^4.1.5", "sade": "^1.8.1", "semver": "^7.5.4", - "svelte2tsx": "~0.7.16" + "svelte2tsx": "~0.7.33" }, "devDependencies": { "@sveltejs/vite-plugin-svelte": "^5.0.1", diff --git a/packages/package/src/index.js b/packages/package/src/index.js index 05d3d3d7116b..2630e657cdb5 100644 --- a/packages/package/src/index.js +++ b/packages/package/src/index.js @@ -34,7 +34,7 @@ async function do_build(options, analyse_code) { const files = scan(input, extensions); if (options.types) { - await emit_dts(input, temp, options.cwd, alias, files, tsconfig); + await emit_dts(input, temp, output, options.cwd, alias, files, tsconfig); } for (const file of files) { @@ -137,7 +137,7 @@ export async function watch(options) { if (!errored && options.types) { try { - await emit_dts(input, output, options.cwd, alias, files, tsconfig); + await emit_dts(input, output, output, options.cwd, alias, files, tsconfig); console.log('Updated .d.ts files'); } catch (e) { errored = true; diff --git a/packages/package/src/typescript.js b/packages/package/src/typescript.js index 7608c8624880..12d1c32d710f 100644 --- a/packages/package/src/typescript.js +++ b/packages/package/src/typescript.js @@ -14,12 +14,13 @@ import { load_pkg_json } from './config.js'; * * @param {string} input * @param {string} output + * @param {string} final_output * @param {string} cwd * @param {Record} alias * @param {import('./types.js').File[]} files * @param {string | undefined} tsconfig */ -export async function emit_dts(input, output, cwd, alias, files, tsconfig) { +export async function emit_dts(input, output, final_output, cwd, alias, files, tsconfig) { const tmp = `${output}/__package_types_tmp__`; rimraf(tmp); mkdirp(tmp); @@ -54,8 +55,28 @@ export async function emit_dts(input, output, cwd, alias, files, tsconfig) { console.warn(`Using $lib/${normalized} instead of generated .d.ts file`); } - const source = fs.readFileSync(path.join(tmp, normalized), 'utf8'); - write(path.join(output, normalized), resolve_aliases(input, normalized, source, alias)); + let source = fs.readFileSync(path.join(tmp, normalized), 'utf8'); + if (file.endsWith('.d.ts.map')) { + // Because we put the .d.ts files in a temporary directory, the relative path needs to be adjusted + const parsed = JSON.parse(source); + if (parsed.sources) { + parsed.sources = /** @type {string[]} */ (parsed.sources).map((source) => + posixify( + path.join( + path.relative( + path.dirname(path.join(final_output, normalized)), + path.dirname(path.join(input, normalized)) + ), + path.basename(source) + ) + ) + ); + source = JSON.stringify(parsed); + } + } else { + source = resolve_aliases(input, normalized, source, alias); + } + write(path.join(output, normalized), source); } rimraf(tmp); diff --git a/packages/package/test/fixtures/typescript-declaration-map/expected/Test.svelte b/packages/package/test/fixtures/typescript-declaration-map/expected/Test.svelte new file mode 100644 index 000000000000..2892a2582db5 --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/expected/Test.svelte @@ -0,0 +1,5 @@ + + +{foo} diff --git a/packages/package/test/fixtures/typescript-declaration-map/expected/Test.svelte.d.ts b/packages/package/test/fixtures/typescript-declaration-map/expected/Test.svelte.d.ts new file mode 100644 index 000000000000..a085b329fba8 --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/expected/Test.svelte.d.ts @@ -0,0 +1,7 @@ +type $$ComponentProps = { + foo: string; +}; +declare const Test: import("svelte").Component<$$ComponentProps, {}, "">; +type Test = ReturnType; +export default Test +//# sourceMappingURL=Test.svelte.d.ts.map \ No newline at end of file diff --git a/packages/package/test/fixtures/typescript-declaration-map/expected/Test.svelte.d.ts.map b/packages/package/test/fixtures/typescript-declaration-map/expected/Test.svelte.d.ts.map new file mode 100644 index 000000000000..fd94dc2b8f55 --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/expected/Test.svelte.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"Test.svelte.d.ts","sourceRoot":"","sources":["../src/lib/Test.svelte.ts"],"names":[],"mappings":"AAGC,KAAK,gBAAgB,GAAI;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAS1C,QAAA,MAAM,IAAI,sDAAsC,CAAC;AACjD,KAAK,IAAI,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC;AACpC,eAAe,IAAI,CAAC"} \ No newline at end of file diff --git a/packages/package/test/fixtures/typescript-declaration-map/expected/index.d.ts b/packages/package/test/fixtures/typescript-declaration-map/expected/index.d.ts new file mode 100644 index 000000000000..3a13848c9369 --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/expected/index.d.ts @@ -0,0 +1,2 @@ +export { default as Test } from './Test.svelte'; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/packages/package/test/fixtures/typescript-declaration-map/expected/index.d.ts.map b/packages/package/test/fixtures/typescript-declaration-map/expected/index.d.ts.map new file mode 100644 index 000000000000..d62430ee3b6c --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/expected/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,eAAe,CAAC"} \ No newline at end of file diff --git a/packages/package/test/fixtures/typescript-declaration-map/expected/index.js b/packages/package/test/fixtures/typescript-declaration-map/expected/index.js new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/expected/index.js @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/package/test/fixtures/typescript-declaration-map/package.json b/packages/package/test/fixtures/typescript-declaration-map/package.json new file mode 100644 index 000000000000..0f71eb5defeb --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/package.json @@ -0,0 +1,16 @@ +{ + "name": "typescript-declaration-map", + "private": true, + "version": "1.0.0", + "description": "standard typescript package with declarationMap:true", + "type": "module", + "peerDependencies": { + "svelte": "^5.0.0" + }, + "exports": { + ".": { + "types": "./dist/index.d.ts", + "svelte": "./dist/index.js" + } + } +} diff --git a/packages/package/test/fixtures/typescript-declaration-map/src/lib/Test.svelte b/packages/package/test/fixtures/typescript-declaration-map/src/lib/Test.svelte new file mode 100644 index 000000000000..50483b0ec942 --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/src/lib/Test.svelte @@ -0,0 +1,5 @@ + + +{foo} diff --git a/packages/package/test/fixtures/typescript-declaration-map/src/lib/index.ts b/packages/package/test/fixtures/typescript-declaration-map/src/lib/index.ts new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/src/lib/index.ts @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/package/test/fixtures/typescript-declaration-map/svelte.config.js b/packages/package/test/fixtures/typescript-declaration-map/svelte.config.js new file mode 100644 index 000000000000..6bd253bdf27d --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/svelte.config.js @@ -0,0 +1,5 @@ +import preprocess from 'svelte-preprocess'; + +export default { + preprocess: preprocess() +}; diff --git a/packages/package/test/fixtures/typescript-declaration-map/tsconfig.json b/packages/package/test/fixtures/typescript-declaration-map/tsconfig.json new file mode 100644 index 000000000000..cd86fcfd945f --- /dev/null +++ b/packages/package/test/fixtures/typescript-declaration-map/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "declaration": true, + "declarationMap": true + } +} diff --git a/packages/package/test/index.js b/packages/package/test/index.js index 45903ecb0b23..f455487e9704 100644 --- a/packages/package/test/index.js +++ b/packages/package/test/index.js @@ -42,7 +42,7 @@ async function test_make_package(path, options) { assert.equal(actual_files, expected_files); - const extensions = ['.json', '.svelte', '.ts', 'js']; + const extensions = ['.json', '.svelte', '.ts', 'js', '.map']; for (const file of actual_files) { const pathname = join(output, file); if (fs.statSync(pathname).isDirectory()) continue; @@ -68,6 +68,10 @@ async function test_make_package(path, options) { * @param {string} content */ async function format(file, content) { + if (file.endsWith('.map')) { + return content; + } + if (file.endsWith('package.json')) { // For some reason these are ordered differently in different test environments const json = JSON.parse(content); @@ -152,6 +156,10 @@ test('SvelteKit interop', async () => { await test_make_package('svelte-kit'); }); +test('create package with declaration map', async () => { + await test_make_package('typescript-declaration-map'); +}); + test('create package with tsconfig specified', async () => { await test_make_package('tsconfig-specified', { tsconfig: 'tsconfig.build.json' }); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0db7e238b763..2ee92a5c7df3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1070,8 +1070,8 @@ importers: specifier: ^7.5.4 version: 7.6.3 svelte2tsx: - specifier: ~0.7.16 - version: 0.7.18(svelte@5.2.9)(typescript@5.6.3) + specifier: ~0.7.33 + version: 0.7.33(svelte@5.2.9)(typescript@5.6.3) devDependencies: '@sveltejs/vite-plugin-svelte': specifier: ^5.0.1 @@ -3397,8 +3397,8 @@ packages: typescript: optional: true - svelte2tsx@0.7.18: - resolution: {integrity: sha512-PCOhH7uQaV472ZvNAcnkvShjFRMMkKUc/eNJImQMH9T4CyHeQpdatedFrEjaMCsweFb/oo3a6bv4qtdfaCPw8g==} + svelte2tsx@0.7.33: + resolution: {integrity: sha512-geogGkzfciwteiKvlbaDBnKOitWuh6e1n2f5KLBBXEfZgui9gy5yRlOBYtNEkdwciO4MC9fTM/EyltsiQrOPNQ==} peerDependencies: svelte: ^3.55 || ^4.0.0-next.0 || ^4.0 || ^5.0.0-next.0 typescript: ^4.9.4 || ^5.0.0 @@ -5816,7 +5816,7 @@ snapshots: postcss-load-config: 3.1.4(postcss@8.5.1) typescript: 5.6.3 - svelte2tsx@0.7.18(svelte@5.2.9)(typescript@5.6.3): + svelte2tsx@0.7.33(svelte@5.2.9)(typescript@5.6.3): dependencies: dedent-js: 1.0.1 pascal-case: 3.1.2