@kcroyweb/vite-plugin-wp-helper 0.1.2 → 0.1.4

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.
@@ -0,0 +1,17 @@
1
+ import postcss from 'postcss';
2
+ import loadPostcssConfig from 'postcss-load-config';
3
+
4
+ declare const wpHandles: Record<string, string>;
5
+ declare const findBlockJsonFiles: (dir: string, list?: string[]) => string[];
6
+ declare const extractDependencies: (chunk: any, handles: Record<string, string>) => string[];
7
+ declare const generateAssetPhp: (deps: string[]) => string;
8
+ declare function buildCss(input: string, output: string, postcssProcessor: typeof postcss, loadPostcssConfigFn: typeof loadPostcssConfig): Promise<void>;
9
+ declare function wpHelper(options: {
10
+ srcBlockDir: string;
11
+ outDir?: string;
12
+ }): {
13
+ name: string;
14
+ buildStart(): Promise<void>;
15
+ };
16
+
17
+ export { buildCss, extractDependencies, findBlockJsonFiles, generateAssetPhp, wpHandles, wpHelper };
@@ -1,7 +1,17 @@
1
- export default function wpHelper(options: {
1
+ import postcss from 'postcss';
2
+ import loadPostcssConfig from 'postcss-load-config';
3
+
4
+ declare const wpHandles: Record<string, string>;
5
+ declare const findBlockJsonFiles: (dir: string, list?: string[]) => string[];
6
+ declare const extractDependencies: (chunk: any, handles: Record<string, string>) => string[];
7
+ declare const generateAssetPhp: (deps: string[]) => string;
8
+ declare function buildCss(input: string, output: string, postcssProcessor: typeof postcss, loadPostcssConfigFn: typeof loadPostcssConfig): Promise<void>;
9
+ declare function wpHelper(options: {
2
10
  srcBlockDir: string;
3
11
  outDir?: string;
4
12
  }): {
5
13
  name: string;
6
14
  buildStart(): Promise<void>;
7
15
  };
16
+
17
+ export { buildCss, extractDependencies, findBlockJsonFiles, generateAssetPhp, wpHandles, wpHelper };
package/dist/wp-helper.js CHANGED
@@ -30,7 +30,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/wp-helper.ts
31
31
  var wp_helper_exports = {};
32
32
  __export(wp_helper_exports, {
33
- default: () => wpHelper
33
+ buildCss: () => buildCss,
34
+ extractDependencies: () => extractDependencies,
35
+ findBlockJsonFiles: () => findBlockJsonFiles,
36
+ generateAssetPhp: () => generateAssetPhp,
37
+ wpHandles: () => wpHandles,
38
+ wpHelper: () => wpHelper
34
39
  });
35
40
  module.exports = __toCommonJS(wp_helper_exports);
36
41
  var import_node_path = require("path");
@@ -38,65 +43,65 @@ var import_node_fs = require("fs");
38
43
  var import_vite = require("vite");
39
44
  var import_postcss = __toESM(require("postcss"));
40
45
  var import_postcss_load_config = __toESM(require("postcss-load-config"));
41
- function wpHelper(options) {
42
- if (!options.srcBlockDir) {
43
- throw new Error("[vite-plugin-wp-helper] srcBlockDir is required");
46
+ var wpHandles = {
47
+ react: "wp-react",
48
+ "react-dom": "wp-react-dom",
49
+ "@wordpress/blocks": "wp-blocks",
50
+ "@wordpress/element": "wp-element",
51
+ "@wordpress/components": "wp-components",
52
+ "@wordpress/data": "wp-data",
53
+ "@wordpress/block-editor": "wp-block-editor",
54
+ "@wordpress/i18n": "wp-i18n",
55
+ "@wordpress/api-fetch": "wp.apiFetch",
56
+ "@wordpress/compose": "wp-compose",
57
+ "@wordpress/hooks": "wp-hooks"
58
+ };
59
+ var findBlockJsonFiles = (dir, list = []) => {
60
+ if (!(0, import_node_fs.existsSync)(dir)) return list;
61
+ for (const file of (0, import_node_fs.readdirSync)(dir)) {
62
+ const full = (0, import_node_path.join)(dir, file);
63
+ const stat = (0, import_node_fs.statSync)(full);
64
+ if (stat.isDirectory()) findBlockJsonFiles(full, list);
65
+ else if (file === "block.json") list.push(full);
44
66
  }
45
- const wpHandles = {
46
- react: "wp-react",
47
- "react-dom": "wp-react-dom",
48
- "@wordpress/blocks": "wp-blocks",
49
- "@wordpress/element": "wp-element",
50
- "@wordpress/components": "wp-components",
51
- "@wordpress/data": "wp-data",
52
- "@wordpress/block-editor": "wp-block-editor",
53
- "@wordpress/i18n": "wp-i18n",
54
- "@wordpress/api-fetch": "wp-api-fetch",
55
- "@wordpress/compose": "wp-compose",
56
- "@wordpress/hooks": "wp-hooks"
57
- };
58
- const outDir = options.outDir ? (0, import_node_path.resolve)(options.outDir) : (0, import_node_path.resolve)("dist");
59
- const findBlockJsonFiles = (dir, list = []) => {
60
- if (!(0, import_node_fs.existsSync)(dir)) return list;
61
- for (const file of (0, import_node_fs.readdirSync)(dir)) {
62
- const full = (0, import_node_path.join)(dir, file);
63
- const stat = (0, import_node_fs.statSync)(full);
64
- if (stat.isDirectory()) findBlockJsonFiles(full, list);
65
- else if (file === "block.json") list.push(full);
67
+ return list;
68
+ };
69
+ var extractDependencies = (chunk, handles) => {
70
+ const deps = /* @__PURE__ */ new Set();
71
+ const scan = (v) => {
72
+ for (const [pkg, handle] of Object.entries(handles)) {
73
+ if (v.includes(pkg)) deps.add(handle);
66
74
  }
67
- return list;
68
- };
69
- const extractDependencies = (chunk) => {
70
- const deps = /* @__PURE__ */ new Set();
71
- const scan = (v) => {
72
- for (const [pkg, handle] of Object.entries(wpHandles)) {
73
- if (v.includes(pkg)) deps.add(handle);
74
- }
75
- };
76
- if (chunk.imports) chunk.imports.forEach(scan);
77
- if (chunk.code) scan(chunk.code);
78
- if (chunk.modules) Object.keys(chunk.modules).forEach(scan);
79
- return [...deps].sort();
80
75
  };
81
- const generateAssetPhp = (deps) => `<?php
76
+ if (chunk.imports) chunk.imports.forEach(scan);
77
+ if (chunk.code) scan(chunk.code);
78
+ if (chunk.modules) Object.keys(chunk.modules).forEach(scan);
79
+ return [...deps].sort();
80
+ };
81
+ var generateAssetPhp = (deps) => `<?php
82
82
  return array(
83
83
  'dependencies' => array(${deps.map((d) => `'${d}'`).join(", ")}),
84
84
  'version' => '${Date.now()}',
85
85
  );
86
86
  `;
87
- async function buildCss(input, output) {
88
- const css = (0, import_node_fs.readFileSync)(input, "utf8");
89
- const { plugins, options: options2 } = await (0, import_postcss_load_config.default)({}, process.cwd());
90
- const result = await (0, import_postcss.default)(plugins).process(css, {
91
- ...options2,
92
- from: input,
93
- to: output
94
- });
95
- (0, import_node_fs.writeFileSync)(output, result.css);
96
- if (result.map) {
97
- (0, import_node_fs.writeFileSync)(output + ".map", result.map.toString());
98
- }
87
+ async function buildCss(input, output, postcssProcessor, loadPostcssConfigFn) {
88
+ const css = (0, import_node_fs.readFileSync)(input, "utf8");
89
+ const { plugins, options } = await loadPostcssConfigFn({}, process.cwd());
90
+ const result = await postcssProcessor(plugins).process(css, {
91
+ ...options,
92
+ from: input,
93
+ to: output
94
+ });
95
+ (0, import_node_fs.writeFileSync)(output, result.css);
96
+ if (result.map) {
97
+ (0, import_node_fs.writeFileSync)(output + ".map", result.map.toString());
99
98
  }
99
+ }
100
+ function wpHelper(options) {
101
+ if (!options.srcBlockDir) {
102
+ throw new Error("[vite-plugin-wp-helper] srcBlockDir is required");
103
+ }
104
+ const outDir = options.outDir ? (0, import_node_path.resolve)(options.outDir) : (0, import_node_path.resolve)("dist");
100
105
  return {
101
106
  name: "vite-plugin-wp-helper",
102
107
  async buildStart() {
@@ -148,7 +153,7 @@ return array(
148
153
  });
149
154
  const chunk = result.output?.find((c) => c.isEntry);
150
155
  if (chunk) {
151
- const deps = extractDependencies(chunk);
156
+ const deps = extractDependencies(chunk, wpHandles);
152
157
  (0, import_node_fs.writeFileSync)(
153
158
  (0, import_node_path.join)(blockOutDir, outFile.replace(".js", ".asset.php")),
154
159
  generateAssetPhp(deps)
@@ -162,11 +167,11 @@ return array(
162
167
  editorStyle: (0, import_node_path.join)(blockDir, "editor.css")
163
168
  };
164
169
  if ((0, import_node_fs.existsSync)(cssFiles.style)) {
165
- await buildCss(cssFiles.style, (0, import_node_path.join)(blockOutDir, "style.css"));
170
+ await buildCss(cssFiles.style, (0, import_node_path.join)(blockOutDir, "style.css"), import_postcss.default, import_postcss_load_config.default);
166
171
  rawBlockJson.viewStyle = "file:./style.css";
167
172
  }
168
173
  if ((0, import_node_fs.existsSync)(cssFiles.editorStyle)) {
169
- await buildCss(cssFiles.editorStyle, (0, import_node_path.join)(blockOutDir, "editor.css"));
174
+ await buildCss(cssFiles.editorStyle, (0, import_node_path.join)(blockOutDir, "editor.css"), import_postcss.default, import_postcss_load_config.default);
170
175
  rawBlockJson.editorStyle = "file:./editor.css";
171
176
  }
172
177
  (0, import_node_fs.writeFileSync)(
@@ -178,3 +183,12 @@ return array(
178
183
  }
179
184
  };
180
185
  }
186
+ // Annotate the CommonJS export names for ESM import in node:
187
+ 0 && (module.exports = {
188
+ buildCss,
189
+ extractDependencies,
190
+ findBlockJsonFiles,
191
+ generateAssetPhp,
192
+ wpHandles,
193
+ wpHelper
194
+ });
@@ -11,65 +11,65 @@ import {
11
11
  import { build as viteBuild } from "vite";
12
12
  import postcss from "postcss";
13
13
  import loadPostcssConfig from "postcss-load-config";
14
- function wpHelper(options) {
15
- if (!options.srcBlockDir) {
16
- throw new Error("[vite-plugin-wp-helper] srcBlockDir is required");
14
+ var wpHandles = {
15
+ react: "wp-react",
16
+ "react-dom": "wp-react-dom",
17
+ "@wordpress/blocks": "wp-blocks",
18
+ "@wordpress/element": "wp-element",
19
+ "@wordpress/components": "wp-components",
20
+ "@wordpress/data": "wp-data",
21
+ "@wordpress/block-editor": "wp-block-editor",
22
+ "@wordpress/i18n": "wp-i18n",
23
+ "@wordpress/api-fetch": "wp.apiFetch",
24
+ "@wordpress/compose": "wp-compose",
25
+ "@wordpress/hooks": "wp-hooks"
26
+ };
27
+ var findBlockJsonFiles = (dir, list = []) => {
28
+ if (!existsSync(dir)) return list;
29
+ for (const file of readdirSync(dir)) {
30
+ const full = join(dir, file);
31
+ const stat = statSync(full);
32
+ if (stat.isDirectory()) findBlockJsonFiles(full, list);
33
+ else if (file === "block.json") list.push(full);
17
34
  }
18
- const wpHandles = {
19
- react: "wp-react",
20
- "react-dom": "wp-react-dom",
21
- "@wordpress/blocks": "wp-blocks",
22
- "@wordpress/element": "wp-element",
23
- "@wordpress/components": "wp-components",
24
- "@wordpress/data": "wp-data",
25
- "@wordpress/block-editor": "wp-block-editor",
26
- "@wordpress/i18n": "wp-i18n",
27
- "@wordpress/api-fetch": "wp-api-fetch",
28
- "@wordpress/compose": "wp-compose",
29
- "@wordpress/hooks": "wp-hooks"
30
- };
31
- const outDir = options.outDir ? resolve(options.outDir) : resolve("dist");
32
- const findBlockJsonFiles = (dir, list = []) => {
33
- if (!existsSync(dir)) return list;
34
- for (const file of readdirSync(dir)) {
35
- const full = join(dir, file);
36
- const stat = statSync(full);
37
- if (stat.isDirectory()) findBlockJsonFiles(full, list);
38
- else if (file === "block.json") list.push(full);
35
+ return list;
36
+ };
37
+ var extractDependencies = (chunk, handles) => {
38
+ const deps = /* @__PURE__ */ new Set();
39
+ const scan = (v) => {
40
+ for (const [pkg, handle] of Object.entries(handles)) {
41
+ if (v.includes(pkg)) deps.add(handle);
39
42
  }
40
- return list;
41
- };
42
- const extractDependencies = (chunk) => {
43
- const deps = /* @__PURE__ */ new Set();
44
- const scan = (v) => {
45
- for (const [pkg, handle] of Object.entries(wpHandles)) {
46
- if (v.includes(pkg)) deps.add(handle);
47
- }
48
- };
49
- if (chunk.imports) chunk.imports.forEach(scan);
50
- if (chunk.code) scan(chunk.code);
51
- if (chunk.modules) Object.keys(chunk.modules).forEach(scan);
52
- return [...deps].sort();
53
43
  };
54
- const generateAssetPhp = (deps) => `<?php
44
+ if (chunk.imports) chunk.imports.forEach(scan);
45
+ if (chunk.code) scan(chunk.code);
46
+ if (chunk.modules) Object.keys(chunk.modules).forEach(scan);
47
+ return [...deps].sort();
48
+ };
49
+ var generateAssetPhp = (deps) => `<?php
55
50
  return array(
56
51
  'dependencies' => array(${deps.map((d) => `'${d}'`).join(", ")}),
57
52
  'version' => '${Date.now()}',
58
53
  );
59
54
  `;
60
- async function buildCss(input, output) {
61
- const css = readFileSync(input, "utf8");
62
- const { plugins, options: options2 } = await loadPostcssConfig({}, process.cwd());
63
- const result = await postcss(plugins).process(css, {
64
- ...options2,
65
- from: input,
66
- to: output
67
- });
68
- writeFileSync(output, result.css);
69
- if (result.map) {
70
- writeFileSync(output + ".map", result.map.toString());
71
- }
55
+ async function buildCss(input, output, postcssProcessor, loadPostcssConfigFn) {
56
+ const css = readFileSync(input, "utf8");
57
+ const { plugins, options } = await loadPostcssConfigFn({}, process.cwd());
58
+ const result = await postcssProcessor(plugins).process(css, {
59
+ ...options,
60
+ from: input,
61
+ to: output
62
+ });
63
+ writeFileSync(output, result.css);
64
+ if (result.map) {
65
+ writeFileSync(output + ".map", result.map.toString());
66
+ }
67
+ }
68
+ function wpHelper(options) {
69
+ if (!options.srcBlockDir) {
70
+ throw new Error("[vite-plugin-wp-helper] srcBlockDir is required");
72
71
  }
72
+ const outDir = options.outDir ? resolve(options.outDir) : resolve("dist");
73
73
  return {
74
74
  name: "vite-plugin-wp-helper",
75
75
  async buildStart() {
@@ -121,7 +121,7 @@ return array(
121
121
  });
122
122
  const chunk = result.output?.find((c) => c.isEntry);
123
123
  if (chunk) {
124
- const deps = extractDependencies(chunk);
124
+ const deps = extractDependencies(chunk, wpHandles);
125
125
  writeFileSync(
126
126
  join(blockOutDir, outFile.replace(".js", ".asset.php")),
127
127
  generateAssetPhp(deps)
@@ -135,11 +135,11 @@ return array(
135
135
  editorStyle: join(blockDir, "editor.css")
136
136
  };
137
137
  if (existsSync(cssFiles.style)) {
138
- await buildCss(cssFiles.style, join(blockOutDir, "style.css"));
138
+ await buildCss(cssFiles.style, join(blockOutDir, "style.css"), postcss, loadPostcssConfig);
139
139
  rawBlockJson.viewStyle = "file:./style.css";
140
140
  }
141
141
  if (existsSync(cssFiles.editorStyle)) {
142
- await buildCss(cssFiles.editorStyle, join(blockOutDir, "editor.css"));
142
+ await buildCss(cssFiles.editorStyle, join(blockOutDir, "editor.css"), postcss, loadPostcssConfig);
143
143
  rawBlockJson.editorStyle = "file:./editor.css";
144
144
  }
145
145
  writeFileSync(
@@ -152,5 +152,10 @@ return array(
152
152
  };
153
153
  }
154
154
  export {
155
- wpHelper as default
155
+ buildCss,
156
+ extractDependencies,
157
+ findBlockJsonFiles,
158
+ generateAssetPhp,
159
+ wpHandles,
160
+ wpHelper
156
161
  };
package/package.json CHANGED
@@ -1,15 +1,23 @@
1
1
  {
2
2
  "name": "@kcroyweb/vite-plugin-wp-helper",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Vite plugin to make WordPress block development possible with a regular JS/TS setup within vite.",
5
5
  "main": "dist/wp-helper.js",
6
+ "module": "dist/wp-helper.mjs",
6
7
  "types": "dist/wp-helper.d.ts",
8
+ "readmeFilename": "README.md",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/wp-helper.mjs",
12
+ "require": "./dist/wp-helper.js"
13
+ }
14
+ },
7
15
  "files": [
8
16
  "dist",
9
17
  "README.md"
10
18
  ],
11
19
  "scripts": {
12
- "build": "tsup src/wp-helper.ts --format cjs,esm --external postcss --external postcss-load-config --external vite && tsc --emitDeclarationOnly --declaration --declarationDir dist --project tsconfig.json",
20
+ "build": "tsup --config tsup.config.ts && rm -f dist/vitest.config.d.mts",
13
21
  "test": "vitest",
14
22
  "coverage": "vitest run --coverage"
15
23
  },
@@ -35,7 +43,7 @@
35
43
  "node": ">=18"
36
44
  },
37
45
  "peerDependencies": {
38
- "vite": "^5.0.0"
46
+ "vite": "^7.0.0"
39
47
  },
40
48
  "packageManager": "pnpm@10.28.1",
41
49
  "devDependencies": {