Skip to content

Commit d26cb86

Browse files
committed
fix(build): avoid default import warning when lib entry has no default export
close #1641
1 parent 369f972 commit d26cb86

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import './setPublicPath'
2+
export * from '~entry'

packages/@vue/cli-service/lib/commands/build/resolveLibConfig.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module.exports = (api, { entry, name }, options) => {
1212
process.exit(1)
1313
}
1414

15-
if (!fs.existsSync(api.resolve(entry))) {
15+
const fullEntryPath = api.resolve(entry)
16+
17+
if (!fs.existsSync(fullEntryPath)) {
1618
abort(
1719
`Failed to resolve lib entry: ${entry}${entry === `src/App.vue` ? ' (default)' : ''}. ` +
1820
`Make sure to specify the correct entry file.`
@@ -71,13 +73,23 @@ module.exports = (api, { entry, name }, options) => {
7173
const entryName = `${libName}.${postfix}`
7274
config.resolve
7375
.alias
74-
.set('~entry', api.resolve(entry))
76+
.set('~entry', fullEntryPath)
7577

7678
// set entry/output after user configureWebpack hooks are applied
7779
const rawConfig = api.resolveWebpackConfig(config)
7880

81+
let realEntry = require.resolve('./entry-lib.js')
82+
83+
// avoid importing default if user entry file does not have default export
84+
if (!isVueEntry) {
85+
const entryContent = fs.readFileSync(fullEntryPath, 'utf-8')
86+
if (!/\b(export\s+default|export\s{[^}]+as\s+default)\b/.test(entryContent)) {
87+
realEntry = require.resolve('./entry-lib-no-default.js')
88+
}
89+
}
90+
7991
rawConfig.entry = {
80-
[entryName]: require.resolve('./entry-lib.js')
92+
[entryName]: realEntry
8193
}
8294

8395
rawConfig.output = Object.assign({

0 commit comments

Comments
 (0)