Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(lib)!: remove format prefixes for cjs and esm (#8107)
  • Loading branch information
sachinraja committed May 12, 2022
1 parent c32e3ac commit ad8c3b1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
16 changes: 8 additions & 8 deletions packages/vite/src/node/__tests__/build.spec.ts
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'],
['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
Expand Up @@ -596,6 +596,10 @@ export function resolveLibFilename(
extension = format === 'es' ? 'mjs' : 'js'
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion playground/lib/index.dist.html
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
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
@@ -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'
import '../dist/lib/style.css'
import { createApp } from 'vue'

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

0 comments on commit ad8c3b1

Please sign in to comment.