@artemsemkin/vite-plugin-wp-headers 1.0.0 → 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/README.md +55 -10
- package/dist/index.d.ts +7 -7
- package/dist/index.js +18 -24
- package/package.json +2 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @artemsemkin/vite-plugin-wp-headers
|
|
2
2
|
|
|
3
|
-
Vite plugin that
|
|
3
|
+
Vite plugin that calls a `generate` function on build start and watches specified files for changes during dev. Designed for WordPress header generation but works with any file-generation workflow.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -8,24 +8,69 @@ Vite plugin that generates WordPress file headers from `package.json` data. Wrap
|
|
|
8
8
|
npm install @artemsemkin/vite-plugin-wp-headers -D
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
## Why
|
|
12
|
+
|
|
13
|
+
WordPress themes and plugins need generated header comments in `style.css` and PHP files. These headers should regenerate on every build start and whenever source data (like `package.json`) changes during development. This plugin handles the Vite lifecycle timing — you provide the generation logic.
|
|
14
|
+
|
|
15
|
+
## API
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
wpHeaders({
|
|
19
|
+
generate(): void // called on build start + file changes during dev
|
|
20
|
+
watch?: string[] // absolute paths to watch during dev server
|
|
21
|
+
}): Plugin
|
|
22
|
+
```
|
|
23
|
+
|
|
11
24
|
## Usage
|
|
12
25
|
|
|
26
|
+
### With `@artemsemkin/wp-headers`
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { resolve } from 'node:path'
|
|
30
|
+
import { processMapping } from '@artemsemkin/wp-headers'
|
|
31
|
+
import { wpHeaders } from '@artemsemkin/vite-plugin-wp-headers'
|
|
32
|
+
|
|
33
|
+
const mappings = [
|
|
34
|
+
{
|
|
35
|
+
type: 'theme',
|
|
36
|
+
slug: 'my-theme',
|
|
37
|
+
entityDir: resolve(__dirname, 'themes/my-theme'),
|
|
38
|
+
},
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
export default {
|
|
42
|
+
plugins: [
|
|
43
|
+
wpHeaders({
|
|
44
|
+
generate() {
|
|
45
|
+
for (const m of mappings) {
|
|
46
|
+
processMapping(m)
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
watch: mappings.map((m) => resolve(m.entityDir, 'package.json')),
|
|
50
|
+
}),
|
|
51
|
+
],
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Standalone
|
|
56
|
+
|
|
13
57
|
```ts
|
|
14
|
-
// vite.config.ts
|
|
15
58
|
import { wpHeaders } from '@artemsemkin/vite-plugin-wp-headers'
|
|
16
59
|
|
|
17
60
|
export default {
|
|
18
61
|
plugins: [
|
|
19
|
-
wpHeaders(
|
|
20
|
-
{
|
|
21
|
-
|
|
22
|
-
slug: 'my-theme',
|
|
23
|
-
entityDir: 'themes/my-theme',
|
|
24
|
-
tgmBasePath: 'themes/my-theme/src/php',
|
|
62
|
+
wpHeaders({
|
|
63
|
+
generate() {
|
|
64
|
+
// any file-generation logic
|
|
25
65
|
},
|
|
26
|
-
|
|
66
|
+
watch: ['/absolute/path/to/source.json'],
|
|
67
|
+
}),
|
|
27
68
|
],
|
|
28
69
|
}
|
|
29
70
|
```
|
|
30
71
|
|
|
31
|
-
|
|
72
|
+
## Behavior
|
|
73
|
+
|
|
74
|
+
- **Build**: `generate()` runs once during `configResolved`
|
|
75
|
+
- **Dev server**: `generate()` runs on startup, then again whenever a watched file changes
|
|
76
|
+
- Errors during dev are logged via Vite's logger (won't crash the server)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Plugin } from 'vite';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export declare function wpHeaders(
|
|
2
|
+
export interface WpHeadersOptions {
|
|
3
|
+
/** Called on build start and when watched files change */
|
|
4
|
+
generate(): void;
|
|
5
|
+
/** Absolute paths to watch during dev */
|
|
6
|
+
watch?: string[];
|
|
7
|
+
}
|
|
8
|
+
export declare function wpHeaders(options: WpHeadersOptions): Plugin;
|
package/dist/index.js
CHANGED
|
@@ -1,33 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
import { processMapping } from '@artemsemkin/wp-headers';
|
|
3
|
-
/**
|
|
4
|
-
* Vite plugin that generates WordPress file headers (style.css, plugin PHP)
|
|
5
|
-
* and patches TGM version entries on build and during dev server.
|
|
6
|
-
*/
|
|
7
|
-
export function wpHeaders(mappings) {
|
|
1
|
+
export function wpHeaders(options) {
|
|
8
2
|
return {
|
|
9
3
|
name: 'vite-plugin-wp-headers',
|
|
10
4
|
configResolved() {
|
|
11
|
-
|
|
12
|
-
processMapping(mapping);
|
|
13
|
-
}
|
|
5
|
+
options.generate();
|
|
14
6
|
},
|
|
15
7
|
configureServer(server) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
try {
|
|
24
|
-
processMapping(mapping);
|
|
25
|
-
}
|
|
26
|
-
catch (err) {
|
|
27
|
-
server.config.logger.error(String(err));
|
|
28
|
-
}
|
|
29
|
-
});
|
|
8
|
+
if (!options.watch?.length) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
for (const filePath of options.watch) {
|
|
12
|
+
server.watcher.add(filePath);
|
|
30
13
|
}
|
|
14
|
+
server.watcher.on('change', (changedPath) => {
|
|
15
|
+
if (!options.watch.includes(changedPath)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
options.generate();
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
server.config.logger.error(String(err));
|
|
23
|
+
}
|
|
24
|
+
});
|
|
31
25
|
},
|
|
32
26
|
};
|
|
33
27
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@artemsemkin/vite-plugin-wp-headers",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Vite plugin
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Vite plugin that calls a generate function on build start and watches files during dev",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Artem Semkin",
|
|
7
7
|
"repository": {
|
|
@@ -35,9 +35,6 @@
|
|
|
35
35
|
"build": "tsc -p tsconfig.build.json",
|
|
36
36
|
"prepublishOnly": "pnpm build"
|
|
37
37
|
},
|
|
38
|
-
"dependencies": {
|
|
39
|
-
"@artemsemkin/wp-headers": "^1.1.0"
|
|
40
|
-
},
|
|
41
38
|
"peerDependencies": {
|
|
42
39
|
"vite": ">=5"
|
|
43
40
|
},
|