Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add indexHtmlTransformer and plugins options to application builder #28607

Open
zip-fa opened this issue Oct 9, 2024 · 3 comments
Labels
needs: more info Reporter must clarify the issue

Comments

@zip-fa
Copy link

zip-fa commented Oct 9, 2024

Command

build

Description

Hi. This feature exists for a long time in angular-builders and nx custom builders' code.
plugins allows adding vite plugins; we use this option to use custom plugin with .env file parsing, adding ENVIRONMENT_NAME and APP_VERSION global variables and much more. I guess there are plenty of other use-cases for this.
indexHtmlTransformer allows to replace some placeholders in index.html while building or serving an app. Useful to make similar DX when building or developing locally my app.

There is not much to do with these two, but i think this will boost DX many times.
Thanks for your time!

Describe the solution you'd like

No response

Describe alternatives you've considered

https://github.com/nrwl/nx/blob/master/packages/angular/src/executors/application/application.impl.ts

@alan-agius4
Copy link
Collaborator

@zip-fa, thanks for this feature request.

While we do offer the option to add additional plugins and modify the index.html file, we do not consider this to be part of the "recommended" or optimal path.

What is the use-case of including environment variables directly in the index.html instead of embedding them in the JS bundle and setting them with the --define option?

@alan-agius4 alan-agius4 added the needs: more info Reporter must clarify the issue label Oct 14, 2024
@zip-fa
Copy link
Author

zip-fa commented Oct 14, 2024

You misunderstood me a bit. NX parses .env.{envName} files automatically and set them inside process.env
We have three environments: staging, prod and dev.
While it's ok to use --define option, it's much easier to depend on .env files. Our plugin to define global env vars is:

const path = require('path');
const { dirname } = require('node:path');
const envPrefix = /^APP_/i;

function getProjectEnvVars() {
  const envVars = {};

  for (const key in process.env) {
    if (envPrefix.test(key)) {
      envVars[key] = process.env[key];
    }
  }

  return envVars;
}

const envVarPlugin = {
  name: 'env-var-plugin',
  setup(build) {
    const options = build.initialOptions;
    const envVars = getProjectEnvVars();
    const sourceDirectory = dirname(build.initialOptions.tsconfig);
    const packageJsonPath = sourceDirectory + '/package.json';

    options.define['__APP_VERSION__'] = `"${ require(packageJsonPath).version }"`;
    options.define['__SOURCE_DIRECTORY__'] = `"${ sourceDirectory }"`;
    options.define['BUILD_ENV'] = JSON.stringify(envVars);
    options.define['ENVIRONMENT_NAME'] = `"${ process.env.NX_TASK_TARGET_CONFIGURATION }"`;
  }
};

module.exports = envVarPlugin

For index.html we change placeholders on the fly:

<head>
{analytics_scripts}
</head>

transformer:

export default function(indexContent: string) {
  const replacements: Record<string, string> = {
    '{analytics_scripts}': '...'
  };

  for (const key in replacements) {
    indexContent = indexContent.replaceAll(key, replacements[key]);
  }

  return indexContent;

@imaksp
Copy link

imaksp commented Nov 20, 2024

Hi all, adding another use case, some scripts needs to be added in head section like this:

 <script async src="https://www.googletagmanager.com/gtag/js?id=GTM-XXXX"></script>

currently we can't use variable for GTM id in index.html, we can also load script using js/angular, but it will have some issue if its loaded later from angular app code. sometimes script's domain name can also be different based on env, fonts can also be different for each env, currently we need to create different index.html files for each env & replace it using fileReplacements config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: more info Reporter must clarify the issue
Projects
None yet
Development

No branches or pull requests

3 participants