Skip to content

Commit

Permalink
fix: overwrite nodenext option when transpiling (#11092)
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Nov 22, 2023
1 parent 07c76b1 commit 35e6e75
Show file tree
Hide file tree
Showing 35 changed files with 104 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-rats-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': patch
---

fix: overwrite nodenext option when transpiling
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"dts-buddy": "^0.2.4",
"rollup": "^3.29.4",
"svelte": "^4.2.7",
"svelte-preprocess": "^5.0.4",
"svelte-preprocess": "^5.1.1",
"typescript": "^4.9.4",
"vite": "^4.4.9",
"vitest": "^0.34.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@types/node": "^16.18.6",
"@types/semver": "^7.5.0",
"svelte": "^4.2.7",
"svelte-preprocess": "^5.0.4",
"svelte-preprocess": "^5.1.1",
"typescript": "^4.9.4",
"uvu": "^0.5.6"
},
Expand Down
9 changes: 8 additions & 1 deletion packages/package/src/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@ export async function emit_dts(input, output, cwd, alias, files) {
*/
export async function transpile_ts(filename, source) {
const ts = await try_load_ts();
const options = load_tsconfig(filename, ts);
// transpileModule treats NodeNext as CommonJS because it doesn't read the package.json. Therefore we need to override it.
// Also see https://github.com/microsoft/TypeScript/issues/53022 (the filename workaround doesn't work).
return ts.transpileModule(source, {
compilerOptions: load_tsconfig(filename, ts),
compilerOptions: {
...options,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.NodeJs // switch this to bundler in the next major, although it probably doesn't make a difference
},
fileName: filename
}).outputText;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "typescript",
"private": true,
"version": "1.0.0",
"description": "standard typescript package",
"description": "typescript package using esnext",
"type": "module",
"peerDependencies": {
"svelte": "^4.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
import { createEventDispatcher } from 'svelte';
export const astring = 'potato';
const dispatch = createEventDispatcher();
dispatch('event', true);
</script>

<slot {astring} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { SvelteComponent } from 'svelte';
declare const __propDef: {
props: {
astring?: string;
};
events: {
event: CustomEvent<boolean>;
} & {
[evt: string]: CustomEvent<any>;
};
slots: {
default: {
astring: string;
};
};
};
export type TestProps = typeof __propDef.props;
export type TestEvents = typeof __propDef.events;
export type TestSlots = typeof __propDef.slots;
export default class Test extends SvelteComponent<TestProps, TestEvents, TestSlots> {
get astring(): string;
}
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
16 changes: 16 additions & 0 deletions packages/package/test/fixtures/typescript-nodenext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "typescript-nodenext",
"private": true,
"version": "1.0.0",
"description": "typescript package using nodenext",
"type": "module",
"peerDependencies": {
"svelte": "^4.0.0"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"svelte": "./dist/index.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
export const astring: string = 'potato';
const dispatch = createEventDispatcher<{ event: boolean }>();
dispatch('event', true);
</script>

<slot {astring} />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import preprocess from 'svelte-preprocess';

export default {
preprocess: preprocess({})
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
}
10 changes: 7 additions & 3 deletions packages/package/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,18 @@ for (const dir of fs.readdirSync(join(__dirname, 'errors'))) {
});
}

test('create standard package with javascript', async () => {
test('create package with javascript', async () => {
// should also preserve filename casing
// should also correctly handle nested folders
await test_make_package('javascript');
});

test('create standard package with typescript', async () => {
await test_make_package('typescript');
test('create package with typescript using esnext', async () => {
await test_make_package('typescript-esnext');
});

test('create package with typescript using nodenext', async () => {
await test_make_package('typescript-nodenext');
});

test('create package and assets are not tampered', async () => {
Expand Down
20 changes: 10 additions & 10 deletions pnpm-lock.yaml

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

0 comments on commit 35e6e75

Please sign in to comment.