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

fix(lib): use proper extension #6827

Merged
merged 10 commits into from
May 5, 2022
2 changes: 1 addition & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default defineConfig(async ({ command, mode }) => {
{
"exports": {
".": {
"import": "./index.esm.js",
"import": "./index.esm.mjs",
"require": "./index.cjs.js"
}
}
Expand Down
11 changes: 7 additions & 4 deletions docs/guide/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ module.exports = defineConfig({
lib: {
entry: path.resolve(__dirname, 'lib/main.js'),
name: 'MyLib',
fileName: (format) => `my-lib.${format}.js`
fileName: (format) => {
const extension = format === 'es' ? 'mjs' : 'js'
return `my-lib.${format}.${extension}`
}
},
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
Expand All @@ -134,7 +137,7 @@ Running `vite build` with this config uses a Rollup preset that is oriented towa
```
$ vite build
building for production...
[write] my-lib.es.js 0.08kb, brotli: 0.07kb
[write] my-lib.es.mjs 0.08kb, brotli: 0.07kb
[write] my-lib.umd.js 0.30kb, brotli: 0.16kb
```

Expand All @@ -145,10 +148,10 @@ Recommended `package.json` for your lib:
"name": "my-lib",
"files": ["dist"],
"main": "./dist/my-lib.umd.js",
"module": "./dist/my-lib.es.js",
"module": "./dist/my-lib.es.mjs",
"exports": {
".": {
"import": "./dist/my-lib.es.js",
"import": "./dist/my-lib.es.mjs",
"require": "./dist/my-lib.umd.js"
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,8 @@ export function resolveLibFilename(
'Name in package.json is required if option "build.lib.fileName" is not provided.'
)

return `${name}.${format}.js`
const extension = format === 'es' ? 'mjs' : 'js'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This only checks for es format now, but it could check if the format includes es to support #6555

return `${name}.${format}.${extension}`
}

function resolveBuildOutputs(
Expand Down