@xh/hoist-dev-utils 8.0.0 → 9.0.0-SNAPSHOT.1711579166181

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,19 +312,22 @@ async function configureWebpack(env) {
310
312
  path: path.resolve(appDirPath, f)
311
313
  };
312
314
  }),
313
- appNames = apps.map(it => it.name);
315
+ clientAppNames = clientApps.map(it => it.name);
314
316
 
315
317
  // Build Webpack entry config, with keys for each JS app to be bundled.
316
318
  const appEntryPoints = {};
317
- apps.forEach(app => {
319
+ clientApps.forEach(clientApp => {
318
320
  // Ensure core-js and regenerator-runtime both imported for every app bundle - they are
319
321
  // specified as dependencies by Hoist and imported once in its polyfills.js file.
320
- 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
+ ];
321
326
  });
322
327
 
323
328
  logSep();
324
329
  logMsg('🎁 App bundle entry points:');
325
- appNames.forEach(it => logMsg(` > ${it}`));
330
+ clientAppNames.forEach(it => logMsg(` > ${it}`));
326
331
  logSep();
327
332
  logMsg('🤕 Something going wrong?');
328
333
  logMsg(' > support@xh.io');
@@ -605,7 +610,7 @@ async function configureWebpack(env) {
605
610
  xhAppBuild: JSON.stringify(appBuild),
606
611
  xhBaseUrl: JSON.stringify(baseUrl),
607
612
  xhBuildTimestamp: buildDate.getTime(),
608
- xhClientApps: JSON.stringify(appNames),
613
+ xhClientApps: JSON.stringify(clientAppNames),
609
614
  xhIsDevelopmentMode: !prodBuild
610
615
  }),
611
616
 
@@ -616,8 +621,8 @@ async function configureWebpack(env) {
616
621
  contextRegExp: /moment$/
617
622
  }),
618
623
 
619
- // Copy preflight script and spinner provided by HR, plus entire /client-app/public
620
- // 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.
621
626
  new CopyWebpackPlugin({
622
627
  patterns: _.compact([
623
628
  {from: preflightScriptPath, to: 'public'},
@@ -629,13 +634,12 @@ async function configureWebpack(env) {
629
634
  }),
630
635
 
631
636
  // Generate HTML index pages - one per JS app.
632
- ...appNames.map(jsAppName => {
637
+ ...clientAppNames.map(clientAppName => {
633
638
  return new HtmlWebpackPlugin({
634
639
  title: appName,
635
640
  favicon: favicon,
636
- // Note: HTML template is sourced from hoist-react.
637
- template: path.resolve(hoistPath, `static/index.html`),
638
- filename: `${jsAppName}/index.html`,
641
+ template: path.resolve(hoistDevUtilsPath, `static/index.html`),
642
+ filename: `${clientAppName}/index.html`,
639
643
 
640
644
  // Take 0 chunks from plugin, because we collect just the ones for the jsAppName
641
645
  // below in templateParameters
@@ -646,11 +650,14 @@ async function configureWebpack(env) {
646
650
 
647
651
  // This will provide the html tag strings for just the css and js that jsAppName uses.
648
652
  templateParameters: (compilation, assets, assetTags, options) => {
649
- const tags = getFileDependenciesByEntrypoint(compilation, jsAppName);
653
+ const {styleTags, scriptTags} = getFileDependenciesByEntrypoint(
654
+ compilation,
655
+ clientAppName
656
+ );
650
657
 
651
- // Output recommended by plugin example:
652
- // https://github.com/jantimon/html-webpack-plugin/blob/main/examples/template-parameters/webpack.config.js
653
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
654
661
  compilation,
655
662
  webpackConfig: compilation.options,
656
663
  htmlWebpackPlugin: {
@@ -658,8 +665,11 @@ async function configureWebpack(env) {
658
665
  files: assets,
659
666
  options
660
667
  },
661
- styleTags: tags.css,
662
- scriptTags: tags.js
668
+ // XH additions
669
+ styleTags,
670
+ scriptTags,
671
+ clientAppName,
672
+ preloadBackgroundColor
663
673
  };
664
674
  },
665
675
  // No need to minify the HTML itself
@@ -669,20 +679,21 @@ async function configureWebpack(env) {
669
679
  });
670
680
  }),
671
681
 
672
- // 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
673
683
  // to HtmlWebpackPlugin above to match the spec here:
674
684
  // https://evilmartians.com/chronicles/how-to-favicon-in-2021-six-files-that-fit-most-needs
675
- new HoistManifestPlugin({
676
- name: appName,
677
- short_name: appName,
678
- description: `${appName} - ${appVersion}`,
679
- display: 'standalone',
680
- orientation: 'any',
681
- start_url: '/',
682
- background_color: '#ffffff',
683
- theme_color: '#212121', // off-black from default `--xh-black` CSS var
684
- icons: manifestIcons,
685
- ...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
+ });
686
697
  }),
687
698
 
688
699
  // Insert a script tag for the (unbundled) preflight script, before all other scripts.
@@ -741,7 +752,7 @@ async function configureWebpack(env) {
741
752
  open: env.devServerOpenPage ? [env.devServerOpenPage] : false,
742
753
  // Support HTML5 history routes for apps, with /appName/ as the base route for each
743
754
  historyApiFallback: {
744
- rewrites: appNames.map(appName => {
755
+ rewrites: clientAppNames.map(appName => {
745
756
  return {
746
757
  from: new RegExp(`^/${appName}`),
747
758
  // helps cache busting during live reload in development
@@ -757,7 +768,15 @@ async function configureWebpack(env) {
757
768
  // Implementation
758
769
  //------------------------
759
770
  class HoistManifestPlugin {
760
- 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
+
761
780
  this.content = content;
762
781
  }
763
782
 
@@ -774,9 +793,11 @@ class HoistManifestPlugin {
774
793
  stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
775
794
  },
776
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.
777
798
  compilation.emitAsset(
778
- '/public/manifest.json',
779
- new RawSource(JSON.stringify(this.content))
799
+ `/public/${this.clientAppName}/manifest.json`,
800
+ new RawSource(JSON.stringify(this.content, null, 2))
780
801
  );
781
802
  }
782
803
  );
@@ -824,17 +845,17 @@ const extraPluginsDev = () => {
824
845
 
825
846
  // Resolves the specific script + style chunks required by a given client app entry point, to be injected into
826
847
  // the generated HTML index page for that client app.
827
- function getFileDependenciesByEntrypoint(compilation, entryName) {
828
- const ret = {css: '', js: ''};
848
+ function getFileDependenciesByEntrypoint(compilation, clientAppName) {
849
+ const ret = {scriptTags: '', styleTags: ''};
829
850
  compilation.entrypoints
830
- .get(entryName)
851
+ .get(clientAppName)
831
852
  .getFiles()
832
853
  .forEach(file => {
833
854
  const ext = path.extname(file).slice(1);
834
855
  if (ext === 'js') {
835
- ret.js += `<script defer src="/${file}"></script>`;
856
+ ret.scriptTags += `<script defer src="/${file}"></script>`;
836
857
  } else if (ext === 'css') {
837
- ret.css += `<link rel="stylesheet" href="/${file}" />`;
858
+ ret.styleTags += `<link rel="stylesheet" href="/${file}" />`;
838
859
  }
839
860
  });
840
861
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xh/hoist-dev-utils",
3
- "version": "8.0.0",
3
+ "version": "9.0.0-SNAPSHOT.1711579166181",
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",
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