env-attr-cleaner-bun 1.0.0
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 +21 -0
- package/dist/index.cjs +59 -0
- package/dist/index.d.cts +74 -0
- package/dist/index.d.mts +72 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.mjs +51 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tcharlyto
|
|
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/dist/index.cjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const DEFAULT_CONFIG = {
|
|
6
|
+
environments: {
|
|
7
|
+
development: [],
|
|
8
|
+
test: [],
|
|
9
|
+
staging: ["data-test-*", "data-debug-*"],
|
|
10
|
+
production: ["data-test-*", "data-debug-*"]
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
function matchPattern(attr, pattern) {
|
|
14
|
+
const regex = new RegExp("^" + pattern.replace(/\*/g, ".*") + "$");
|
|
15
|
+
return regex.test(attr);
|
|
16
|
+
}
|
|
17
|
+
function shouldStrip(attr, patterns) {
|
|
18
|
+
return patterns.some((pattern) => matchPattern(attr, pattern));
|
|
19
|
+
}
|
|
20
|
+
function stripDataAttributes(code, stripPatterns) {
|
|
21
|
+
return code.replace(/\s+(data-[\w-]+)=(?:"[^"]*"|'[^']*')/g, (match, attr) => {
|
|
22
|
+
if (shouldStrip(attr, stripPatterns)) {
|
|
23
|
+
return "";
|
|
24
|
+
}
|
|
25
|
+
return match;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function resolvePatterns(config) {
|
|
29
|
+
const env = process.env.NODE_ENV ?? "development";
|
|
30
|
+
return config.environments[env] ?? [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const BUN_FILE_PATTERN = /\.(tsx?|jsx?|vue|svelte)$/;
|
|
34
|
+
function envAttrCleaner(userConfig = {}) {
|
|
35
|
+
const config = {
|
|
36
|
+
environments: { ...DEFAULT_CONFIG.environments, ...userConfig.environments }
|
|
37
|
+
};
|
|
38
|
+
const allowedPatterns = resolvePatterns(config);
|
|
39
|
+
return {
|
|
40
|
+
name: "bun-plugin-env-attr-cleaner",
|
|
41
|
+
setup(build) {
|
|
42
|
+
build.onLoad({ filter: BUN_FILE_PATTERN }, async (args) => {
|
|
43
|
+
const fs = await import('fs');
|
|
44
|
+
const source = fs.readFileSync(args.path, "utf8");
|
|
45
|
+
const loader = args.path.endsWith(".ts") || args.path.endsWith(".tsx") ? "ts" : "js";
|
|
46
|
+
return {
|
|
47
|
+
contents: stripDataAttributes(source, allowedPatterns),
|
|
48
|
+
loader
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
exports.default = envAttrCleaner;
|
|
56
|
+
exports.envAttrCleaner = envAttrCleaner;
|
|
57
|
+
exports.matchPattern = matchPattern;
|
|
58
|
+
exports.shouldStrip = shouldStrip;
|
|
59
|
+
exports.stripDataAttributes = stripDataAttributes;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-environment configuration for data-* attribute patterns to strip.
|
|
3
|
+
* Each key is an environment name and its value is a list of glob-style patterns to remove.
|
|
4
|
+
*/
|
|
5
|
+
interface IEnvAttrCleanerConfig {
|
|
6
|
+
/** Map of environment names to lists of data-* attribute patterns to strip. */
|
|
7
|
+
environments: Record<string, string[]>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Returns whether a data-* attribute name matches a glob-style pattern.
|
|
11
|
+
* Supports `*` as a wildcard.
|
|
12
|
+
*
|
|
13
|
+
* @param attr - The attribute name to test (e.g. `data-test-id`).
|
|
14
|
+
* @param pattern - The glob pattern to match against (e.g. `data-test-*`).
|
|
15
|
+
*/
|
|
16
|
+
declare function matchPattern(attr: string, pattern: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Returns whether a data-* attribute should be stripped,
|
|
19
|
+
* i.e. whether it matches at least one of the strip patterns.
|
|
20
|
+
*
|
|
21
|
+
* @param attr - The attribute name to test.
|
|
22
|
+
* @param patterns - List of glob patterns marking attributes for removal.
|
|
23
|
+
*/
|
|
24
|
+
declare function shouldStrip(attr: string, patterns: string[]): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Strips data-* attributes from an HTML or source code string
|
|
27
|
+
* when they match one of the given patterns. All other attributes are preserved.
|
|
28
|
+
*
|
|
29
|
+
* @param code - The source string to process.
|
|
30
|
+
* @param stripPatterns - List of glob patterns for attributes to remove.
|
|
31
|
+
* @returns The processed string with matched data-* attributes removed.
|
|
32
|
+
*/
|
|
33
|
+
declare function stripDataAttributes(code: string, stripPatterns: string[]): string;
|
|
34
|
+
|
|
35
|
+
/** Subset of the Bun build API used by the envAttrCleaner plugin. */
|
|
36
|
+
interface IBunBuild {
|
|
37
|
+
onLoad: (options: {
|
|
38
|
+
filter: RegExp;
|
|
39
|
+
}, callback: (args: {
|
|
40
|
+
path: string;
|
|
41
|
+
}) => Promise<{
|
|
42
|
+
contents: string;
|
|
43
|
+
loader: string;
|
|
44
|
+
}>) => void;
|
|
45
|
+
}
|
|
46
|
+
/** Shape of a Bun bundler plugin. */
|
|
47
|
+
interface IBunPlugin {
|
|
48
|
+
name: string;
|
|
49
|
+
setup: (build: IBunBuild) => void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Bun plugin that strips non-allowed data-* attributes from source files at build time,
|
|
53
|
+
* based on the current `NODE_ENV`.
|
|
54
|
+
*
|
|
55
|
+
* @param userConfig - Optional partial configuration to override the default environment patterns.
|
|
56
|
+
* @returns A Bun plugin object to pass to `Bun.build({ plugins: [...] })`.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import envAttrCleaner from 'env-attr-cleaner-bun'
|
|
61
|
+
*
|
|
62
|
+
* await Bun.build({
|
|
63
|
+
* entrypoints: ['./src/index.tsx'],
|
|
64
|
+
* outdir: './dist',
|
|
65
|
+
* plugins: [envAttrCleaner()]
|
|
66
|
+
* })
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function envAttrCleaner(userConfig?: Partial<IEnvAttrCleanerConfig>): IBunPlugin;
|
|
70
|
+
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
export = envAttrCleaner;
|
|
73
|
+
export { envAttrCleaner, matchPattern, shouldStrip, stripDataAttributes };
|
|
74
|
+
export type { IBunBuild, IBunPlugin, IEnvAttrCleanerConfig };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-environment configuration for data-* attribute patterns to strip.
|
|
3
|
+
* Each key is an environment name and its value is a list of glob-style patterns to remove.
|
|
4
|
+
*/
|
|
5
|
+
interface IEnvAttrCleanerConfig {
|
|
6
|
+
/** Map of environment names to lists of data-* attribute patterns to strip. */
|
|
7
|
+
environments: Record<string, string[]>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Returns whether a data-* attribute name matches a glob-style pattern.
|
|
11
|
+
* Supports `*` as a wildcard.
|
|
12
|
+
*
|
|
13
|
+
* @param attr - The attribute name to test (e.g. `data-test-id`).
|
|
14
|
+
* @param pattern - The glob pattern to match against (e.g. `data-test-*`).
|
|
15
|
+
*/
|
|
16
|
+
declare function matchPattern(attr: string, pattern: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Returns whether a data-* attribute should be stripped,
|
|
19
|
+
* i.e. whether it matches at least one of the strip patterns.
|
|
20
|
+
*
|
|
21
|
+
* @param attr - The attribute name to test.
|
|
22
|
+
* @param patterns - List of glob patterns marking attributes for removal.
|
|
23
|
+
*/
|
|
24
|
+
declare function shouldStrip(attr: string, patterns: string[]): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Strips data-* attributes from an HTML or source code string
|
|
27
|
+
* when they match one of the given patterns. All other attributes are preserved.
|
|
28
|
+
*
|
|
29
|
+
* @param code - The source string to process.
|
|
30
|
+
* @param stripPatterns - List of glob patterns for attributes to remove.
|
|
31
|
+
* @returns The processed string with matched data-* attributes removed.
|
|
32
|
+
*/
|
|
33
|
+
declare function stripDataAttributes(code: string, stripPatterns: string[]): string;
|
|
34
|
+
|
|
35
|
+
/** Subset of the Bun build API used by the envAttrCleaner plugin. */
|
|
36
|
+
interface IBunBuild {
|
|
37
|
+
onLoad: (options: {
|
|
38
|
+
filter: RegExp;
|
|
39
|
+
}, callback: (args: {
|
|
40
|
+
path: string;
|
|
41
|
+
}) => Promise<{
|
|
42
|
+
contents: string;
|
|
43
|
+
loader: string;
|
|
44
|
+
}>) => void;
|
|
45
|
+
}
|
|
46
|
+
/** Shape of a Bun bundler plugin. */
|
|
47
|
+
interface IBunPlugin {
|
|
48
|
+
name: string;
|
|
49
|
+
setup: (build: IBunBuild) => void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Bun plugin that strips non-allowed data-* attributes from source files at build time,
|
|
53
|
+
* based on the current `NODE_ENV`.
|
|
54
|
+
*
|
|
55
|
+
* @param userConfig - Optional partial configuration to override the default environment patterns.
|
|
56
|
+
* @returns A Bun plugin object to pass to `Bun.build({ plugins: [...] })`.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import envAttrCleaner from 'env-attr-cleaner-bun'
|
|
61
|
+
*
|
|
62
|
+
* await Bun.build({
|
|
63
|
+
* entrypoints: ['./src/index.tsx'],
|
|
64
|
+
* outdir: './dist',
|
|
65
|
+
* plugins: [envAttrCleaner()]
|
|
66
|
+
* })
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function envAttrCleaner(userConfig?: Partial<IEnvAttrCleanerConfig>): IBunPlugin;
|
|
70
|
+
|
|
71
|
+
export { envAttrCleaner as default, envAttrCleaner, matchPattern, shouldStrip, stripDataAttributes };
|
|
72
|
+
export type { IBunBuild, IBunPlugin, IEnvAttrCleanerConfig };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-environment configuration for data-* attribute patterns to strip.
|
|
3
|
+
* Each key is an environment name and its value is a list of glob-style patterns to remove.
|
|
4
|
+
*/
|
|
5
|
+
interface IEnvAttrCleanerConfig {
|
|
6
|
+
/** Map of environment names to lists of data-* attribute patterns to strip. */
|
|
7
|
+
environments: Record<string, string[]>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Returns whether a data-* attribute name matches a glob-style pattern.
|
|
11
|
+
* Supports `*` as a wildcard.
|
|
12
|
+
*
|
|
13
|
+
* @param attr - The attribute name to test (e.g. `data-test-id`).
|
|
14
|
+
* @param pattern - The glob pattern to match against (e.g. `data-test-*`).
|
|
15
|
+
*/
|
|
16
|
+
declare function matchPattern(attr: string, pattern: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Returns whether a data-* attribute should be stripped,
|
|
19
|
+
* i.e. whether it matches at least one of the strip patterns.
|
|
20
|
+
*
|
|
21
|
+
* @param attr - The attribute name to test.
|
|
22
|
+
* @param patterns - List of glob patterns marking attributes for removal.
|
|
23
|
+
*/
|
|
24
|
+
declare function shouldStrip(attr: string, patterns: string[]): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Strips data-* attributes from an HTML or source code string
|
|
27
|
+
* when they match one of the given patterns. All other attributes are preserved.
|
|
28
|
+
*
|
|
29
|
+
* @param code - The source string to process.
|
|
30
|
+
* @param stripPatterns - List of glob patterns for attributes to remove.
|
|
31
|
+
* @returns The processed string with matched data-* attributes removed.
|
|
32
|
+
*/
|
|
33
|
+
declare function stripDataAttributes(code: string, stripPatterns: string[]): string;
|
|
34
|
+
|
|
35
|
+
/** Subset of the Bun build API used by the envAttrCleaner plugin. */
|
|
36
|
+
interface IBunBuild {
|
|
37
|
+
onLoad: (options: {
|
|
38
|
+
filter: RegExp;
|
|
39
|
+
}, callback: (args: {
|
|
40
|
+
path: string;
|
|
41
|
+
}) => Promise<{
|
|
42
|
+
contents: string;
|
|
43
|
+
loader: string;
|
|
44
|
+
}>) => void;
|
|
45
|
+
}
|
|
46
|
+
/** Shape of a Bun bundler plugin. */
|
|
47
|
+
interface IBunPlugin {
|
|
48
|
+
name: string;
|
|
49
|
+
setup: (build: IBunBuild) => void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Bun plugin that strips non-allowed data-* attributes from source files at build time,
|
|
53
|
+
* based on the current `NODE_ENV`.
|
|
54
|
+
*
|
|
55
|
+
* @param userConfig - Optional partial configuration to override the default environment patterns.
|
|
56
|
+
* @returns A Bun plugin object to pass to `Bun.build({ plugins: [...] })`.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import envAttrCleaner from 'env-attr-cleaner-bun'
|
|
61
|
+
*
|
|
62
|
+
* await Bun.build({
|
|
63
|
+
* entrypoints: ['./src/index.tsx'],
|
|
64
|
+
* outdir: './dist',
|
|
65
|
+
* plugins: [envAttrCleaner()]
|
|
66
|
+
* })
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function envAttrCleaner(userConfig?: Partial<IEnvAttrCleanerConfig>): IBunPlugin;
|
|
70
|
+
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
export = envAttrCleaner;
|
|
73
|
+
export { envAttrCleaner, matchPattern, shouldStrip, stripDataAttributes };
|
|
74
|
+
export type { IBunBuild, IBunPlugin, IEnvAttrCleanerConfig };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const DEFAULT_CONFIG = {
|
|
2
|
+
environments: {
|
|
3
|
+
development: [],
|
|
4
|
+
test: [],
|
|
5
|
+
staging: ["data-test-*", "data-debug-*"],
|
|
6
|
+
production: ["data-test-*", "data-debug-*"]
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
function matchPattern(attr, pattern) {
|
|
10
|
+
const regex = new RegExp("^" + pattern.replace(/\*/g, ".*") + "$");
|
|
11
|
+
return regex.test(attr);
|
|
12
|
+
}
|
|
13
|
+
function shouldStrip(attr, patterns) {
|
|
14
|
+
return patterns.some((pattern) => matchPattern(attr, pattern));
|
|
15
|
+
}
|
|
16
|
+
function stripDataAttributes(code, stripPatterns) {
|
|
17
|
+
return code.replace(/\s+(data-[\w-]+)=(?:"[^"]*"|'[^']*')/g, (match, attr) => {
|
|
18
|
+
if (shouldStrip(attr, stripPatterns)) {
|
|
19
|
+
return "";
|
|
20
|
+
}
|
|
21
|
+
return match;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function resolvePatterns(config) {
|
|
25
|
+
const env = process.env.NODE_ENV ?? "development";
|
|
26
|
+
return config.environments[env] ?? [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const BUN_FILE_PATTERN = /\.(tsx?|jsx?|vue|svelte)$/;
|
|
30
|
+
function envAttrCleaner(userConfig = {}) {
|
|
31
|
+
const config = {
|
|
32
|
+
environments: { ...DEFAULT_CONFIG.environments, ...userConfig.environments }
|
|
33
|
+
};
|
|
34
|
+
const allowedPatterns = resolvePatterns(config);
|
|
35
|
+
return {
|
|
36
|
+
name: "bun-plugin-env-attr-cleaner",
|
|
37
|
+
setup(build) {
|
|
38
|
+
build.onLoad({ filter: BUN_FILE_PATTERN }, async (args) => {
|
|
39
|
+
const fs = await import('fs');
|
|
40
|
+
const source = fs.readFileSync(args.path, "utf8");
|
|
41
|
+
const loader = args.path.endsWith(".ts") || args.path.endsWith(".tsx") ? "ts" : "js";
|
|
42
|
+
return {
|
|
43
|
+
contents: stripDataAttributes(source, allowedPatterns),
|
|
44
|
+
loader
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { envAttrCleaner as default, envAttrCleaner, matchPattern, shouldStrip, stripDataAttributes };
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "env-attr-cleaner-bun",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Bun plugin to strip data-* attributes from production builds",
|
|
5
|
+
"author": "techmefr",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.cjs",
|
|
15
|
+
"module": "./dist/index.mjs",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"bun",
|
|
22
|
+
"bundler",
|
|
23
|
+
"data-attributes",
|
|
24
|
+
"testing",
|
|
25
|
+
"e2e",
|
|
26
|
+
"playwright",
|
|
27
|
+
"cypress"
|
|
28
|
+
],
|
|
29
|
+
"homepage": "https://github.com/techmefr/env-attr-cleaner/tree/main/packages/bun#readme",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/techmefr/env-attr-cleaner.git",
|
|
33
|
+
"directory": "packages/bun"
|
|
34
|
+
},
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/techmefr/env-attr-cleaner/issues"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"unbuild": "^3.0.0",
|
|
40
|
+
"vitest": "^1.6.0",
|
|
41
|
+
"typescript": "^5.3.3"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "unbuild",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"typecheck": "tsc --noEmit"
|
|
47
|
+
}
|
|
48
|
+
}
|