Skip to content

Commit

Permalink
feat: use json plugin to allow treeshaking
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 21, 2021
1 parent ff14446 commit 562e4d1
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'
import alias from '@rollup/plugin-alias'
import esbuild from 'rollup-plugin-esbuild'
import dts from 'rollup-plugin-dts'
import json from '@rollup/plugin-json'
import { relative, resolve } from 'pathe'
import consola from 'consola'
import { getpkg } from '../utils'
Expand All @@ -15,7 +16,7 @@ import { CJSBridgePlugin } from './utils/cjs-bridge'
export async function rollupBuild (ctx: BuildContext) {
if (ctx.stub) {
for (const entry of ctx.entries.filter(entry => entry.builder === 'rollup')) {
const output = resolve(ctx.rootDir, ctx.outDir, entry.name)
const output = resolve(ctx.rootDir, ctx.outDir, entry.name!)
if (ctx.emitCJS) {
await writeFile(output + '.cjs', `module.exports = require('jiti')(null, { interopDefault: true })('${entry.input}')`)
}
Expand All @@ -40,7 +41,7 @@ export async function rollupBuild (ctx: BuildContext) {
}
if (entry.isEntry) {
ctx.buildEntries.push({
path: relative(ctx.rootDir, resolve(outputOptions.dir, entry.fileName)),
path: relative(ctx.rootDir, resolve(outputOptions.dir!, entry.fileName)),
bytes: entry.code.length * 4,
exports: entry.exports
})
Expand Down Expand Up @@ -127,16 +128,9 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {
preferBuiltins: true
}),

{
name: 'json',
transform (json: string, id: string) {
if (!id || id[0] === '\0' || !id.endsWith('.json')) { return null }
return {
code: 'module.exports = ' + json,
map: null
}
}
},
json({
preferConst: true
}),

esbuild({
target: 'es2020'
Expand Down

0 comments on commit 562e4d1

Please sign in to comment.