Last active
March 18, 2019 15:05
-
-
Save meDavid/73608a3c813a4c3ca0175707a4b82303 to your computer and use it in GitHub Desktop.
Example ngx-build-plus plugin to register a typescript transformer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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