From 39db33d625f39c9684adfacf66d541e078ce591d Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 8 Nov 2021 18:33:42 +0800 Subject: [PATCH] fix(bridge): default export detection (#1774) --- packages/bridge/src/vite/plugins/default-export.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/bridge/src/vite/plugins/default-export.ts b/packages/bridge/src/vite/plugins/default-export.ts index 52ec342f8bc..644be259be8 100644 --- a/packages/bridge/src/vite/plugins/default-export.ts +++ b/packages/bridge/src/vite/plugins/default-export.ts @@ -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 () { @@ -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 }