@lipemat/js-boilerplate 10.13.0 → 10.14.0-beta.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/bin/watch.ts CHANGED
@@ -5,6 +5,7 @@ import chalk from 'chalk';
5
5
  const toWatch = [
6
6
  'config/*.ts',
7
7
  'helpers/*.ts',
8
+ 'lib/*.ts',
8
9
  ];
9
10
 
10
11
  /**
@@ -12,15 +13,15 @@ const toWatch = [
12
13
  */
13
14
  chokidar.watch( toWatch, {ignored: /(^|[\/\\])\../} )
14
15
  .on( 'change', path => {
15
- console.log( chalk.yellowBright( '[watch]' ), `${path} changed` );
16
+ console.debug( chalk.yellowBright( '[watch]' ), `${path} changed` );
16
17
  exec( 'yarn run build', ( err, stdout ) => {
17
18
  if ( err ) {
18
19
  console.error( chalk.red( stdout ) );
19
20
  } else {
20
- console.log( stdout );
21
+ console.debug( stdout );
21
22
  }
22
23
  } );
23
24
  } )
24
25
  .once( 'ready', () => {
25
- console.log( chalk.greenBright( '[watch]' ), 'Watching for changes…' );
26
+ console.debug( chalk.greenBright( '[watch]' ), 'Watching for changes…' );
26
27
  } )
@@ -31,7 +31,7 @@ if ('object' === typeof presetEnv.features && includedPlugins.includes('postcss-
31
31
  /**
32
32
  * Fixes `focus-visible` feature for CSS modules.
33
33
  *
34
- * Only needed if our browsers list includes non-supported browsers
34
+ * Only needed if our browser list includes non-supported browsers
35
35
  * such as Safari 15.3 and below.
36
36
  *
37
37
  * Requires `focus-visible` polyfill to be loaded externally.
@@ -43,7 +43,7 @@ if ( 'object' === typeof presetEnv.features && includedPlugins.includes( 'postcs
43
43
  /**
44
44
  * Fixes `focus-visible` feature for CSS modules.
45
45
  *
46
- * Only needed if our browsers list includes non-supported browsers
46
+ * Only needed if our browser list includes non-supported browsers
47
47
  * such as Safari 15.3 and below.
48
48
  *
49
49
  * Requires `focus-visible` polyfill to be loaded externally.
@@ -83,12 +83,15 @@ const config = {
83
83
  test: /\.pcss$/,
84
84
  use: [
85
85
  'style-loader',
86
+ {
87
+ loader: path_1.default.resolve(__dirname, '../lib/format-css-module-typings.ts'),
88
+ },
86
89
  {
87
90
  loader: '@teamsupercell/typings-for-css-modules-loader',
88
91
  options: {
89
92
  banner: '// Autogenerated by typings-for-css-modules-loader.',
90
93
  disableLocalsExport: true,
91
- prettierConfigFile: path_1.default.resolve(__dirname, '../helpers/.prettierrc.json'),
94
+ formatter: 'none',
92
95
  },
93
96
  },
94
97
  {
@@ -86,12 +86,15 @@ const config: WebpackConfig = {
86
86
  test: /\.pcss$/,
87
87
  use: [
88
88
  'style-loader',
89
+ {
90
+ loader: path.resolve( __dirname, '../lib/format-css-module-typings.ts' ),
91
+ },
89
92
  {
90
93
  loader: '@teamsupercell/typings-for-css-modules-loader',
91
94
  options: {
92
95
  banner: '// Autogenerated by typings-for-css-modules-loader.',
93
96
  disableLocalsExport: true,
94
- prettierConfigFile: path.resolve( __dirname, '../helpers/.prettierrc.json' ),
97
+ formatter: 'none',
95
98
  },
96
99
  },
97
100
  {
@@ -142,12 +142,15 @@ const config = {
142
142
  test: /\.pcss$/,
143
143
  use: [
144
144
  mini_css_extract_plugin_1.default.loader,
145
+ {
146
+ loader: path_1.default.resolve(__dirname, '../lib/format-css-module-typings.ts'),
147
+ },
145
148
  {
146
149
  loader: '@teamsupercell/typings-for-css-modules-loader',
147
150
  options: {
148
151
  banner: '// Autogenerated by typings-for-css-modules-loader.',
149
152
  disableLocalsExport: true,
150
- prettierConfigFile: path_1.default.resolve(__dirname, '../helpers/.prettierrc.json'),
153
+ formatter: 'none',
151
154
  },
152
155
  },
153
156
  {
@@ -145,12 +145,15 @@ const config: WebpackConfig = {
145
145
  test: /\.pcss$/,
146
146
  use: [
147
147
  MiniCssExtractPlugin.loader,
148
+ {
149
+ loader: path.resolve( __dirname, '../lib/format-css-module-typings.ts' ),
150
+ },
148
151
  {
149
152
  loader: '@teamsupercell/typings-for-css-modules-loader',
150
153
  options: {
151
154
  banner: '// Autogenerated by typings-for-css-modules-loader.',
152
155
  disableLocalsExport: true,
153
- prettierConfigFile: path.resolve( __dirname, '../helpers/.prettierrc.json' ),
156
+ formatter: 'none',
154
157
  },
155
158
  },
156
159
  {
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fs_1 = require("fs");
4
+ /**
5
+ * Formats CSS Module typings files.
6
+ *
7
+ * This loader reads the `.pcss.d.ts` file, formats its content, and writes it back.
8
+ * It ensures that the typings are consistent and follow a specific style.
9
+ *
10
+ * Replaces the prettier library with a custom formatting function to avoid
11
+ * unnecessary dependencies and unplugging issues.
12
+ *
13
+ * @link https://webpack.js.org/api/loaders/
14
+ *
15
+ * @param {string} content - The content of the CSS Module file.
16
+ * @param {...[]} args - Additional arguments passed to the loader.
17
+ */
18
+ function formatCssModuleTypings(content, ...args) {
19
+ try {
20
+ const resourcePath = this.resourcePath;
21
+ const typingsPath = resourcePath.replace(/\.pcss$/, '.pcss.d.ts');
22
+ if (!(0, fs_1.existsSync)(typingsPath)) {
23
+ this.callback(null, content, ...args);
24
+ return;
25
+ }
26
+ let typingsContent = (0, fs_1.readFileSync)(typingsPath, 'utf8');
27
+ typingsContent = formatDefinitionContent(typingsContent);
28
+ (0, fs_1.writeFileSync)(typingsPath, typingsContent);
29
+ this.callback(null, content, ...args);
30
+ }
31
+ catch (error) {
32
+ this.emitError(error);
33
+ }
34
+ }
35
+ /**
36
+ * Formats TypeScript content for CSS Module typings.
37
+ *
38
+ * @param {string} content - The content of the TypeScript file to format.
39
+ * @return {string} - The formatted TypeScript content.
40
+ */
41
+ function formatDefinitionContent(content) {
42
+ let formatted = content;
43
+ formatted = formatted.replace(/: string; /g, ': string;');
44
+ formatted = formatted.replace(/^([ \t]*)'([\w-]+)':/gm, function (match, indent, key) {
45
+ if (-1 === key.indexOf('-')) {
46
+ return indent + key + ':';
47
+ }
48
+ return indent + '\'' + key + '\':';
49
+ });
50
+ formatted = formatted.replace(/^( {2})+/gm, function (match) {
51
+ const tabCount = match.length / 2;
52
+ return '\t'.repeat(tabCount);
53
+ });
54
+ formatted = formatted.replace(/\s+$/gm, '');
55
+ formatted = formatted.replace(/^}\ndeclare/gm, '}\n\ndeclare');
56
+ formatted = formatted.replace(/;$\nexport/gm, ';\n\nexport');
57
+ return formatted.trim() + '\n';
58
+ }
59
+ module.exports = formatCssModuleTypings;
60
+ exports.default = formatCssModuleTypings;
@@ -0,0 +1,70 @@
1
+ import type {LoaderContext} from 'webpack';
2
+ import {existsSync, readFileSync, writeFileSync} from 'fs';
3
+
4
+ /**
5
+ * Formats CSS Module typings files.
6
+ *
7
+ * This loader reads the `.pcss.d.ts` file, formats its content, and writes it back.
8
+ * It ensures that the typings are consistent and follow a specific style.
9
+ *
10
+ * Replaces the prettier library with a custom formatting function to avoid
11
+ * unnecessary dependencies and unplugging issues.
12
+ *
13
+ * @link https://webpack.js.org/api/loaders/
14
+ *
15
+ * @param {string} content - The content of the CSS Module file.
16
+ * @param {...[]} args - Additional arguments passed to the loader.
17
+ */
18
+
19
+ function formatCssModuleTypings( this: LoaderContext<Record<string, never>>, content: string, ...args: [] ): void {
20
+ try {
21
+ const resourcePath = this.resourcePath;
22
+ const typingsPath = resourcePath.replace( /\.pcss$/, '.pcss.d.ts' );
23
+
24
+ if ( ! existsSync( typingsPath ) ) {
25
+ this.callback( null, content, ...args );
26
+ return;
27
+ }
28
+
29
+ let typingsContent = readFileSync( typingsPath, 'utf8' );
30
+ typingsContent = formatDefinitionContent( typingsContent );
31
+
32
+ writeFileSync( typingsPath, typingsContent );
33
+
34
+ this.callback( null, content, ...args );
35
+ } catch ( error ) {
36
+ this.emitError( error as Error );
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Formats TypeScript content for CSS Module typings.
42
+ *
43
+ * @param {string} content - The content of the TypeScript file to format.
44
+ * @return {string} - The formatted TypeScript content.
45
+ */
46
+ function formatDefinitionContent( content: string ): string {
47
+ let formatted = content;
48
+
49
+ formatted = formatted.replace( /: string; /g, ': string;' );
50
+ formatted = formatted.replace( /^([ \t]*)'([\w-]+)':/gm, function( match, indent, key ) {
51
+ if ( -1 === key.indexOf( '-' ) ) {
52
+ return indent + key + ':';
53
+ }
54
+ return indent + '\'' + key + '\':';
55
+ } );
56
+
57
+ formatted = formatted.replace( /^( {2})+/gm, function( match ) {
58
+ const tabCount = match.length / 2;
59
+ return '\t'.repeat( tabCount );
60
+ } );
61
+ formatted = formatted.replace( /\s+$/gm, '' );
62
+
63
+ formatted = formatted.replace( /^}\ndeclare/gm, '}\n\ndeclare' );
64
+ formatted = formatted.replace( /;$\nexport/gm, ';\n\nexport' );
65
+
66
+ return formatted.trim() + '\n';
67
+ }
68
+
69
+ module.exports = formatCssModuleTypings;
70
+ export default formatCssModuleTypings;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lipemat/js-boilerplate",
3
- "version": "10.13.0",
3
+ "version": "10.14.0-beta.1",
4
4
  "description": "Dependencies and scripts for a no config JavaScript app",
5
5
  "author": "Mat Lipe",
6
6
  "license": "MIT",
@@ -85,12 +85,11 @@
85
85
  "postcss-preset-env": "^7.8.3",
86
86
  "postcss-scss": "^4.0.4",
87
87
  "postcss-sort-media-queries": "^5.2.0",
88
- "prettier": "^3.1.1",
89
88
  "react-refresh": "^0.14.0",
90
89
  "style-loader": "^3.3.1",
91
90
  "ts-node": "^10.9.1",
92
- "typescript": "^5.5.2",
93
- "webpack": "^5.99.9",
91
+ "typescript": "^5.9.2",
92
+ "webpack": "^5.101.0",
94
93
  "webpack-assets-manifest": "^6.2.1",
95
94
  "webpack-cli": "^6.0.1",
96
95
  "webpack-dev-server": "^5.2.2",
@@ -43,7 +43,7 @@ function analyze() {
43
43
  // Use the default webpack stats configuration.
44
44
  delete webpackConfig.stats;
45
45
 
46
- webpack( webpackConfig ).run( ( err, stats ) => {
46
+ webpack( webpackConfig )?.run( ( err, stats ) => {
47
47
  if ( err ) {
48
48
  throw err;
49
49
  }
package/scripts/dist.ts CHANGED
@@ -1,12 +1,15 @@
1
1
  import {getConfig} from '../helpers/config';
2
- import webpack, {type Configuration, type Stats} from 'webpack';
2
+ import webpack, {type Compiler, type Configuration, type Stats} from 'webpack';
3
3
 
4
4
  process.env.BABEL_ENV = 'production';
5
5
  process.env.NODE_ENV = 'production';
6
6
 
7
7
  const config: Configuration = getConfig( 'webpack.dist' );
8
8
 
9
- const compiler = webpack( config );
9
+ const compiler: Compiler | null = webpack( config );
10
+ if ( null === compiler ) {
11
+ throw new Error( 'Failed to create the webpack compiler.' );
12
+ }
10
13
  compiler.run( ( err: Error | null, stats: Stats ) => {
11
14
  if ( err ) {
12
15
  throw err;
@@ -14,11 +14,11 @@ Validate CSS modules using .d.ts definition files for each CSS module.
14
14
 
15
15
  Usage: lipemat-js-boilerplate validate-css-modules
16
16
 
17
- --help, -h Show help menu.`;
17
+ --help, -h Show the help menu.`;
18
18
 
19
19
  const args = process.argv.slice( 3 );
20
20
  if ( '-h' === args[ 0 ] || '--help' === args[ 0 ] ) {
21
- console.log( help );
21
+ console.debug( help );
22
22
  process.exit( 0 );
23
23
  }
24
24
 
@@ -41,9 +41,9 @@ async function validate() {
41
41
  return rule;
42
42
  } )
43
43
 
44
- // Wait for generation to finish before continuing.
44
+ // Wait for the generation to finish before continuing.
45
45
  await new Promise( ( resolve, reject ) => {
46
- webpack( webpackConfig ).run( ( err, stats ) => {
46
+ webpack( webpackConfig )?.run( ( err, stats ) => {
47
47
  if ( err ) {
48
48
  reject( err );
49
49
  }
@@ -56,12 +56,12 @@ async function validate() {
56
56
  } );
57
57
  } );
58
58
 
59
- console.log( '>>> CSS Module definitions generated.' );
59
+ console.debug( '>>> CSS Module definitions generated.' );
60
60
 
61
61
  // A fresh config for CSS validation.
62
62
  webpackConfig = getConfig( 'webpack.dist' );
63
63
  webpackConfig.stats = 'errors-warnings';
64
- webpack( webpackConfig ).run( ( err, stats ) => {
64
+ webpack( webpackConfig )?.run( ( err, stats ) => {
65
65
  if ( err ) {
66
66
  throw err;
67
67
  }
@@ -70,7 +70,7 @@ async function validate() {
70
70
  console.error( stats.toString( webpackConfig.stats ) );
71
71
  process.exit( 1 );
72
72
  }
73
- console.log( '>>> CSS validation completed.' );
73
+ console.debug( '>>> CSS validation completed.' );
74
74
  } );
75
75
  }
76
76