mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 06:05:11 +00:00
fix(bridge): default export detection (#1774)
This commit is contained in:
parent
c676493011
commit
39db33d625
@ -1,12 +1,11 @@
|
|||||||
import type { Plugin } from 'vite'
|
import type { Plugin } from 'vite'
|
||||||
import fse from 'fs-extra'
|
import fse from 'fs-extra'
|
||||||
|
import { findExports } from 'mlly'
|
||||||
|
|
||||||
// const PREFIX = 'defaultexport:'
|
|
||||||
const PREFIX = 'defaultexport:'
|
const PREFIX = 'defaultexport:'
|
||||||
const hasPrefix = (id: string = '') => id.startsWith(PREFIX)
|
const hasPrefix = (id: string = '') => id.startsWith(PREFIX)
|
||||||
const removePrefix = (id: string = '') => hasPrefix(id) ? id.substr(PREFIX.length) : id
|
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 () => {}'
|
const addDefaultExport = (code: string = '') => code + '\n\n' + 'export default () => {}'
|
||||||
|
|
||||||
export function defaultExportPlugin () {
|
export function defaultExportPlugin () {
|
||||||
@ -26,7 +25,8 @@ export function defaultExportPlugin () {
|
|||||||
async load (id) {
|
async load (id) {
|
||||||
if (hasPrefix(id)) {
|
if (hasPrefix(id)) {
|
||||||
let code = await fse.readFile(removePrefix(id), 'utf8')
|
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)
|
code = addDefaultExport(code)
|
||||||
}
|
}
|
||||||
return { map: null, code }
|
return { map: null, code }
|
||||||
|
Loading…
Reference in New Issue
Block a user