Skip to content

Commit fbcfc1c

Browse files
committed
chore: add export check
1 parent 58a503f commit fbcfc1c

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

packages/vite/rollup.config.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,11 @@ const cjsConfig = defineConfig({
201201
format: 'cjs',
202202
},
203203
external: Object.keys(pkg.dependencies),
204-
plugins: [...createSharedNodePlugins({}), bundleSizeLimit(175)],
204+
plugins: [
205+
...createSharedNodePlugins({}),
206+
bundleSizeLimit(175),
207+
exportCheck(),
208+
],
205209
})
206210

207211
export default defineConfig([
@@ -342,4 +346,29 @@ function bundleSizeLimit(limit: number): Plugin {
342346
}
343347
}
344348

349+
function exportCheck(): Plugin {
350+
return {
351+
name: 'export-check',
352+
async writeBundle() {
353+
// escape import so that it's not bundled while config load
354+
const dynImport = (id: string) => import(id)
355+
356+
const esmNamespace = await dynImport('./dist/node/index.js')
357+
const cjsModuleExports = (await dynImport('./index.cjs')).default
358+
const cjsModuleExportsKeys = new Set(
359+
Object.getOwnPropertyNames(cjsModuleExports),
360+
)
361+
const lackingExports = Object.keys(esmNamespace).filter(
362+
(key) => !cjsModuleExportsKeys.has(key),
363+
)
364+
if (lackingExports.length > 0) {
365+
this.error(
366+
`Exports missing from cjs build: ${lackingExports.join(', ')}.` +
367+
` Please update index.cjs or src/publicUtils.ts.`,
368+
)
369+
}
370+
},
371+
}
372+
}
373+
345374
// #endregion

0 commit comments

Comments
 (0)