How to use the @ngtools/webpack/src/compiler_host.WebpackCompilerHost.prototype function in @ngtools/webpack

To help you get started, we’ve selected a few @ngtools/webpack examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github shlomiassaf / ngc-webpack / test / lib-usage.spec.ts View on Github external
});
    });

    const config = require(configs.pluginLib.wp)(true);
    const pluginIdx = findPluginIndex(config.plugins);
    const options = {
      skipCodeGeneration: false,
      tsConfigPath: tmpTsConfig
    };
    const plugin = NgcWebpackPlugin.clone(config.plugins[pluginIdx], { options });

    config.plugins.splice(pluginIdx, 1, plugin);

    await run(config);

    Object.defineProperty(WebpackCompilerHost.prototype, 'writeFile', writeFilePropertyDescriptor);

    rimraf.sync(tmpTsConfig);

    // the directory of index.ts:
    const root = Path.dirname(Path.resolve(Path.dirname(configs.pluginLib.ts), tsConfig.files[0]));
    const metadataJsonPath = Path.resolve(root, 'my-lib.ng-flat.metadata.json');
    const meta = await readFile(metadataJsonPath);
    rimraf.sync(metadataJsonPath);

    const metadataBundle: ModuleMetadata = JSON.parse(meta);
    const LibComponentComponent: any = metadataBundle.metadata.LibComponentComponent;

    // flat module with @ngtools/webpack doesn't inline resources into metadata
    expect(LibComponentComponent.decorators[0].arguments[0]).to.eql({
      "selector": "lib-component",
      "templateUrl": "./lib-component.component.html",
github typebytes / ngx-template-streams / projects / ngx-template-streams / src / internal / plugin.ts View on Github external
import * as ts from 'typescript';
import * as webpack from 'webpack';
import { jitTransformers } from './transformers';
import { getSourceFile } from './webpack-compiler-host';

// This key is used to access a private property on the AngularCompilerPlugin
// that indicates whether we are running in JIT mode or not
export const JIT_MODE = '_JitMode';

/**
 * For AOT we have to patch the WebpackCompilerHost's getSourceFile method to run source code transformations
 * before the compiler generates AOT artifacts (both Ivy and ViewEngine).
 * Platform transformers are called too late in the pipeline.
 * See https://github.com/angular/angular-cli/blob/master/packages/ngtools/webpack/src/compiler_host.ts#L265-L291
 */
WebpackCompilerHost.prototype.getSourceFile = getSourceFile;

export default {
  config(config: webpack.Configuration) {
    const angularCompilerPlugin = findAngularCompilerPlugin(config) as AngularCompilerPlugin;

    if (!angularCompilerPlugin) {
      throw new Error('Could not inject TypeScript Transformer: Webpack AngularCompilerPlugin not found');
    }

    const jitMode = isJitMode(angularCompilerPlugin);

    // Turn off direct template loading. By default this option is `true`, causing
    // the plugin to load component templates (HTML) directly from the filesystem.
    // This is more efficient than using the raw-loader. However, if we want to add
    // a custom html-loader we have to turn off direct template loading. To do so
    // we have to remove the old compiler plugin and create a new instance with
github shlomiassaf / ngc-webpack / test / lib-usage.spec.ts View on Github external
it('should run with library config', async () => {
    const WebpackCompilerHost = require('@ngtools/webpack/src/compiler_host').WebpackCompilerHost;
    const writeFilePropertyDescriptor = Object.getOwnPropertyDescriptor(WebpackCompilerHost.prototype, 'writeFile');

    try {
      NgcWebpackPlugin = require('../dist').NgcWebpackPlugin;
      findPluginIndex = require('../dist/src/cli/cli').findPluginIndex;
      require('../dist/src/patch-ngtools-compiler-host-for-flat-module');
    } catch (e) {
      NgcWebpackPlugin = require('../index').NgcWebpackPlugin;
      findPluginIndex = require('../src/cli/cli').findPluginIndex;
      require('../src/patch-ngtools-compiler-host-for-flat-module');
    }

    delOutDir();

    const tmpTsConfig = await createTempTsConfig( config => {
      return Object.assign(config, {
        angularCompilerOptions: {