fileReplacements for non TypeScript (JavaScript) files doesn't work
🐞 Bug report
Command (mark with an x)
- [ ] new
- [x] build
- [ ] serve
Description
fileReplacements for non TypeScript (JavaScript) files doesn't work with ng build. ng serve correctly replaces all files defined in angular.json as stated in angular docs The main CLI configuration file, angular.json, contains a fileReplacements section in the configuration for each build target, which allows you to replace any file with a target-specific version of that file.
This feature is critical for our project because some of our scss, html and assets files are different per configuration.
In #19717 have been discussion about this being expected behavior. I don't agree because ng serve works correctly and docs state this should work for any file.
If you don't agree, please fix docs and provide us alternative solution to replace files at compile time.
🌍 Your Environment
@angular-devkit/architect 0.1100.4
@angular-devkit/build-angular 0.1100.4
@angular-devkit/core 11.0.4
@angular-devkit/schematics 11.1.1
@angular/cdk 11.0.2
@angular/cli 11.1.1
@majci237, Thanks for pointing out that the docs are incorrect. I did a PR to fix that https://github.com/angular/angular/pull/40687
As for a solution to replace non TypeScript compilation files during compilation time, there isn't an official way to do so using the Angular CLI directly.
You can use 3rd party extensions of the Angular CLI like to configure the replacements in Webpack
- https://github.com/manfredsteyer/ngx-build-plus
- https://www.npmjs.com/package/@angular-builders/custom-webpack
It's important to point out however, that custom Webpack configurations and Webpack specific features and not supported by the Angular tooling team and can break in future versions.
Closing as doc have been fixed.
Thanks for help.
My solution is fileReplacements for TS files and assets for assets.
For HTML and SCSS I used @angular-builders/custom-webpack and file-replace-loader.
My custom webpack is
module.exports = (config, options, targetOptions) => {
const paths = replacementPaths[targetOptions.project][targetOptions.configuration];
config.plugins[config.plugins.length - 1]._options.directTemplateLoading = false; // To load html via webpack
config.module.rules.push({
test: /.*\.html$/,
use: [{
loader: 'raw-loader'
}]
}, {
test: /.*/,
use: [{
loader: 'file-replace-loader',
options: {
condition: 'always',
replacement(resource) {
if (paths[resource] != null) {
return path.resolve(paths[resource]);
} else {
return path.resolve(resource);
}
}
}
}]
});
return config;
}
It's not perfect, but it works for me and I hope it also helps others.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.