Skip to content

Commit 13edbea

Browse files
committed
fix: generate stub file
1 parent 9a1fcbc commit 13edbea

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

scripts/build-rolldown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ async function main() {
128128
configs.map(c =>
129129
rolldown(c).then(bundle => {
130130
return bundle.write(c.output as OutputOptions).then(() => {
131-
// @ts-expect-error
132131
return path.join(
133132
'packages',
134133
target,
135134
'dist',
135+
// @ts-expect-error
136136
path.basename(c.output.file)
137137
)
138138
})

scripts/rolldown.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import polyfillNode from '@rolldown/plugin-node-polyfills'
22
import { minify as minifySwc } from '@swc/core'
3+
import { promises as fs } from 'node:fs'
34
import { createRequire } from 'node:module'
45
import path from 'node:path'
56
import { fileURLToPath } from 'node:url'
@@ -52,15 +53,14 @@ export function createConfigsForPackage({
5253
* Released under the ${pkg.license} License.
5354
*/`
5455

55-
// TODO:
56-
/*
5756
const stubs = {
58-
[`dist/${name}.cjs`]: `${name}.cjs.js`,
59-
[`dist/${name}.mjs`]: `${name}.esm-bundler.js`,
60-
[`dist/${name}.runtime.mjs`]: `${name}.runtime.esm-bundler.js`,
61-
[`dist/${name}.prod.cjs`]: `${name}.cjs.prod.js`
57+
[resolve(`dist/${name}.cjs`)]: resolve(`dist/${name}.cjs.js`),
58+
[resolve(`dist/${name}.mjs`)]: resolve(`dist/${name}.esm-bundler.js`),
59+
[resolve(`dist/${name}.runtime.mjs`)]: resolve(
60+
`dist/${name}.runtime.esm-bundler.js`
61+
),
62+
[resolve(`dist/${name}.prod.cjs`)]: resolve(`dist/${name}.cjs.prod.js`)
6263
}
63-
*/
6464

6565
const outputConfigs: Record<string, OutputOptions> = {
6666
mjs: {
@@ -138,6 +138,7 @@ export function createConfigsForPackage({
138138
console.log(pc.yellow(`invalid format: "${format}"`))
139139
process.exit(1)
140140
}
141+
const rawFile = output.file
141142

142143
const isProductionBuild =
143144
process.env.__DEV__ === 'false' ||
@@ -303,8 +304,28 @@ export function createConfigsForPackage({
303304
plugins: [
304305
...resolveReplace(),
305306
...resolveNodePlugins(),
306-
...plugins
307-
// TODO: stub
307+
...plugins,
308+
{
309+
name: 'write-stub',
310+
async writeBundle() {
311+
if (rawFile == null) {
312+
return
313+
}
314+
const stub = stubs[rawFile]
315+
if (!stub) {
316+
return
317+
}
318+
319+
const filename = path.basename(rawFile)
320+
const contents =
321+
format === 'cjs'
322+
? `module.exports = require('./${filename}')`
323+
: `export * from './${filename}'`
324+
325+
await fs.writeFile(stub, contents)
326+
// console.log(`created stub ${pc.bold(path.join('packages', target, 'dist', path.basename(stub)))}`)
327+
}
328+
}
308329
],
309330
output,
310331
treeshake: {

0 commit comments

Comments
 (0)