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 .cspell.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "0.1",
"language": "en",
"words": ["codefend", "Bundlephobia", "tsup"],
"words": ["codefend", "Bundlephobia", "tsup", "octocat"],
"ignorePaths": [
"package.json",
"package-lock.json",
Expand Down
33 changes: 33 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Contributing Guidelines

_Pull requests, bug reports, and all other forms of contribution are welcomed and highly encouraged!_ :octocat:

### Contents

- [Code of Conduct](#book-code-of-conduct)
- [Asking Questions](#bulb-asking-questions)
- [How can I Contribute?](#inbox_tray-how-can-i-contribute)

> **This guide serves to set clear expectations for everyone involved with the project so that we can improve it together while also creating a welcoming space for everyone to participate. Following these guidelines will help ensure a positive experience for contributors and maintainers.**

## :book: Code of Conduct

Please review our [Code of Conduct](./CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code.

## :bulb: Asking Questions

If you have any question that does not relate to a bug or a feature request, please use [GitHub Discussions](https://github.com/Codefend/codefend/discussions) instead of GitHub issues.

## :inbox_tray: How can I Contribute?

**GitHub issues**

If you encounter a problem with this library or if you have a new feature you'd like to see in this project, please create [a new issue](https://github.com/Codefend/codefend/issues/new/choose).

**GitHub Pull requests**

Please leverage the repository's own tools to make sure the code is aligned with our standards:

1. Run all check commands before submitting the PR (`type:check`, `format:check`, `lint:check`, `test:coverage` and `spell:check`)
2. Please commit your changes and run a `setup` command so you can actually check how would the template look like once cleaned up
3. Always leverage the `cz` command to create a commit. We heavily rely on this for automatic releases.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Codefend
Copyright (c) 2024 Codefend

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

# webpack-plugin-codefend

![NPM](https://img.shields.io/npm/dt/webpack-plugin-codefend)
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/Codefend/webpack-plugin-codefend/ci.yaml?branch=main)
![NPM](https://img.shields.io/npm/dt/webpack-plugin-codefend)
![Bundlephobia](https://img.shields.io/bundlephobia/min/webpack-plugin-codefend)
![Node version](https://img.shields.io/node/v/webpack-plugin-codefend)
![NPM](https://img.shields.io/npm/l/webpack-plugin-codefend)
Expand Down Expand Up @@ -60,6 +60,11 @@ export default {

//will skip obfuscation for the following words
ignore: ["node_modules"],

/* By using everyday words such as books or lorem ipsum in the pool,
you can subtly obfuscate your code,
concealing its true nature without making it apparent that obfuscation is occurring.*/
pool: ["QueenOfHeart"],
},
debug: {
// to display detailed stats about the words that have been obfuscated
Expand Down Expand Up @@ -93,7 +98,8 @@ class l_Calculator {

//>>>>>>==== Will Become ======<<<<<<

class Ox0 {
class QueenOfHeart /* 'QueenOfHeart' defined in pool*/ {
/* Ox1, Ox2... prefix transformation*/
Ox1(Ox2, Ox3) {
const Ox4 = Ox2 + Ox3;
console.log("node_modules"); // has not been obfuscated
Expand Down
44 changes: 12 additions & 32 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"devDependencies": {
"@types/webpack": "^5.28.5",
"@types/webpack-sources": "^3.2.3",
"@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "^7.18.0",
"cspell": "^8.10.4",
Expand All @@ -57,11 +58,10 @@
"tsup": "^8.1.0",
"typescript": "^5.5.3",
"vitest": "^2.0.4",
"webpack-sources": "^3.2.3",
"@types/webpack-sources": "^3.2.3"
"webpack-sources": "^3.2.3"
},
"dependencies": {
"codefend": "^2.1.0",
"codefend": "^3.1.0",
"schema-utils": "^4.2.0"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/data/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
export const DEFAULT_TRANSFORMATION_PREFIX = "Ox";
export const DEFAULT_PARSER_REGEX_LIST = [
{
flag: "g",
name: "main",
value: "([a-zA-Z]+(_[a-zA-Z0-9]+)+)",
regex: new RegExp("([a-zA-Z]+(_[a-zA-Z0-9]+)+)", "g"),
},
];

Expand All @@ -15,3 +14,4 @@ export const WEBPACK_IGNORED_WORDS = [
"WEBPACK_IMPORTED_MODULE_0",
"webpack_module_cache",
];
export const DEFAULT_PARSER_NAME = "codeOnly";
36 changes: 18 additions & 18 deletions src/data/Types.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import type {
ICodefendDebugOptions,
ICodefendTransformationOptions,
ICodefendInternalDebugOptions,
ICodefendInternalParserOptions,
ICodefendInternalTransformationOptions,
buildRuntimeOptions,
} from "codefend";

export type IWebpackCodefendOptions = {
transformation?: {
prefix?: string;
static?: ICodefendTransformationStatic[];
ignore?: string[];
};
debug?: {
stats?: boolean;
};
parser?: {
regexList?: ICodefendParserRegexListItem[];
};
transformation?: ICodefendTransformationOptions;
debug?: IWebpackCodefendDebugOptions;
};

export type ICodefendParserRegexListItem = {
name: string;
value: string;
export type IWebpackCodefendInternalOptions = {
transformation: ICodefendInternalTransformationOptions;
debug: ICodefendInternalDebugOptions;
parser: ICodefendInternalParserOptions;
};

export type ICodefendTransformationStatic = {
from: string;
to: string;
};
type IWebpackCodefendDebugOptions = Omit<ICodefendDebugOptions, "ignoredWarnings">;

export type ICodefendRuntimeOptions = ReturnType<typeof buildRuntimeOptions>;
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { IObfuscationOptions } from "codefend/build/src/core/options";
import { obfuscate, buildRuntimeOptions, stats } from "codefend";
import { Compilation, Compiler } from "webpack";
import { IRuntimeOptions } from "codefend/build/src/core/runtime";
import { WEBPACK_IGNORED_WORDS } from "./data/Constants";
import { OptionsBuilder } from "./options/OptionsBuilder";
import { IWebpackCodefendOptions } from "./data/Types";
import { ICodefendRuntimeOptions, IWebpackCodefendInternalOptions, IWebpackCodefendOptions } from "./data/Types";
import { ConcatSource } from "webpack-sources";

export class WebpackPluginCodefend {
_name: string;
_options: IObfuscationOptions;
_runtimeOptions: IRuntimeOptions;
___options: IWebpackCodefendInternalOptions;
_runtimeOptions: ICodefendRuntimeOptions;

constructor(options?: IWebpackCodefendOptions) {
this._name = "WebpackPluginCodefend";
this._options = new OptionsBuilder(this._name)
this.___options = new OptionsBuilder(this._name)
.setOptions(options ?? {})
.setAdditionalIgnoredWords(WEBPACK_IGNORED_WORDS)
.build();
Expand All @@ -35,7 +33,7 @@ export class WebpackPluginCodefend {
});

compiler.hooks.done.tap(this._name, () => {
stats({ stats: this._options.stats }, this._runtimeOptions);
stats({ stats: this.___options.debug.stats }, this._runtimeOptions);
});
}

Expand All @@ -44,7 +42,7 @@ export class WebpackPluginCodefend {
}

private _obfuscateSource(source: string): string {
return obfuscate(source, this._options, this._runtimeOptions);
return obfuscate(source, this.___options.transformation, this.___options.parser, this._runtimeOptions);
}
private _overrideFileSource(compilation: Compilation, fileName: string, source: string): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
49 changes: 7 additions & 42 deletions src/options/OptionsAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,14 @@
import { IObfuscationOptions, IPredefinedWordOption, IRegexListOption } from "codefend/build/src/core/options";
import { IWebpackCodefendOptions } from "../data/Types";
import { DEFAULT_TRANSFORMATION_PREFIX, DEFAULT_PARSER_REGEX_LIST } from "../data/Constants";
import { buildDebugOptions, buildParserOptions, buildTransformationOptions } from "codefend";
import { IWebpackCodefendInternalOptions, IWebpackCodefendOptions } from "../data/Types";
import { DEFAULT_PARSER_NAME } from "../data/Constants";

class OptionsAdapter {
transform(pluginOptions: IWebpackCodefendOptions): IObfuscationOptions {
transform(pluginOptions: IWebpackCodefendOptions): IWebpackCodefendInternalOptions {
return {
stats: this.stats(pluginOptions),
prefix: this.prefix(pluginOptions),
ignoredWords: this.ignoredWords(pluginOptions),
predefinedWords: this.predefinedWords(pluginOptions),
regexList: this.regexList(pluginOptions),
parser: buildParserOptions({ name: DEFAULT_PARSER_NAME }).data!,
transformation: buildTransformationOptions(pluginOptions.transformation),
debug: buildDebugOptions(pluginOptions.debug),
};
}

private stats(pluginOptions: IWebpackCodefendOptions): boolean {
return pluginOptions?.debug?.stats ?? true;
}

private prefix(pluginOptions: IWebpackCodefendOptions): string {
return pluginOptions?.transformation?.prefix ?? DEFAULT_TRANSFORMATION_PREFIX;
}

private ignoredWords(pluginOptions: IWebpackCodefendOptions): string[] {
return pluginOptions?.transformation?.ignore ?? [];
}
private predefinedWords(pluginOptions: IWebpackCodefendOptions): IPredefinedWordOption[] {
return (
pluginOptions?.transformation?.static?.map(({ from, to }) => {
return {
originalWord: from,
targetWord: to,
};
}) ?? []
);
}
private regexList(pluginOptions: IWebpackCodefendOptions): IRegexListOption[] {
return (
pluginOptions?.parser?.regexList?.map((e) => {
return {
flag: "g",
name: e.name,
value: e.value,
};
}) ?? DEFAULT_PARSER_REGEX_LIST
);
}
}
export default new OptionsAdapter();
12 changes: 5 additions & 7 deletions src/options/OptionsBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { IObfuscationOptions } from "codefend/build/src/core/options";
import { buildObfuscationOptions } from "codefend";
import { IWebpackCodefendOptions } from "../data/Types";
import { IWebpackCodefendInternalOptions, IWebpackCodefendOptions } from "../data/Types";
import OptionsAdapter from "./OptionsAdapter";
import OptionsValidator from "./OptionsValidator";

export class OptionsBuilder {
name: string;
libraryOptions!: IObfuscationOptions;
libraryOptions!: IWebpackCodefendInternalOptions;
pluginOptions!: IWebpackCodefendOptions;
additionalIgnoredWords: string[] = [];

Expand All @@ -16,7 +14,7 @@ export class OptionsBuilder {

setOptions(pluginOptions: IWebpackCodefendOptions): this {
this.pluginOptions = pluginOptions;
this.libraryOptions = buildObfuscationOptions(OptionsAdapter.transform(pluginOptions));
this.libraryOptions = OptionsAdapter.transform(pluginOptions);
return this;
}

Expand All @@ -25,11 +23,11 @@ export class OptionsBuilder {
return this;
}

build(): IObfuscationOptions {
build(): IWebpackCodefendInternalOptions {
OptionsValidator.validateOptions(this.name, this.pluginOptions);

this.additionalIgnoredWords.forEach((word) => {
this.libraryOptions.ignoredWords.push(word);
this.libraryOptions.transformation.ignore.push(word);
});
return this.libraryOptions;
}
Expand Down
Loading