@xh/hoist-dev-utils 8.0.0-SNAPSHOT.1710791660398 → 8.1.0

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.
@@ -81,6 +81,7 @@ try {
81
81
  * @param {Object} [env.manifestConfig] - override values for manifest.json file. This controls options related to
82
82
  * adding a mobile app to a device home screen, as well as "installing" an app via Chrome's "create shortcut"
83
83
  * option. See https://developer.mozilla.org/en-US/docs/Web/Manifest for options.
84
+ * @param {string} [env.preloadBackgroundColor] - background color to use for the preloader spinner. Defaults to white.
84
85
  * @param {string[]} [env.targetBrowsers] - array of browserslist queries specifying target browsers for Babel and CSS
85
86
  * transpiling and processing.
86
87
  * @param {Object} [env.babelPresetEnvOptions] - options to spread onto / override defaults passed here to the Babel
@@ -139,6 +140,7 @@ async function configureWebpack(env) {
139
140
  parseChangelog = env.parseChangelog !== false,
140
141
  favicon = env.favicon || null,
141
142
  manifestConfig = env.manifestConfig || {},
143
+ preloadBackgroundColor = env.preloadBackgroundColor || 'white',
142
144
  stats = env.stats || 'errors-only',
143
145
  infrastructureLoggingLevel = env.infrastructureLoggingLevel || 'error',
144
146
  targetBrowsers = env.targetBrowsers || [
@@ -226,9 +228,9 @@ async function configureWebpack(env) {
226
228
  // unprocessed/unbundled and injected into HTML index files prior to any bundles.
227
229
  const preflightScriptPath = path.resolve(hoistPath, 'static/preflight.js');
228
230
 
229
- // Resolve path to spinner image included in HR >= 43 to prep for copy into public assets.
231
+ // Resolve path to preload spinner image to prep for copy into public assets.
230
232
  // Displayed by generated HTML index page while JS app downloads and starts.
231
- const preloadSpinnerPath = path.resolve(hoistPath, 'static/spinner.png');
233
+ const preloadSpinnerPath = path.resolve(hoistDevUtilsPath, 'static/spinner.png');
232
234
 
233
235
  // Tell webpack where to look for modules when resolving imports - this is the key to getting
234
236
  // inlineHoist mode to look in within the checked-out hoist-react project at hoistPath.
@@ -301,7 +303,7 @@ async function configureWebpack(env) {
301
303
 
302
304
  // Resolve app entry points - one for each file within src/apps/ - to create bundles below.
303
305
  const appDirPath = path.resolve(srcPath, 'apps'),
304
- apps = fs
306
+ clientApps = fs
305
307
  .readdirSync(appDirPath)
306
308
  .filter(f => f.endsWith('.js') || f.endsWith('.ts'))
307
309
  .map(f => {
@@ -310,20 +312,22 @@ async function configureWebpack(env) {
310
312
  path: path.resolve(appDirPath, f)
311
313
  };
312
314
  }),
313
- appNames = apps.map(it => it.name),
314
- chunkNames = getChunkCombinations(appNames.sort());
315
+ clientAppNames = clientApps.map(it => it.name);
315
316
 
316
317
  // Build Webpack entry config, with keys for each JS app to be bundled.
317
318
  const appEntryPoints = {};
318
- apps.forEach(app => {
319
+ clientApps.forEach(clientApp => {
319
320
  // Ensure core-js and regenerator-runtime both imported for every app bundle - they are
320
321
  // specified as dependencies by Hoist and imported once in its polyfills.js file.
321
- appEntryPoints[app.name] = [path.resolve(hoistPath, 'static/polyfills.js'), app.path];
322
+ appEntryPoints[clientApp.name] = [
323
+ path.resolve(hoistPath, 'static/polyfills.js'),
324
+ clientApp.path
325
+ ];
322
326
  });
323
327
 
324
328
  logSep();
325
329
  logMsg('🎁 App bundle entry points:');
326
- appNames.forEach(it => logMsg(` > ${it}`));
330
+ clientAppNames.forEach(it => logMsg(` > ${it}`));
327
331
  logSep();
328
332
  logMsg('🤕 Something going wrong?');
329
333
  logMsg(' > support@xh.io');
@@ -337,11 +341,7 @@ async function configureWebpack(env) {
337
341
  entry: appEntryPoints,
338
342
 
339
343
  output: {
340
- // Output built assets in directories per entry point / chunk.
341
- // Use chunkhash in prod to get distinct hashes for app vs. common chunks (throws error in dev - investigate)
342
- filename: prodBuild
343
- ? '[name]/[name].[chunkhash:8].js'
344
- : '[name]/[name].[fullhash:8].js',
344
+ filename: '[name].[chunkhash:8].js',
345
345
  path: outPath,
346
346
  // (URL) path on which fully built app is served - i.e. root context
347
347
  publicPath: contextRoot,
@@ -359,15 +359,8 @@ async function configureWebpack(env) {
359
359
  sideEffects: false,
360
360
 
361
361
  // Produce chunks for any shared imports across JS apps.
362
- // Chunks are named by the JS app entry points to which they belong, concatenated with '~',
363
- // e.g. `app.js`, `admin.js`, and `admin~app.js` for code shared by both apps.
364
362
  splitChunks: {
365
- chunks: 'all',
366
- name: (module, chunks) =>
367
- chunks
368
- .sort()
369
- .map(it => it.name)
370
- .join('~')
363
+ chunks: 'all'
371
364
  },
372
365
 
373
366
  // Improved debugging with readable module/chunk names.
@@ -617,7 +610,7 @@ async function configureWebpack(env) {
617
610
  xhAppBuild: JSON.stringify(appBuild),
618
611
  xhBaseUrl: JSON.stringify(baseUrl),
619
612
  xhBuildTimestamp: buildDate.getTime(),
620
- xhClientApps: JSON.stringify(appNames),
613
+ xhClientApps: JSON.stringify(clientAppNames),
621
614
  xhIsDevelopmentMode: !prodBuild
622
615
  }),
623
616
 
@@ -628,8 +621,8 @@ async function configureWebpack(env) {
628
621
  contextRegExp: /moment$/
629
622
  }),
630
623
 
631
- // Copy preflight script and spinner provided by HR, plus entire /client-app/public
632
- // directory into the build output.
624
+ // Copy preflight script provided by HR, preload spinner included here in DU,
625
+ // plus entire /client-app/public directory into the build output.
633
626
  new CopyWebpackPlugin({
634
627
  patterns: _.compact([
635
628
  {from: preflightScriptPath, to: 'public'},
@@ -641,17 +634,44 @@ async function configureWebpack(env) {
641
634
  }),
642
635
 
643
636
  // Generate HTML index pages - one per JS app.
644
- ...appNames.map(jsAppName => {
637
+ ...clientAppNames.map(clientAppName => {
645
638
  return new HtmlWebpackPlugin({
646
639
  title: appName,
647
640
  favicon: favicon,
648
- // Note: HTML template is sourced from hoist-react.
649
- template: path.resolve(hoistPath, `static/index-manifest.html`),
650
- filename: `${jsAppName}/index.html`,
651
- // Only include chunks that contain the js app name
652
- chunks: chunkNames.filter(
653
- it => it.startsWith(jsAppName) || it.includes('~' + jsAppName)
654
- ),
641
+ template: path.resolve(hoistDevUtilsPath, `static/index.html`),
642
+ filename: `${clientAppName}/index.html`,
643
+
644
+ // Take 0 chunks from plugin, because we collect just the ones for the jsAppName
645
+ // below in templateParameters
646
+ chunks: [],
647
+
648
+ // No need to ever cache here, either in production or development
649
+ cache: false,
650
+
651
+ // This will provide the html tag strings for just the css and js that jsAppName uses.
652
+ templateParameters: (compilation, assets, assetTags, options) => {
653
+ const {styleTags, scriptTags} = getFileDependenciesByEntrypoint(
654
+ compilation,
655
+ clientAppName
656
+ );
657
+
658
+ return {
659
+ // Base output recommended by plugin example:
660
+ // https://github.com/jantimon/html-webpack-plugin/blob/main/examples/template-parameters/webpack.config.js
661
+ compilation,
662
+ webpackConfig: compilation.options,
663
+ htmlWebpackPlugin: {
664
+ tags: assetTags,
665
+ files: assets,
666
+ options
667
+ },
668
+ // XH additions
669
+ styleTags,
670
+ scriptTags,
671
+ clientAppName,
672
+ preloadBackgroundColor
673
+ };
674
+ },
655
675
  // No need to minify the HTML itself
656
676
  minify: false,
657
677
  // Flag read within template file to include apple icon.
@@ -659,20 +679,21 @@ async function configureWebpack(env) {
659
679
  });
660
680
  }),
661
681
 
662
- // Create a manifest.json. The icon choices here work with the favicon provided
682
+ // Create a manifest.json for each app. The icon choices here work with the favicon provided
663
683
  // to HtmlWebpackPlugin above to match the spec here:
664
684
  // https://evilmartians.com/chronicles/how-to-favicon-in-2021-six-files-that-fit-most-needs
665
- new HoistManifestPlugin({
666
- name: appName,
667
- short_name: appName,
668
- description: `${appName} - ${appVersion}`,
669
- display: 'standalone',
670
- orientation: 'any',
671
- start_url: '/',
672
- background_color: '#ffffff',
673
- theme_color: '#212121', // off-black from default `--xh-black` CSS var
674
- icons: manifestIcons,
675
- ...manifestConfig
685
+ ...clientAppNames.map(clientAppName => {
686
+ return new HoistManifestPlugin(clientAppName, {
687
+ name: appName,
688
+ short_name: appName,
689
+ description: `${appName} - ${appVersion}`,
690
+ display: 'standalone',
691
+ orientation: 'any',
692
+ background_color: preloadBackgroundColor, // ignored by Safari, but also used within index.html
693
+ theme_color: '#212121', // off-black from default `--xh-black` CSS var
694
+ icons: manifestIcons,
695
+ ...manifestConfig
696
+ });
676
697
  }),
677
698
 
678
699
  // Insert a script tag for the (unbundled) preflight script, before all other scripts.
@@ -720,7 +741,7 @@ async function configureWebpack(env) {
720
741
  : {
721
742
  host: devHost,
722
743
  port: devWebpackPort,
723
- hot: true,
744
+ hot: false, // Hot module replacement is not currently supported by Hoist, but live reload is.
724
745
  client: {overlay: devClientOverlay},
725
746
  server:
726
747
  devHttps === true
@@ -731,10 +752,11 @@ async function configureWebpack(env) {
731
752
  open: env.devServerOpenPage ? [env.devServerOpenPage] : false,
732
753
  // Support HTML5 history routes for apps, with /appName/ as the base route for each
733
754
  historyApiFallback: {
734
- rewrites: appNames.map(appName => {
755
+ rewrites: clientAppNames.map(appName => {
735
756
  return {
736
757
  from: new RegExp(`^/${appName}`),
737
- to: `/${appName}/index.html`
758
+ // helps cache busting during live reload in development
759
+ to: `/${appName}/index.html?_=${Date.now()}`
738
760
  };
739
761
  })
740
762
  }
@@ -746,7 +768,15 @@ async function configureWebpack(env) {
746
768
  // Implementation
747
769
  //------------------------
748
770
  class HoistManifestPlugin {
749
- constructor(content = {}) {
771
+ constructor(clientAppName, content = {}) {
772
+ this.clientAppName = clientAppName;
773
+
774
+ // We create one of these per clientApp. Default start_url to the clientApp's root, to bring user back to the
775
+ // clientApp from which they added the bookmark without any need for redirects, respecting possible override.
776
+ if (!content.start_url) {
777
+ content = {...content, start_url: `/${clientAppName}/`};
778
+ }
779
+
750
780
  this.content = content;
751
781
  }
752
782
 
@@ -763,9 +793,11 @@ class HoistManifestPlugin {
763
793
  stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
764
794
  },
765
795
  () => {
796
+ // Emit client-app specific manifest.json within /public, to avoid issues with deeper routes
797
+ // and relative paths. This is picked up by this project's /static/index.html template.
766
798
  compilation.emitAsset(
767
- '/public/manifest.json',
768
- new RawSource(JSON.stringify(this.content))
799
+ `/public/${this.clientAppName}/manifest.json`,
800
+ new RawSource(JSON.stringify(this.content, null, 2))
769
801
  );
770
802
  }
771
803
  );
@@ -777,7 +809,7 @@ const extraPluginsProd = terserOptions => {
777
809
  return [
778
810
  // Extract built CSS files into subdirectories by chunk / entry point name.
779
811
  new MiniCssExtractPlugin({
780
- filename: '[name]/[name].[contenthash:8].css'
812
+ filename: '[name].[contenthash:8].css'
781
813
  }),
782
814
 
783
815
  // Minify and tree-shake via Terser - https://github.com/terser/terser#readme
@@ -811,18 +843,22 @@ const extraPluginsDev = () => {
811
843
  ];
812
844
  };
813
845
 
814
- // Generate combinations for given chunkNames. Given a list of apps such as [admin, app, mobile],
815
- // possible chunk combos will be of the form admin, admin~app, app~mobile, and so on...
816
- function getChunkCombinations(appNames) {
817
- let ret = [];
818
- const combineRecursiveFn = (path, arr, sep) => {
819
- for (let i = 0; i < arr.length; i++) {
820
- const newPath = path + sep + arr[i];
821
- ret.push(newPath);
822
- combineRecursiveFn(newPath, arr.slice(i + 1), '~');
823
- }
824
- };
825
- combineRecursiveFn('', appNames, '');
846
+ // Resolves the specific script + style chunks required by a given client app entry point, to be injected into
847
+ // the generated HTML index page for that client app.
848
+ function getFileDependenciesByEntrypoint(compilation, clientAppName) {
849
+ const ret = {scriptTags: '', styleTags: ''};
850
+ compilation.entrypoints
851
+ .get(clientAppName)
852
+ .getFiles()
853
+ .forEach(file => {
854
+ const ext = path.extname(file).slice(1);
855
+ if (ext === 'js') {
856
+ ret.scriptTags += `<script defer src="/${file}"></script>`;
857
+ } else if (ext === 'css') {
858
+ ret.styleTags += `<link rel="stylesheet" href="/${file}" />`;
859
+ }
860
+ });
861
+
826
862
  return ret;
827
863
  }
828
864
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xh/hoist-dev-utils",
3
- "version": "8.0.0-SNAPSHOT.1710791660398",
3
+ "version": "8.1.0",
4
4
  "description": "Utilities for configuring builds of Hoist-React client applications.",
5
5
  "repository": "github:xh/hoist-dev-utils",
6
6
  "homepage": "https://github.com/xh/hoist-dev-utils",
@@ -12,17 +12,17 @@
12
12
  "static/**/*"
13
13
  ],
14
14
  "dependencies": {
15
- "@babel/core": "^7.23.9",
16
- "@babel/plugin-proposal-decorators": "^7.23.9",
17
- "@babel/preset-env": "^7.23.9",
15
+ "@babel/core": "^7.24.0",
16
+ "@babel/plugin-proposal-decorators": "^7.24.0",
17
+ "@babel/preset-env": "^7.24.0",
18
18
  "@babel/preset-react": "^7.23.3",
19
19
  "@babel/preset-typescript": "^7.23.3",
20
- "@cerner/duplicate-package-checker-webpack-plugin": "~2.3.0",
20
+ "@cerner/duplicate-package-checker-webpack-plugin": "2.3.0",
21
21
  "@types/lodash": "4.x",
22
22
  "@types/react": "18.x",
23
23
  "@xh/eslint-config": "^6.0",
24
- "autoprefixer": "~10.4.14",
25
- "babel-loader": "~9.1.2",
24
+ "autoprefixer": "~10.4.18",
25
+ "babel-loader": "~9.1.3",
26
26
  "babel-plugin-transform-imports": "~2.0.0",
27
27
  "case-sensitive-paths-webpack-plugin": "~2.4.0",
28
28
  "changelog-parser": "~3.0.1",
@@ -32,22 +32,22 @@
32
32
  "html-webpack-plugin": "~5.6.0",
33
33
  "html-webpack-tags-plugin": "~3.0.2",
34
34
  "lodash": "~4.17.21",
35
- "mini-css-extract-plugin": "~2.8.0",
36
- "postcss": "~8.4.33",
35
+ "mini-css-extract-plugin": "~2.8.1",
36
+ "postcss": "~8.4.35",
37
37
  "postcss-flexbugs-fixes": "~5.0.2",
38
- "postcss-loader": "~8.1.0",
38
+ "postcss-loader": "~8.1.1",
39
39
  "rimraf": "~5.0.5",
40
- "sass": "~1.70.0",
41
- "sass-loader": "~14.1.0",
40
+ "sass": "~1.72.0",
41
+ "sass-loader": "~14.1.1",
42
42
  "sass-material-colors": "~0.0.5",
43
43
  "style-loader": "~3.3.2",
44
44
  "terser-webpack-plugin": "~5.3.7",
45
45
  "url-loader": "~4.1.1",
46
- "webpack": "~5.90.1",
46
+ "webpack": "~5.90.3",
47
47
  "webpack-bundle-analyzer": "~4.10.1",
48
48
  "webpack-cli": "~5.1.4",
49
- "webpack-dev-server": "~4.15.0",
50
- "webpackbar": "~6.0.0"
49
+ "webpack-dev-server": "~5.0.3",
50
+ "webpackbar": "~6.0.1"
51
51
  },
52
52
  "devDependencies": {
53
53
  "prettier": "3.x"
package/static/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Build-related files
2
2
 
3
+ * `index.html` - passed to `HtmlWebpackPlugin` to be the template index.html file for each client app. Note that this
4
+ had been sourced from hoist-react, but as of hoist-dev-utils >= v8.1 is included with this project for simplicity.
3
5
  * `requiredBlueprintIcons.js` - included by hoist-dev-utils >= v6.2 as a (very) streamlined replacement for the
4
6
  full set of Blueprint JS icons otherwise required by that library. See the comment at the top of the file for
5
7
  additional details and links.
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0, shrink-to-fit=no">
6
+ <link rel="manifest" href="<%= webpackConfig.output.publicPath %>public/<%= clientAppName %>/manifest.json" crossorigin="use-credentials">
7
+ <% if (htmlWebpackPlugin.options.includeAppleIcon) { %>
8
+ <link rel="apple-touch-icon" href="<%= webpackConfig.output.publicPath %>public/apple-touch-icon.png">
9
+ <% } %>
10
+ <title><%= htmlWebpackPlugin.options.title %></title>
11
+ <%= styleTags %>
12
+ </head>
13
+ <body class="xh-app" style="margin: 0">
14
+
15
+ <noscript>
16
+ This application requires that JavaScript be enabled for this site in your browser.
17
+ </noscript>
18
+
19
+ <div id="xh-preload-spinner" style="height: 100vh; width: 100vw; display: flex; align-items: center; justify-content: center; background-color: <%= preloadBackgroundColor %>">
20
+ <img src="<%= webpackConfig.output.publicPath %>public/spinner.png" style="width: 50px; height: 50px;">
21
+ </div>
22
+
23
+ <div id="xh-root"></div>
24
+ <%= scriptTags %>
25
+ </body>
26
+ </html>
Binary file