Skip to content

Commit

Permalink
fix(rollup): use cjs compatible json exports
Browse files Browse the repository at this point in the history
nuxt/framework#1335
  • Loading branch information
pi0 committed Oct 21, 2021
1 parent e459daa commit 06d7b9d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ 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'
import type { BuildContext } from '../types'
import { JSONPlugin } from './utils/json'
import { CJSBridgePlugin } from './utils/cjs-bridge'

export async function rollupBuild (ctx: BuildContext) {
Expand Down Expand Up @@ -128,7 +128,7 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {
preferBuiltins: true
}),

json({
JSONPlugin({
preferConst: true
}),

Expand Down
19 changes: 19 additions & 0 deletions src/builder/utils/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Plugin } from 'rollup'
import type { RollupJsonOptions } from '@rollup/plugin-json'
import rollupJSONPlugin from '@rollup/plugin-json'

const EXPORT_DEFAULT = 'export default '

export function JSONPlugin (options: RollupJsonOptions): Plugin {
const plugin = rollupJSONPlugin(options)
return <Plugin>{
...plugin,
transform (code, id) {
const res = plugin.transform!.call(this, code, id)
if (res && typeof res !== 'string' && 'code' in res && res.code && res.code.startsWith(EXPORT_DEFAULT)) {
res.code = res.code.replace(EXPORT_DEFAULT, 'module.exports = ')
}
return res
}
}
}

0 comments on commit 06d7b9d

Please sign in to comment.