Skip to content

Instantly share code, notes, and snippets.

@carlosdelfino
Last active February 14, 2020 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosdelfino/c098df92189fc12fc171ecc4a18b3db6 to your computer and use it in GitHub Desktop.
Save carlosdelfino/c098df92189fc12fc171ecc4a18b3db6 to your computer and use it in GitHub Desktop.
Sugestão de configuração para Angular gravar arquivos JS em pastas diferentes do index.html

Baseado em angular/angular-cli#2688

For anyone needs a way to achieve this goal through angular-cli, please have a look here: softdays/angularcli-index-location@c9918fd

Key steps

npm i -D @angular-builders/custom-webpack

angular.json > projects > your-project-name > architect > build > replace builder value by:

"builder": "@angular-builders/custom-webpack:browser"

angular.json > projects > your-project-name > architect > build > options > outputPath:

Prepend output path with your subfolder, i.e. "outputPath": "dist/app"

angular.json > projects > your-project-name > architect > build > options, add:

"customWebpackConfig": { 
    "path": "./extra-webpack.config.js", 
    "replaceDuplicatePlugins": true 
}`

angular.json > projects > your-project-name > architect > build > configurations, add:

"deployUrl": "app/",
"baseHref": "",

Create extra-webpack.config.js file at project root with the content below:

const fs = require('fs');
module.exports = (config, options) => {
    config.plugins.push({
        apply: (compiler) => {
            compiler.hooks.afterEmit.tap('MyAfterEmitPlugin', (compilation) => {
                // Replace 'app' path below with your subfolder path
                fs.renameSync('./dist/app/index.html', './dist/index.html');
                fs.renameSync('./dist/app/favicon.ico', './dist/favicon.ico');
            });
        }
    });
    return config;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment