Skip to content

Commit

Permalink
Fix shared chunk extension along with parent chunk (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Aug 11, 2024
1 parent e17fcfb commit 4fa92de
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
} from 'rollup'
import { convertCompilerOptions } from './typescript'

import path, { resolve, dirname, join, basename, extname } from 'path'
import path, { resolve, dirname, join, basename } from 'path'
import { wasm } from '@rollup/plugin-wasm'
import { swc } from 'rollup-plugin-swc3'
import commonjs from '@rollup/plugin-commonjs'
Expand Down Expand Up @@ -443,7 +443,7 @@ async function buildOutputConfigs(
// Add esm mark and interop helper if esm export is detected
const useEsModuleMark = tsCompilerOptions?.esModuleInterop // hasEsmExport(exportPaths, tsCompilerOptions)
const absoluteOutputFile = resolve(cwd, bundleConfig.file!)
const outputFileExtension = extname(absoluteOutputFile)
const isEsmPkg = isESModulePackage(pkg.type)
const name = filePathWithoutExtension(absoluteOutputFile)
const dtsFile = resolve(
cwd,
Expand Down Expand Up @@ -482,10 +482,13 @@ async function buildOutputConfigs(
entryFiles,
),
chunkFileNames() {
const isCjsFormat = format === 'cjs'
const ext = dts
? 'd.ts'
: format === 'cjs' && outputFileExtension === '.cjs'
: isCjsFormat && isEsmPkg
? 'cjs'
: !isCjsFormat && !isEsmPkg
? 'mjs'
: 'js'
return '[name]-[hash].' + ext
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
import { c as client } from './client-client-Cm06vGwQ.js';
import { c as client } from './client-client-Cm06vGwQ.mjs';

var input = (()=>{
return client();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"use strict";import{c as t}from"./client-client-B-RDfYre.js";var e=()=>t();export{e as default};
"use strict";import{c as t}from"./client-client-B-RDfYre.mjs";var e=()=>t();export{e as default};
29 changes: 29 additions & 0 deletions test/integration/shared-module-ts-esm/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { promises as fsp } from 'fs'
import { createIntegrationTest } from '../utils'
import path from 'path'

describe('integration shared-module-ts-esm', () => {
it('should contain correct type file path of shared chunks', async () => {
await createIntegrationTest(
{
directory: __dirname,
},
async ({ distDir }) => {
const cjsFiles = await fsp.readdir(path.join(distDir, 'cjs'))
const esmFiles = await fsp.readdir(path.join(distDir, 'es'))

expect(cjsFiles).toEqual(
expect.arrayContaining([
expect.stringMatching(/util-shared-\w+\.js/),
]),
)

expect(esmFiles).toEqual(
expect.arrayContaining([
expect.stringMatching(/util-shared-\w+\.mjs/),
]),
)
},
)
})
})
18 changes: 18 additions & 0 deletions test/integration/shared-module-ts-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "shared-module-ts-esm",
"exports": {
".": {
"import": {
"types": "./dist/es/index.d.mts",
"default": "./dist/es/index.mjs"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
}
}
},
"dependencies": {
"react": "*"
}
}
2 changes: 2 additions & 0 deletions test/integration/shared-module-ts-esm/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const index = 'index'
export { sharedApi } from './lib/util.shared-runtime'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sharedApi() {
return 'common:shared'
}
2 changes: 1 addition & 1 deletion test/integration/shared-module-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "shared-module",
"name": "shared-module-ts",
"type": "module",
"exports": {
".": {
Expand Down

0 comments on commit 4fa92de

Please sign in to comment.