sdc-build-wp 4.0.2 → 4.1.1

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/eslint.config.js CHANGED
@@ -2,7 +2,7 @@ export default [
2
2
  {
3
3
  languageOptions: {
4
4
  parserOptions: {
5
- ecmaVersion: 6,
5
+ ecmaVersion: "latest",
6
6
  sourceType: "module",
7
7
  ecmaFeatures: {
8
8
  jsx: true,
package/index.js CHANGED
@@ -4,12 +4,11 @@ import project from './lib/project.js';
4
4
  import parseArgs from 'minimist';
5
5
  const argv = parseArgs(process.argv.slice(2));
6
6
  import chokidar from 'chokidar';
7
- import { glob, globSync } from 'glob';
7
+ import { glob, readdir } from 'node:fs/promises';
8
8
  import { existsSync } from 'node:fs';
9
9
  import { Tail } from 'tail';
10
10
 
11
11
  import log from './lib/logging.js';
12
- import bustCache from './lib/bustCache.js';
13
12
  import { buildSass, buildSassTheme } from './lib/style.js';
14
13
  import buildJS from './lib/scripts.js';
15
14
  import { default as buildPHP, shouldPHPLint } from './lib/php.js';
@@ -18,149 +17,244 @@ import buildImages from './lib/images.js';
18
17
  import buildFonts from './lib/fonts.js';
19
18
  import buildBrowserSync from './lib/browsersync.js';
20
19
 
20
+ let paths = {
21
+ theme: {
22
+ json: `${project.path}/theme.json`,
23
+ scss: `${project.path}/_src/style/partials/_theme.scss`
24
+ },
25
+ nodeModules: `${project.path}/node_modules`,
26
+ composer: {
27
+ vendor: `${project.path}/vendor`
28
+ },
29
+ images: project.package?.sdc?.imagesPath || `${project.path}/_src/images`,
30
+ errorLog: process.env.ERROR_LOG_PATH || project.package.sdc?.error_log_path || '../../../../../logs/php/error.log'
31
+ };
32
+
21
33
  let chokidarOpts = {
22
34
  ignoreInitial: true,
23
35
  ignored: [
24
- project.path + '/node_modules',
25
- project.path + '/vendor',
26
- project.path + '/blocks/*/build/**/*'
36
+ paths.nodeModules,
37
+ paths.composer.vendor,
38
+ paths.theme.scss
27
39
  ]
28
40
  };
29
41
 
30
- let phpGlobPath = project.package?.sdc?.phpGlobPath || project.path + '/**/*.php';
31
- let phpGlob = globSync(phpGlobPath, {
32
- ignore: [
33
- project.path + '/node_modules',
34
- project.path + '/vendor'
35
- ]
36
- });
37
- let sassGlobPath = project.package?.sdc?.sassGlobPath || project.path + '{/_src/style,/blocks}/**/*.scss';
38
- let sassGlob = globSync(sassGlobPath, {
39
- ignore: [
40
- project.path + '/_src/style/partials/_theme.scss'
41
- ]
42
- });
43
- let jsGlobPath = project.package?.sdc?.jsGlobPath || project.path + '/_src/scripts/**/*.js';
44
- let jsGlob = globSync(jsGlobPath, {
45
- ignore: []
46
- });
47
- let blockGlobPath = project.package?.sdc?.blockGlobPath || project.path + '/blocks/*';
48
- let blockGlob = globSync(blockGlobPath);
49
-
50
- function bustFunctionsCache() {
51
- bustCache(project.path + '/functions.php');
52
- }
42
+ let globs = {};
43
+ let entries = {};
44
+ let filesSass = [];
45
+ let filesJS = [];
46
+
47
+ let builds = argv.builds ? argv.builds.split(',') : [
48
+ 'sass',
49
+ 'js',
50
+ 'blocks',
51
+ 'images',
52
+ 'fonts',
53
+ 'php'
54
+ ];
53
55
 
54
- function frontrunImages() {
55
- [
56
- project.path + '/_src/images/',
57
- project.path + '/_src/images/**/*/'
58
- ].forEach((block) => {
59
- const imageDirectories = globSync(block);
60
- imageDirectories.forEach((dir) => {
61
- buildImages(dir);
56
+ (async() => {
57
+
58
+ if (builds.includes('sass')) {
59
+ globs.sass = await Array.fromAsync(
60
+ glob(project.package?.sdc?.sassGlobPath ||
61
+ `${project.path}{/_src/style,/blocks}/**/*.scss`)
62
+ );
63
+ }
64
+ if (builds.includes('js')) {
65
+ globs.js = await Array.fromAsync(
66
+ glob(project.package?.sdc?.jsGlobPath ||
67
+ `${project.path}/_src/scripts/**/*.js`)
68
+ );
69
+ }
70
+ if (builds.includes('blocks')) {
71
+ globs.blocks = await Array.fromAsync(
72
+ glob(`${project.path}/blocks/*`)
73
+ );
74
+ globs.blocksSass = await Array.fromAsync(
75
+ glob(`${project.path}/blocks/*/src/*.scss`)
76
+ );
77
+ }
78
+ if (builds.includes('images')) {
79
+ globs.images = await Array.fromAsync(
80
+ glob(project.package?.sdc?.imagesPath ||
81
+ `${paths.images}/**/*`)
82
+ );
83
+ globs.imageDirectories = [
84
+ paths.images,
85
+ ...await getAllSubdirectories(paths.images)
86
+ ];
87
+ }
88
+ if (builds.includes('php')) {
89
+ globs.php = await Array.fromAsync(
90
+ glob(project.package?.sdc?.jsGlobPath ||
91
+ `${project.path}/**/*.php`)
92
+ );
93
+ globs.blocksPHP = await Array.fromAsync(
94
+ glob(`${project.path}/blocks/*/build/*.php`)
95
+ );
96
+ chokidarOpts.ignored = [
97
+ ...chokidarOpts.ignored,
98
+ ...globs.blocksPHP
99
+ ];
100
+ }
101
+
102
+ for (const [name, files] of Object.entries(project.package.sdc.entries)) {
103
+ entries[name] = [];
104
+ files.forEach(function(file) {
105
+ entries[name].push(project.path + file);
62
106
  });
107
+ }
108
+ // for (var filename of globs.blocksSass) {
109
+ // entries[`blocks/${path.basename(path.dirname(filename))}/style`] = [ filename ];
110
+ // }
111
+
112
+ for (const [name, files] of Object.entries(entries)) {
113
+ files.forEach(function(file) {
114
+ switch (path.parse(file).ext) {
115
+ case '.scss':
116
+ if (builds.includes('sass')) {
117
+ filesSass.push({
118
+ 'name': name,
119
+ 'file': file
120
+ });
121
+ }
122
+ break;
123
+ case '.js':
124
+ if (builds.includes('js')) {
125
+ filesJS.push({
126
+ 'name': name,
127
+ 'file': file
128
+ });
129
+ }
130
+ break;
131
+ }
132
+ });
133
+ }
134
+
135
+ if (builds.includes('sass')) {
136
+ await runSass(true);
137
+ }
138
+ if (builds.includes('js')) {
139
+ await runJS();
140
+ }
141
+ if (builds.includes('blocks')) {
142
+ await runBlocks();
143
+ }
144
+ if (builds.includes('images')) {
145
+ await frontrunImages();
146
+ }
147
+ if (builds.includes('fonts')) {
148
+ await buildFonts(project.path + '/_src/fonts');
149
+ }
150
+ // if (builds.includes('php') && shouldPHPLint) {
151
+ // await runPHP(null, 'warn'); // this errors "Fatal error: Allowed memory size"
152
+ // }
153
+
154
+ if (argv.watch) {
155
+
156
+ buildBrowserSync();
157
+
158
+ if (builds.includes('sass')) {
159
+ chokidar.watch([
160
+ ...[paths.theme.json],
161
+ globs.sass
162
+ ], {
163
+ ...chokidarOpts
164
+ }).on('all', (event, path) => {
165
+ runSass(path == paths.theme.json);
166
+ });
167
+ }
168
+
169
+ if (builds.includes('js')) {
170
+ chokidar.watch(globs.js, {
171
+ ...chokidarOpts
172
+ }).on('all', (event, path) => {
173
+ runJS();
174
+ });
175
+ }
176
+
177
+ if (builds.includes('blocks')) {
178
+ for (let block of globs.blocks) {
179
+ chokidar.watch(`${block}/src`, {
180
+ ...chokidarOpts
181
+ }).on('all', (event, path) => {
182
+ runBlocks(block);
183
+ });
184
+ }
185
+ }
186
+
187
+ if (builds.includes('images')) {
188
+ chokidar.watch(paths.images, chokidarOpts).on('all', (event, path) => {
189
+ frontrunImages();
190
+ });
191
+ }
192
+
193
+ if (builds.includes('php') && shouldPHPLint) {
194
+ chokidar.watch(globs.php, {
195
+ ...chokidarOpts
196
+ }).on('all', (event, path) => {
197
+ runPHP(path);
198
+ });
199
+ }
200
+
201
+ if (existsSync(paths.errorLog)) {
202
+ let errorLogTail = new Tail(paths.errorLog);
203
+ errorLogTail.on('line', function(data) {
204
+ log('php', data);
205
+ });
206
+ } else {
207
+ log('info', `Cannot find error log @ ${paths.errorLog}. Skipping watching php error logs`);
208
+ }
209
+ }
210
+
211
+ })();
212
+
213
+ async function frontrunImages() {
214
+ globs.imageDirectories.forEach(directory => {
215
+ buildImages(directory);
63
216
  });
64
217
  }
65
218
 
66
- function runBlocks() {
67
- for (var block of blockGlob) {
68
- buildBlock(block);
219
+ async function runBlocks(singleBlock) {
220
+ if (singleBlock) {
221
+ await buildBlock(singleBlock);
222
+ } else {
223
+ for (var block of globs.blocks) {
224
+ await buildBlock(block);
225
+ }
69
226
  }
70
- bustFunctionsCache();
71
227
  }
72
228
 
73
- function runSass() {
74
- buildSassTheme();
229
+ async function runSass(buildTheme = true) {
230
+ if (buildTheme) {
231
+ await buildSassTheme();
232
+ }
75
233
  for (var block of filesSass) {
76
- buildSass(block.file, block.name, sassGlob);
77
- bustFunctionsCache();
234
+ await buildSass(block.file, block.name, globs.sass);
78
235
  }
79
236
  }
80
237
 
81
- function runJS() {
238
+ async function runJS() {
82
239
  for (var block of filesJS) {
83
- buildJS(block.file, block.name, jsGlob);
84
- bustFunctionsCache();
240
+ await buildJS(block.file, block.name, globs.js);
85
241
  }
86
242
  }
87
243
 
88
- function runPHP(file, method) {
89
- buildPHP(file, method);
90
- }
91
-
92
- let entries = {};
93
- for (const [name, files] of Object.entries(project.package.sdc.entries)) {
94
- entries[name] = [];
95
- files.forEach(function(file) {
96
- entries[name].push(project.path + file);
97
- });
98
- }
99
- let sassBlocksGlob = globSync(project.path + '/blocks/*/*.scss');
100
- for (var filename of sassBlocksGlob) {
101
- entries[`blocks/${path.basename(path.dirname(filename))}/style`] = [ filename ];
244
+ async function runPHP(file, method) {
245
+ await buildPHP(file, method);
102
246
  }
103
247
 
104
- let filesSass = [];
105
- let filesJS = [];
106
-
107
- for (const [name, files] of Object.entries(entries)) {
108
- files.forEach(function(file) {
109
- switch (path.parse(file).ext) {
110
- case '.scss':
111
- filesSass.push({
112
- 'name': name,
113
- 'file': file
114
- });
115
- break;
116
- case '.js':
117
- filesJS.push({
118
- 'name': name,
119
- 'file': file
120
- });
121
- break;
248
+ async function getAllSubdirectories(dir) {
249
+ let subdirectories = [];
250
+ const subdirectoriesEntries = await readdir(dir, { withFileTypes: true });
251
+ for (const subdirectoriesEntry of subdirectoriesEntries) {
252
+ if (subdirectoriesEntry.isDirectory()) {
253
+ const subdirPath = path.join(dir, subdirectoriesEntry.name);
254
+ subdirectories.push(subdirPath);
255
+ const nestedSubdirs = await getAllSubdirectories(subdirPath);
256
+ subdirectories = subdirectories.concat(nestedSubdirs);
122
257
  }
123
- });
124
- }
125
-
126
- // if (shouldPHPLint) {
127
- // runPHP(null, 'warn'); // this errors "Fatal error: Allowed memory size"
128
- // }
129
- runBlocks();
130
- runSass();
131
- runJS();
132
- frontrunImages()
133
- buildFonts(project.path + '/_src/fonts');
134
-
135
- if (argv.watch) {
136
- buildBrowserSync();
137
- chokidar.watch(blockGlob, chokidarOpts).on('all', (event, path) => {
138
- runBlocks();
139
- });
140
- chokidar.watch(sassGlob, chokidarOpts).on('all', (event, path) => {
141
- runSass();
142
- });
143
- chokidar.watch(project.path + '/theme.json', chokidarOpts).on('all', (event, path) => {
144
- runSass();
145
- });
146
- chokidar.watch(jsGlob, chokidarOpts).on('all', (event, path) => {
147
- runJS();
148
- });
149
- chokidar.watch(project.path + '/_src/images/**/*', chokidarOpts).on('all', (event, path) => {
150
- frontrunImages();
151
- });
152
- let errorLogPath = process.env.ERROR_LOG_PATH || project.package.sdc?.error_log_path || '../../../../../logs/php/error.log';
153
- if (existsSync(errorLogPath)) {
154
- let errorLogTail = new Tail(errorLogPath);
155
- errorLogTail.on('line', function(data) {
156
- log('php', data);
157
- });
158
- } else {
159
- log('info', `Cannot find error log @ ${errorLogPath}. Skipping watching php error logs`);
160
- }
161
- if (shouldPHPLint) {
162
- chokidar.watch(phpGlob, chokidarOpts).on('change', (path) => {
163
- runPHP(path);
164
- });
165
258
  }
259
+ return subdirectories;
166
260
  }
package/lib/blocks.js CHANGED
@@ -6,6 +6,8 @@ import { stat } from 'fs/promises';
6
6
  import { spawn } from 'child_process';
7
7
  import process from 'process';
8
8
 
9
+ let activeEntry;
10
+
9
11
  function cmd(...command) {
10
12
  let p = spawn(command[0], command.slice(1), {
11
13
  shell: true
@@ -14,13 +16,13 @@ function cmd(...command) {
14
16
  p.stdout.on('data', (x) => {
15
17
  if (x.toString().includes('Error:')) {
16
18
  process.stdout.write(x.toString());
17
- log('error', `Failed building blocks - See above error.`);
19
+ log('error', `Failed building ${activeEntry.replace(project.path, '')} block - See above error.`);
18
20
  }
19
21
  });
20
22
  p.stderr.on('data', (x) => {
21
23
  if (x.toString().includes('Error:')) {
22
24
  process.stderr.write(x.toString());
23
- log('error', `Failed building blocks - See above error.`);
25
+ log('error', `Failed building ${activeEntry.replace(project.path, '')} block - See above error.`);
24
26
  }
25
27
  });
26
28
  p.on('exit', (code) => {
@@ -30,6 +32,7 @@ function cmd(...command) {
30
32
  }
31
33
 
32
34
  const buildBlock = async (entry) => {
35
+ activeEntry = entry;
33
36
  let workingBlockJson = null;
34
37
  let potentialBlockJsonLocations = [
35
38
  `${entry}/src/block.json`,
@@ -50,11 +53,10 @@ const buildBlock = async (entry) => {
50
53
  }
51
54
  let timerStart = Date.now();
52
55
  let cmds = [`build`];
53
- for (var file of (await fs.readdir(`${entry}/src`)).filter(file => path.extname(file) == '.js')) {
54
- cmds.push(`src/${file}`);
55
- }
56
+ cmds.push(`--source-path=.${entry.replace(project.path, '')}/src`);
57
+ cmds.push(`--output-path=.${entry.replace(project.path, '')}/build`);
56
58
  cmds.push('--webpack-copy-php');
57
- await cmd(`cd ${entry} && ${project.path}/node_modules/@wordpress/scripts/bin/wp-scripts.js`, ...cmds);
59
+ await cmd(`${project.path}/node_modules/@wordpress/scripts/bin/wp-scripts.js`, ...cmds);
58
60
  log('success', `Built ${entry.replace(project.path, '')} in ${Date.now() - timerStart}ms`);
59
61
  };
60
62
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdc-build-wp",
3
- "version": "4.0.2",
3
+ "version": "4.1.1",
4
4
  "description": "Custom WordPress build process.",
5
5
  "engines": {
6
6
  "node": ">=20"
@@ -27,11 +27,10 @@
27
27
  "autoprefixer": "^10.4.20",
28
28
  "browser-sync": "^3.0.3",
29
29
  "chalk": "^5.4.1",
30
- "chokidar": "^3.6.0",
30
+ "chokidar": "^4.0.3",
31
31
  "esbuild": "^0.25.0",
32
32
  "eslint": "^9.21.0",
33
33
  "fs-extra": "^11.3.0",
34
- "glob": "^11.0.1",
35
34
  "imagemin": "^9.0.0",
36
35
  "imagemin-jpegtran": "^8.0.0",
37
36
  "imagemin-pngquant": "^10.0.0",
package/lib/bustCache.js DELETED
@@ -1,14 +0,0 @@
1
- import fs from 'fs';
2
-
3
- const bustCache = (file) => {
4
- fs.readFile(file, 'utf8', function (err, data) {
5
- if (err) { return console.log(err); }
6
- if (!data.includes('$cacheVersion')) { return; }
7
- var result = data.replace(/(\$cacheVersion\ \=\ \')(.*)(\'\;)/g, "$1" + new Date().getTime() + "$3");
8
- fs.writeFile(file, result, 'utf8', function (err) {
9
- if (err) return console.log(err);
10
- });
11
- });
12
- };
13
-
14
- export default bustCache;