@caweb/webpack 1.6.5 → 1.6.7

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.
@@ -7,7 +7,7 @@ export default function replace(value, search, replace, options){
7
7
  }
8
8
 
9
9
  // if nested content exists
10
- if( value.fn && value.fn().length ){
10
+ if( value && value.fn && value.fn().length ){
11
11
  response += value.fn().replace(new RegExp(search, "g"), replace);
12
12
  }
13
13
 
package/lib/args.js CHANGED
@@ -25,9 +25,15 @@ let { values: flags } = parseArgs( {
25
25
  allowPositionals: true,
26
26
  allowNegative: true,
27
27
 
28
- // in order for the flags to work in the cli's afterStart hook,
29
- // we need to set the options that are used in the start command
28
+ // in order for the flags to work with the cli's params,
29
+ // we need to set the options
30
30
  options: {
31
+ template: {
32
+ type: 'string',
33
+ },
34
+ scheme: {
35
+ type: 'string',
36
+ },
31
37
  cwd: {
32
38
  type: 'string',
33
39
  },
@@ -0,0 +1,69 @@
1
+ // This handlebars assumes we are using the @caweb/template as the fallback for any partials that are not found in the application. This allows us to use the default template partials without having to copy them into the application. It also allows us to use any custom partials that the application may have without having to worry about them being overwritten by the default template partials.
2
+ import Handlebars from 'handlebars';
3
+ import path from 'path';
4
+ import fs from 'fs';
5
+ import { fileURLToPath } from 'url';
6
+ import { createRequire } from 'module';
7
+
8
+ const currentPath = path.dirname(fileURLToPath(import.meta.url));
9
+ const require = createRequire(import.meta.url);
10
+
11
+ let appPath = process.cwd();
12
+ let fallbackPath = path.join(appPath, 'node_modules', '@caweb', 'template');
13
+
14
+ let templatePartials = {
15
+ 'branding': 'semantics/branding.html',
16
+ 'footer': 'semantics/footer.html',
17
+ 'header': 'semantics/header.html',
18
+ 'mobileControls': 'semantics/mobile-controls.html',
19
+ 'navFooter': 'semantics/nav-footer.html',
20
+ 'navHeader': 'semantics/nav-header.html',
21
+ 'utilityHeader': 'semantics/utility-header.html',
22
+ 'head': 'semantics/head.html',
23
+ 'alert': 'components/alert/alert.html',
24
+ 'searchForm': 'forms/search.html'
25
+ }
26
+
27
+ // Register partials.
28
+ Object.entries(templatePartials).forEach(
29
+ ([p, f]) => {
30
+ let partial = false;
31
+
32
+ // if the application has a partial with the same name as the template partials we use that instead of the default template partial
33
+ if( fs.existsSync( path.join(appPath, f) ) ){
34
+ partial = path.join(appPath, f);
35
+ // the fallback path exists we use that
36
+ } else if( fs.existsSync( path.join(fallbackPath, f) ) ){
37
+ partial = path.join(fallbackPath, f);
38
+ }
39
+
40
+ if( partial ) {
41
+ Handlebars.registerPartial(p, fs.readFileSync(partial).toString() );
42
+ }
43
+ } );
44
+
45
+ // Register helpers.
46
+ fs.readdirSync( path.join(currentPath, '..', 'helpers'), { recursive:true } )
47
+ .filter( (file) => file.endsWith('.js') )
48
+ .filter( Boolean )
49
+ .map( (file) => {
50
+ let name = path.basename(file).replace('.js', '');
51
+ let helper = require( path.join(currentPath, '..', 'helpers', file) ).default;
52
+
53
+ Handlebars.registerHelper( name, helper );
54
+ } );
55
+
56
+ // Register customer helpers from the application if they exist.
57
+ if( fs.existsSync( path.join(appPath, 'helpers') ) ){
58
+ fs.readdirSync( path.join(appPath, 'helpers'), { recursive:true } )
59
+ .filter( (file) => file.endsWith('.js') )
60
+ .filter( Boolean )
61
+ .map( (file) => {
62
+ let name = path.basename(file).replace('.js', '');
63
+ let helper = require( path.join(appPath, 'helpers', file) ).default;
64
+
65
+ Handlebars.registerHelper( name, helper );
66
+ } );
67
+ }
68
+
69
+ export default Handlebars;
package/lib/server.js CHANGED
@@ -97,6 +97,18 @@ let devServer = {
97
97
  liveReload: true,
98
98
  hot: true,
99
99
 
100
+ historyApiFallback: {
101
+ index: '/404.html',
102
+ rewrites: [
103
+ // Rewrite any request that doesn't have a file extension to your 404 page
104
+ // This is a simple regex; adjust as needed
105
+ {
106
+ from: /^\/.*[^.][^/]*$/, // Matches paths without file extensions
107
+ to: '/404.html',
108
+ },
109
+ ],
110
+ },
111
+
100
112
  // watchFiles
101
113
  // we watch all folders in the app except build, node_modules and src
102
114
  watchFiles: {
@@ -18,7 +18,7 @@ import { getArgVal } from './args.js';
18
18
  // Wordpress ignores the webpack --mode flag
19
19
  // if the flag is passed we use that mode
20
20
  // otherwise use whatever Wordpress is using
21
- let mode = getArgVal('--mode') ? getArgVal('--mode') : baseConfig.mode;
21
+ let mode = getArgVal('mode') ? getArgVal('mode') : baseConfig.mode;
22
22
  let isProduction = mode === 'production';
23
23
 
24
24
  // Update some of the default WordPress webpack rules.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caweb/webpack",
3
- "version": "1.6.5",
3
+ "version": "1.6.7",
4
4
  "description": "CAWebPublishing Webpack Configuration",
5
5
  "main": "webpack.config.js",
6
6
  "files": [
@@ -47,7 +47,7 @@
47
47
  "html-webpack-skip-assets-plugin": "^1.0.4",
48
48
  "thread-loader": "^4.0.4",
49
49
  "ts-loader": "^9.5.4",
50
- "webpack": "^5.105.1",
50
+ "webpack": "^5.105.2",
51
51
  "webpack-cli": "^6.0.1",
52
52
  "webpack-dev-server": "^5.2.3",
53
53
  "webpack-merge": "^6.0.1",
@@ -15,7 +15,7 @@ import Handlebars from 'handlebars';
15
15
  /**
16
16
  * Internal Dependencies
17
17
  */
18
- import { getArgVal } from '../lib/args.js';
18
+ import { getArgVal, flags } from '../lib/args.js';
19
19
 
20
20
  // this is the path to the current project directory
21
21
  const appPath = process.cwd();
@@ -36,51 +36,15 @@ let caweb = deepmerge( testCaweb, defaultCaweb );
36
36
 
37
37
  let templatePath = path.join(appPath, 'node_modules', '@caweb', 'template');
38
38
  let template = getArgVal( 'template', path.join(templatePath, 'patterns', 'default.html') );
39
- let searchTemplate = getArgVal( 'search-template', path.join(templatePath, 'patterns', 'search.html') );
40
-
41
39
  let scheme = getArgVal( 'scheme', 'oceanside' );
42
40
  let favicon = caweb?.site?.favicon ?? null;
43
41
 
44
- // // Additional pages directory
45
- let basePageDir = path.join(appPath, 'content', 'pages');
46
-
47
- let additionalPages = ! fs.existsSync( basePageDir ) ? [] :
48
- fs.readdirSync( basePageDir, { withFileTypes: true, recursive: true } )
49
- .filter( dirent => dirent.isFile() && (dirent.name.endsWith('.html') || dirent.name.endsWith('.handlebars')) )
50
- .map( ( dirent ) => {
51
-
52
- let fileTemplate = path.join( dirent.parentPath, dirent.name );
53
-
54
- // replace .html, uppercase the first letter of each word
55
- // this is to make sure the title is readable
56
- // and not just a file name
57
- let title = dirent.name.replace('.html', '').replace(/\b\w/g, c => c.toUpperCase());
58
- let content = fs.readFileSync( fileTemplate, 'utf-8' );
59
- let data = {
60
- ...caweb.site,
61
- scheme
62
- };
63
- let compiler = Handlebars.compile( content );
64
- let compiledContent = compiler(data);
65
-
66
- return new HtmlWebpackPlugin({
67
- template,
68
- filename: fileTemplate.replace(basePageDir, ''),
69
- title,
70
- templateParameters: {
71
- ...caweb.site, // we spread the site data found in the caweb.json file
72
- scheme,
73
- partial: compiledContent,
74
- },
75
-
76
- });
77
- });
78
42
 
79
43
  export default {
80
44
  plugins: [
81
45
  // this plugin generates the main landing page using the template found in patterns/index.html
82
46
  new HtmlWebpackPlugin({
83
- template,
47
+ template: path.resolve(template),
84
48
  favicon,
85
49
  templateParameters: {
86
50
  ...caweb.site, // we spread the site data found in the caweb.json file
@@ -88,19 +52,5 @@ export default {
88
52
  }
89
53
  }),
90
54
 
91
- // this plugin generates Search Results page using the template found in patterns/search.html
92
- caweb?.site?.google?.search ? new HtmlWebpackPlugin({
93
- template: searchTemplate,
94
- favicon,
95
- filename: 'serp.html',
96
- title: 'Search Results Page',
97
- templateParameters: {
98
- ...caweb.site, // we spread the site data found in the caweb.json file
99
- scheme
100
- },
101
- }) : false,
102
-
103
- // this plugin generates additional pages under content/pages directory using the template found in patterns/index.html
104
- ...additionalPages
105
55
  ].filter(Boolean)
106
56
  };
package/webpack.config.js CHANGED
@@ -31,7 +31,7 @@ import {HtmlWebpackSkipAssetsPlugin} from 'html-webpack-skip-assets-plugin';
31
31
  /**
32
32
  * Internal dependencies
33
33
  */
34
- import { flags, flagExists, getArgVal, addFlag } from './lib/args.js';
34
+ import { getArgVal } from './lib/args.js';
35
35
  import handlebarsLoaderOptions from './lib/loader.js';
36
36
  import { addToServer, getServer, updateTarget } from './lib/server.js';
37
37