Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: derive proper js extension from package type #8382

Merged
merged 7 commits into from May 30, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 23 additions & 10 deletions packages/vite/src/node/build.ts
Expand Up @@ -422,18 +422,19 @@ async function doBuild(
try {
const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
const cjsSsrBuild = ssr && config.ssr?.format === 'cjs'
const format = output.format || cjsSsrBuild ? 'cjs' : 'es'
return {
dir: outDir,
// Default format is 'es' for regular and for SSR builds
format: cjsSsrBuild ? 'cjs' : 'es',
format,
exports: cjsSsrBuild ? 'named' : 'auto',
sourcemap: options.sourcemap,
name: libOptions ? libOptions.name : undefined,
generatedCode: 'es2015',
entryFileNames: ssr
? `[name].js`
? `[name].${resolveOutputJsExtensionFromRoot(format, config.root)}`
: libOptions
? resolveLibFilename(libOptions, output.format || 'es', config.root)
? resolveLibFilename(libOptions, format, config.root)
: path.posix.join(options.assetsDir, `[name].[hash].js`),
chunkFileNames: libOptions
? `[name].[hash].js`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sachinraja @antfu shouldn't chunks also have a derived mjs/cjs/js extension for chunks in lib mode (and also SSR?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me I think it's better to consist the extension for both entry and chunks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they must to be compliant with node resolution

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, it should now work for SSR and chunks

Expand Down Expand Up @@ -575,6 +576,24 @@ function getPkgName(name: string) {
return name?.startsWith('@') ? name.split('/')[1] : name
}

function resolveOutputJsExtensionFromRoot(
format: ModuleFormat,
root: string
): 'js' | 'cjs' | 'mjs' {
return resolveOutputJsExtension(format, getPkgJson(root)?.type)
}

function resolveOutputJsExtension(
format: ModuleFormat,
type: string = 'commonjs'
): 'js' | 'cjs' | 'mjs' {
if (type === 'module') {
return format === 'cjs' || format === 'umd' ? 'cjs' : 'js'
} else {
return format === 'es' ? 'mjs' : 'js'
}
}

export function resolveLibFilename(
libOptions: LibraryOptions,
format: ModuleFormat,
Expand All @@ -592,13 +611,7 @@ export function resolveLibFilename(
'Name in package.json is required if option "build.lib.fileName" is not provided.'
)

let extension: string

if (packageJson?.type === 'module') {
extension = format === 'cjs' || format === 'umd' ? 'cjs' : 'js'
} else {
extension = format === 'es' ? 'mjs' : 'js'
}
const extension = resolveOutputJsExtension(format, packageJson?.type)

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