Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">

<img height="160" src="https://obfuscator.io/images/logo.png" alt="vite-plugin-bundle-obfuscator logo" />
<img height="160" src="https://obfuscator.io/logo.png" alt="vite-plugin-bundle-obfuscator logo" />

# vite-plugin-bundle-obfuscator

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,13 @@ export default function viteBundleObfuscator(config?: Partial<Config>): PluginOp
const configResolvedHandler: (resolvedConfig: ResolvedConfig) => void | Promise<void> = (resolvedConfig) => {
const sourcemap = resolvedConfig.build.sourcemap;
if (sourcemap) {
const output = resolvedConfig.build.rollupOptions?.output;
const sourcemapBaseUrl = !isArray(output) ? output?.sourcemapBaseUrl : undefined;
Comment on lines +111 to +112

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: sourcemapBaseUrl is ignored when rollupOptions.output is an array, which might be surprising for multi-output builds.

Because of the !isArray(output) guard, sourcemapBaseUrl is effectively unsupported for multi-output Rollup configs, even when all outputs could share the same base URL. If multi-output builds are a target use case, consider either handling the array case (e.g., ensure all outputs have the same sourcemapBaseUrl and use it) or clearly documenting that this option only applies to single-output configs.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Invert ternary operator to remove negation (invert-ternary)

Suggested change
const sourcemapBaseUrl = !isArray(output) ? output?.sourcemapBaseUrl : undefined;
const sourcemapBaseUrl = isArray(output) ? undefined : output?.sourcemapBaseUrl;


ExplanationNegated conditions are more difficult to read than positive ones, so it is best
to avoid them where we can. By inverting the ternary condition and swapping the
expressions we can simplify the code.

finalConfig.options = {
...finalConfig.options,
sourceMap: true,
sourceMapMode: sourcemap === 'inline' ? 'inline' : 'separate',
...(sourcemapBaseUrl && { sourceMapBaseUrl: sourcemapBaseUrl }),
};
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function obfuscateBundle(finalConfig: Config, fileName: string, bundleIte
? {
...finalConfig.options,
inputFileName: fileName,
sourceMapFileName: `${fileName}.map`,
sourceMapFileName: `${path.basename(fileName)}.map`,
}
: finalConfig.options;
const obfuscated = javascriptObfuscator.obfuscate(bundleItem.code, fileSpecificOptions);
Expand Down Expand Up @@ -212,7 +212,7 @@ export function obfuscateLibBundle(finalConfig: Config, fileName: string, code:
? {
...finalConfig.options,
inputFileName: fileName,
sourceMapFileName: `${fileName}.map`,
sourceMapFileName: `${path.basename(fileName)}.map`,
}
: finalConfig.options;
const obfuscated = javascriptObfuscator.obfuscate(code, fileSpecificOptions);
Expand Down
3 changes: 2 additions & 1 deletion src/worker/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parentPort } from 'node:worker_threads';
import path from 'node:path';
import javascriptObfuscator, { ObfuscatorOptions } from 'javascript-obfuscator';
import { composeSourcemaps, Log, ObfuscatedFilesRegistry } from '../utils';
import type { ObfuscationResult, WorkerMessage } from '../type';
Expand Down Expand Up @@ -28,7 +29,7 @@ if (parentPort) {
? {
...message.config.options,
inputFileName: fileName,
sourceMapFileName: `${fileName}.map`,
sourceMapFileName: `${path.basename(fileName)}.map`,
}
: message.config.options;
const obfuscated = javascriptObfuscator.obfuscate(bundleItem.code, fileSpecificOptions);
Expand Down