Skip to content

Commit 87e0e21

Browse files
committed
feat: add replaceParentNode
selectively decide if the parent node needs to be replaced in preact
1 parent 7b6c9e6 commit 87e0e21

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

esbuild.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { defu } = require('defu')
88

99
exports = module.exports = esbuildPlugin
1010

11+
/**@type {import("../lib/types").ESbuildOptions} */
1112
const defaultOptions = {
1213
rootDir: '.',
1314
baseURL: '/public',
@@ -17,6 +18,7 @@ const defaultOptions = {
1718
client: {
1819
tsconfig: './tsconfig.json',
1920
output: './dist/client',
21+
replaceParentNode: false,
2022
},
2123
}
2224

lib/plugin.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ function generateIslandsWithSource(sourceCode, filepath, options) {
4343
}
4444
const baseURL = options.baseURL || '/'
4545

46-
let isIsland = hasTopLevelComment(sourceCode)
47-
const ast = astFromCode(sourceCode)
46+
let isIsland = /.island.(js|ts)x?$/.test(filepath)
4847

48+
if (!isIsland) {
49+
isIsland = hasTopLevelComment(sourceCode)
50+
}
51+
52+
const ast = astFromCode(sourceCode)
4953
if (!isIsland) {
5054
isIsland = staticAnalyseIfIsland(ast)
5155
}
@@ -87,6 +91,7 @@ function generateIslandsWithSource(sourceCode, filepath, options) {
8791

8892
const clientCode = constructIslandClient(componentName, filepath, {
8993
viewMods: viewModifier,
94+
replaceParentNode: options.client.replaceParentNode,
9095
})
9196
const serverCode = constructIslandServer(ast, componentName, {
9297
baseURL,
@@ -123,7 +128,11 @@ function getIslandName(name) {
123128
return `island${name.replace(/([A-Z])/g, '-$1').toLowerCase()}`
124129
}
125130

126-
function constructIslandClient(name, importPath, { viewMods } = {}) {
131+
function constructIslandClient(
132+
name,
133+
importPath,
134+
{ viewMods, replaceParentNode = false } = {}
135+
) {
127136
const islandName = getIslandName(name)
128137

129138
return `
@@ -170,7 +179,9 @@ customElements.define("${islandName}", class Island${name} extends HTMLElement {
170179
171180
renderIsland(){
172181
mergePropsWithDOM(this, this.baseProps);
173-
render(restoreTree(this.component.default, this.baseProps), this, this)
182+
render(restoreTree(this.component.default, this.baseProps), this,${
183+
replaceParentNode ? 'this' : 'undefined'
184+
} )
174185
}
175186
})`
176187
}

lib/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface Options {
1818
hash?: boolean
1919
tsconfig: string | TSConfig
2020
client: {
21+
replaceParentNode: boolean
2122
tsconfig?: string | TSConfig
2223
output: string
2324
}

rollup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ const { defu } = require('defu')
1313

1414
exports = module.exports = rollupPlugin
1515

16+
/**@type {import("../lib/types").Options} */
1617
const defaultOptions = {
1718
rootDir: '.',
1819
baseURL: '/public',
1920
atomic: true,
2021
hash: false,
2122
tsconfig: './tsconfig.json',
2223
client: {
24+
replaceParentNode: false,
2325
tsconfig: './tsconfig.json',
2426
output: './dist/client',
2527
},

0 commit comments

Comments
 (0)