Skip to content

Commit

Permalink
Fix duplicated dts jobs when matching special convention (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Jun 19, 2023
1 parent 585e913 commit d412e2d
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 27 deletions.
21 changes: 13 additions & 8 deletions src/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,22 @@ export async function buildEntryConfig(
Object.keys(exportPaths).forEach(async (entryExport) => {
// TODO: improve the source detection
const exportCond = exportPaths[entryExport]
const hasEdgeLight = !!exportCond['edge-light']
const hasReactServer = !!exportCond['react-server']

const buildConfigs = [
createBuildConfig('', exportCond) // default config
]
if (hasEdgeLight) {
buildConfigs.push(createBuildConfig('edge-light', exportCond))
}
if (hasReactServer) {
buildConfigs.push(createBuildConfig('react-server', exportCond))

// For dts job, only build the default config.
// For assets job, build all configs.
if (!dts) {
if (exportCond['edge-light']) {
buildConfigs.push(createBuildConfig('edge-light', exportCond))
}
if (exportCond['react-server']) {
buildConfigs.push(createBuildConfig('react-server', exportCond))
}
if (exportCond['react-native']) {
buildConfigs.push(createBuildConfig('react-native', exportCond))
}
}

async function createBuildConfig(exportType: string, exportCondRef: FullExportCondition) {
Expand Down
35 changes: 17 additions & 18 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs/promises'
import { execSync, fork } from 'child_process'
import { resolve, join } from 'path'
import { stripANSIColor, existsFile } from './testing-utils'
import { stripANSIColor, existsFile, assertFilesContent } from './testing-utils'
import * as debug from './utils/debug'

jest.setTimeout(10 * 60 * 1000)
Expand All @@ -12,7 +12,7 @@ const getPath = (filepath: string) => join(integrationTestDir, filepath)

const testCases: {
name: string
args: string[]
args?: string[]
expected(
f: string,
{ stderr, stdout }: { stderr: string; stdout: string }
Expand Down Expand Up @@ -60,6 +60,17 @@ const testCases: {
}
},
},
{
name: 'pkg-exports-ts-rsc',
async expected(dir) {
assertFilesContent(dir, {
'./dist/index.mjs': /const shared = true/,
'./dist/react-server.mjs': /'react-server'/,
'./dist/react-native.js': /'react-native'/,
'./dist/index.d.ts': /declare const shared = true/,
})
},
},
{
name: 'pkg-exports-default',
args: ['index.js'],
Expand Down Expand Up @@ -109,19 +120,7 @@ const testCases: {
'./dist/server/react-server.mjs': /'server.react-server'/,
}

for (const relativeFile of distFiles) {
const file = join(dir, relativeFile)
expect({
[file]: await existsFile(file) ? 'existed' : 'missing'
}).toMatchObject({ [file]: 'existed' })
}

for (const [file, regex] of Object.entries(contentsRegex)) {
const content = await fs.readFile(join(dir, file), {
encoding: 'utf-8',
})
expect(content).toMatch(regex)
}
assertFilesContent(dir, contentsRegex)

const log = `\
✓ Typed dist/client/index.d.ts - 74 B
Expand Down Expand Up @@ -203,9 +202,9 @@ const testCases: {

async function runBundle(
dir: string,
_args: string[]
args_: string[]
): Promise<{ code: number | null; stdout: string; stderr: string }> {
const args = _args.concat(['--cwd', dir])
const args = (args_ || []).concat(['--cwd', dir])
const ps = fork(
`${__dirname + '/../node_modules/.bin/tsx'}`,
[__dirname + '/../src/cli.ts'].concat(args),
Expand All @@ -228,7 +227,7 @@ async function runBundle(

function runTests() {
for (const testCase of testCases) {
const { name, args, expected } = testCase
const { name, args = [], expected } = testCase
const dir = getPath(name)
test(`integration ${name}`, async () => {
debug.log(`Command: bunchee ${args.join(' ')}`)
Expand Down
4 changes: 3 additions & 1 deletion test/integration/multi-entries/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
"edge-light": "./dist/shared/edge-light.mjs"
},
"./server": {
"types": "./dist/server/index.d.ts",
"react-server": "./dist/server/react-server.mjs",
"edge-light": "./dist/server/edge.mjs",
"react-server": "./dist/server/react-server.mjs"
"import": "./dist/server/index.mjs"
}
}
}
2 changes: 2 additions & 0 deletions test/integration/multi-entries/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const name = 'server.index'
export const main = true
9 changes: 9 additions & 0 deletions test/integration/pkg-exports-ts-rsc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "pkg-export-ts-rsc",
"exports": {
"types": "./dist/index.d.ts",
"react-server": "./dist/react-server.mjs",
"react-native": "./dist/react-native.js",
"import": "./dist/index.mjs"
}
}
2 changes: 2 additions & 0 deletions test/integration/pkg-exports-ts-rsc/src/index.react-native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default 'react-native'

2 changes: 2 additions & 0 deletions test/integration/pkg-exports-ts-rsc/src/index.react-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default 'react-server'

3 changes: 3 additions & 0 deletions test/integration/pkg-exports-ts-rsc/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default 'index'
export const shared = true

1 change: 1 addition & 0 deletions test/integration/pkg-exports-ts-rsc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
22 changes: 22 additions & 0 deletions test/testing-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs/promises'
import path from 'path'

export function stripANSIColor(str: string) {
return str.replace(
Expand All @@ -16,3 +17,24 @@ export async function existsFile(filePath: string) {
throw err
}
}

export async function assertFilesContent(dir: string, contentsRegex: Record<string, RegExp | string>) {
const distFiles = Object.keys(contentsRegex)
for (const relativeFile of distFiles) {
const file = path.join(dir, relativeFile)
expect({
[file]: await existsFile(file) ? 'existed' : 'missing'
}).toMatchObject({ [file]: 'existed' })
}

for (const [file, regex] of Object.entries(contentsRegex)) {
const content = await fs.readFile(path.join(dir, file), {
encoding: 'utf-8',
})
if (regex instanceof RegExp) {
expect(content).toMatch(regex)
} else {
expect(content).toContain(regex)
}
}
}

0 comments on commit d412e2d

Please sign in to comment.