Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(bridge): default export detection #1774

Merged
merged 2 commits into from
Nov 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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