Skip to content

Commit 95cdd81

Browse files
authoredMay 30, 2022
feat: derive proper js extension from package type (#8382)
1 parent 3d14372 commit 95cdd81

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed
 

‎packages/vite/src/node/build.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -438,21 +438,26 @@ async function doBuild(
438438
try {
439439
const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
440440
const cjsSsrBuild = ssr && config.ssr?.format === 'cjs'
441+
const format = output.format || (cjsSsrBuild ? 'cjs' : 'es')
442+
const jsExt =
443+
(ssr && config.ssr?.target !== 'webworker') || libOptions
444+
? resolveOutputJsExtension(format, getPkgJson(config.root)?.type)
445+
: 'js'
441446
return {
442447
dir: outDir,
443448
// Default format is 'es' for regular and for SSR builds
444-
format: cjsSsrBuild ? 'cjs' : 'es',
449+
format,
445450
exports: cjsSsrBuild ? 'named' : 'auto',
446451
sourcemap: options.sourcemap,
447452
name: libOptions ? libOptions.name : undefined,
448453
generatedCode: 'es2015',
449454
entryFileNames: ssr
450-
? `[name].js`
455+
? `[name].${jsExt}`
451456
: libOptions
452-
? resolveLibFilename(libOptions, output.format || 'es', config.root)
457+
? resolveLibFilename(libOptions, format, config.root, jsExt)
453458
: path.posix.join(options.assetsDir, `[name].[hash].js`),
454459
chunkFileNames: libOptions
455-
? `[name].[hash].js`
460+
? `[name].[hash].${jsExt}`
456461
: path.posix.join(options.assetsDir, `[name].[hash].js`),
457462
assetFileNames: libOptions
458463
? `[name].[ext]`
@@ -591,10 +596,24 @@ function getPkgName(name: string) {
591596
return name?.startsWith('@') ? name.split('/')[1] : name
592597
}
593598

599+
type JsExt = 'js' | 'cjs' | 'mjs'
600+
601+
function resolveOutputJsExtension(
602+
format: ModuleFormat,
603+
type: string = 'commonjs'
604+
): JsExt {
605+
if (type === 'module') {
606+
return format === 'cjs' || format === 'umd' ? 'cjs' : 'js'
607+
} else {
608+
return format === 'es' ? 'mjs' : 'js'
609+
}
610+
}
611+
594612
export function resolveLibFilename(
595613
libOptions: LibraryOptions,
596614
format: ModuleFormat,
597-
root: string
615+
root: string,
616+
extension?: JsExt
598617
): string {
599618
if (typeof libOptions.fileName === 'function') {
600619
return libOptions.fileName(format)
@@ -608,13 +627,7 @@ export function resolveLibFilename(
608627
'Name in package.json is required if option "build.lib.fileName" is not provided.'
609628
)
610629

611-
let extension: string
612-
613-
if (packageJson?.type === 'module') {
614-
extension = format === 'cjs' || format === 'umd' ? 'cjs' : 'js'
615-
} else {
616-
extension = format === 'es' ? 'mjs' : 'js'
617-
}
630+
extension ??= resolveOutputJsExtension(format, packageJson.type)
618631

619632
if (format === 'cjs' || format === 'es') {
620633
return `${name}.${extension}`

‎playground/legacy/__tests__/ssr/serve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function serve(): Promise<{ close(): Promise<void> }> {
2222

2323
app.use('/', async (_req, res) => {
2424
const { render } = await import(
25-
path.resolve(rootDir, './dist/server/entry-server.js')
25+
path.resolve(rootDir, './dist/server/entry-server.mjs')
2626
)
2727
const html = await render()
2828
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)

‎playground/lib/__tests__/lib.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ describe.runIf(isBuild)('build', () => {
4040
'hello vite'
4141
)
4242
const code = fs.readFileSync(
43-
path.join(testDir, 'dist/lib/dynamic-import-message.es.js'),
43+
path.join(testDir, 'dist/lib/dynamic-import-message.es.mjs'),
4444
'utf-8'
4545
)
4646
expect(code).not.toMatch('__vitePreload')
4747

4848
// Test that library chunks are hashed
49-
expect(code).toMatch(/await import\("\.\/message.[a-z\d]{8}.js"\)/)
49+
expect(code).toMatch(/await import\("\.\/message.[a-z\d]{8}.mjs"\)/)
5050
})
5151

5252
test('@import hoist', async () => {

‎playground/lib/vite.dyimport.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
entry: path.resolve(__dirname, 'src/main2.js'),
1111
formats: ['es', 'iife'],
1212
name: 'message',
13-
fileName: (format) => `dynamic-import-message.${format}.js`
13+
fileName: (format) => `dynamic-import-message.${format}.mjs`
1414
},
1515
outDir: 'dist/lib'
1616
}

0 commit comments

Comments
 (0)
Please sign in to comment.