Skip to content

Instantly share code, notes, and snippets.

@meDavid
Last active March 18, 2019 15:05
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 meDavid/73608a3c813a4c3ca0175707a4b82303 to your computer and use it in GitHub Desktop.
Save meDavid/73608a3c813a4c3ca0175707a4b82303 to your computer and use it in GitHub Desktop.
Example ngx-build-plus plugin to register a typescript transformer
import { dummyTransformer } from './ng-ts-dummy-transformer';
import { AngularCompilerPlugin } from '@ngtools/webpack';
function findAngularCompilerPlugin(webpackCfg): AngularCompilerPlugin | null {
return webpackCfg.plugins.find(plugin => plugin instanceof AngularCompilerPlugin);
}
// The AngularCompilerPlugin has nog public API to add transformations, user private API _transformers instead.
function addTransformerToAngularCompilerPlugin(acp, transformer): void {
acp._transformers = [transformer, ...acp._transformers];
}
export default {
pre() {
// This hook is not used in our example
},
// This hook is used to manipulate the webpack configuration
config(cfg) {
// Find the AngularCompilerPlugin in the webpack configuration
const angularCompilerPlugin = findAngularCompilerPlugin(cfg);
if (!angularCompilerPlugin) {
console.error('Could not inject the typescript transformer: Webpack AngularCompilerPlugin not found');
return;
}
addTransformerToAngularCompilerPlugin(angularCompilerPlugin, dummyTransformer);
return cfg;
},
post() {
// This hook is not used in our example
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment