Skip to content

Commit

Permalink
fix(bridge): default export detection (nuxt#1774)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Nov 8, 2021
1 parent c676493 commit 39db33d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/bridge/src/vite/plugins/default-export.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { Plugin } from 'vite'
import fse from 'fs-extra'
import { findExports } from 'mlly'

// const PREFIX = 'defaultexport:'
const PREFIX = 'defaultexport:'
const hasPrefix = (id: string = '') => id.startsWith(PREFIX)
const removePrefix = (id: string = '') => hasPrefix(id) ? id.substr(PREFIX.length) : id

const hasDefaultExport = (code: string = '') => code.includes('export default')
const addDefaultExport = (code: string = '') => code + '\n\n' + 'export default () => {}'

export function defaultExportPlugin () {
Expand All @@ -26,7 +25,8 @@ export function defaultExportPlugin () {
async load (id) {
if (hasPrefix(id)) {
let code = await fse.readFile(removePrefix(id), 'utf8')
if (!hasDefaultExport(code)) {
const exports = findExports(code)
if (!exports.find(i => i.type === 'default' || i.name === 'default')) {
code = addDefaultExport(code)
}
return { map: null, code }
Expand Down

0 comments on commit 39db33d

Please sign in to comment.