@castlenine/vite-remove-attribute 1.0.1 → 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,98 +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
 
20
+ - [Disclaimer](#disclaimer)
12
21
  - [Features](#features)
22
+ - [Requirements](#requirements)
13
23
  - [Installation](#installation)
14
24
  - [Usage](#usage)
15
25
  - [Prerequisites](#prerequisites)
16
- - [Examples](#examples)
26
+ - [Notes](#notes)
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)
38
+
39
+ ## Disclaimer
40
+
41
+ **Only tested with Svelte, SvelteKit and Vue.js projects**. Please open an issue if you encounter any problems with
42
+ other frameworks.
17
43
 
18
44
  ## Features
19
45
 
20
- - Removes specified attributes
46
+ - Removes specified attributes in their quoted, expression (`={…}`) and bare forms
21
47
  - Allows you to specify the file extensions to be processed
22
48
  - Can ignore certain folders or files based on configuration
23
- - 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) |
24
58
 
25
59
  ## Installation
26
60
 
27
- Use your package manager to install:
61
+ Use your package manager to install it as a development dependency:
28
62
 
29
63
  ```shell
30
- 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
31
69
  ```
32
70
 
33
71
  ## Usage
34
72
 
35
73
  ### Prerequisites
36
74
 
37
- To use this plugin, you need to have a Vite project set up. Import and use the plugin in your `vite.config.js` or
38
- `vite.config.ts` file.
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.
77
+
78
+ ### Notes
79
+
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.
39
113
 
40
114
  ## Examples
41
115
 
42
- ### Example 1: Removing 'data-testid' attributes from `.svelte` files
116
+ ### SvelteKit example 1: Removing 'data-testid' attributes from `.svelte` files
43
117
 
44
- 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.
45
119
 
46
120
  ```typescript
47
121
  import { defineConfig } from 'vite';
48
122
  import { sveltekit } from '@sveltejs/kit/vite';
49
123
 
50
- import removeAttribute from '@castlenine/vite-remove-attribute';
124
+ import removeAttribute from '@castlenine/vite-plugin-remove-attribute';
125
+
126
+ export default defineConfig(({ mode }) => ({
127
+ plugins: [
128
+ mode === 'production'
129
+ ? removeAttribute({
130
+ extensions: ['svelte'],
131
+ attributes: ['data-testid'],
132
+ })
133
+ : null,
134
+
135
+ sveltekit(), // SvelteKit plugin should be placed after removeAttribute
136
+ ],
137
+ }));
138
+ ```
139
+
140
+ ### SvelteKit example 2: Ignoring specific folders and files
51
141
 
52
- const IS_PRODUCTION = process.env.NODE_ENV == 'production';
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.
145
+
146
+ ```typescript
147
+ import { defineConfig } from 'vite';
148
+ import { sveltekit } from '@sveltejs/kit/vite';
149
+
150
+ import removeAttribute from '@castlenine/vite-plugin-remove-attribute';
53
151
 
54
152
  export default defineConfig({
55
- plugins: [
56
- sveltekit(),
57
-
58
- IS_PRODUCTION
59
- ? removeAttribute({
60
- extensions: ['svelte'],
61
- attributes: ['data-testid'],
62
- })
63
- : null,
64
- ],
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
+ ],
65
163
  });
66
164
  ```
67
165
 
68
- ### Example 2: Ignoring specific folders and files
166
+ ### Vue.js example 1: Removing 'data-testid' attributes from `.vue` files
69
167
 
70
- 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.
168
+ This configuration will remove `data-testid` attributes from all `.vue` files in the production build only.
71
169
 
72
170
  ```typescript
73
171
  import { defineConfig } from 'vite';
74
- import { sveltekit } from '@sveltejs/kit/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
+ ```
75
188
 
76
- import removeAttribute from '@castlenine/vite-remove-attribute';
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';
77
200
 
78
201
  export default defineConfig({
79
- plugins: [
80
- sveltekit(),
81
- removeAttribute({
82
- extensions: ['svelte', 'ts', 'js'],
83
- attributes: ['data-testid', 'data-id'],
84
- ignoreFolders: ['src/tests', 'src/utilities'],
85
- ignoreFiles: ['Header.svelte', 'src/components/Modal.svelte', 'src/layouts/LayoutAuth.svelte'],
86
- }),
87
- ],
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
+ ],
88
211
  });
89
212
  ```
90
213
 
91
- <br />
214
+ ### Opting out of the built-in ignore list
215
+
216
+ With `ignoreDefaults: false` only your own `ignoreFolders` / `ignoreFiles` tokens apply, so files under `public/` or
217
+ `build/` are processed too.
218
+
219
+ ```typescript
220
+ removeAttribute({
221
+ extensions: ['svelte'],
222
+ attributes: ['data-testid'],
223
+ ignoreDefaults: false,
224
+ ignoreFolders: ['node_modules'],
225
+ });
226
+ ```
227
+
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
92
253
 
93
- Forked from [mustafadalga/remove-attr](https://github.com/mustafadalga/remove-attr)
254
+ [MIT](./LICENSE)
94
255
 
95
- [npm]: https://www.npmjs.com/package/@castlenine/vite-remove-attribute
96
- [npm.badge]: https://img.shields.io/npm/v/@castlenine/vite-remove-attribute
97
- [download]: https://www.npmjs.com/package/@castlenine/vite-remove-attribute
98
- [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