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)!: remove format prefixes for cjs and esm #8107

Merged
merged 4 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('resolveLibFilename', () => {
resolve(__dirname, 'packages/name')
)

expect(filename).toBe('custom-filename.es.mjs')
expect(filename).toBe('custom-filename.mjs')
})

test('package name as filename', () => {
Expand All @@ -45,7 +45,7 @@ describe('resolveLibFilename', () => {
resolve(__dirname, 'packages/name')
)

expect(filename).toBe('mylib.es.mjs')
expect(filename).toBe('mylib.mjs')
})

test('custom filename and no package name', () => {
Expand All @@ -58,7 +58,7 @@ describe('resolveLibFilename', () => {
resolve(__dirname, 'packages/noname')
)

expect(filename).toBe('custom-filename.es.mjs')
expect(filename).toBe('custom-filename.mjs')
})

test('missing filename', () => {
Expand All @@ -75,9 +75,9 @@ describe('resolveLibFilename', () => {

test('commonjs package extensions', () => {
const formatsToFilenames: FormatsToFileNames = [
['es', 'my-lib.es.mjs'],
['es', 'my-lib.mjs'],
['umd', 'my-lib.umd.js'],
['cjs', 'my-lib.cjs.js'],
['cjs', 'my-lib.js'],
['iife', 'my-lib.iife.js']
]

Expand All @@ -94,9 +94,9 @@ describe('resolveLibFilename', () => {

test('module package extensions', () => {
const formatsToFilenames: FormatsToFileNames = [
['es', 'my-lib.es.js'],
['es', 'my-lib.js'],
['umd', 'my-lib.umd.cjs'],
['cjs', 'my-lib.cjs.cjs'],
['cjs', 'my-lib.cjs'],
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
['iife', 'my-lib.iife.js']
]

Expand All @@ -107,7 +107,7 @@ describe('resolveLibFilename', () => {
resolve(__dirname, 'packages/module')
)

expect(filename).toBe(expectedFilename)
expect(expectedFilename).toBe(filename)
}
})
})
4 changes: 4 additions & 0 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,10 @@ export function resolveLibFilename(
extension = format === 'es' ? 'mjs' : 'js'
}

if (format === 'cjs' || format === 'es') {
Copy link
Member

Choose a reason for hiding this comment

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

Not for this PR, but should we allow es(deprecate this) / esm (new value)

Copy link
Member

Choose a reason for hiding this comment

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

Do you have some links to support esm over es? Rollup is using es for their format config, I think alignment with them is good here.

Copy link
Member

Choose a reason for hiding this comment

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

Oh interesting, I thought this was coming from cjs/esm wordings that also e.g. TypeScript is using

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

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

Expand Down
2 changes: 1 addition & 1 deletion playground/lib/index.dist.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="dynamic-import-message"></div>

<script type="module">
import myLib from './my-lib-custom-filename.es.mjs'
import myLib from './my-lib-custom-filename.mjs'

myLib('.es')
</script>
Expand Down
2 changes: 1 addition & 1 deletion playground/resolve-config/__tests__/resolve-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const build = (configName: string) => {

const getDistFile = (configName: string, extension: string) => {
return fs.readFileSync(
fromTestDir(`${configName}/dist/index.es.${extension}`),
fromTestDir(`${configName}/dist/index.${extension}`),
'utf8'
)
}
Expand Down
5 changes: 2 additions & 3 deletions playground/vue-lib/src-consumer/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable node/no-missing-import */
import { createApp } from 'vue'
// @ts-ignore
import { CompA } from '../dist/lib/my-vue-lib.es'
import { CompA } from '../dist/lib/my-vue-lib'
sachinraja marked this conversation as resolved.
Show resolved Hide resolved
import '../dist/lib/style.css'
import { createApp } from 'vue'

const app = createApp(CompA)
app.mount('#app')