Skip to content

Commit

Permalink
feat: support custom jiti plugins for stub mode (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 authored Jun 4, 2024
1 parent 44fa118 commit 7926335
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@
"untyped": "^1.4.2"
},
"devDependencies": {
"@babel/plugin-transform-class-properties": "^7.24.6",
"@types/node": "^20.14.1",
"@vitest/coverage-v8": "^1.6.0",
"changelogen": "^0.5.5",
"eslint": "^9.4.0",
"eslint-config-unjs": "^0.3.2",
"prettier": "^3.3.0",
"typescript": "^5.4.5",
"vitest": "^1.6.0",
"unbuild": "workspace:."
"unbuild": "workspace:.",
"vitest": "^1.6.0"
},
"peerDependencies": {
"typescript": "^5.4.5"
Expand Down
85 changes: 85 additions & 0 deletions pnpm-lock.yaml

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

42 changes: 42 additions & 0 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,51 @@ const DEFAULT_EXTENSIONS = [
export async function rollupBuild(ctx: BuildContext) {
if (ctx.options.stub) {
const jitiPath = await resolvePath("jiti", { url: import.meta.url });
const babelPlugins = ctx.options.stubOptions.jiti.transformOptions?.babel
?.plugins as any;
const importedBabelPlugins: Array<string> = [];
const serializedJitiOptions = JSON.stringify(
{
...ctx.options.stubOptions.jiti,
alias: {
...resolveAliases(ctx),
...ctx.options.stubOptions.jiti.alias,
},
transformOptions: {
...ctx.options.stubOptions.jiti.transformOptions,
babel: {
...ctx.options.stubOptions.jiti.transformOptions?.babel,
plugins: "__$BABEL_PLUGINS",
},
},
},
null,
2,
).replace(
'"__$BABEL_PLUGINS"',
Array.isArray(babelPlugins)
? "[" +
babelPlugins
.map((plugin: string | Array<any>, i) => {
if (Array.isArray(plugin)) {
const [name, ...args] = plugin;
importedBabelPlugins.push(name);
return (
`[` +
[
`plugin${i}`,
...args.map((val) => JSON.stringify(val)),
].join(", ") +
"]"
);
} else {
importedBabelPlugins.push(plugin);
return `plugin${i}`;
}
})
.join(",") +
"]"
: "[]",
);

for (const entry of ctx.options.entries.filter(
Expand Down Expand Up @@ -97,6 +132,10 @@ export async function rollupBuild(ctx: BuildContext) {
shebang +
[
`const jiti = require(${JSON.stringify(jitiPath)})`,
...importedBabelPlugins.map(
(plugin, i) =>
`const plugin${i} = require(${JSON.stringify(plugin)})`,
),
"",
`const _jiti = jiti(null, ${serializedJitiOptions})`,
"",
Expand Down Expand Up @@ -127,6 +166,9 @@ export async function rollupBuild(ctx: BuildContext) {
shebang +
[
`import jiti from ${JSON.stringify(pathToFileURL(jitiPath).href)};`,
...importedBabelPlugins.map(
(plugin, i) => `import plugin${i} from ${JSON.stringify(plugin)}`,
),
"",
`const _jiti = jiti(null, ${serializedJitiOptions})`,
"",
Expand Down
9 changes: 9 additions & 0 deletions test/fixture/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export default defineBuildConfig([
},
{ input: "src/schema", builder: "untyped" },
],
stubOptions: {
jiti: {
transformOptions: {
babel: {
plugins: [["@babel/plugin-transform-class-properties"]],
},
},
},
},
},
// Minified with sourcemaps
{
Expand Down

0 comments on commit 7926335

Please sign in to comment.