@castlenine/vite-remove-attribute 1.0.2 → 2.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/CHANGELOG.md ADDED
@@ -0,0 +1,36 @@
1
+ # Changelog
2
+
3
+ ## 2.0.0
4
+
5
+ ### Fixed
6
+
7
+ - Ignore tokens were matched as substrings of the **absolute** module id, so a checkout path containing `build`, `dist`,
8
+ `public`, `logs`, `e2e`, `.cache` or `.env` (Cloudflare Workers clones into `/opt/buildhome/repo`) disabled the plugin
9
+ entirely. Tokens are now matched on path-segment boundaries against the path relative to the Vite root: `build`
10
+ matches `build/app.js` but no longer `buildhome/app.js`. Multi-segment tokens such as `src/tests` and
11
+ `src/components/Modal.svelte` keep working.
12
+ - The expression form `attribute={…}` was never removed. Brace-balanced values, template literals and nested `${…}`
13
+ placeholders are now handled; an unbalanced expression is left untouched.
14
+ - A bare attribute closing a self-closing tag (`<input data-testid />`) was not removed.
15
+ - The `?query` suffix of a Vite module id is stripped before extension and ignore matching.
16
+ - Attribute names and extensions are regex-escaped.
17
+ - `transform` returns `null` when a module is skipped or unchanged instead of echoing the source back.
18
+
19
+ ### Added
20
+
21
+ - Every transformed module now carries a sourcemap (generated in-house, the package still has no runtime dependency), so
22
+ the consumer's sourcemaps keep pointing at the original lines and columns.
23
+ - `ignoreDefaults` option (default `true`) to opt out of the built-in ignore list.
24
+ - `*` in an ignore token matches within a single path segment (`*.log`, `.env.*`, `*.stories.svelte`).
25
+ - The `Options` type is exported.
26
+ - `engines.node` is declared as `>=18.0.0`. The bundle targets ES2022, which Node 18 runs natively, so the plugin works
27
+ with every Vite version from 2 to 8 on any Node release those versions support from 18 upwards.
28
+ - `CHANGELOG.md` ships in the npm tarball.
29
+
30
+ ### Changed
31
+
32
+ - The package has been renamed from `@castlenine/vite-remove-attribute` to `@castlenine/vite-plugin-remove-attribute` to
33
+ align with the Vite plugin naming convention. Uninstall the old name, install the new one and update the import
34
+ specifier; the default export, the options and the behaviour documented above are otherwise the same.
35
+ - The CommonJS entry moved from `dist/index.umd.cjs` to `dist/index.cjs`. `require()` still returns the plugin function
36
+ and `.default` is also available; the entry is typed by `dist/index.d.cts`.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  # MIT License
2
2
 
3
- Copyright (c) 2024 Alexandre "Castlenine"
3
+ Copyright (c) 2024–2026, Alex "Castlenine"
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,146 +1,259 @@
1
1
  <div align="center">
2
2
 
3
- # `@castlenine/vite-remove-attribute`
3
+ # `@castlenine/vite-plugin-remove-attribute`
4
4
 
5
5
  [![npm.badge]][npm] [![download.badge]][download]
6
6
 
7
- Vite plugin that allows the removal of specified attributes and supports a variety of options, including file extensions, attributes, ignored folders, and files.
7
+ Vite plugin that allows the removal of specified attributes and supports a variety of options, including file
8
+ extensions, attributes, ignored folders, and files.
8
9
  </div>
9
10
 
11
+ # ⚠️ DEPRECATED ⚠️
12
+
13
+ **This package (`@castlenine/vite-remove-attribute`) has been renamed to
14
+ [@castlenine/vite-plugin-remove-attribute](https://www.npmjs.com/package/@castlenine/vite-plugin-remove-attribute) to
15
+ follow Vite plugin naming conventions. Please use the new package. This package is now deprecated and will be removed in
16
+ December 2027.**
17
+
10
18
  ## Table of Contents
11
19
 
12
20
  - [Disclaimer](#disclaimer)
13
21
  - [Features](#features)
22
+ - [Requirements](#requirements)
14
23
  - [Installation](#installation)
15
24
  - [Usage](#usage)
16
25
  - [Prerequisites](#prerequisites)
17
26
  - [Notes](#notes)
18
- - [Examples](#examples)
27
+ - [Options](#options)
28
+ - [Ignore matching](#ignore-matching)
29
+ - [Sourcemaps](#sourcemaps)
30
+ - [Examples](#examples)
31
+ - [SvelteKit](#sveltekit-example-1-removing-data-testid-attributes-from-svelte-files)
32
+ - [Vue.js](#vuejs-example-1-removing-data-testid-attributes-from-vue-files)
33
+ - [Opting out of the built-in ignore list](#opting-out-of-the-built-in-ignore-list)
34
+ - [CommonJS](#commonjs)
35
+ - [Changelog](#changelog)
36
+ - [Acknowledgement](#acknowledgement)
37
+ - [License](#license)
19
38
 
20
39
  ## Disclaimer
21
40
 
22
- **Only tested with Svelte, SvelteKit and Vue.js projects**. Please open an issue if you encounter any problems with other frameworks.
41
+ **Only tested with Svelte, SvelteKit and Vue.js projects**. Please open an issue if you encounter any problems with
42
+ other frameworks.
23
43
 
24
44
  ## Features
25
45
 
26
- - Removes specified attributes
46
+ - Removes specified attributes in their quoted, expression (`={…}`) and bare forms
27
47
  - Allows you to specify the file extensions to be processed
28
48
  - Can ignore certain folders or files based on configuration
29
- - Ensures clean production code by removing unnecessary attributes, like 'data-testid' used in testing
49
+ - Emits a sourcemap for every transformed file, so stack traces and debuggers keep pointing at the original code
50
+ - Ensures clean production code by removing unnecessary attributes, like `data-testid` used in testing
51
+
52
+ ## Requirements
53
+
54
+ | Requirement | Version |
55
+ | ----------- | --------------------------- |
56
+ | Node.js | `>=18.0.0` |
57
+ | Vite | `>=2.0.0` (peer dependency) |
30
58
 
31
59
  ## Installation
32
60
 
33
- Use your package manager to install:
61
+ Use your package manager to install it as a development dependency:
34
62
 
35
63
  ```shell
36
- npm i --save-dev @castlenine/vite-remove-attribute
64
+ pnpm add -D @castlenine/vite-plugin-remove-attribute
65
+ # or
66
+ npm i -D @castlenine/vite-plugin-remove-attribute
67
+ # or
68
+ yarn add -D @castlenine/vite-plugin-remove-attribute
37
69
  ```
38
70
 
39
71
  ## Usage
40
72
 
41
73
  ### Prerequisites
42
74
 
43
- To use this plugin, you must have a Vite config file set up in your project. If you don't have one, create a `vite.config.js` or `vite.config.ts` file in the root of your project.
75
+ To use this plugin, you must have a Vite config file set up in your project. If you don't have one, create a
76
+ `vite.config.js` or `vite.config.ts` file in the root of your project.
44
77
 
45
78
  ### Notes
46
79
 
47
- For some frameworks, like Svelte & SvelteKit, this plugin should be placed first (before the framework's plugin) in the `plugins` array and for others, like Vue.js, it should be placed after the framework's plugin.
80
+ For some frameworks, like Svelte & SvelteKit, this plugin should be placed first (before the framework's plugin) in the
81
+ `plugins` array and for others, like Vue.js, it should be placed after the framework's plugin.
82
+
83
+ ### Options
84
+
85
+ | Option | Type | Default | Description |
86
+ | ---------------- | ---------- | ------- | ------------------------------------------------------------------------------------------- |
87
+ | `extensions` | `string[]` | — | File extensions to process, without the leading dot (e.g. `['svelte', 'vue']`) |
88
+ | `attributes` | `string[]` | — | Attribute names to remove (e.g. `['data-testid']`) |
89
+ | `ignoreFolders` | `string[]` | `[]` | Folders to skip, relative to the Vite root (e.g. `['src/tests']`) |
90
+ | `ignoreFiles` | `string[]` | `[]` | Files to skip, relative to the Vite root (e.g. `['Header.svelte', 'src/lib/Modal.svelte']`) |
91
+ | `ignoreDefaults` | `boolean` | `true` | Apply the built-in ignore list on top of `ignoreFolders` / `ignoreFiles` |
92
+
93
+ The attribute is removed in its quoted form (`data-testid="a"`), its expression form (`data-testid={value}`, including
94
+ template literals with nested `${…}`), its bare form (`<input data-testid />`) and with a Vue binding prefix
95
+ (`:data-testid`, `v-bind:data-testid`). Matching is case-insensitive on the attribute name, and longer names such as
96
+ `data-testid-extra` are left untouched.
97
+
98
+ ### Ignore matching
99
+
100
+ Ignore tokens are matched against the module path **relative to the Vite root**, on path-segment boundaries: `build`
101
+ matches `build/app.js` but not `buildhome/app.js`, and `src/tests` matches `src/tests/a.svelte` but not
102
+ `src/tests-e2e/a.svelte`. A `*` matches any characters within a single segment (`*.stories.svelte`, `.env.*`).
103
+
104
+ The built-in ignore list covers `node_modules`, `.git`, `.idea`, `.vscode`, `.DS_Store`, `Thumbs.db`, `.env`, `.env.*`,
105
+ `logs`, `*.log`, `public`, `build`, `.svelte-kit`, `dist`, `.nuxt`, `.next`, `.remix`, `e2e`, `angular.json`,
106
+ `browserslist` and `.cache`. Set `ignoreDefaults: false` to keep only your own tokens.
107
+
108
+ ### Sourcemaps
109
+
110
+ Every transformed file returns a sourcemap (generated without any runtime dependency), so a `build.sourcemap` setting in
111
+ your Vite config keeps mapping the generated code to the original lines and columns even when an attribute sat on its
112
+ own line.
48
113
 
49
114
  ## Examples
50
115
 
51
116
  ### SvelteKit example 1: Removing 'data-testid' attributes from `.svelte` files
52
117
 
53
- This configuration will remove `data-testid` attributes from all `.svelte` files in the production build.
118
+ This configuration will remove `data-testid` attributes from all `.svelte` files in the production build only.
54
119
 
55
120
  ```typescript
56
121
  import { defineConfig } from 'vite';
57
122
  import { sveltekit } from '@sveltejs/kit/vite';
58
123
 
59
- import removeAttribute from '@castlenine/vite-remove-attribute';
124
+ import removeAttribute from '@castlenine/vite-plugin-remove-attribute';
60
125
 
61
- const IS_PRODUCTION = process.env.NODE_ENV == 'production';
126
+ export default defineConfig(({ mode }) => ({
127
+ plugins: [
128
+ mode === 'production'
129
+ ? removeAttribute({
130
+ extensions: ['svelte'],
131
+ attributes: ['data-testid'],
132
+ })
133
+ : null,
62
134
 
63
- export default defineConfig({
64
- plugins: [
65
- IS_PRODUCTION
66
- ? removeAttribute({
67
- extensions: ['svelte'],
68
- attributes: ['data-testid'],
69
- })
70
- : null,
71
-
72
- sveltekit(), // SvelteKit plugin should be placed after removeAttribute
73
- ],
74
- });
135
+ sveltekit(), // SvelteKit plugin should be placed after removeAttribute
136
+ ],
137
+ }));
75
138
  ```
76
139
 
77
140
  ### SvelteKit example 2: Ignoring specific folders and files
78
141
 
79
- This configuration will remove `data-testid` and `data-id` attributes from all `.svelte`, `.ts`, and `.js` files, with the exception of those located in the `src/tests` and `src/utilities` folders, as well as the `Header.svelte`, `src/components/Modal.svelte`, and `src/layouts/LayoutAuth.svelte` files in all builds.
142
+ This configuration will remove `data-testid` and `data-id` attributes from all `.svelte`, `.ts`, and `.js` files, with
143
+ the exception of those located in the `src/tests` and `src/utilities` folders, as well as the `Header.svelte`,
144
+ `src/components/Modal.svelte`, and `src/layouts/LayoutAuth.svelte` files in all builds.
80
145
 
81
146
  ```typescript
82
147
  import { defineConfig } from 'vite';
83
148
  import { sveltekit } from '@sveltejs/kit/vite';
84
149
 
85
- import removeAttribute from '@castlenine/vite-remove-attribute';
150
+ import removeAttribute from '@castlenine/vite-plugin-remove-attribute';
86
151
 
87
152
  export default defineConfig({
88
- plugins: [
89
- removeAttribute({
90
- extensions: ['svelte', 'ts', 'js'],
91
- attributes: ['data-testid', 'data-id'],
92
- ignoreFolders: ['src/tests', 'src/utilities'],
93
- ignoreFiles: ['Header.svelte', 'src/components/Modal.svelte', 'src/layouts/LayoutAuth.svelte'],
94
- }),
95
-
96
- sveltekit(), // SvelteKit plugin should be placed after removeAttribute
97
- ],
153
+ plugins: [
154
+ removeAttribute({
155
+ extensions: ['svelte', 'ts', 'js'],
156
+ attributes: ['data-testid', 'data-id'],
157
+ ignoreFolders: ['src/tests', 'src/utilities'],
158
+ ignoreFiles: ['Header.svelte', 'src/components/Modal.svelte', 'src/layouts/LayoutAuth.svelte'],
159
+ }),
160
+
161
+ sveltekit(), // SvelteKit plugin should be placed after removeAttribute
162
+ ],
98
163
  });
99
164
  ```
100
165
 
101
- ### Vue.js example 1: Removing 'data-testid' attributes from '.vue' files
166
+ ### Vue.js example 1: Removing 'data-testid' attributes from `.vue` files
102
167
 
103
- This configuration will remove 'data-testid' attributes from all '.vue' files in the production build.
168
+ This configuration will remove `data-testid` attributes from all `.vue` files in the production build only.
104
169
 
105
170
  ```typescript
106
- const IS_PRODUCTION = process.env.NODE_ENV == 'production';
171
+ import { defineConfig } from 'vite';
172
+ import vue from '@vitejs/plugin-vue';
173
+
174
+ import removeAttribute from '@castlenine/vite-plugin-remove-attribute';
175
+
176
+ export default defineConfig(({ mode }) => ({
177
+ plugins: [
178
+ vue(), // Vue plugin should be placed before removeAttribute
179
+ mode === 'production'
180
+ ? removeAttribute({
181
+ extensions: ['vue'],
182
+ attributes: ['data-testid'],
183
+ })
184
+ : null,
185
+ ],
186
+ }));
187
+ ```
188
+
189
+ ### Vue.js example 2: Ignoring specific folders and files
190
+
191
+ This configuration will remove `data-testid` and `data-id` attributes from all `.vue`, `.ts`, and `.js` files, with the
192
+ exception of those located in the `src/tests` and `src/utilities` folders, as well as the `Header.vue`,
193
+ `src/components/Modal.vue`, and `src/layouts/LayoutAuth.vue` files in all builds.
194
+
195
+ ```typescript
196
+ import { defineConfig } from 'vite';
197
+ import vue from '@vitejs/plugin-vue';
198
+
199
+ import removeAttribute from '@castlenine/vite-plugin-remove-attribute';
107
200
 
108
201
  export default defineConfig({
109
- plugins: [
110
- vue(), // Vue plugin should be placed before removeAttribute
111
- IS_PRODUCTION
112
- ? removeAttribute({
113
- extensions: ['vue'],
114
- attributes: ['data-testid'],
115
- })
116
- : null,
117
- ]
118
- })
202
+ plugins: [
203
+ vue(), // Vue plugin should be placed before removeAttribute
204
+ removeAttribute({
205
+ extensions: ['vue', 'ts', 'js'],
206
+ attributes: ['data-testid', 'data-id'],
207
+ ignoreFolders: ['src/tests', 'src/utilities'],
208
+ ignoreFiles: ['Header.vue', 'src/components/Modal.vue', 'src/layouts/LayoutAuth.vue'],
209
+ }),
210
+ ],
211
+ });
119
212
  ```
120
213
 
121
- #### Vue.js example 2: Ignoring specific folders and files
214
+ ### Opting out of the built-in ignore list
122
215
 
123
- This configuration will remove `data-testid` and `data-id` attributes from all `.vue`, `.ts`, and `.js` files, with the exception of those located in the `src/tests` and `src/utilities` folders, as well as the `Header.vue`, `src/components/Modal.vue`, and `src/layouts/LayoutAuth.vue` files in all builds.
216
+ With `ignoreDefaults: false` only your own `ignoreFolders` / `ignoreFiles` tokens apply, so files under `public/` or
217
+ `build/` are processed too.
124
218
 
125
219
  ```typescript
126
- export default defineConfig({
127
- plugins: [
128
- vue(), // Vue plugin should be placed before removeAttribute
129
- removeAttr({
130
- extensions: [ 'vue', "ts", "js" ],
131
- attributes: [ 'data-testid', "data-id" ],
132
- ignoreFolders: [ 'src/tests', "src/utilities" ],
133
- ignoreFiles: [ 'Header.vue', 'src/components/Modal.vue', "src/layouts/LayoutAuth.vue" ]
134
- })
135
- ]
136
- })
220
+ removeAttribute({
221
+ extensions: ['svelte'],
222
+ attributes: ['data-testid'],
223
+ ignoreDefaults: false,
224
+ ignoreFolders: ['node_modules'],
225
+ });
137
226
  ```
138
227
 
139
- <br />
228
+ ### CommonJS
229
+
230
+ The package also ships a CommonJS entry. `require()` returns the plugin function directly (`.default` is available as
231
+ well):
232
+
233
+ ```javascript
234
+ // vite.config.cjs
235
+ const { defineConfig } = require('vite');
236
+ const removeAttribute = require('@castlenine/vite-plugin-remove-attribute');
237
+
238
+ module.exports = defineConfig({
239
+ plugins: [removeAttribute({ extensions: ['svelte'], attributes: ['data-testid'] })],
240
+ });
241
+ ```
242
+
243
+ ## Changelog
244
+
245
+ See [CHANGELOG.md](./CHANGELOG.md).
246
+
247
+ ## Acknowledgement
248
+
249
+ This project is a fork of [mustafadalga/remove-attr](https://github.com/mustafadalga/remove-attr). See
250
+ [ACKNOWLEDGEMENT.md](./ACKNOWLEDGEMENT.md).
251
+
252
+ ## License
140
253
 
141
- Forked from [mustafadalga/remove-attr](https://github.com/mustafadalga/remove-attr)
254
+ [MIT](./LICENSE)
142
255
 
143
- [npm]: https://www.npmjs.com/package/@castlenine/vite-remove-attribute
144
- [npm.badge]: https://img.shields.io/npm/v/@castlenine/vite-remove-attribute
145
- [download]: https://www.npmjs.com/package/@castlenine/vite-remove-attribute
146
- [download.badge]: https://img.shields.io/npm/d18m/@castlenine/vite-remove-attribute
256
+ [npm]: https://www.npmjs.com/package/@castlenine/vite-plugin-remove-attribute
257
+ [npm.badge]: https://img.shields.io/npm/v/@castlenine/vite-plugin-remove-attribute
258
+ [download]: https://www.npmjs.com/package/@castlenine/vite-plugin-remove-attribute
259
+ [download.badge]: https://img.shields.io/npm/d18m/@castlenine/vite-plugin-remove-attribute