@xh/hoist-dev-utils 8.0.0 → 8.1.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.
@@ -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,20 +228,21 @@ 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.
235
237
  if (inlineHoist) {
236
238
  resolveAliases['@xh/hoist'] = hoistPath;
237
239
 
238
- // This ensures that we use the same instance of react in hoist-react as in the app - needed
239
- // to get hooks working since they will throw an error if the react instance that the hook
240
+ // This ensures that we use the same instance of libs in hoist-react as in the app - needed
241
+ // to get hooks working since they will throw an error if the lib instance that the hook
240
242
  // was imported from is different than the instance that was used to render the component
241
243
  // (which will always be the instance hoist-react has when using element factories)
242
- resolveAliases.react = path.resolve('./node_modules/react');
244
+ resolveAliases['react'] = path.resolve('./node_modules/react');
245
+ resolveAliases['@ag-grid-community'] = path.resolve('./node_modules/@ag-grid-community');
243
246
  }
244
247
 
245
248
  // When running inline, resolve inline Hoist's own node_modules package so we can tell Babel to exclude
@@ -301,7 +304,7 @@ async function configureWebpack(env) {
301
304
 
302
305
  // Resolve app entry points - one for each file within src/apps/ - to create bundles below.
303
306
  const appDirPath = path.resolve(srcPath, 'apps'),
304
- apps = fs
307
+ clientApps = fs
305
308
  .readdirSync(appDirPath)
306
309
  .filter(f => f.endsWith('.js') || f.endsWith('.ts'))
307
310
  .map(f => {
@@ -310,19 +313,22 @@ async function configureWebpack(env) {
310
313
  path: path.resolve(appDirPath, f)
311
314
  };
312
315
  }),
313
- appNames = apps.map(it => it.name);
316
+ clientAppNames = clientApps.map(it => it.name);
314
317
 
315
318
  // Build Webpack entry config, with keys for each JS app to be bundled.
316
319
  const appEntryPoints = {};
317
- apps.forEach(app => {
320
+ clientApps.forEach(clientApp => {
318
321
  // Ensure core-js and regenerator-runtime both imported for every app bundle - they are
319
322
  // 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];
323
+ appEntryPoints[clientApp.name] = [
324
+ path.resolve(hoistPath, 'static/polyfills.js'),
325
+ clientApp.path
326
+ ];
321
327
  });
322
328
 
323
329
  logSep();
324
330
  logMsg('🎁 App bundle entry points:');
325
- appNames.forEach(it => logMsg(` > ${it}`));
331
+ clientAppNames.forEach(it => logMsg(` > ${it}`));
326
332
  logSep();
327
333
  logMsg('🤕 Something going wrong?');
328
334
  logMsg(' > support@xh.io');
@@ -605,7 +611,7 @@ async function configureWebpack(env) {
605
611
  xhAppBuild: JSON.stringify(appBuild),
606
612
  xhBaseUrl: JSON.stringify(baseUrl),
607
613
  xhBuildTimestamp: buildDate.getTime(),
608
- xhClientApps: JSON.stringify(appNames),
614
+ xhClientApps: JSON.stringify(clientAppNames),
609
615
  xhIsDevelopmentMode: !prodBuild
610
616
  }),
611
617
 
@@ -616,8 +622,8 @@ async function configureWebpack(env) {
616
622
  contextRegExp: /moment$/
617
623
  }),
618
624
 
619
- // Copy preflight script and spinner provided by HR, plus entire /client-app/public
620
- // directory into the build output.
625
+ // Copy preflight script provided by HR, preload spinner included here in DU,
626
+ // plus entire /client-app/public directory into the build output.
621
627
  new CopyWebpackPlugin({
622
628
  patterns: _.compact([
623
629
  {from: preflightScriptPath, to: 'public'},
@@ -629,13 +635,12 @@ async function configureWebpack(env) {
629
635
  }),
630
636
 
631
637
  // Generate HTML index pages - one per JS app.
632
- ...appNames.map(jsAppName => {
638
+ ...clientAppNames.map(clientAppName => {
633
639
  return new HtmlWebpackPlugin({
634
640
  title: appName,
635
641
  favicon: favicon,
636
- // Note: HTML template is sourced from hoist-react.
637
- template: path.resolve(hoistPath, `static/index.html`),
638
- filename: `${jsAppName}/index.html`,
642
+ template: path.resolve(hoistDevUtilsPath, `static/index.html`),
643
+ filename: `${clientAppName}/index.html`,
639
644
 
640
645
  // Take 0 chunks from plugin, because we collect just the ones for the jsAppName
641
646
  // below in templateParameters
@@ -646,11 +651,14 @@ async function configureWebpack(env) {
646
651
 
647
652
  // This will provide the html tag strings for just the css and js that jsAppName uses.
648
653
  templateParameters: (compilation, assets, assetTags, options) => {
649
- const tags = getFileDependenciesByEntrypoint(compilation, jsAppName);
654
+ const {styleTags, scriptTags} = getFileDependenciesByEntrypoint(
655
+ compilation,
656
+ clientAppName
657
+ );
650
658
 
651
- // Output recommended by plugin example:
652
- // https://github.com/jantimon/html-webpack-plugin/blob/main/examples/template-parameters/webpack.config.js
653
659
  return {
660
+ // Base output recommended by plugin example:
661
+ // https://github.com/jantimon/html-webpack-plugin/blob/main/examples/template-parameters/webpack.config.js
654
662
  compilation,
655
663
  webpackConfig: compilation.options,
656
664
  htmlWebpackPlugin: {
@@ -658,8 +666,11 @@ async function configureWebpack(env) {
658
666
  files: assets,
659
667
  options
660
668
  },
661
- styleTags: tags.css,
662
- scriptTags: tags.js
669
+ // XH additions
670
+ styleTags,
671
+ scriptTags,
672
+ clientAppName,
673
+ preloadBackgroundColor
663
674
  };
664
675
  },
665
676
  // No need to minify the HTML itself
@@ -669,20 +680,21 @@ async function configureWebpack(env) {
669
680
  });
670
681
  }),
671
682
 
672
- // Create a manifest.json. The icon choices here work with the favicon provided
683
+ // Create a manifest.json for each app. The icon choices here work with the favicon provided
673
684
  // to HtmlWebpackPlugin above to match the spec here:
674
685
  // 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
686
+ ...clientAppNames.map(clientAppName => {
687
+ return new HoistManifestPlugin(clientAppName, {
688
+ name: appName,
689
+ short_name: appName,
690
+ description: `${appName} - ${appVersion}`,
691
+ display: 'standalone',
692
+ orientation: 'any',
693
+ background_color: preloadBackgroundColor, // ignored by Safari, but also used within index.html
694
+ theme_color: '#212121', // off-black from default `--xh-black` CSS var
695
+ icons: manifestIcons,
696
+ ...manifestConfig
697
+ });
686
698
  }),
687
699
 
688
700
  // Insert a script tag for the (unbundled) preflight script, before all other scripts.
@@ -741,7 +753,7 @@ async function configureWebpack(env) {
741
753
  open: env.devServerOpenPage ? [env.devServerOpenPage] : false,
742
754
  // Support HTML5 history routes for apps, with /appName/ as the base route for each
743
755
  historyApiFallback: {
744
- rewrites: appNames.map(appName => {
756
+ rewrites: clientAppNames.map(appName => {
745
757
  return {
746
758
  from: new RegExp(`^/${appName}`),
747
759
  // helps cache busting during live reload in development
@@ -757,7 +769,15 @@ async function configureWebpack(env) {
757
769
  // Implementation
758
770
  //------------------------
759
771
  class HoistManifestPlugin {
760
- constructor(content = {}) {
772
+ constructor(clientAppName, content = {}) {
773
+ this.clientAppName = clientAppName;
774
+
775
+ // We create one of these per clientApp. Default start_url to the clientApp's root, to bring user back to the
776
+ // clientApp from which they added the bookmark without any need for redirects, respecting possible override.
777
+ if (!content.start_url) {
778
+ content = {...content, start_url: `/${clientAppName}/`};
779
+ }
780
+
761
781
  this.content = content;
762
782
  }
763
783
 
@@ -774,9 +794,11 @@ class HoistManifestPlugin {
774
794
  stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
775
795
  },
776
796
  () => {
797
+ // Emit client-app specific manifest.json within /public, to avoid issues with deeper routes
798
+ // and relative paths. This is picked up by this project's /static/index.html template.
777
799
  compilation.emitAsset(
778
- '/public/manifest.json',
779
- new RawSource(JSON.stringify(this.content))
800
+ `/public/${this.clientAppName}/manifest.json`,
801
+ new RawSource(JSON.stringify(this.content, null, 2))
780
802
  );
781
803
  }
782
804
  );
@@ -824,17 +846,17 @@ const extraPluginsDev = () => {
824
846
 
825
847
  // Resolves the specific script + style chunks required by a given client app entry point, to be injected into
826
848
  // the generated HTML index page for that client app.
827
- function getFileDependenciesByEntrypoint(compilation, entryName) {
828
- const ret = {css: '', js: ''};
849
+ function getFileDependenciesByEntrypoint(compilation, clientAppName) {
850
+ const ret = {scriptTags: '', styleTags: ''};
829
851
  compilation.entrypoints
830
- .get(entryName)
852
+ .get(clientAppName)
831
853
  .getFiles()
832
854
  .forEach(file => {
833
855
  const ext = path.extname(file).slice(1);
834
856
  if (ext === 'js') {
835
- ret.js += `<script defer src="/${file}"></script>`;
857
+ ret.scriptTags += `<script defer src="/${file}"></script>`;
836
858
  } else if (ext === 'css') {
837
- ret.css += `<link rel="stylesheet" href="/${file}" />`;
859
+ ret.styleTags += `<link rel="stylesheet" href="/${file}" />`;
838
860
  }
839
861
  });
840
862
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xh/hoist-dev-utils",
3
- "version": "8.0.0",
3
+ "version": "8.1.1",
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