env-attr-cleaner 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 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,67 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const unplugin = require('unplugin');
6
+
7
+ const DEFAULT_CONFIG = {
8
+ environments: {
9
+ development: [],
10
+ test: [],
11
+ staging: ["data-test-*", "data-debug-*"],
12
+ production: ["data-test-*", "data-debug-*"]
13
+ }
14
+ };
15
+ function matchPattern(attr, pattern) {
16
+ const regex = new RegExp("^" + pattern.replace(/\*/g, ".*") + "$");
17
+ return regex.test(attr);
18
+ }
19
+ function shouldStrip(attr, patterns) {
20
+ return patterns.some((pattern) => matchPattern(attr, pattern));
21
+ }
22
+ function stripDataAttributes(code, stripPatterns) {
23
+ return code.replace(
24
+ /\s+(?:v-bind:|:)?(data-[\w-]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|\{(?:[^{}]|\{[^{}]*\})*\}|[^\s"'`<>/{}]+)|(?=[\s/>]|$))/g,
25
+ (match, attr) => shouldStrip(attr, stripPatterns) ? "" : match
26
+ );
27
+ }
28
+ function resolvePatterns(config) {
29
+ const env = process.env.NODE_ENV ?? "development";
30
+ return config.environments[env] ?? [];
31
+ }
32
+
33
+ const FILE_PATTERN = /\.(vue|svelte|astro|jsx?|tsx?)$/;
34
+ const envAttrCleanerPlugin = unplugin.createUnplugin((userConfig = {}) => {
35
+ const config = {
36
+ environments: { ...DEFAULT_CONFIG.environments, ...userConfig.environments }
37
+ };
38
+ const allowedPatterns = resolvePatterns(config);
39
+ return {
40
+ name: "env-attr-cleaner",
41
+ enforce: "pre",
42
+ transformInclude(id) {
43
+ return FILE_PATTERN.test(id);
44
+ },
45
+ transform(code) {
46
+ return stripDataAttributes(code, allowedPatterns);
47
+ },
48
+ vite: {
49
+ transformIndexHtml(html) {
50
+ return stripDataAttributes(html, allowedPatterns);
51
+ }
52
+ }
53
+ };
54
+ });
55
+ const vite = envAttrCleanerPlugin.vite;
56
+ const rollup = envAttrCleanerPlugin.rollup;
57
+ const webpack = envAttrCleanerPlugin.webpack;
58
+ const esbuild = envAttrCleanerPlugin.esbuild;
59
+
60
+ exports.default = envAttrCleanerPlugin;
61
+ exports.esbuild = esbuild;
62
+ exports.matchPattern = matchPattern;
63
+ exports.rollup = rollup;
64
+ exports.shouldStrip = shouldStrip;
65
+ exports.stripDataAttributes = stripDataAttributes;
66
+ exports.vite = vite;
67
+ exports.webpack = webpack;
@@ -0,0 +1,79 @@
1
+ import * as _esbuild from 'esbuild';
2
+ import * as _webpack from 'webpack';
3
+ import * as _rollup from 'rollup';
4
+ import * as _vite from 'vite';
5
+ import * as unplugin from 'unplugin';
6
+
7
+ /**
8
+ * Per-environment configuration for data-* attribute patterns to strip.
9
+ * Each key is an environment name and its value is a list of glob-style patterns to remove.
10
+ */
11
+ interface IEnvAttrCleanerConfig {
12
+ /** Map of environment names to lists of data-* attribute patterns to strip. */
13
+ environments: Record<string, string[]>;
14
+ }
15
+ /**
16
+ * Returns whether a data-* attribute name matches a glob-style pattern.
17
+ * Supports `*` as a wildcard.
18
+ *
19
+ * @param attr - The attribute name to test (e.g. `data-test-id`).
20
+ * @param pattern - The glob pattern to match against (e.g. `data-test-*`).
21
+ */
22
+ declare function matchPattern(attr: string, pattern: string): boolean;
23
+ /**
24
+ * Returns whether a data-* attribute should be stripped,
25
+ * i.e. whether it matches at least one of the strip patterns.
26
+ *
27
+ * @param attr - The attribute name to test.
28
+ * @param patterns - List of glob patterns marking attributes for removal.
29
+ */
30
+ declare function shouldStrip(attr: string, patterns: string[]): boolean;
31
+ /**
32
+ * Strips data-* attributes from an HTML or source code string
33
+ * when they match one of the given patterns. All other attributes are preserved.
34
+ *
35
+ * Recognised attribute forms:
36
+ * - quoted values: `data-test-id="x"`, `data-test-id='x'`
37
+ * - expression values: `data-test-id={id}`, `` data-test-id={`p-${id}`} `` (JSX / Svelte)
38
+ * - bound names: `:data-test-id="x"`, `v-bind:data-test-id="x"` (Vue)
39
+ * - unquoted values: `data-test-id=x`
40
+ * - value-less attributes: `data-test-active`
41
+ *
42
+ * @param code - The source string to process.
43
+ * @param stripPatterns - List of glob patterns for attributes to remove.
44
+ * @returns The processed string with matched data-* attributes removed.
45
+ */
46
+ declare function stripDataAttributes(code: string, stripPatterns: string[]): string;
47
+
48
+ /**
49
+ * Universal envAttrCleaner plugin built with unplugin.
50
+ * Strips non-allowed data-* attributes from source files and HTML at build time,
51
+ * based on the current `NODE_ENV`.
52
+ *
53
+ * Supports Vite, Rollup, Webpack, and esbuild via the named exports below.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * // vite.config.ts
58
+ * import { vite as envAttrCleaner } from 'env-attr-cleaner'
59
+ * plugins: [envAttrCleaner()]
60
+ *
61
+ * // next.config.ts
62
+ * import { webpack as envAttrCleaner } from 'env-attr-cleaner'
63
+ * webpack(config) { config.plugins.push(envAttrCleaner()); return config }
64
+ * ```
65
+ */
66
+ declare const envAttrCleanerPlugin: unplugin.UnpluginInstance<Partial<IEnvAttrCleanerConfig>, boolean>;
67
+ /** Vite plugin adapter. */
68
+ declare const vite: (options: Partial<IEnvAttrCleanerConfig>) => _vite.Plugin<any> | _vite.Plugin<any>[];
69
+ /** Rollup plugin adapter. */
70
+ declare const rollup: (options: Partial<IEnvAttrCleanerConfig>) => _rollup.Plugin<any> | _rollup.Plugin<any>[];
71
+ /** Webpack plugin adapter (also works with Next.js). */
72
+ declare const webpack: (options: Partial<IEnvAttrCleanerConfig>) => _webpack.WebpackPluginInstance;
73
+ /** esbuild plugin adapter. */
74
+ declare const esbuild: (options: Partial<IEnvAttrCleanerConfig>) => _esbuild.Plugin;
75
+
76
+ // @ts-ignore
77
+ export = envAttrCleanerPlugin;
78
+ export { esbuild, matchPattern, rollup, shouldStrip, stripDataAttributes, vite, webpack };
79
+ export type { IEnvAttrCleanerConfig };
@@ -0,0 +1,77 @@
1
+ import * as _esbuild from 'esbuild';
2
+ import * as _webpack from 'webpack';
3
+ import * as _rollup from 'rollup';
4
+ import * as _vite from 'vite';
5
+ import * as unplugin from 'unplugin';
6
+
7
+ /**
8
+ * Per-environment configuration for data-* attribute patterns to strip.
9
+ * Each key is an environment name and its value is a list of glob-style patterns to remove.
10
+ */
11
+ interface IEnvAttrCleanerConfig {
12
+ /** Map of environment names to lists of data-* attribute patterns to strip. */
13
+ environments: Record<string, string[]>;
14
+ }
15
+ /**
16
+ * Returns whether a data-* attribute name matches a glob-style pattern.
17
+ * Supports `*` as a wildcard.
18
+ *
19
+ * @param attr - The attribute name to test (e.g. `data-test-id`).
20
+ * @param pattern - The glob pattern to match against (e.g. `data-test-*`).
21
+ */
22
+ declare function matchPattern(attr: string, pattern: string): boolean;
23
+ /**
24
+ * Returns whether a data-* attribute should be stripped,
25
+ * i.e. whether it matches at least one of the strip patterns.
26
+ *
27
+ * @param attr - The attribute name to test.
28
+ * @param patterns - List of glob patterns marking attributes for removal.
29
+ */
30
+ declare function shouldStrip(attr: string, patterns: string[]): boolean;
31
+ /**
32
+ * Strips data-* attributes from an HTML or source code string
33
+ * when they match one of the given patterns. All other attributes are preserved.
34
+ *
35
+ * Recognised attribute forms:
36
+ * - quoted values: `data-test-id="x"`, `data-test-id='x'`
37
+ * - expression values: `data-test-id={id}`, `` data-test-id={`p-${id}`} `` (JSX / Svelte)
38
+ * - bound names: `:data-test-id="x"`, `v-bind:data-test-id="x"` (Vue)
39
+ * - unquoted values: `data-test-id=x`
40
+ * - value-less attributes: `data-test-active`
41
+ *
42
+ * @param code - The source string to process.
43
+ * @param stripPatterns - List of glob patterns for attributes to remove.
44
+ * @returns The processed string with matched data-* attributes removed.
45
+ */
46
+ declare function stripDataAttributes(code: string, stripPatterns: string[]): string;
47
+
48
+ /**
49
+ * Universal envAttrCleaner plugin built with unplugin.
50
+ * Strips non-allowed data-* attributes from source files and HTML at build time,
51
+ * based on the current `NODE_ENV`.
52
+ *
53
+ * Supports Vite, Rollup, Webpack, and esbuild via the named exports below.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * // vite.config.ts
58
+ * import { vite as envAttrCleaner } from 'env-attr-cleaner'
59
+ * plugins: [envAttrCleaner()]
60
+ *
61
+ * // next.config.ts
62
+ * import { webpack as envAttrCleaner } from 'env-attr-cleaner'
63
+ * webpack(config) { config.plugins.push(envAttrCleaner()); return config }
64
+ * ```
65
+ */
66
+ declare const envAttrCleanerPlugin: unplugin.UnpluginInstance<Partial<IEnvAttrCleanerConfig>, boolean>;
67
+ /** Vite plugin adapter. */
68
+ declare const vite: (options: Partial<IEnvAttrCleanerConfig>) => _vite.Plugin<any> | _vite.Plugin<any>[];
69
+ /** Rollup plugin adapter. */
70
+ declare const rollup: (options: Partial<IEnvAttrCleanerConfig>) => _rollup.Plugin<any> | _rollup.Plugin<any>[];
71
+ /** Webpack plugin adapter (also works with Next.js). */
72
+ declare const webpack: (options: Partial<IEnvAttrCleanerConfig>) => _webpack.WebpackPluginInstance;
73
+ /** esbuild plugin adapter. */
74
+ declare const esbuild: (options: Partial<IEnvAttrCleanerConfig>) => _esbuild.Plugin;
75
+
76
+ export { envAttrCleanerPlugin as default, esbuild, matchPattern, rollup, shouldStrip, stripDataAttributes, vite, webpack };
77
+ export type { IEnvAttrCleanerConfig };
@@ -0,0 +1,79 @@
1
+ import * as _esbuild from 'esbuild';
2
+ import * as _webpack from 'webpack';
3
+ import * as _rollup from 'rollup';
4
+ import * as _vite from 'vite';
5
+ import * as unplugin from 'unplugin';
6
+
7
+ /**
8
+ * Per-environment configuration for data-* attribute patterns to strip.
9
+ * Each key is an environment name and its value is a list of glob-style patterns to remove.
10
+ */
11
+ interface IEnvAttrCleanerConfig {
12
+ /** Map of environment names to lists of data-* attribute patterns to strip. */
13
+ environments: Record<string, string[]>;
14
+ }
15
+ /**
16
+ * Returns whether a data-* attribute name matches a glob-style pattern.
17
+ * Supports `*` as a wildcard.
18
+ *
19
+ * @param attr - The attribute name to test (e.g. `data-test-id`).
20
+ * @param pattern - The glob pattern to match against (e.g. `data-test-*`).
21
+ */
22
+ declare function matchPattern(attr: string, pattern: string): boolean;
23
+ /**
24
+ * Returns whether a data-* attribute should be stripped,
25
+ * i.e. whether it matches at least one of the strip patterns.
26
+ *
27
+ * @param attr - The attribute name to test.
28
+ * @param patterns - List of glob patterns marking attributes for removal.
29
+ */
30
+ declare function shouldStrip(attr: string, patterns: string[]): boolean;
31
+ /**
32
+ * Strips data-* attributes from an HTML or source code string
33
+ * when they match one of the given patterns. All other attributes are preserved.
34
+ *
35
+ * Recognised attribute forms:
36
+ * - quoted values: `data-test-id="x"`, `data-test-id='x'`
37
+ * - expression values: `data-test-id={id}`, `` data-test-id={`p-${id}`} `` (JSX / Svelte)
38
+ * - bound names: `:data-test-id="x"`, `v-bind:data-test-id="x"` (Vue)
39
+ * - unquoted values: `data-test-id=x`
40
+ * - value-less attributes: `data-test-active`
41
+ *
42
+ * @param code - The source string to process.
43
+ * @param stripPatterns - List of glob patterns for attributes to remove.
44
+ * @returns The processed string with matched data-* attributes removed.
45
+ */
46
+ declare function stripDataAttributes(code: string, stripPatterns: string[]): string;
47
+
48
+ /**
49
+ * Universal envAttrCleaner plugin built with unplugin.
50
+ * Strips non-allowed data-* attributes from source files and HTML at build time,
51
+ * based on the current `NODE_ENV`.
52
+ *
53
+ * Supports Vite, Rollup, Webpack, and esbuild via the named exports below.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * // vite.config.ts
58
+ * import { vite as envAttrCleaner } from 'env-attr-cleaner'
59
+ * plugins: [envAttrCleaner()]
60
+ *
61
+ * // next.config.ts
62
+ * import { webpack as envAttrCleaner } from 'env-attr-cleaner'
63
+ * webpack(config) { config.plugins.push(envAttrCleaner()); return config }
64
+ * ```
65
+ */
66
+ declare const envAttrCleanerPlugin: unplugin.UnpluginInstance<Partial<IEnvAttrCleanerConfig>, boolean>;
67
+ /** Vite plugin adapter. */
68
+ declare const vite: (options: Partial<IEnvAttrCleanerConfig>) => _vite.Plugin<any> | _vite.Plugin<any>[];
69
+ /** Rollup plugin adapter. */
70
+ declare const rollup: (options: Partial<IEnvAttrCleanerConfig>) => _rollup.Plugin<any> | _rollup.Plugin<any>[];
71
+ /** Webpack plugin adapter (also works with Next.js). */
72
+ declare const webpack: (options: Partial<IEnvAttrCleanerConfig>) => _webpack.WebpackPluginInstance;
73
+ /** esbuild plugin adapter. */
74
+ declare const esbuild: (options: Partial<IEnvAttrCleanerConfig>) => _esbuild.Plugin;
75
+
76
+ // @ts-ignore
77
+ export = envAttrCleanerPlugin;
78
+ export { esbuild, matchPattern, rollup, shouldStrip, stripDataAttributes, vite, webpack };
79
+ export type { IEnvAttrCleanerConfig };
package/dist/index.mjs ADDED
@@ -0,0 +1,56 @@
1
+ import { createUnplugin } from 'unplugin';
2
+
3
+ const DEFAULT_CONFIG = {
4
+ environments: {
5
+ development: [],
6
+ test: [],
7
+ staging: ["data-test-*", "data-debug-*"],
8
+ production: ["data-test-*", "data-debug-*"]
9
+ }
10
+ };
11
+ function matchPattern(attr, pattern) {
12
+ const regex = new RegExp("^" + pattern.replace(/\*/g, ".*") + "$");
13
+ return regex.test(attr);
14
+ }
15
+ function shouldStrip(attr, patterns) {
16
+ return patterns.some((pattern) => matchPattern(attr, pattern));
17
+ }
18
+ function stripDataAttributes(code, stripPatterns) {
19
+ return code.replace(
20
+ /\s+(?:v-bind:|:)?(data-[\w-]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|\{(?:[^{}]|\{[^{}]*\})*\}|[^\s"'`<>/{}]+)|(?=[\s/>]|$))/g,
21
+ (match, attr) => shouldStrip(attr, stripPatterns) ? "" : match
22
+ );
23
+ }
24
+ function resolvePatterns(config) {
25
+ const env = process.env.NODE_ENV ?? "development";
26
+ return config.environments[env] ?? [];
27
+ }
28
+
29
+ const FILE_PATTERN = /\.(vue|svelte|astro|jsx?|tsx?)$/;
30
+ const envAttrCleanerPlugin = createUnplugin((userConfig = {}) => {
31
+ const config = {
32
+ environments: { ...DEFAULT_CONFIG.environments, ...userConfig.environments }
33
+ };
34
+ const allowedPatterns = resolvePatterns(config);
35
+ return {
36
+ name: "env-attr-cleaner",
37
+ enforce: "pre",
38
+ transformInclude(id) {
39
+ return FILE_PATTERN.test(id);
40
+ },
41
+ transform(code) {
42
+ return stripDataAttributes(code, allowedPatterns);
43
+ },
44
+ vite: {
45
+ transformIndexHtml(html) {
46
+ return stripDataAttributes(html, allowedPatterns);
47
+ }
48
+ }
49
+ };
50
+ });
51
+ const vite = envAttrCleanerPlugin.vite;
52
+ const rollup = envAttrCleanerPlugin.rollup;
53
+ const webpack = envAttrCleanerPlugin.webpack;
54
+ const esbuild = envAttrCleanerPlugin.esbuild;
55
+
56
+ export { envAttrCleanerPlugin as default, esbuild, matchPattern, rollup, shouldStrip, stripDataAttributes, vite, webpack };
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "env-attr-cleaner",
3
+ "version": "1.0.0",
4
+ "description": "Universal 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
+ "unplugin",
22
+ "vite",
23
+ "webpack",
24
+ "rollup",
25
+ "esbuild",
26
+ "nuxt",
27
+ "nextjs",
28
+ "data-attributes",
29
+ "testing",
30
+ "e2e",
31
+ "playwright",
32
+ "cypress"
33
+ ],
34
+ "homepage": "https://github.com/techmefr/env-attr-cleaner/tree/main/packages/unplugin#readme",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/techmefr/env-attr-cleaner.git",
38
+ "directory": "packages/unplugin"
39
+ },
40
+ "bugs": {
41
+ "url": "https://github.com/techmefr/env-attr-cleaner/issues"
42
+ },
43
+ "dependencies": {
44
+ "unplugin": "^2.0.0"
45
+ },
46
+ "peerDependencies": {
47
+ "vite": "^4.0.0 || ^5.0.0 || ^6.0.0",
48
+ "webpack": "^4.0.0 || ^5.0.0",
49
+ "next": "^13.0.0 || ^14.0.0 || ^15.0.0"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "vite": {
53
+ "optional": true
54
+ },
55
+ "webpack": {
56
+ "optional": true
57
+ },
58
+ "next": {
59
+ "optional": true
60
+ }
61
+ },
62
+ "devDependencies": {
63
+ "unbuild": "^3.0.0",
64
+ "vite": "^5.4.21",
65
+ "vitest": "^1.6.0",
66
+ "typescript": "^5.3.3"
67
+ },
68
+ "scripts": {
69
+ "build": "unbuild",
70
+ "test": "vitest run",
71
+ "typecheck": "tsc --noEmit"
72
+ }
73
+ }