build-firewall-plugin 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aliaksei Malinouski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
File without changes
@@ -0,0 +1,37 @@
1
+ import { Compiler, RspackPluginInstance } from "@rspack/core";
2
+ type Options = {
3
+ /**
4
+ * Path to your JSON file with project rules
5
+ */
6
+ path: string;
7
+ /**
8
+ * Generate a template JSON files with project rules
9
+ */
10
+ generateFile?: boolean;
11
+ /**
12
+ * Name of your JSON file with project rules
13
+ */
14
+ name?: string;
15
+ /**
16
+ * Generate a JSON file with latest report of checking your project
17
+ * will be added to .gitignore if this file is in your project
18
+ */
19
+ report?: boolean;
20
+ /**
21
+ * What kind of logs you will get
22
+ */
23
+ mode?: 'error' | 'warn';
24
+ /**
25
+ * exceptions that will not be checked, plugin already has a default value:
26
+ *
27
+ * node_modules, assets, images, types, intefaces, styles file and directories
28
+ */
29
+ exceptions?: string[];
30
+ };
31
+ export declare class BuildFirewallPlugin implements RspackPluginInstance {
32
+ private readonly config;
33
+ private shouldRunAgain;
34
+ constructor(options?: Options);
35
+ apply(compiler: Compiler): void;
36
+ }
37
+ export {};
package/build/index.js ADDED
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BuildFirewallPlugin = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ /**
10
+ * empty settings of modules rules map
11
+ */
12
+ const settings = {
13
+ allow: [],
14
+ deny: [],
15
+ };
16
+ /**
17
+ * empty template of project rules map
18
+ */
19
+ const template = {
20
+ layers: {
21
+ ui: settings,
22
+ lib: settings,
23
+ hooks: settings,
24
+ pages: settings,
25
+ models: settings,
26
+ widgets: settings,
27
+ services: settings,
28
+ components: settings,
29
+ declarations: settings,
30
+ },
31
+ mode: 'warn',
32
+ };
33
+ /**
34
+ * dependecies exceptions
35
+ */
36
+ const exceptions = ['node_modules', 'assets', '.css', '.scss', 'styles', 'style', 'images', 'types', 'type', 'inteface', 'intefaces'];
37
+ /**
38
+ * @function guard validates received file by path
39
+ */
40
+ const isValidSchema = (value) => {
41
+ var _a, _b;
42
+ return (!!value &&
43
+ typeof value === 'object' &&
44
+ 'mode' in value &&
45
+ 'layers' in value &&
46
+ typeof value.layers === 'object' &&
47
+ Object.keys((_a = value.layers) !== null && _a !== void 0 ? _a : {}).length > 0 &&
48
+ (value.mode === 'warn' || value.mode === 'error') &&
49
+ Object.entries((_b = value.layers) !== null && _b !== void 0 ? _b : {}).every(([_, layer]) => typeof layer === 'object' && 'allow' in layer && 'deny' in layer
50
+ && Array.isArray(layer.allow) && Array.isArray(layer.deny)));
51
+ };
52
+ /**
53
+ * @function resolveLayer normilize module resource
54
+ */
55
+ function resolveLayer(resource, layers) {
56
+ const normalized = resource.replace(/\\/g, '/');
57
+ for (const layer of Object.keys(layers)) {
58
+ if (normalized.includes(`/src/${layer}/`)) {
59
+ return layer;
60
+ }
61
+ }
62
+ return null;
63
+ }
64
+ class BuildFirewallPlugin {
65
+ constructor(options) {
66
+ this.config = {
67
+ path: '',
68
+ exceptions,
69
+ report: false,
70
+ generateFile: false,
71
+ mode: template.mode,
72
+ layers: template.layers,
73
+ name: 'build-firewall-plugin.rules.json',
74
+ };
75
+ this.shouldRunAgain = false;
76
+ this.config = { ...this.config, ...(Boolean(options) && { ...options }) };
77
+ }
78
+ ;
79
+ apply(compiler) {
80
+ this.shouldRunAgain = false;
81
+ compiler.hooks.thisCompilation.tap('BuildFirewallPluginThisCompilation', (compilation) => {
82
+ compilation.hooks.finishModules.tap('BuildFirewallPluginThisCompilationFinishModules', (modules) => {
83
+ /**
84
+ * Creating or Validating a rules file
85
+ */
86
+ if (!this.config.path) {
87
+ try {
88
+ const pathToFile = path_1.default.resolve(__dirname, this.config.name);
89
+ fs_1.default.writeFileSync(pathToFile, JSON.stringify(template));
90
+ this.shouldRunAgain = true;
91
+ this.config.path = pathToFile;
92
+ console.log(`Created a template of ${this.config.name} file successfully`);
93
+ console.log(`Check and edit a ${this.config.name} file if it is not passing for your project`);
94
+ }
95
+ catch (e) {
96
+ console.error(`Could not create a rules file: \n ${e}`);
97
+ return;
98
+ }
99
+ ;
100
+ }
101
+ else {
102
+ try {
103
+ const pathToFile = path_1.default.resolve(__dirname, this.config.name);
104
+ const rawFile = fs_1.default.readFileSync(pathToFile).toString();
105
+ const extention = path_1.default.extname(pathToFile);
106
+ const isJSON = extention.endsWith('.json');
107
+ if (!isJSON) {
108
+ console.error(`Your ${this.config.name} file is not a JSON`);
109
+ return;
110
+ }
111
+ ;
112
+ if (rawFile.length < 0) {
113
+ console.error(`Your ${this.config.name} file is empty`);
114
+ return;
115
+ }
116
+ ;
117
+ const parsedFile = JSON.parse(rawFile);
118
+ const isObject = typeof parsedFile === 'object';
119
+ if (!isObject) {
120
+ console.error(`Your ${this.config.name} file value is not an object`);
121
+ return;
122
+ }
123
+ ;
124
+ const hasValidSchema = isValidSchema(parsedFile);
125
+ if (!hasValidSchema) {
126
+ console.error(`Your ${this.config.name} file value is not valid schema, it has to be like this: \n
127
+ {
128
+ layers: {
129
+ [name]: { allow: [], deny: [] },
130
+ mode: 'warn'
131
+ }
132
+ }
133
+ `);
134
+ return;
135
+ }
136
+ ;
137
+ this.config.layers = parsedFile.layers;
138
+ this.config.mode = parsedFile.mode || 'warn';
139
+ console.log(`Your ${this.config.name} file is valid!`);
140
+ }
141
+ catch (e) {
142
+ console.error(`Error while validating your ${this.config.name} file \n ${e}`);
143
+ return;
144
+ }
145
+ ;
146
+ }
147
+ ;
148
+ if (this.shouldRunAgain)
149
+ return;
150
+ /**
151
+ * plugin logic
152
+ */
153
+ const graph = compilation.moduleGraph;
154
+ for (const module of modules) {
155
+ const fromResource = module.nameForCondition();
156
+ if (!fromResource)
157
+ continue;
158
+ const fromLayer = resolveLayer(fromResource, this.config.layers);
159
+ if (!fromLayer)
160
+ continue;
161
+ for (const dependency of module.dependencies) {
162
+ const toModule = compilation.moduleGraph.getModule(dependency);
163
+ const toModuleResource = toModule === null || toModule === void 0 ? void 0 : toModule.nameForCondition();
164
+ if (!toModuleResource)
165
+ continue;
166
+ const isException = this.config.exceptions.some(e => toModuleResource.includes(e));
167
+ if (isException) {
168
+ continue;
169
+ }
170
+ const toLayer = resolveLayer(toModuleResource, this.config.layers);
171
+ if (!toLayer)
172
+ continue;
173
+ if (fromLayer === toLayer && fromResource === toModuleResource) {
174
+ continue;
175
+ }
176
+ const rules = this.config.layers[fromLayer];
177
+ if (!rules)
178
+ continue;
179
+ const isDenided = rules.deny.includes(toLayer);
180
+ const isAllowed = rules.allow.includes(toLayer);
181
+ if (isDenided || !isAllowed) {
182
+ /** report */
183
+ }
184
+ }
185
+ }
186
+ });
187
+ });
188
+ }
189
+ ;
190
+ }
191
+ exports.BuildFirewallPlugin = BuildFirewallPlugin;
192
+ ;
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "build-firewall-plugin",
3
+ "version": "0.0.1",
4
+ "type": "commonjs",
5
+ "license": "MIT",
6
+ "main": "build/index.js",
7
+ "types": "build/index.d.ts",
8
+ "author": "Aliaksei Malinouski",
9
+ "description": "",
10
+ "keywords": [
11
+ "rspack",
12
+ "rspack-plugin",
13
+ "build-tracing",
14
+ "dependency-analysis",
15
+ "React",
16
+ "TypeScript",
17
+ "build-optimization",
18
+ "module-tracking"
19
+ ],
20
+ "files": [
21
+ "build",
22
+ "LICENSE"
23
+ ],
24
+ "scripts": {
25
+ "test": "vitest",
26
+ "build": "tsc -p tsconfig.build.json"
27
+ },
28
+ "peerDependencies": {
29
+ "@rspack/core": ">=0.5.0"
30
+ },
31
+ "devDependencies": {
32
+ "@rspack/cli": "1.6.5",
33
+ "@rspack/core": "1.6.5",
34
+ "@types/node": "24.10.1",
35
+ "typescript": "5.9.3",
36
+ "vitest": "4.0.15"
37
+ }
38
+ }