@sumotto/configs 0.0.27 → 0.0.29

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.
@@ -1,5 +1,6 @@
1
- const globby = require( 'globby' );
1
+ const glob = require( 'glob' );
2
2
  const rimraf = require( 'rimraf' );
3
+ const fs = require( 'fs' );
3
4
 
4
5
  let isRunning = false;
5
6
 
@@ -28,9 +29,51 @@ class CleanWebpackPlugin {
28
29
  }
29
30
 
30
31
  del() {
31
- globby.sync( this.patterns ).forEach( ( file ) => {
32
+ const inclusionPatterns = this.patterns.filter(
33
+ ( pattern ) => ! pattern.startsWith( '!' )
34
+ );
35
+ const exclusionPatterns = this.patterns
36
+ .filter( ( pattern ) => pattern.startsWith( '!' ) )
37
+ .map( ( pattern ) => pattern.slice( 1 ) );
38
+
39
+ let directories = [];
40
+ let filesToDelete = [];
41
+ inclusionPatterns.forEach( ( pattern ) => {
42
+ const results = glob.sync( pattern, { withFileTypes: true } );
43
+ results.forEach( ( result ) => {
44
+ if ( result.isDirectory() ) {
45
+ directories.push( result.fullpath() );
46
+ } else {
47
+ filesToDelete.push( result.fullpath() );
48
+ }
49
+ } );
50
+ } );
51
+
52
+ let excludedFiles = [];
53
+ exclusionPatterns.forEach( ( pattern ) => {
54
+ const files = glob.sync( pattern, { nodir: true } );
55
+ excludedFiles = excludedFiles.concat( files );
56
+ } );
57
+
58
+ directories = excludedFiles.filter(
59
+ ( e, index ) => directories.indexOf( e ) === index
60
+ );
61
+ excludedFiles = excludedFiles.filter(
62
+ ( e, index ) => excludedFiles.indexOf( e ) === index
63
+ );
64
+ filesToDelete = filesToDelete
65
+ .filter( ( e, index ) => filesToDelete.indexOf( e ) === index )
66
+ .filter( ( file ) => ! excludedFiles.includes( file ) );
67
+
68
+ filesToDelete.forEach( ( file ) => {
32
69
  rimraf.sync( file, { glob: false } );
33
70
  } );
71
+
72
+ directories.forEach( ( directory ) => {
73
+ if ( fs.readdirSync( directory ).length === 0 ) {
74
+ fs.rmdirSync( directory );
75
+ }
76
+ } );
34
77
  }
35
78
  }
36
79
 
@@ -0,0 +1,183 @@
1
+ const chokidar = require( 'chokidar' );
2
+ const path = require( 'path' );
3
+ const fs = require( 'fs' );
4
+
5
+ function logError( label, error ) {
6
+ // eslint-disable-next-line no-console
7
+ console.error( label, error );
8
+ }
9
+
10
+ class SyncWebpackPlugin {
11
+ constructor( options ) {
12
+ this.options = options;
13
+ this.ignored = new RegExp(
14
+ [
15
+ /(^|[\/\\])\..*|node_modules/.source,
16
+ ...this.options.exclude.map( ( regex ) => regex.source ),
17
+ ].join( '|' )
18
+ );
19
+ }
20
+
21
+ // noinspection JSUnusedGlobalSymbols
22
+ apply( compiler ) {
23
+ compiler.hooks.run.tap( 'SumoTToSyncPlugin', () => {
24
+ this.clearDirectory( this.options.targetDir );
25
+ this.copyDirectory(
26
+ this.options.sourceDir,
27
+ this.options.targetDir
28
+ );
29
+ } );
30
+
31
+ compiler.hooks.watchRun.tap( 'SumoTToSyncPlugin', () => {
32
+ const watcher = chokidar.watch( this.options.sourceDir, {
33
+ ignored: this.ignored,
34
+ persistent: true,
35
+ } );
36
+
37
+ watcher
38
+ .on( 'add', ( filePath ) => this.copyFile( filePath ) )
39
+ .on( 'addDir', ( dirPath ) => this.copyDir( dirPath ) )
40
+ .on( 'change', ( filePath ) => this.copyFile( filePath ) )
41
+ .on( 'unlink', ( filePath ) => this.deleteFile( filePath ) )
42
+ .on( 'unlinkDir', ( dirPath ) => this.deleteDir( dirPath ) )
43
+ .on( 'error', ( error ) =>
44
+ logError( 'Watcher error: ', error )
45
+ );
46
+
47
+ this.watcher = watcher;
48
+ } );
49
+
50
+ compiler.hooks.watchClose.tap( 'SumoTToSyncPlugin', () => {
51
+ this.watcher.close().catch( ( err ) => {
52
+ logError(
53
+ 'Error when stopping SumoTToSyncPlugin watch: ',
54
+ err
55
+ );
56
+ } );
57
+ } );
58
+ }
59
+
60
+ copyFile( filePath ) {
61
+ const relativePath = path.relative( this.options.sourceDir, filePath );
62
+ const destinationPath = path.join(
63
+ this.options.targetDir,
64
+ relativePath
65
+ );
66
+
67
+ const destinationDir = path.dirname( destinationPath );
68
+ if ( ! fs.existsSync( destinationDir ) ) {
69
+ fs.mkdirSync( destinationDir, { recursive: true } );
70
+ }
71
+
72
+ this.retryCopy( filePath, destinationPath );
73
+ }
74
+
75
+ retryCopy( filePath, destinationPath, retries = 5, delay = 100 ) {
76
+ fs.copyFile( filePath, destinationPath, ( error ) => {
77
+ if ( error ) {
78
+ if ( error.code === 'EBUSY' && retries > 0 ) {
79
+ setTimeout(
80
+ () =>
81
+ this.retryCopy(
82
+ filePath,
83
+ destinationPath,
84
+ retries - 1,
85
+ delay * 2
86
+ ),
87
+ delay
88
+ );
89
+ } else {
90
+ logError( 'File copy error: ', error );
91
+ }
92
+ }
93
+ } );
94
+ }
95
+
96
+ copyDir( dirPath ) {
97
+ const relativePath = path.relative( this.options.sourceDir, dirPath );
98
+ const destinationPath = path.join(
99
+ this.options.targetDir,
100
+ relativePath
101
+ );
102
+
103
+ fs.mkdir( destinationPath, { recursive: true }, ( error ) => {
104
+ if ( error ) {
105
+ logError( 'Directory creation error: ', error );
106
+ }
107
+ } );
108
+ }
109
+
110
+ deleteFile( filePath ) {
111
+ const sourceRelativePath = path.relative(
112
+ this.options.sourceDir,
113
+ filePath
114
+ );
115
+ const targetPath = path.join(
116
+ this.options.targetDir,
117
+ sourceRelativePath
118
+ );
119
+
120
+ fs.unlink( targetPath, ( error ) => {
121
+ if ( error ) {
122
+ logError( 'File deletion error: ', error );
123
+ }
124
+ } );
125
+ }
126
+
127
+ deleteDir( dirPath ) {
128
+ const sourceRelativePath = path.relative(
129
+ this.options.sourceDir,
130
+ dirPath
131
+ );
132
+ const targetPath = path.join(
133
+ this.options.targetDir,
134
+ sourceRelativePath
135
+ );
136
+
137
+ if ( fs.lstatSync( targetPath ).isDirectory() ) {
138
+ this.clearDirectory( targetPath, true );
139
+ if ( fs.readdirSync( targetPath ).length === 0 ) {
140
+ fs.rmdirSync( targetPath );
141
+ }
142
+ }
143
+ }
144
+
145
+ clearDirectory( dirPath, force = false ) {
146
+ if ( fs.existsSync( dirPath ) ) {
147
+ fs.readdirSync( dirPath ).forEach( ( file ) => {
148
+ const curPath = path.join( dirPath, file );
149
+ if ( false === force && this.ignored.test( curPath ) ) {
150
+ return;
151
+ }
152
+
153
+ if ( fs.lstatSync( curPath ).isDirectory() ) {
154
+ this.clearDirectory( curPath, force );
155
+ if ( fs.readdirSync( curPath ).length === 0 ) {
156
+ fs.rmdirSync( curPath );
157
+ }
158
+ } else {
159
+ fs.unlinkSync( curPath );
160
+ }
161
+ } );
162
+ }
163
+ }
164
+
165
+ copyDirectory( sourceDir, targetDir ) {
166
+ if ( ! fs.existsSync( targetDir ) ) {
167
+ fs.mkdirSync( targetDir );
168
+ }
169
+
170
+ fs.readdirSync( sourceDir ).forEach( ( file ) => {
171
+ const source = path.join( sourceDir, file );
172
+ const target = path.join( targetDir, file );
173
+
174
+ if ( fs.lstatSync( source ).isDirectory() ) {
175
+ this.copyDirectory( source, target );
176
+ } else {
177
+ fs.copyFileSync( source, target );
178
+ }
179
+ } );
180
+ }
181
+ }
182
+
183
+ module.exports = SyncWebpackPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sumotto/configs",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "description": "Configs for my work",
5
5
  "scripts": {
6
6
  "release": "np --yolo",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "homepage": "https://github.com/SumoTTo/configs#readme",
22
22
  "devDependencies": {
23
- "np": "^10.0.7"
23
+ "np": "^10.1.0"
24
24
  },
25
25
  "files": [
26
26
  "helpers/*",
@@ -28,14 +28,15 @@
28
28
  "wp/*"
29
29
  ],
30
30
  "dependencies": {
31
- "@wordpress/scripts": "^30.3.0",
31
+ "@wordpress/dependency-extraction-webpack-plugin": "^6.13.0",
32
+ "@wordpress/scripts": "^30.6.0",
33
+ "chokidar": "^4.0.1",
32
34
  "copy-webpack-plugin": "^12.0.2",
33
- "dotenv": "^16.4.5",
35
+ "dotenv": "^16.4.7",
34
36
  "find-free-port-sync": "^1.0.0",
35
37
  "glob": "^11.0.0",
36
- "globby": "^14.0.2",
37
- "image-minimizer-webpack-plugin": "^4.1.0",
38
- "mini-css-extract-plugin": "^2.9.1",
38
+ "image-minimizer-webpack-plugin": "^4.1.1",
39
+ "mini-css-extract-plugin": "^2.9.2",
39
40
  "rimraf": "^6.0.1",
40
41
  "rtlcss-webpack-plugin": "^4.0.7",
41
42
  "svgo": "^3.3.2",
package/wp/phpcs.xml CHANGED
@@ -25,6 +25,7 @@
25
25
  <element value="theme" />
26
26
  <element value="the-modifications" />
27
27
  <element value="the-builder" />
28
+ <element value="the-woocommerce" />
28
29
  <element value="woocommerce" />
29
30
  <element value="default" />
30
31
  </property>
@@ -1,7 +1,7 @@
1
1
  const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
2
2
  const RtlCssPlugin = require( 'rtlcss-webpack-plugin' );
3
3
  const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
4
- const SyncDirectoryWebpackPlugin = require( '../helpers/sync-directory-webpack-plugin' );
4
+ const SyncWebpackPlugin = require( '../helpers/sync-webpack-plugin' );
5
5
  const CleanWebpackPlugin = require( '../helpers/clean-webpack-plugin' );
6
6
  const findFreePort = require( 'find-free-port-sync' );
7
7
  const { resolve } = require( 'node:path' );
@@ -60,7 +60,7 @@ if ( process.env.WP_CONTENT_DIR ) {
60
60
  defaultConfigWP.output.path = buildPath;
61
61
  modulesConfigWP.output.path = buildPath;
62
62
 
63
- const syncDirectory = new SyncDirectoryWebpackPlugin( {
63
+ const syncDirectory = new SyncWebpackPlugin( {
64
64
  sourceDir: rootPath,
65
65
  targetDir: outputPath,
66
66
  exclude: [ /[\/\\]build[\/\\]/ ],
@@ -0,0 +1,44 @@
1
+ const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
2
+ const CleanWebpackPlugin = require( '../helpers/clean-webpack-plugin' );
3
+ const SyncWebpackPlugin = require( '../helpers/sync-webpack-plugin' );
4
+ const { resolve } = require( 'node:path' );
5
+ const { Config, modulesConfigWP } = require( '../helpers/webpack' );
6
+ const rootPath = process.cwd().replace( /\\/g, '/' );
7
+ const outputPath = process.env.WP_CONTENT_DIR
8
+ ? resolve( process.env.WP_CONTENT_DIR, 'plugins/the-woocommerce' )
9
+ : rootPath;
10
+
11
+ const modulesConfig = new Config( modulesConfigWP, 'modules' )
12
+ .resetEntries()
13
+ .addEntries( 'src/scripts/modules/*.{j,t}s' )
14
+ .addPlugin(
15
+ new CleanWebpackPlugin( {
16
+ patterns: [ resolve( outputPath, '/build/scripts/modules/**/*' ) ],
17
+ } )
18
+ )
19
+ .replacePlugin(
20
+ new DependencyExtractionWebpackPlugin( {
21
+ requestToExternalModule( request ) {
22
+ if (
23
+ typeof request === 'string' &&
24
+ /^@the-woocommerce\//.test( request )
25
+ ) {
26
+ return `import ${ request }`;
27
+ }
28
+ },
29
+ } )
30
+ );
31
+
32
+ if ( process.env.WP_CONTENT_DIR ) {
33
+ modulesConfigWP.output.path = resolve( outputPath, 'build' );
34
+
35
+ const syncDirectory = new SyncWebpackPlugin( {
36
+ sourceDir: rootPath,
37
+ targetDir: outputPath,
38
+ exclude: [ /[\/\\]build[\/\\]/ ],
39
+ } );
40
+
41
+ modulesConfig.addPlugin( syncDirectory );
42
+ }
43
+
44
+ module.exports = [ modulesConfig.get() ];
@@ -2,7 +2,7 @@ const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
2
2
  const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
3
3
  const RtlCssPlugin = require( 'rtlcss-webpack-plugin' );
4
4
  const ImageMinimizerPlugin = require( 'image-minimizer-webpack-plugin' );
5
- const SyncDirectoryWebpackPlugin = require( '../helpers/sync-directory-webpack-plugin' );
5
+ const SyncWebpackPlugin = require( '../helpers/sync-webpack-plugin' );
6
6
  const CleanWebpackPlugin = require( '../helpers/clean-webpack-plugin' );
7
7
  const findFreePort = require( 'find-free-port-sync' );
8
8
  const { resolve } = require( 'node:path' );
@@ -123,7 +123,7 @@ if ( process.env.WP_CONTENT_DIR ) {
123
123
  defaultConfigWP.output.path = buildPath;
124
124
  modulesConfigWP.output.path = buildPath;
125
125
 
126
- const syncDirectory = new SyncDirectoryWebpackPlugin( {
126
+ const syncDirectory = new SyncWebpackPlugin( {
127
127
  sourceDir: rootPath,
128
128
  targetDir: outputPath,
129
129
  exclude: [ /[\/\\]build[\/\\]/ ],