Skip to content

Commit

Permalink
Fix shared chunk type file extension (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Aug 11, 2024
1 parent 17cfcbd commit e17fcfb
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,11 @@ async function buildOutputConfigs(
entryFiles,
),
chunkFileNames() {
const ext =
format === 'cjs' && outputFileExtension === '.cjs' ? 'cjs' : 'js'
const ext = dts
? 'd.ts'
: format === 'cjs' && outputFileExtension === '.cjs'
? 'cjs'
: 'js'
return '[name]-[hash].' + ext
},
// By default in rollup, when creating multiple chunks, transitive imports of entry chunks
Expand Down
35 changes: 35 additions & 0 deletions test/integration/shared-module-ts/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { readFileSync, promises as fsp } from 'fs'
import { createIntegrationTest } from '../utils'
import path from 'path'

describe('integration shared-module-ts', () => {
it('should contain correct type file path of shared chunks', async () => {
await createIntegrationTest(
{
directory: __dirname,
},
async ({ distDir }) => {
const files = await fsp.readdir(distDir)
const sharedUtilModuleFile = files.find((file) =>
/util-shared-\w+\.d\.ts/.test(file),
)!
const appContextSharedFile = files.find((file) =>
/app-context-shared-\w+\.d\.ts/.test(file),
)!

const indexType = files.find((file) => file === 'index.d.ts')!
const indexFileContent = readFileSync(
path.join(distDir, indexType),
'utf-8',
)

expect(indexFileContent).toContain(
sharedUtilModuleFile.replace('.d.ts', '.js'),
)
expect(indexFileContent).toContain(
appContextSharedFile.replace('.d.ts', '.js'),
)
},
)
})
})
20 changes: 20 additions & 0 deletions test/integration/shared-module-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "shared-module",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"react-server": "./dist/index.react-server.js",
"import": "./dist/index.js",
"default": "./dist/index.cjs"
},
"./another": {
"types": "./dist/another.d.ts",
"import": "./dist/another.js",
"default": "./dist/another.cjs"
}
},
"dependencies": {
"react": "*"
}
}
1 change: 1 addition & 0 deletions test/integration/shared-module-ts/src/another.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { sharedApi as anotherSharedApi } from './lib/util.shared-runtime'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AppContext } from './lib/app-context.shared-runtime'
3 changes: 3 additions & 0 deletions test/integration/shared-module-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const index = 'index'
export { sharedApi } from './lib/util.shared-runtime'
export { AppContext } from './lib/app-context.shared-runtime'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use client'

import React from 'react'

export const AppContext = React.createContext(null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sharedApi() {
return 'common:shared'
}

0 comments on commit e17fcfb

Please sign in to comment.