Skip to content

Commit

Permalink
feat(build): set hoistTransitiveImports to false in library builds (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaciras authored Jan 15, 2024
1 parent df8f5a5 commit e6ebc7b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
47 changes: 46 additions & 1 deletion packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'node:path'
import { basename, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import colors from 'picocolors'
import { describe, expect, test, vi } from 'vitest'
Expand Down Expand Up @@ -102,6 +102,51 @@ describe('build', () => {
])
assertOutputHashContentChange(result[0], result[1])
})

test('external modules should not be hoisted in library build', async () => {
const [esBundle] = (await build({
logLevel: 'silent',
build: {
lib: {
entry: ['foo.js', 'bar.js'],
formats: ['es'],
},
rollupOptions: {
external: 'external',
},
write: false,
},
plugins: [
{
name: 'test',
resolveId(id) {
const name = basename(id)
if (name === 'foo.js' || name === 'bar.js') {
return name
}
},
load(id) {
if (id === 'foo.js') {
return `
import bar from 'bar.js'
export default bar()
`
}
if (id === 'bar.js') {
return `
import ext from 'external';
export default ext();`
}
},
},
],
})) as RollupOutput[]

const foo = esBundle.output.find(
(chunk) => chunk.fileName === 'foo.js',
) as OutputChunk
expect(foo.code).not.contains('import "external"')
})
})

const baseLibOptions: LibraryOptions = {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ export async function build(
exports: 'auto',
sourcemap: options.sourcemap,
name: libOptions ? libOptions.name : undefined,
hoistTransitiveImports: libOptions ? false : undefined,
// es2015 enables `generatedCode.symbols`
// - #764 add `Symbol.toStringTag` when build es module into cjs chunk
// - #1048 add `Symbol.toStringTag` for module default export
Expand Down

0 comments on commit e6ebc7b

Please sign in to comment.