From 33fd29997756faf802504467a9587fe568ee8820 Mon Sep 17 00:00:00 2001 From: s3xysteak Date: Fri, 5 Apr 2024 19:09:11 +0800 Subject: [PATCH 1/5] fix: `rollup.external` should check alias path. --- src/builder/rollup.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index 47894bc8..40793296 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -288,6 +288,14 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions { ].filter(Boolean), external(id) { + const transformAliases = (id: string): string => { + for (const [find, replacement] of Object.entries(resolveAliases(ctx))) { + if(id.startsWith(find)) + return id.replace(find, replacement) + } + return id + } + id = transformAliases(id) const pkg = getpkg(id); const isExplicitExternal = arrayIncludes(ctx.options.externals, pkg) || From ef08786594803f9e9a78ddb987000028b7155e22 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 5 Jun 2024 00:42:08 +0200 Subject: [PATCH 2/5] reuse --- src/builder/rollup.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index ede764e8..5228042d 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -272,6 +272,7 @@ const getChunkFilename = ( }; export function getRollupOptions(ctx: BuildContext): RollupOptions { + const _aliases = resolveAliases(ctx); return ({ input: Object.fromEntries( ctx.options.entries @@ -315,8 +316,8 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions { external(id) { const transformAliases = (id: string): string => { - for (const [find, replacement] of Object.entries(resolveAliases(ctx))) { - if(id.startsWith(find)) + for (const [find, replacement] of Object.entries(_aliases)) { + if(id.startsWith(find)) return id.replace(find, replacement) } return id @@ -363,7 +364,7 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions { ctx.options.rollup.alias && alias({ ...ctx.options.rollup.alias, - entries: resolveAliases(ctx), + entries: _aliases }), ctx.options.rollup.resolve && From d661268b5c36a4ac6ed65c54d56a654786dc4535 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:42:35 +0000 Subject: [PATCH 3/5] chore: apply automated updates --- src/builder/rollup.ts | 74 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index 5228042d..8212fc9a 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -53,6 +53,9 @@ 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 = []; const serializedJitiOptions = JSON.stringify( { ...ctx.options.stubOptions.jiti, @@ -60,9 +63,41 @@ export async function rollupBuild(ctx: BuildContext) { ...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, 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( @@ -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})`, "", @@ -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})`, "", @@ -142,17 +184,22 @@ export async function rollupBuild(ctx: BuildContext) { ); // DTS Stub - await writeFile( - output + ".d.ts", - [ + if (ctx.options.declaration) { + const dtsContent = [ `export * from ${JSON.stringify(resolvedEntryForTypeImport)};`, hasDefaultExport - ? `export { default } from ${JSON.stringify( - resolvedEntryForTypeImport, - )};` + ? `export { default } from ${JSON.stringify(resolvedEntryForTypeImport)};` : "", - ].join("\n"), - ); + ].join("\n"); + await writeFile(output + ".d.cts", dtsContent); + await writeFile(output + ".d.mts", dtsContent); + if ( + ctx.options.declaration === "compatible" || + ctx.options.declaration === true + ) { + await writeFile(output + ".d.ts", dtsContent); + } + } if (shebang) { await makeExecutable(output + ".cjs"); @@ -317,12 +364,11 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions { external(id) { const transformAliases = (id: string): string => { for (const [find, replacement] of Object.entries(_aliases)) { - if(id.startsWith(find)) - return id.replace(find, replacement) + if (id.startsWith(find)) return id.replace(find, replacement); } - return id - } - id = transformAliases(id) + return id; + }; + id = transformAliases(id); const pkg = getpkg(id); const isExplicitExternal = arrayIncludes(ctx.options.externals, pkg) || @@ -364,7 +410,7 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions { ctx.options.rollup.alias && alias({ ...ctx.options.rollup.alias, - entries: _aliases + entries: _aliases, }), ctx.options.rollup.resolve && From 635a069e00c1a689d770c9fd8699f50d640d25e1 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 5 Jun 2024 00:45:45 +0200 Subject: [PATCH 4/5] extract fn --- src/builder/rollup.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index 8212fc9a..0dbce089 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -362,13 +362,7 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions { ].filter(Boolean), external(id) { - const transformAliases = (id: string): string => { - for (const [find, replacement] of Object.entries(_aliases)) { - if (id.startsWith(find)) return id.replace(find, replacement); - } - return id; - }; - id = transformAliases(id); + id = resolveAlias(id, _aliases); const pkg = getpkg(id); const isExplicitExternal = arrayIncludes(ctx.options.externals, pkg) || @@ -478,6 +472,16 @@ function resolveAliases(ctx: BuildContext) { return aliases; } +// TODO: use pathe utils to handle nested aliases +function resolveAlias (id: string, aliases: Record): string { + for (const [find, replacement] of Object.entries(aliases)) { + if (id.startsWith(find)) { + return id.replace(find, replacement) + } + } + return id; +}; + export function _watch(rollupOptions: RollupOptions) { const watcher = rollupWatch(rollupOptions); From 0c71beda5fbfdd46e458b42424618dd38c76be48 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:46:18 +0000 Subject: [PATCH 5/5] chore: apply automated updates --- src/builder/rollup.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index 0dbce089..d6117b86 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -473,14 +473,14 @@ function resolveAliases(ctx: BuildContext) { } // TODO: use pathe utils to handle nested aliases -function resolveAlias (id: string, aliases: Record): string { +function resolveAlias(id: string, aliases: Record): string { for (const [find, replacement] of Object.entries(aliases)) { if (id.startsWith(find)) { - return id.replace(find, replacement) + return id.replace(find, replacement); } } return id; -}; +} export function _watch(rollupOptions: RollupOptions) { const watcher = rollupWatch(rollupOptions);