Skip to content

Commit 0d47a67

Browse files
committed
feat: node_modules split chunk
1 parent 01e0753 commit 0d47a67

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/index.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Config, ViteConfigFn} from "./type";
33
import {
44
createWorkerTask,
55
formatTime,
6+
getChunkName,
67
getManualChunks,
78
getThreadPoolSize,
89
getValidBundleList,
@@ -14,7 +15,7 @@ import {
1415
obfuscateBundle
1516
} from "./utils";
1617
import {isArray, isFunction, isObject} from "./utils/is";
17-
import {defaultConfig, LOG_COLOR, NODE_MODULES} from "./utils/constants";
18+
import {defaultConfig, LOG_COLOR, NODE_MODULES, VENDOR_MODULES} from "./utils/constants";
1819

1920
export default function viteBundleObfuscator(config?: Partial<Config>): PluginOption {
2021
const finalConfig = {...defaultConfig, ...config};
@@ -29,20 +30,13 @@ export default function viteBundleObfuscator(config?: Partial<Config>): PluginOp
2930

3031
const manualChunks = [...getManualChunks(finalConfig)];
3132

32-
const addChunks2Excludes = (): void => {
33-
finalConfig.excludes.push(NODE_MODULES, ...manualChunks.map(modifyChunkName));
33+
const addChunks2Excludes = () => {
34+
finalConfig.excludes.push(VENDOR_MODULES, ...manualChunks.map(modifyChunkName));
3435
}
3536

36-
const getChunkName = (id: string): string => {
37-
for (const chunkName of manualChunks) {
38-
if (id.includes(chunkName)) return modifyChunkName(chunkName);
39-
}
40-
41-
return NODE_MODULES;
42-
};
43-
44-
const defaultManualChunks = (id: string): string | undefined => {
45-
if (id.includes('node_modules')) return getChunkName(id);
37+
const defaultManualChunks = (id: string) => {
38+
if (id.includes(NODE_MODULES)) return getChunkName(id, manualChunks);
39+
return undefined;
4640
};
4741

4842
if (!output) {

src/utils/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export const defaultConfig: Readonly<Config> = {
3838
}
3939
};
4040

41-
export const NODE_MODULES = 'vendor-modules';
41+
export const NODE_MODULES = 'node_modules';
42+
43+
export const VENDOR_MODULES = 'vendor-modules';
4244

4345
export const CHUNK_PREFIX = 'vendor-';
4446

src/utils/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import {isBoolean, isFileNameExcluded, isObject} from "./is";
66
import {Worker} from "node:worker_threads";
77
import path from "node:path";
88
import javascriptObfuscator from "javascript-obfuscator";
9-
import {CHUNK_PREFIX} from "./constants";
9+
import {CHUNK_PREFIX, VENDOR_MODULES} from "./constants";
1010

1111
export class Log {
1212
private readonly _log: (msg: string) => void;
1313

14-
constructor(private show: boolean) {
14+
constructor(show: boolean) {
1515
this._log = show ? console.log.bind(console) : this.noop;
1616
}
1717

@@ -95,6 +95,14 @@ export function getValidBundleList(finalConfig: Config, bundle: Rollup.OutputBun
9595
return validItems;
9696
}
9797

98+
export function getChunkName(id: string, manualChunks: string[]): string {
99+
for (const chunkName of manualChunks) {
100+
if (id.includes(chunkName)) return modifyChunkName(chunkName);
101+
}
102+
103+
return VENDOR_MODULES;
104+
}
105+
98106
export function obfuscateBundle(finalConfig: Config, fileName: string, bundleItem: Rollup.OutputChunk): string {
99107
const _log = new Log(finalConfig.log);
100108
_log.info(`obfuscating ${fileName}...`);

0 commit comments

Comments
 (0)