Skip to content

Commit fea71b7

Browse files
authored
Merge pull request #16 from barelyhuman/fix/ts-island
fix: resolution and output fixes for typescript files
2 parents 52b04a6 + 3b8bf78 commit fea71b7

File tree

7 files changed

+54
-11
lines changed

7 files changed

+54
-11
lines changed

esbuild.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ function esbuildPlugin(options = defaultOptions) {
2828
name: 'preact-island-plugin',
2929
async setup(build) {
3030
build.onLoad({ filter: /\.(js|ts)x?$/ }, async args => {
31-
let isIsland = false
3231
let generatorOutput
3332

3433
if (args.path.endsWith('.ts') || args.path.endsWith('.tsx')) {
34+
let isIsland = false
3535
const sourceCode = await readFile(args.path, 'utf8')
3636

3737
if (
@@ -46,7 +46,7 @@ function esbuildPlugin(options = defaultOptions) {
4646
(options && options.esbuild) || {}
4747
)
4848

49-
const jsCode = esbuild.transform(sourceCode, {
49+
const jsCode = await esbuild.transform(sourceCode, {
5050
loader: 'tsx',
5151
platform: 'node',
5252
target: 'node16',
@@ -57,8 +57,14 @@ function esbuildPlugin(options = defaultOptions) {
5757
...esbuildTransformOptions,
5858
})
5959

60+
let inputCode = jsCode.code
61+
62+
if (isIsland) {
63+
inputCode = '//@island\n' + inputCode
64+
}
65+
6066
generatorOutput = generateIslandsWithSource(
61-
(await jsCode).code,
67+
inputCode,
6268
args.path,
6369
options
6470
)

lib/plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ function generateIslandsWithSource(sourceCode, filepath, options) {
5454
}
5555

5656
clientOutput = resolve(options.client.output, path.basename(filepath))
57+
const fileExt = extname(filepath)
5758

5859
if (options.hash) {
5960
const hash = toHash(sourceCode)
60-
const fileExt = extname(filepath)
6161
clientOutput = clientOutput.replace(fileExt, `-${hash}.js`)
62+
} else {
63+
clientOutput = clientOutput.replace(fileExt, `.js`)
6264
}
6365

6466
if (!options.atomic) {

lib/typescript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function resolveTsConfig(config) {
2121
if (typeof config == 'string') {
2222
if (existsSync(resolve(config))) {
2323
const configDef = await readFile(resolve(config), 'utf8')
24-
return configDef
24+
return JSON.parse(configDef)
2525
}
2626
return {}
2727
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

playground/CounterTS.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
// @island
2-
import type { Signal } from '@preact/signals'
2+
3+
import { signal, type Signal } from '@preact/signals'
34

45
type Props = {
56
value: Signal<number>
67
inc: () => void
78
}
89

10+
const internalCount = signal(0)
11+
912
export default function CounterTS({ value, inc }: Props) {
1013
// @ts-ignore no tsconfig in the playgroud
11-
return <button onClick={inc}>{value}</button>
14+
return (
15+
<>
16+
<button onClick={inc}>{value}</button>
17+
<button onClick={() => (internalCount.value += 1)}>
18+
internal:{internalCount}
19+
</button>
20+
</>
21+
)
1222
}

playground/tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react-jsx",
4+
"jsxImportSource": "preact"
5+
}
6+
}

rollup.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,42 @@ function rollupPlugin(options = defaultOptions) {
3939
const typescript = autoLoadTypescriptPlug()
4040

4141
if (id.endsWith('ts') || id.endsWith('tsx')) {
42+
let isIsland = false
43+
const baseTransformTSConfig = await resolveTsConfig(options.tsconfig)
4244
const builder = await rollup.rollup({
4345
input: id,
4446
acornInjectPlugins: [jsx()],
4547
plugins: [
4648
typescript({
47-
...(await resolveTsConfig(options.client.tsconfig)),
4849
// Override given tsconfig's jsx property
4950
// for the client source since, it is the expected
5051
// input for the plugin
5152
// and modified by the plugin
52-
jsx: 'preserve',
53+
...baseTransformTSConfig,
54+
compilerOptions: {
55+
...baseTransformTSConfig.compilerOptions,
56+
jsx: 'preserve',
57+
},
5358
}),
5459
],
5560
})
5661
const build = await builder.generate({})
5762
const sourceCode = build.output[0].code
58-
generatedOutput = generateIslandsWithSource(sourceCode, id, options)
63+
64+
if (
65+
sourceCode.indexOf('//@island') > -1 ||
66+
sourceCode.indexOf('// @island') > -1
67+
) {
68+
isIsland = true
69+
}
70+
71+
let inputCode = sourceCode
72+
73+
if (isIsland) {
74+
inputCode = '//@island\n' + inputCode
75+
}
76+
77+
generatedOutput = generateIslandsWithSource(inputCode, id, options)
5978
} else {
6079
generatedOutput = generateIslands(id, options)
6180
}

0 commit comments

Comments
 (0)