unplugin / unplugin-vue-components

📲 On-demand components auto importing for Vue
https://www.npmjs.com/package/unplugin-vue-components
MIT License
3.68k stars 342 forks source link

自定义组件库按需引入 运行时报错 Module parse failed: 'import' and 'export' may only appear at the top level (115:0) #649

Closed G1King closed 1 year ago

G1King commented 1 year ago

Describe the bug

"unplugin-auto-import": "^0.16.4", "unplugin-element-plus": "^0.7.1", "unplugin-vue-components": "^0.25.1", 自定义组件库 使用按需引入 使用最新版本 dev之后 报错 :Module parse failed: 'import' and 'export' may only appear at the top level (115:0) 使用 unplugin-vue-components:'^0.18.4' 版本没问题

Reproduction

ElementResolver 使用的是 ElementPlusResolver 自定义组件库是基于element-plus

System Info

系统 mac 浏览器谷歌 114.0.5735.133  node v16.16.0
vue:^3.2.38  webpack 5.73.0

Used Package Manager

npm

Validations

G1King commented 1 year ago

function getSideEffectsLegacy(partialName, options) { const { importStyle } = options if (!importStyle) return const themeFolder = 'bc-element/theme-chalk' if (importStyle === 'less') { return [${themeFolder}/src/${partialName}.less] } else if (importStyle === true || importStyle === 'css') { return [${themeFolder}/bc-${partialName}.css] } } function kebabCase(key) { const result = key.replace(/([A-Z])/g, ' $1').trim() return result.split(' ').join('-').toLowerCase() } function resolveComponent(name, options) { if (options.exclude && name.match(options.exclude)) return if (!name.match(/^Bc[A-Z]/)) return const partialName = kebabCase(name.slice(2)) // ElTableColumn -> table-column return { name: name.slice(2), from: bc-element/es, sideEffects: getSideEffectsLegacy(partialName, options), } } const noStylesComponents = []

function BcElementResolver(options) { let optionsResolved

async function resolveOptions() { if (optionsResolved) return optionsResolved optionsResolved = { importStyle: 'css', exclude: undefined, noStylesComponents: (options && options.noStylesComponents) || [], ...options, } return optionsResolved }

return [ { type: 'component', resolve: async name => { const options = await resolveOptions()

    if ([...options.noStylesComponents, ...noStylesComponents].includes(name))
      return resolveComponent(name, { ...options, importStyle: false })
    else return resolveComponent(name, options)
  },
},

] } module.exports = BcElementResolver

G1King commented 1 year ago

删除 node_modules 重新安装