@sumotto/configs 0.0.13 → 0.0.15

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,23 @@
1
+ const syncDirectory = require( 'sync-directory' );
2
+
3
+ class SyncDirectoryWebpackPlugin {
4
+ constructor( options ) {
5
+ this.sourceDir = options.sourceDir;
6
+ this.targetDir = options.targetDir;
7
+ this.watcher = syncDirectory( this.sourceDir, this.targetDir, {
8
+ watch: true,
9
+ } );
10
+ }
11
+
12
+ // noinspection JSUnusedGlobalSymbols
13
+ apply( compiler ) {
14
+ compiler.hooks.done.tap( 'SyncDirectoryPlugin', () => {
15
+ if ( 'development' !== compiler.options.mode ) {
16
+ // noinspection JSUnresolvedReference
17
+ this.watcher.close();
18
+ }
19
+ } );
20
+ }
21
+ }
22
+
23
+ module.exports = SyncDirectoryWebpackPlugin;
@@ -56,6 +56,32 @@ module.exports.Config = class {
56
56
  constructor( config, name, port = 'auto' ) {
57
57
  this.config = config;
58
58
  this.config.name = name;
59
+ this.entries = {};
60
+ this.entriesFunctions = [];
61
+
62
+ if ( typeof this.config.entry === 'function' ) {
63
+ this.entriesFunctions.push( this.config.entry );
64
+ } else {
65
+ this.entries = {
66
+ ...this.config.entry,
67
+ };
68
+ }
69
+
70
+ this.config.entry = () => {
71
+ let entries = {};
72
+
73
+ for ( const entriesFunction of this.entriesFunctions ) {
74
+ entries = {
75
+ ...entries,
76
+ ...entriesFunction(),
77
+ };
78
+ }
79
+
80
+ return {
81
+ ...this.entries,
82
+ ...entries,
83
+ };
84
+ };
59
85
 
60
86
  if ( config.devServer ) {
61
87
  // noinspection JSUnusedGlobalSymbols
@@ -90,23 +116,14 @@ module.exports.Config = class {
90
116
  }
91
117
 
92
118
  resetEntries() {
93
- this.config.entry = {};
119
+ this.entries = {};
120
+ this.entriesFunctions = [];
94
121
 
95
122
  return this;
96
123
  }
97
124
 
98
125
  addEntries( globPattern ) {
99
- if ( typeof this.config.entry === 'function' ) {
100
- this.config.entry = {
101
- ...this.config.entry(),
102
- ...getEntries( globPattern ),
103
- };
104
- } else {
105
- this.config.entry = {
106
- ...this.config.entry,
107
- ...getEntries( globPattern ),
108
- };
109
- }
126
+ this.entriesFunctions.push( () => getEntries( globPattern ) );
110
127
 
111
128
  return this;
112
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sumotto/configs",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "Configs for my work",
5
5
  "scripts": {
6
6
  "release": "np --yolo"
@@ -26,16 +26,18 @@
26
26
  "wp/*"
27
27
  ],
28
28
  "dependencies": {
29
- "@wordpress/scripts": "^28.1.0",
30
- "clean-webpack-plugin": "^4.0.0",
29
+ "@wordpress/scripts": "^28.2.0",
31
30
  "copy-webpack-plugin": "^12.0.2",
32
31
  "dotenv": "^16.4.5",
32
+ "filemanager-webpack-plugin": "^8.0.0",
33
33
  "find-free-port-sync": "^1.0.0",
34
34
  "glob": "^10.4.2",
35
35
  "image-minimizer-webpack-plugin": "^4.0.2",
36
36
  "mini-css-extract-plugin": "^2.9.0",
37
+ "postcss-increase-specificity": "^0.6.0",
37
38
  "rtlcss-webpack-plugin": "^4.0.7",
38
39
  "svgo": "^3.3.2",
40
+ "sync-directory": "^6.0.5",
39
41
  "webpack-remove-empty-scripts": "^1.0.4"
40
42
  },
41
43
  "eslintConfig": {
@@ -0,0 +1,32 @@
1
+ module.exports = ( api ) => {
2
+ const isProduction = api.mode === 'production';
3
+
4
+ return {
5
+ ident: 'postcss',
6
+ sourceMap: ! isProduction,
7
+ plugins: [
8
+ [ 'autoprefixer', { grid: true } ],
9
+ [
10
+ 'postcss-increase-specificity',
11
+ {
12
+ repeat: 1,
13
+ overrideIds: false,
14
+ stackableRoot: ':is(.is-root-container,.wp-site-blocks)',
15
+ },
16
+ ],
17
+ isProduction && [
18
+ 'cssnano',
19
+ {
20
+ preset: [
21
+ 'default',
22
+ {
23
+ discardComments: {
24
+ removeAll: true,
25
+ },
26
+ },
27
+ ],
28
+ },
29
+ ],
30
+ ],
31
+ };
32
+ };
@@ -1,13 +1,16 @@
1
1
  const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
2
2
  const RtlCssPlugin = require( 'rtlcss-webpack-plugin' );
3
- const { CleanWebpackPlugin } = require( 'clean-webpack-plugin' );
4
3
  const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
4
+ const FileManagerPlugin = require( 'filemanager-webpack-plugin' );
5
+ const SyncDirectoryWebpackPlugin = require( '../helpers/sync-directory-webpack-plugin' );
5
6
  const findFreePort = require( 'find-free-port-sync' );
7
+ const { resolve } = require( 'node:path' );
6
8
  const {
7
9
  Config,
8
10
  defaultConfigWP,
9
11
  modulesConfigWP,
10
12
  } = require( '../helpers/webpack' );
13
+ const root = process.cwd().replace( /\\/g, '/' );
11
14
 
12
15
  const port =
13
16
  process.env.THEME_BLOCK_DEV_SERVER_PORT ||
@@ -28,28 +31,59 @@ const defaultConfig = new Config( defaultConfigWP, 'default', port )
28
31
  } )
29
32
  )
30
33
  .addPlugin(
31
- new CleanWebpackPlugin( {
32
- cleanOnceBeforeBuildPatterns: [
33
- '**/*',
34
- // Each link in the path has been added, because if this is not done,
35
- // the scripts will be separated since it does not fall under the condition !blocks/*/scripts/module.*,
36
- // and accordingly, the module files will be included with it...
37
- '!blocks',
38
- '!blocks/*',
39
- '!blocks/*/scripts',
40
- '!blocks/*/scripts/module.*',
41
- ],
42
- cleanStaleWebpackAssets: false,
43
- } ),
44
- 'before'
45
- )
46
- .addPlugin(
47
- // For styles remove JS and styles .asset.php
48
- new RemoveEmptyScriptsPlugin( {
49
- enabled: ! Config.hasDevServer( defaultConfigWP ),
34
+ new FileManagerPlugin( {
35
+ events: {
36
+ onStart: {
37
+ delete: [
38
+ {
39
+ source: './build/*',
40
+ options: {
41
+ ignore: '**/module.*',
42
+ },
43
+ },
44
+ ],
45
+ },
46
+ },
47
+ runOnceInWatchMode: true,
50
48
  } )
51
49
  );
52
50
 
53
- const modulesConfig = new Config( modulesConfigWP, 'modules' );
51
+ if ( ! Config.hasDevServer( defaultConfigWP ) ) {
52
+ defaultConfig.addPlugin(
53
+ // For styles remove JS and styles .asset.php
54
+ new RemoveEmptyScriptsPlugin()
55
+ );
56
+ } else {
57
+ defaultConfig.addWatch( 'src/blocks/*/scripts/module.{j,t}s' );
58
+ }
59
+
60
+ const modulesConfig = new Config( modulesConfigWP, 'modules' ).addPlugin(
61
+ new FileManagerPlugin( {
62
+ events: {
63
+ onStart: {
64
+ delete: [
65
+ {
66
+ source: '**/module.*',
67
+ options: {},
68
+ },
69
+ ],
70
+ },
71
+ },
72
+ runOnceInWatchMode: true,
73
+ } )
74
+ );
75
+
76
+ if ( process.env.WP_CONTENT_DIR ) {
77
+ const syncDirectory = new SyncDirectoryWebpackPlugin( {
78
+ sourceDir: root,
79
+ targetDir: resolve(
80
+ process.env.WP_CONTENT_DIR,
81
+ 'plugins/theme-blocks'
82
+ ),
83
+ } );
84
+
85
+ defaultConfig.addPlugin( syncDirectory );
86
+ modulesConfig.addPlugin( syncDirectory );
87
+ }
54
88
 
55
89
  module.exports = [ defaultConfig.get(), modulesConfig.get() ];
@@ -0,0 +1,94 @@
1
+ const RtlCssPlugin = require( 'rtlcss-webpack-plugin' );
2
+ const glob = require( 'glob' );
3
+ const path = require( 'path' );
4
+
5
+ require( 'dotenv' ).config( {
6
+ path: [
7
+ path.resolve( process.cwd(), '.env.local' ),
8
+ path.resolve( process.cwd(), '.env' ),
9
+ path.resolve( process.cwd(), '../.env.local' ),
10
+ path.resolve( process.cwd(), '../.env' ),
11
+ path.resolve( process.cwd(), '../../.env.local' ),
12
+ path.resolve( process.cwd(), '../../.env' ),
13
+ ],
14
+ } );
15
+
16
+ const [
17
+ defaultConfig,
18
+ modulesConfig,
19
+ ] = require( '@wordpress/scripts/config/webpack.config.js' );
20
+
21
+ function getEntryName( entryPath ) {
22
+ const entryData = path.parse( entryPath );
23
+ return path
24
+ .normalize(
25
+ path.join(
26
+ path.relative(
27
+ path.join( process.cwd(), 'src' ),
28
+ entryData.dir
29
+ ),
30
+ entryData.name
31
+ )
32
+ )
33
+ .replaceAll( '\\', '/' );
34
+ }
35
+
36
+ function getEntries( globPattern, replace = '' ) {
37
+ return glob
38
+ .sync( globPattern, {
39
+ absolute: true,
40
+ cwd: process.cwd(),
41
+ } )
42
+ .reduce( function ( entries, entryPath ) {
43
+ let name = getEntryName( entryPath ).replace( /^scripts\//, '' );
44
+ if ( replace ) {
45
+ name = name.replace( replace, '' );
46
+ }
47
+
48
+ entries[ name ] = entryPath;
49
+
50
+ return entries;
51
+ }, {} );
52
+ }
53
+
54
+ defaultConfig.entry = {
55
+ ...getEntries( 'src/*.{j,t}s{,x}' ),
56
+ };
57
+
58
+ defaultConfig.plugins.forEach( ( plugin, number ) => {
59
+ if ( plugin instanceof RtlCssPlugin ) {
60
+ defaultConfig.plugins.splice( number, 1 );
61
+ }
62
+ } );
63
+
64
+ modulesConfig.entry = {
65
+ ...getEntries( 'src/scripts/modules/*.{j,t}s', 'modules/' ),
66
+ };
67
+
68
+ defaultConfig.name = 'default';
69
+ modulesConfig.name = 'modules';
70
+
71
+ defaultConfig.output.path = path.resolve( __dirname, 'build/scripts' );
72
+ modulesConfig.output.path = path.resolve( __dirname, 'build/modules' );
73
+
74
+ const reactJSXRuntimeConfig = {
75
+ name: 'react',
76
+ entry: {
77
+ 'react-jsx-runtime': {
78
+ import: 'react/jsx-runtime',
79
+ },
80
+ },
81
+ output: {
82
+ path: path.resolve( __dirname, 'build/polyfill' ),
83
+ filename: 'react-jsx-runtime.js',
84
+ library: {
85
+ name: 'ReactJSXRuntime',
86
+ type: 'window',
87
+ },
88
+ },
89
+ externals: {
90
+ react: 'React',
91
+ },
92
+ };
93
+
94
+ module.exports = [ defaultConfig, modulesConfig, reactJSXRuntimeConfig ];
@@ -1,15 +1,17 @@
1
- const { CleanWebpackPlugin } = require( 'clean-webpack-plugin' );
2
1
  const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
3
2
  const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
4
3
  const RtlCssPlugin = require( 'rtlcss-webpack-plugin' );
5
4
  const ImageMinimizerPlugin = require( 'image-minimizer-webpack-plugin' );
6
- const { resolve } = require( 'node:path' );
5
+ const SyncDirectoryWebpackPlugin = require( '../helpers/sync-directory-webpack-plugin' );
6
+ const FileManagerPlugin = require( 'filemanager-webpack-plugin' );
7
7
  const findFreePort = require( 'find-free-port-sync' );
8
+ const { resolve } = require( 'node:path' );
8
9
  const {
9
10
  Config,
10
11
  defaultConfigWP,
11
12
  modulesConfigWP,
12
13
  } = require( '../helpers/webpack' );
14
+ const root = process.cwd().replace( /\\/g, '/' );
13
15
 
14
16
  const port =
15
17
  process.env.THEME_DEV_SERVER_PORT ||
@@ -23,7 +25,6 @@ const defaultConfig = new Config( defaultConfigWP, 'default', port )
23
25
  .addEntries( 'src/scripts/*.{j,t}s' )
24
26
  .changeRule( '/\\.(sc|sa)ss$/', ( rule ) => {
25
27
  const last = rule.use.length - 1;
26
- const root = process.cwd();
27
28
 
28
29
  rule.use[ last ].options.sassOptions = {
29
30
  includePaths: [ resolve( root, 'src/styles/partials' ) ],
@@ -31,19 +32,21 @@ const defaultConfig = new Config( defaultConfigWP, 'default', port )
31
32
  } )
32
33
  .removePlugin( RtlCssPlugin )
33
34
  .addPlugin(
34
- new CleanWebpackPlugin( {
35
- cleanOnceBeforeBuildPatterns: [
36
- '**/*',
37
- // Each link in the path has been added, because if this is not done,
38
- // the scripts will be separated since it does not fall under the condition !scripts/modules/*,
39
- // and accordingly, the module files will be included with it...
40
- '!scripts',
41
- '!scripts/modules',
42
- '!scripts/modules/*',
43
- ],
44
- cleanStaleWebpackAssets: false,
45
- } ),
46
- 'before'
35
+ new FileManagerPlugin( {
36
+ events: {
37
+ onStart: {
38
+ delete: [
39
+ {
40
+ source: './build/*',
41
+ options: {
42
+ ignore: './build/scripts/modules/**/*',
43
+ },
44
+ },
45
+ ],
46
+ },
47
+ },
48
+ runOnceInWatchMode: true,
49
+ } )
47
50
  )
48
51
  .addPlugin(
49
52
  // For styles remove JS and styles .asset.php
@@ -110,11 +113,35 @@ const defaultConfig = new Config( defaultConfigWP, 'default', port )
110
113
  },
111
114
  ],
112
115
  } )
113
- )
114
- .addWatch( 'src/scripts/modules/*' );
116
+ );
115
117
 
116
118
  const modulesConfig = new Config( modulesConfigWP, 'modules' )
117
119
  .resetEntries()
118
- .addEntries( 'src/scripts/modules/*.{j,t}s' );
120
+ .addEntries( 'src/scripts/modules/*.{j,t}s' )
121
+ .addPlugin(
122
+ new FileManagerPlugin( {
123
+ events: {
124
+ onStart: {
125
+ delete: [
126
+ {
127
+ source: './build/scripts/modules/*',
128
+ options: {},
129
+ },
130
+ ],
131
+ },
132
+ },
133
+ runOnceInWatchMode: true,
134
+ } )
135
+ );
136
+
137
+ if ( process.env.WP_CONTENT_DIR ) {
138
+ const syncDirectory = new SyncDirectoryWebpackPlugin( {
139
+ sourceDir: root,
140
+ targetDir: resolve( process.env.WP_CONTENT_DIR, 'themes/theme-child' ),
141
+ } );
142
+
143
+ defaultConfig.addPlugin( syncDirectory );
144
+ modulesConfig.addPlugin( syncDirectory );
145
+ }
119
146
 
120
147
  module.exports = [ defaultConfig.get(), modulesConfig.get() ];