Skip to content

Commit

Permalink
enhance: simplfy chunk type (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Feb 16, 2025
1 parent 9ccc5aa commit 2a0bf5b
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"@types/clean-css": "^4.2.11",
"@types/node": "^22.9.3",
"@types/picomatch": "^3.0.1",
"@types/react": "19.0.1",
"@types/react": "^19.0.9",
"@types/yargs": "^17.0.33",
"bunchee": "link:./",
"cross-env": "^7.0.3",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function bundle(

const defaultTsOptions: TypescriptOptions = {
tsConfigPath: tsConfig?.tsConfigPath,
tsCompilerOptions: tsConfig?.tsCompilerOptions || {},
tsCompilerOptions: tsConfig?.tsCompilerOptions,
}

// Handle single entry file
Expand Down
10 changes: 6 additions & 4 deletions src/rollup/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ async function createDtsPlugin(
cwd: string,
) {
const enableIncrementalWithoutBuildInfo =
tsCompilerOptions.incremental && !tsCompilerOptions.tsBuildInfoFile
tsCompilerOptions?.incremental && !tsCompilerOptions?.tsBuildInfoFile
const incrementalOptions = enableIncrementalWithoutBuildInfo
? {
incremental: false,
}
: undefined
const compositeOptions = tsCompilerOptions.composite
const compositeOptions = tsCompilerOptions?.composite
? {
composite: false,
}
Expand All @@ -69,7 +69,7 @@ async function createDtsPlugin(
// resolving types from <reference> from node_modules
preserveSymlinks: false,
target: 'ESNext',
...(!tsCompilerOptions.jsx
...(!tsCompilerOptions?.jsx
? {
jsx: 'react-jsx',
}
Expand Down Expand Up @@ -137,7 +137,9 @@ export async function buildInputConfig(

const { useTypeScript } = buildContext
const { runtime, target: jscTarget, minify: shouldMinify } = bundleConfig
const hasSpecifiedTsTarget = Boolean(tsCompilerOptions.target && tsConfigPath)
const hasSpecifiedTsTarget = Boolean(
tsCompilerOptions?.target && tsConfigPath,
)

const swcParserConfig: import('@swc/types').ParserConfig = {
syntax: useTypeScript ? 'typescript' : 'ecmascript',
Expand Down
18 changes: 14 additions & 4 deletions src/rollup/split-chunks.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { type GetManualChunk } from 'rollup'
import { type CustomPluginOptions } from 'rollup'
import path from 'path'
import { memoize } from '../lib/memoize'

function getModuleLayer(moduleMeta: CustomPluginOptions) {
const hashTo3Char = memoize((input: string): string => {
let hash = 0
for (let i = 0; i < input.length; i++) {
hash = (hash << 5) - hash + input.charCodeAt(i) // Simple hash shift
}
return (hash >>> 0).toString(36).slice(0, 3) // Base36 + trim to 3 chars
})

function getModuleLayer(moduleMeta: CustomPluginOptions): string | undefined {
const directives = (
moduleMeta.preserveDirectives || { directives: [] }
).directives
.map((d: string) => d.replace(/^use /, ''))
.filter((d: string) => d !== 'strict')

const moduleLayer = directives[0]
return moduleLayer
return moduleLayer ? hashTo3Char(moduleLayer) : undefined
}

// dependencyGraphMap: Map<subModuleId, Set<entryParentId>>
export function createSplitChunks(
dependencyGraphMap: Map<string, Set<[string, string]>>,
dependencyGraphMap: Map<string, Set<[string, string | undefined]>>,
entryFiles: Set<string>,
): GetManualChunk {
// If there's existing chunk being splitted, and contains a layer { <id>: <chunkGroup> }
Expand Down Expand Up @@ -84,7 +93,8 @@ export function createSplitChunks(
}

const chunkName = path.basename(id, path.extname(id))
const chunkGroup = `${chunkName}-${moduleLayer}`
const layerSuffix = hashTo3Char(moduleLayer)
const chunkGroup = `${chunkName}-${layerSuffix}`

splitChunksGroupMap.set(id, chunkGroup)
return chunkGroup
Expand Down
2 changes: 1 addition & 1 deletion src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { logger } from './logger'

export type TypescriptOptions = {
tsConfigPath: string | undefined
tsCompilerOptions: CompilerOptions
tsCompilerOptions: CompilerOptions | undefined
}

let hasLoggedTsWarning = false
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.mjs';
import { c as client } from './client-12s-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.mjs";var e=()=>t();export{e as default};
"use strict";import{c as t}from"./client-12s-B-RDfYre.mjs";var s=()=>t();export{s as default};
4 changes: 2 additions & 2 deletions test/integration/mixed-directives/mixed-directives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('integration - mixed-directives', () => {

expect(fileContents).toMatchInlineSnapshot(`
{
"action-server-DOTFC6td.js": "'use server';
"action-12x-DOTFC6td.js": "'use server';
async function action1() {
return 'action1';
}
Expand All @@ -19,7 +19,7 @@ describe('integration - mixed-directives', () => {
",
"client.js": "'use client';
import { jsx } from 'react/jsx-runtime';
import { a as action1 } from './action-server-DOTFC6td.js';
import { a as action1 } from './action-12x-DOTFC6td.js';
function Page() {
return /*#__PURE__*/ jsx("button", {
Expand Down
8 changes: 4 additions & 4 deletions test/integration/server-components/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ describe('integration server-components', () => {
expect(jsFiles).toEqual([
'index.cjs',
'index.js',
'mod_actions-server-B2kXJwqw.cjs',
'mod_actions-server-DSdgX-jM.js',
'mod_client-client-BO96FYFA.js',
'mod_client-client-DAeHkA4H.cjs',
'mod_actions-12x-B2kXJwqw.cjs',
'mod_actions-12x-DSdgX-jM.js',
'mod_client-12s-BO96FYFA.js',
'mod_client-12s-DAeHkA4H.cjs',
'ui.cjs',
'ui.js',
])
Expand Down

0 comments on commit 2a0bf5b

Please sign in to comment.