-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve
"use client"
module boundaries (#516)
- Loading branch information
Showing
4 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@preconstruct/cli": minor | ||
--- | ||
|
||
Modules with `"use client"` directives are now built as their own chunk with the `"use client"` directive preserved. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { AcornNode, Plugin, SourceDescription } from "rollup"; | ||
import { Program } from "estree"; | ||
import MagicString, { SourceMap } from "magic-string"; | ||
|
||
export function serverComponentsPlugin({ | ||
sourceMap, | ||
}: { | ||
sourceMap: boolean; | ||
}): Plugin { | ||
return { | ||
name: "server-components", | ||
|
||
transform(code, id) { | ||
if (/['"]use client['"]/.test(code)) { | ||
const ast: Program = (() => { | ||
const babelMeta = this.getModuleInfo(id)!.meta.babel; | ||
if (babelMeta?.codeAtBabelTime === code) { | ||
return this.getModuleInfo(id)!.meta.babel.ast; | ||
} | ||
return this.parse(code); | ||
})(); | ||
|
||
for (const [idx, node] of ast.body.entries()) { | ||
if ( | ||
node.type !== "ExpressionStatement" || | ||
node.expression.type !== "Literal" || | ||
typeof node.expression.value !== "string" | ||
) { | ||
return; | ||
} | ||
if (node.expression.value === "use client") { | ||
let magicString = new MagicString(code); | ||
this.emitFile({ type: "chunk", id }); | ||
ast.body.splice(idx, 1); | ||
const start: number = (node as any).start; | ||
const end: number = (node as any).end; | ||
const len = end - start; | ||
magicString.overwrite(start, end, " ".repeat(len)); | ||
let output: SourceDescription = { | ||
code: magicString.toString(), | ||
ast: (ast as unknown) as AcornNode, | ||
}; | ||
|
||
if (sourceMap) { | ||
output.map = magicString.generateMap({ hires: true }); | ||
} | ||
if (!output.meta) { | ||
output.meta = {}; | ||
} | ||
output.meta.isUseClientEntry = true; | ||
return output; | ||
} | ||
} | ||
} | ||
return null; | ||
}, | ||
renderChunk(code, chunk) { | ||
if (chunk.facadeModuleId !== null) { | ||
const moduleInfo = this.getModuleInfo(chunk.facadeModuleId); | ||
if (moduleInfo?.meta.isUseClientEntry) { | ||
const magicString = new MagicString(code); | ||
magicString.prepend('"use client";\n'); | ||
const chunkInfo: { code: string; map?: SourceMap } = { | ||
code: magicString.toString(), | ||
}; | ||
if (sourceMap) { | ||
chunkInfo.map = magicString.generateMap({ hires: true }); | ||
} | ||
return chunkInfo; | ||
} | ||
} | ||
return null; | ||
}, | ||
}; | ||
} |