mod-build 4.0.1-beta.6 → 4.0.1-beta.7a

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/CHANGELOG.md CHANGED
@@ -1,8 +1,3 @@
1
- ## 4.0.1
2
-
3
- - Clean up some plugins into separate files
4
- - Add footAssetsConfig to additionalAssets
5
-
6
1
  ## 4.0.0-beta.5
7
2
 
8
3
  - Updated the `transformIndexHtml` hook to use the `writeBundle` hook in the ViteConfig.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mod-build",
3
- "version": "4.0.1-beta.6",
3
+ "version": "4.0.1-beta.7a",
4
4
  "description": "Share components for S3 sites.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -22,6 +22,7 @@
22
22
  "gulp-tap": "^2.0.0",
23
23
  "husky": "^8.0.3",
24
24
  "lodash.merge": "^4.6.2",
25
+ "lodash.mergewith": "^4.6.2",
25
26
  "sass": "^1.69.5",
26
27
  "vite": "^5.0.0",
27
28
  "vite-plugin-eslint": "^1.8.1",
@@ -1,9 +1,27 @@
1
+ function createScript(asset, location = 'head', env = 'qa.modernize.com') {
2
+ const { src, isLocal, localSrc, useModCdn, defer, async } = asset;
3
+ const script = document.createElement('script');
4
+ script.src = (isLocal ? localSrc : (useModCdn ? `https://${env}/${src}` : src));
5
+ script.async = async;
6
+ script.defer = defer;
7
+ location === 'head' ? document.head.appendChild(script) : document.body.appendChild(script);
8
+ }
9
+
10
+ function createStyle(asset, location = 'head', env = 'qa.modernize.com') {
11
+ const { src, isLocal, localSrc, useModCdn } = asset;
12
+ const link = document.createElement('link');
13
+ link.rel = 'stylesheet';
14
+ link.href = (isLocal ? localSrc : (useModCdn ? `https://${env}/${src}` : src));
15
+ location === 'head' ? document.head.appendChild(link) : document.body.appendChild(link);
16
+ }
17
+
1
18
  export const loadAdditionalAssets = (config) => {
2
- const { page } = config;
3
- const { additionalAssets } = page.headConfig;
19
+ const { page, nodeEnv } = config;
20
+ const { additionalAssets: headAdditionalAssets } = page.headConfig;
21
+ const { additionalAssets: footAdditionalAssets } = page.footAssetsConfig
4
22
 
5
- if (additionalAssets) {
6
- const { afterBegin, beforeEnd } = additionalAssets;
23
+ if (headAdditionalAssets) {
24
+ const { afterBegin, beforeEnd } = headAdditionalAssets;
7
25
 
8
26
  if (afterBegin || beforeEnd) {
9
27
  let additionalHeadAssets = [];
@@ -15,22 +33,28 @@ export const loadAdditionalAssets = (config) => {
15
33
  }
16
34
 
17
35
  additionalHeadAssets.forEach((asset) => {
18
- const { type, src, isLocal, localSrc, useModCdn, defer, async } = asset;
36
+ const { type } = asset;
19
37
  if (type === 'script') {
20
- const script = document.createElement(type);
21
- script.src = (isLocal ? localSrc : (useModCdn ? `https://${config.nodeEnv}/${src}` : src));
22
- script.async = async;
23
- script.defer = defer;
24
- document.head.appendChild(script);
38
+ createScript(asset, 'head', nodeEnv);
25
39
  }
26
40
 
27
41
  if (type === 'style') {
28
- const link = document.createElement('link');
29
- link.rel = 'stylesheet';
30
- link.href = (isLocal ? localSrc : (useModCdn ? `https://${config.nodeEnv}/${src}` : src));
31
- document.head.appendChild(link);
42
+ createStyle(asset, 'head', nodeEnv);
32
43
  }
33
44
  });
34
45
  }
35
46
  }
47
+
48
+ if (footAdditionalAssets) {
49
+ footAdditionalAssets.forEach((asset) => {
50
+ const { type } = asset;
51
+ if (type === 'script') {
52
+ createScript(asset, 'body', nodeEnv);
53
+ }
54
+
55
+ if (type === 'style') {
56
+ createStyle(asset, 'body', nodeEnv);
57
+ }
58
+ });
59
+ }
36
60
  }
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs";
2
+ import mergeWith from "lodash.mergewith";
2
3
  import merge from "lodash.merge";
3
4
  import gulpHandlebarsFileInclude from "gulp-handlebars-file-include";
4
- import { glob } from 'glob'
5
5
  import { defaultSettings } from "../data/config.js";
6
6
  import footer from '../data/footer.js';
7
7
  import { handlebarsHelpers } from "./utils.js";
@@ -44,13 +44,23 @@ export const updateConfig = async (config) => {
44
44
  ...{ isLocal, nodeEnv, buildPath }
45
45
  };
46
46
 
47
+ const customizer = (objValue, srcValue) => {
48
+ // If the key exists in parsedConfig but not in mergedConfig, drop it from the merged result
49
+ if (srcValue === undefined) {
50
+ return undefined;
51
+ }
52
+
53
+ // Otherwise, use the default merging behavior
54
+ return merge(objValue, srcValue); // or you can return merge(objValue, srcValue) to merge nested objects
55
+ };
56
+
47
57
  const checkForTempConfig = () => {
48
58
  return new Promise((resolve) => {
49
59
  tempConfigExists = fs.existsSync(`src/.tmp/config.json`);
50
60
 
51
61
  const configData = fs.readFileSync('src/.tmp/config.json', 'utf8');
52
62
  const parsedConfig = JSON.parse(configData);
53
- mergedConfig = merge(parsedConfig, mergedConfig);
63
+ mergedConfig = mergeWith(parsedConfig, mergedConfig, customizer);
54
64
 
55
65
  resolve();
56
66
  });
@@ -80,29 +90,4 @@ export const updateConfig = async (config) => {
80
90
  gulpHandlebarsFileInclude(mergedConfig, { handlebarsHelpers, maxRecursion: 500 });
81
91
  }
82
92
  }
83
- }
84
-
85
- export const replaceSrcPaths = (config) => {
86
- return {
87
- name: 'replace-src-paths',
88
- writeBundle: async () => {
89
- const files = glob.sync(defaultSettings.distFolder + '/**/index.html');
90
- for (const file of files) {
91
- let html = await fs.promises.readFile(file, 'utf-8');
92
- let updatedHtml = '';
93
-
94
- if (config.pathSubdirectory && config.pathSubdirectory.length > 0) {
95
- updatedHtml = html
96
- .replace(/(src|data-load)="\/?src\//g, `$1="${config.pathSubdirectory}`)
97
- .replace(/(src|srcset|data-load|href)="\/?assets\//g, `$1="${config.pathSubdirectory}assets/`)
98
- .replace(/(src|data-load|href)="\/?resources\//g, `$1="${config.pathSubdirectory}resources/`)
99
- } else {
100
- updatedHtml = html.replace(/(src|data-load)="\/?src\//g, '$1="/');
101
- }
102
-
103
- await fs.promises.writeFile(file, updatedHtml);
104
- }
105
- },
106
- apply: 'build',
107
- }
108
93
  }
package/tasks/grab-cdn.js CHANGED
@@ -13,8 +13,8 @@ async function duplicateHtmlFilestoHbs(externalResources) {
13
13
 
14
14
  if (fileExtension === '.html') {
15
15
  const newFileName = fileName.replace('.html', '.hbs');
16
- const sourceFilePath = path.join('src', destPath, fileName);
17
- const destinationFilePath = path.join('src', destPath, newFileName);
16
+ const sourceFilePath = path.join(destPath, fileName);
17
+ const destinationFilePath = path.join(destPath, newFileName);
18
18
 
19
19
  if (fs.existsSync(sourceFilePath) && !fs.existsSync(destinationFilePath)) {
20
20
  fs.copyFileSync(sourceFilePath, destinationFilePath);
@@ -29,15 +29,15 @@ function streamToDestination(inputPath, destPath, fileName) {
29
29
  const url = `https://${defaultSettings.nodeEnv}${inputPath}`;
30
30
 
31
31
  return new Promise(resolve => {
32
- if (!fs.existsSync(`src${destPath}`)) {
33
- fs.mkdirSync(`src${destPath}`, { recursive: true });
32
+ if (!fs.existsSync(`${destPath}`)) {
33
+ fs.mkdirSync(`${destPath}`, { recursive: true });
34
34
  }
35
35
 
36
36
  // if file exists, do not create it again
37
- if (fs.existsSync(`src${destPath}${fileName}`)) {
37
+ if (fs.existsSync(`${destPath}${fileName}`)) {
38
38
  resolve();
39
39
  } else {
40
- const writer = createWriteStream(`src${destPath}${fileName}`);
40
+ const writer = createWriteStream(`${destPath}${fileName}`);
41
41
  const options = {
42
42
  url,
43
43
  method: 'get',
@@ -49,7 +49,7 @@ function streamToDestination(inputPath, destPath, fileName) {
49
49
  throw new Error(`${resp.status}: Error while fetching ${url}`);
50
50
  }
51
51
  resp.data.pipe(writer);
52
- console.log(`src${destPath}${fileName} created...`);
52
+ console.log(`${destPath}${fileName} created...`);
53
53
  return finished(writer);
54
54
  }).then(() => {
55
55
  resolve();
@@ -58,58 +58,53 @@ function streamToDestination(inputPath, destPath, fileName) {
58
58
  resolve();
59
59
  });
60
60
  }
61
- });
62
- }
61
+ });
62
+ }
63
63
 
64
64
  export default async function(config) {
65
- const { nodeEnv, isLocal } = defaultSettings;
65
+ const { nodeEnv, srcFolder, publicFolder } = defaultSettings;
66
66
  const { isQSPage, isWhiteLabel, page } = config;
67
67
  const themeFile = page?.themeFile;
68
68
  const cssThemes = page?.cssThemes;
69
69
  const isModWhiteLabel = isWhiteLabel && !isQSPage
70
- const domainHasModernize = config?.domain?.indexOf('modernize') > -1;
70
+ const domainHasModernize = config?.domain?.indexOf('modernize') > -1 && !(config?.domain?.includes('bestcompany'));
71
71
 
72
72
  let externalResources;
73
73
  // listing of Static Resources sub-path and site destination paths
74
74
  // key: inputPath, value: destPath
75
75
 
76
76
  externalResources = {
77
- '/quote/resources/mod-site/templates/scripts/trusted-form.html': ['/templates/scripts/', 'trusted-form.html'],
78
- '/quote/resources/data/tcpa.json': ['/resources/data/', 'tcpa.json'],
77
+ '/quote/resources/mod-site/templates/scripts/trusted-form.html': [`${publicFolder}/resources/scripts/`, 'trusted-form.html'],
78
+ '/quote/resources/data/tcpa.json': [`${srcFolder}/resources/data/`, 'tcpa.json'],
79
79
  };
80
80
 
81
81
  // grab footer modals and place under the resources
82
82
  Object.assign(externalResources, {
83
- '/quote/resources/shared-resources/templates/modals/about/': ['/resources/templates/modals/about/', 'index.html'],
84
- '/quote/resources/shared-resources/templates/modals/privacy/': ['/resources/templates/modals/privacy/', 'index.html'],
85
- '/quote/resources/shared-resources/templates/modals/terms/': ['/resources/templates/modals/terms/', 'index.html'],
86
- '/quote/resources/shared-resources/templates/modals/contact-us/': ['/resources/templates/modals/contact-us/', 'index.html'],
87
- '/quote/resources/shared-resources/templates/modals/faq/': ['/resources/templates/modals/faq/', 'index.html']
83
+ '/quote/resources/shared-resources/templates/modals/about/': [`${srcFolder}/resources/templates/modals/about/`, 'index.html'],
84
+ '/quote/resources/shared-resources/templates/modals/privacy/': [`${srcFolder}/resources/templates/modals/privacy/`, 'index.html'],
85
+ '/quote/resources/shared-resources/templates/modals/terms/': [`${srcFolder}/resources/templates/modals/terms/`, 'index.html'],
86
+ '/quote/resources/shared-resources/templates/modals/contact-us/': [`${srcFolder}/resources/templates/modals/contact-us/`, 'index.html'],
87
+ '/quote/resources/shared-resources/templates/modals/faq/': [`${srcFolder}/resources/templates/modals/faq/`, 'index.html']
88
88
  })
89
89
 
90
- Object.assign(externalResources, {'/quote/resources/mod-site/templates/scripts/jornaya.html': ['/templates/scripts/', 'jornaya.html']});
90
+ Object.assign(externalResources, {'/quote/resources/mod-site/templates/scripts/jornaya.html': [`${publicFolder}/resources/scripts/`, 'jornaya.html']});
91
91
 
92
92
  if (isModWhiteLabel || domainHasModernize) {
93
- Object.assign(externalResources, {'/quote/resources/mod-site/templates/scripts/recaptcha.html': ['/templates/scripts/', 'recaptcha.html']});
93
+ Object.assign(externalResources, {'/quote/resources/mod-site/templates/scripts/recaptcha.html': [`${publicFolder}/resources/scripts/`, 'recaptcha.html']});
94
94
  }
95
95
 
96
96
  if (cssThemes && cssThemes.length > 0) {
97
97
  cssThemes.forEach(theme => {
98
98
  const themeFileName = `_${theme}.scss`;
99
99
  const themePath = `/quote/resources/shared-resources/styles/themes/${themeFileName}`;
100
- Object.assign(externalResources, {[themePath]: ['/resources/styles/themes/', themeFileName]});
100
+ Object.assign(externalResources, {[themePath]: [`${srcFolder}/resources/styles/themes/`, themeFileName]});
101
101
  });
102
102
  }
103
103
 
104
- // local dev files
105
- const remoteFilesForLocalDev = {
106
- '/quote/resources/mod-site/scripts/vendor/maxmind-geoip2.js': ['/temp/scripts/', 'maxmind-geoip2.js']
107
- };
108
-
109
104
  // theme JSON
110
105
  if (isQSPage && themeFile) {
111
106
  Object.assign(externalResources, {
112
- [`/quote/resources/data/themes/${themeFile}.json`]: ['/data/', 'theme.json']
107
+ [`/quote/resources/data/themes/${themeFile}.json`]: [`${srcFolder}/data/`, 'theme.json']
113
108
  });
114
109
  }
115
110
 
@@ -117,11 +112,6 @@ export default async function(config) {
117
112
  throw new Error('Missing environment variables. Did you start with gulp instead of npm run...?');
118
113
  }
119
114
 
120
- // only save modform/utils if its local development
121
- if (isLocal) {
122
- externalResources = { ...externalResources, ...remoteFilesForLocalDev };
123
- }
124
-
125
115
  const filesPromiseMap = Object.keys(externalResources).map(async key => {
126
116
  const destinationPath = externalResources[key][0];
127
117
  const fileName = externalResources[key][1];
package/vite.config.js CHANGED
@@ -4,8 +4,10 @@ import eslint from 'vite-plugin-eslint'
4
4
  import { defaultSettings } from './src/data/config.js'
5
5
  import { defineConfig } from 'vite'
6
6
  import { startModBuild } from './tasks/serve.js'
7
- import { updateConfig, replaceSrcPaths, STATIC_COPY_TARGETS } from './src/scripts/plugins.js'
7
+ import { updateConfig, STATIC_COPY_TARGETS } from './src/scripts/plugins.js'
8
8
  import { viteStaticCopy } from 'vite-plugin-static-copy'
9
+ import { glob } from 'glob'
10
+ import fs from "node:fs";
9
11
 
10
12
  export default defineConfig((config) => ({
11
13
  build: {
@@ -39,7 +41,28 @@ export default defineConfig((config) => ({
39
41
  apply: 'serve',
40
42
  enforce: 'post'
41
43
  },
42
- replaceSrcPaths(config)
44
+ {
45
+ name: 'replace-src-paths',
46
+ writeBundle: async () => {
47
+ const files = glob.sync(defaultSettings.distFolder + '/**/index.html');
48
+ for (const file of files) {
49
+ let html = await fs.promises.readFile(file, 'utf-8');
50
+ let updatedHtml = '';
51
+
52
+ if (config.pathSubdirectory && config.pathSubdirectory.length > 0) {
53
+ updatedHtml = html
54
+ .replace(/(src|data-load)="\/?src\//g, `$1="${config.pathSubdirectory}`)
55
+ .replace(/(src|srcset|data-load|href)="\/?assets\//g, `$1="${config.pathSubdirectory}assets/`)
56
+ .replace(/(src|data-load|href)="\/?resources\//g, `$1="${config.pathSubdirectory}resources/`)
57
+ } else {
58
+ updatedHtml = html.replace(/(src|data-load)="\/?src\//g, '$1="/');
59
+ }
60
+
61
+ await fs.promises.writeFile(file, updatedHtml);
62
+ }
63
+ },
64
+ apply: 'build',
65
+ }
43
66
  ],
44
67
  define: {
45
68
  'process.env': process.env