sdc-build-wp 2.1.0 → 2.6.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/index.js CHANGED
@@ -9,6 +9,7 @@ import glob from 'glob';
9
9
  import bustCache from './lib/bustCache.js';
10
10
  import buildSass from './lib/style.js';
11
11
  import buildJS from './lib/scripts.js';
12
+ import buildBlocks from './lib/blocks.js';
12
13
  import buildImages from './lib/images.js';
13
14
  import buildFonts from './lib/fonts.js';
14
15
  import buildBrowserSync from './lib/browsersync.js';
@@ -17,12 +18,14 @@ let chokidarOpts = {
17
18
  ignoreInitial: true
18
19
  };
19
20
 
20
- let sassGlobPath = project.package?.sdc?.sassGlobPath || project.path + '/_src/style/**/*.scss';
21
+ let sassGlobPath = project.package?.sdc?.sassGlobPath || project.path + '{/_src/style,/blocks}/**/*.scss';
21
22
  let sassGlob = glob.sync(sassGlobPath, {
22
23
  ignore: [
23
24
  project.path + '/_src/style/partials/_theme.scss'
24
25
  ]
25
26
  });
27
+ let blockGlobPath = project.package?.sdc?.blockGlobPath || project.path + '/_src/blocks/**/*.{js,jsx}';
28
+ let blockGlob = glob.sync(blockGlobPath);
26
29
 
27
30
  function bustFunctionsCache() {
28
31
  bustCache(project.path + '/functions.php');
@@ -45,6 +48,10 @@ for (const [name, files] of Object.entries(project.package.sdc.entries)) {
45
48
  entries[name].push(project.path + file);
46
49
  });
47
50
  }
51
+ let sassBlocksGlob = glob.sync(project.path + '/blocks/*/*.scss');
52
+ for (var filename of sassBlocksGlob) {
53
+ entries[`blocks/${path.basename(path.dirname(filename))}/style`] = [ filename ];
54
+ }
48
55
 
49
56
  let filesSass = [];
50
57
 
@@ -52,7 +59,10 @@ for (const [name, files] of Object.entries(entries)) {
52
59
  files.forEach(function(file) {
53
60
  switch (path.parse(file).ext) {
54
61
  case '.scss':
55
- filesSass.push(file);
62
+ filesSass.push({
63
+ 'name': name,
64
+ 'file': file
65
+ });
56
66
  break;
57
67
  case '.js':
58
68
  buildJS(file);
@@ -68,24 +78,31 @@ for (const [name, files] of Object.entries(entries)) {
68
78
  });
69
79
  }
70
80
 
71
- filesSass.forEach((file) => {
72
- buildSass(file, sassGlob);
73
- bustFunctionsCache();
74
- });
81
+ buildBlocks(blockGlob);
82
+ bustFunctionsCache();
83
+ if (argv.watch) {
84
+ chokidar.watch(blockGlob, chokidarOpts).on('all', (event, path) => {
85
+ buildBlocks(blockGlob);
86
+ bustFunctionsCache();
87
+ });
88
+ }
89
+
90
+ function runSass() {
91
+ for (var block of filesSass) {
92
+ buildSass(block.file, block.name, sassGlob);
93
+ bustFunctionsCache();
94
+ }
95
+ }
96
+
97
+ runSass();
75
98
  if (argv.watch) {
76
99
  chokidar.watch(sassGlob, chokidarOpts).on('all', (event, path) => {
77
- filesSass.forEach((file) => {
78
- buildSass(file, sassGlob);
79
- bustFunctionsCache();
80
- });
100
+ runSass();
81
101
  });
82
102
  }
83
103
  if (argv.watch) {
84
104
  chokidar.watch(project.path + '/theme.json', chokidarOpts).on('all', (event, path) => {
85
- filesSass.forEach((file) => {
86
- buildSass(file, sassGlob);
87
- bustFunctionsCache();
88
- });
105
+ runSass();
89
106
  });
90
107
  }
91
108
 
package/lib/blocks.js ADDED
@@ -0,0 +1,47 @@
1
+ import path from 'path';
2
+ import project from '../lib/project.js';
3
+ import log from './logging.js';
4
+ import esbuild from 'esbuild';
5
+ import { ESLint } from 'eslint';
6
+ import { readFile } from 'fs/promises';
7
+
8
+ const buildBlocks = async (entries) => {
9
+ let timerStart = Date.now();
10
+ try {
11
+ const eslint = new ESLint({
12
+ baseConfig: JSON.parse(await readFile(new URL('../.eslintrc', import.meta.url))),
13
+ fix: true
14
+ });
15
+ const lintresults = await eslint.lintFiles(entries);
16
+ await ESLint.outputFixes(lintresults);
17
+ const formatter = await eslint.loadFormatter('stylish');
18
+ const formatterOutput = formatter.format(lintresults);
19
+ if (formatterOutput) { console.log(formatterOutput.replace(project.path + '/_src/blocks/', '')); }
20
+ } catch (error) {
21
+ console.log(error);
22
+ log('error', `Failed linting ${entries.length} blocks - See above error.`);
23
+ return false;
24
+ }
25
+ try {
26
+ const result = await esbuild.build({
27
+ platform: 'node',
28
+ entryPoints: entries,
29
+ bundle: true,
30
+ loader: { '.js' : 'jsx' },
31
+ minify: true,
32
+ outdir: 'dist/blocks/',
33
+ entryNames: '[dir]/[name].min',
34
+ plugins: [],
35
+ sourcemap: true
36
+ });
37
+ if (result.warnings.length > 0) {
38
+ log('warn', result.warnings);
39
+ }
40
+ } catch (error) {
41
+ log('error', `Failed building ${entries.length} blocks - See above error.`);
42
+ return false;
43
+ }
44
+ log('success', `Built ${entries.length} blocks in ${Date.now() - timerStart}ms`);
45
+ };
46
+
47
+ export default buildBlocks;
@@ -9,7 +9,8 @@ const buildBrowserSync = () => {
9
9
  files: [
10
10
  project.path + '/dist/**/*',
11
11
  project.path + '/**/*.php',
12
- project.path + '/**/*.html'
12
+ project.path + '/**/*.html',
13
+ project.path + '/**/*.json',
13
14
  ],
14
15
  open: project.package.sdc?.open || false,
15
16
  https: (process.env.SSL_KEY_PATH && process.env.SSL_CRT_PATH ? {
package/lib/style.js CHANGED
@@ -39,10 +39,13 @@ const buildColors = async () => {
39
39
  }
40
40
  };
41
41
 
42
- const buildSass = async (entry, entriesToLint) => {
42
+ const buildSass = async (entry, name, entriesToLint) => {
43
43
  await buildColors();
44
44
  let timerStart = Date.now();
45
- let outFile = project.path + '/dist/style/' + path.parse(entry).name + '.min.css';
45
+ let outFile = project.path + '/dist/' + name + '.min.css';
46
+ if (name.startsWith('blocks/')) {
47
+ outFile = project.path + '/' + name + '.min.css';
48
+ }
46
49
  let entryLabel = outFile.replace(project.path, '');
47
50
  stylelint.lint({
48
51
  configFile: path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../.stylelintrc'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdc-build-wp",
3
- "version": "2.1.0",
3
+ "version": "2.6.0",
4
4
  "description": "Custom WordPress build process.",
5
5
  "author": {
6
6
  "name": "Robert Sefer",