Skip to content

Commit

Permalink
feat: support mark any underscore module as private (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Dec 29, 2024
1 parent 4a0672f commit f0f7aa5
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ export const DEFAULT_TS_CONFIG = {
}

export const BINARY_TAG = '$binary'

export const PRIVATE_GLOB_PATTERN = '**/_*/**'
18 changes: 9 additions & 9 deletions src/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
SRC,
runtimeExportConventions,
specialExportConventions,
PRIVATE_GLOB_PATTERN,
} from './constants'
import { posixRelativify } from './lib/format'

Expand Down Expand Up @@ -300,7 +301,7 @@ export async function collectSourceEntriesByExportPath(
const files = await glob(globalPatterns, {
cwd: dirPath,
nodir: true,
ignore: '**/_*',
ignore: PRIVATE_GLOB_PATTERN,
})

for (const file of files) {
Expand Down Expand Up @@ -390,14 +391,12 @@ export async function collectSourceEntriesFromExportPaths(
}

// Search private shared module files which are not in the parsedExportsInfo, but start with _.
// e.g. _utils.ts, _utils/index.ts
// e.g. _utils.development.ts, _utils/index.development.js
const privatePattern = [
`**/_*{,/index}.{${[...availableExtensions].join(',')}}`,
`**/_*{,/index}.{${[...runtimeExportConventions].join(',')}}.{${[
...availableExtensions,
].join(',')}}`,
]
// Leading underscore: e.g. _utils.ts, _utils/index.ts
// Segment contains leading underscore: e.g. a/_b/_c.ts, a/b/_c/index.ts
// Contains special suffix: e.g. _utils.development.ts, _utils/index.development.js
const suffixPattern = [...runtimeExportConventions].join(',')
const extPattern = [...availableExtensions].join(',')
const privatePattern = `**/_*{,/*}{,{.${suffixPattern}}}.{${extPattern}}`
const privateFiles = await glob(privatePattern, {
cwd: sourceFolderPath,
nodir: true,
Expand All @@ -419,6 +418,7 @@ export async function collectSourceEntriesFromExportPaths(

// Map private shared files to the dist directory
// e.g. ./_utils.ts -> ./dist/_utils.js
// TODO: improve the logic to only generate the required files, not all possible files
const privateExportInfo: [string, string][] = [
[
posixRelativify(
Expand Down
10 changes: 7 additions & 3 deletions src/prepare/prepare-entries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { existsSync } from 'fs'
import { glob } from 'glob'
import { BINARY_TAG, availableExtensions } from '../constants'
import {
BINARY_TAG,
PRIVATE_GLOB_PATTERN,
availableExtensions,
} from '../constants'

import { collectSourceEntriesByExportPath } from '../entries'
import { normalizePath, sourceFilenameToExportFullPath } from '../utils'
Expand All @@ -24,13 +28,13 @@ export async function collectSourceEntries(sourceFolderPath: string) {
const binMatches = await glob(binPattern, {
cwd: sourceFolderPath,
nodir: true,
ignore: '**/_*', // ignore private entries
ignore: PRIVATE_GLOB_PATTERN, // ignore private entries
})

const srcMatches = await glob(srcPattern, {
cwd: sourceFolderPath,
nodir: true,
ignore: '**/_*', // ignore private entries
ignore: PRIVATE_GLOB_PATTERN, // ignore private entries
})

for (const file of binMatches) {
Expand Down
35 changes: 35 additions & 0 deletions test/integration/shared-any-module/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
assertFilesContent,
createIntegrationTest,
getFileNamesFromDirectory,
} from '../utils'

describe('integration shared-module', () => {
it('should split all shared module into different chunks', async () => {
await createIntegrationTest(
{
directory: __dirname,
},
async ({ distDir, stdout }) => {
const jsFiles = await getFileNamesFromDirectory(distDir)
expect(jsFiles).toEqual([
'_internal/util-a.cjs',
'_internal/util-a.js',
'_internal/util-b.cjs',
'_internal/util-b.js',
'export-a.js',
'export-b.js',
'export-c.js',
'private/_nested/util-c.cjs',
'private/_nested/util-c.js',
])

await assertFilesContent(distDir, {
'export-a.js': `'./_internal/util-a.js'`,
'export-b.js': `'./_internal/util-b.js'`,
'export-c.js': `'./private/_nested/util-c.js'`,
})
},
)
})
})
12 changes: 12 additions & 0 deletions test/integration/shared-any-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "shared-module",
"type": "module",
"exports": {
"./export-a": "./dist/export-a.js",
"./export-b": "./dist/export-b.js",
"./export-c": "./dist/export-c.js"
},
"dependencies": {
"react": "*"
}
}
1 change: 1 addition & 0 deletions test/integration/shared-any-module/src/_internal/util-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const utilA = 'utilA'
1 change: 1 addition & 0 deletions test/integration/shared-any-module/src/_internal/util-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const utilB = 'utilB'
6 changes: 6 additions & 0 deletions test/integration/shared-any-module/src/export-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { utilA } from './_internal/util-a'
import { utilB } from './_internal/util-b'

export const getName = () => {
return 'export-a' + utilA + utilB
}
6 changes: 6 additions & 0 deletions test/integration/shared-any-module/src/export-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { utilB } from './_internal/util-b'
import { utilC } from './private/_nested/util-c'

export const getName = () => {
return 'export-b' + utilB + utilC
}
6 changes: 6 additions & 0 deletions test/integration/shared-any-module/src/export-c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { utilA } from './_internal/util-a'
import { utilC } from './private/_nested/util-c'

export const getName = () => {
return 'export-c' + utilA + utilC
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const utilC = 'utilC'

0 comments on commit f0f7aa5

Please sign in to comment.