Angular 2 AOT - Can't resolve $$_gendir…ngfactory

倾然丶 夕夏残阳落幕 提交于 2019-12-24 09:54:06

问题


https://github.com/stefankendall/broken-aot

I'm trying to setup a simple AOT angular 2 build with webpack and gulp. In my minimal example I've created, I have the following:

./src/app/components - components used by the main application, a separate ngmodule ./src/app/examples - the main application

ERROR in ./src/index.ts
Module not found: Error: Can't resolve './../$$_gendir/src/app/examples/app.module.ts.ngfactory' in '/Users/username/workpath/projectname/src'
 @ ./src/index.ts 8:32-98

Based on a previous post, index.ts looks like this:

import 'core-js/client/shim';
import 'zone.js/dist/zone';

import '@angular/common';
import 'rxjs';

import {enableProdMode} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app/examples/app.module';

declare let process: any;
if (process.env.NODE_ENV === 'production') {
  enableProdMode();
} else {
  Error['stackTraceLimit'] = Infinity; // tslint:disable-line:no-string-literal
  require('zone.js/dist/long-stack-trace-zone'); // tslint:disable-line:no-var-requires
}

platformBrowserDynamic().bootstrapModule(AppModule);

This loads AppModule from ./src/app/examples/app.module.ts, which looks like this:

import {CUSTOM_ELEMENTS_SCHEMA, NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {BrowserModule} from '@angular/platform-browser';

import {MainComponent} from './MainComponent';
import {AppComponent} from './AppComponent';
import {routing} from './routes';
import {ComponentsModule} from '../components/index';

@NgModule({
  imports: [
    BrowserModule,
    ComponentsModule,
    FormsModule,
    routing
  ],
  declarations: [
    AppComponent,
    MainComponent
  ],
  bootstrap: [AppComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule {
}

Running locally without AOT works fine, but attempting an AOT build with the following webpack config yields the error:

const webpack = require('webpack');
const conf = require('./gulp.conf');
const path = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const FailPlugin = require('webpack-fail-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const autoprefixer = require('autoprefixer');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: {
    app: './src/index.ts'
  },
  module: {
    loaders: [
      {
        test: /\.json$/,
        loaders: [
          'json-loader'
        ]
      },
      {
        test: /\.(ttf|otf|eot|svg|png|jpg|woff(2)?)(\?[a-z0-9]+)?$/,
        loader: 'file-loader'
      },
      {
        test: /\.ts$/,
        exclude: /node_modules/,
        loader: 'tslint-loader',
        enforce: 'pre'
      },
      {
        test: /\.css$/,
        loaders: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: 'css-loader?minimize!sass-loader!postcss-loader'
        })
      },
      {
        test: /\.scss$/,
        loaders: ['raw-loader', 'sass-loader']
      },
      {
        test: /\.ts$/,
        exclude: /node_modules/,
        loaders: [
          '@ngtools/webpack'
        ]
      },
      {
        test: /\.html$/,
        loaders: [
          'html-loader'
        ]
      }
    ]
  },
  plugins: [
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    FailPlugin,
    new HtmlWebpackPlugin({
      template: conf.path.src('index.html')
    }),
    new webpack.ContextReplacementPlugin(
      /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
      conf.paths.src
    ),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': '"production"'
    }),
    new ExtractTextPlugin('index-[contenthash].css'),
    new webpack.optimize.CommonsChunkPlugin({name: 'vendor'}),
    new webpack.LoaderOptionsPlugin({
      options: {
        postcss: () => [autoprefixer],
        resolve: {},
        ts: {
          configFileName: 'tsconfig.json'
        },
        tslint: {
          configuration: require('../tslint.json')
        }
      }
    }),
    new CopyWebpackPlugin([
      {from: 'index.html'}
    ]),
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery',
      Tether: 'tether'
    }),
    new AotPlugin({
      tsConfigPath: './tsconfig.json',
      entryModule: path.resolve('./src/app/examples/app.module.ts#AppModule')
    }),
    new webpack.optimize.UglifyJsPlugin({
      output: {comments: false},
      compress: {unused: true, dead_code: true, warnings: false} // eslint-disable-line camelcase
    })
  ],
  output: {
    path: path.join(process.cwd(), conf.paths.dist, 'aot'),
    filename: '[name]-[hash].js'
  },
  resolve: {
    extensions: [
      '.webpack.js',
      '.web.js',
      '.js',
      '.ts'
    ]
  }
};

What am I missing?


回答1:


Full stack trace can help here. I faced the same issue and then tried to find the reason in anything related to AOT compiling without any success. The reason for me was in loader related to linting, I can see you use it too. TSLint is enforced to run as "pre", and, as far as I understand, there is simply no *.ngfactory.ts generated at the moment linter runs, so it fails. And yes, disabling this tslint-loader helped, all compiled successfully.

Interesting thing is that I wasn't able to force tslint to ignore this import in bootstrap file in any way: neither exclude option in loader nor tslint:disable* directives doesn't helped. Moreover, making linter running as "post" or normal loader doesn't helped too. I even tried to extract factory importing into another file (separate from AOT boot entry, which I presumed it can be loaded anyway no matter ignored it or not), so I extracted that import and ignored that separate file, but still with no luck.

I think that there is no option to workaround this issue for now, and, honestly, it preventing me from using AOT.

I wrote about this issue to tslint-loader guys (https://github.com/wbuchwalter/tslint-loader/issues/83), maybe they will suggest something working.




回答2:


I also was seeing this error for the longest time. What worked for me is changing the @ngtools/webpack module to handle ngfactory files too.

In your example, it's look like this:

  {
    test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
    exclude: /node_modules/,
    loaders: [
      '@ngtools/webpack'
    ]
  },

Hope that helps!




回答3:


AOT is broken. Don't bother.

Edit: It's fine if you have it from the start. Converting a project is nearly impossible, however.



来源:https://stackoverflow.com/questions/46279458/angular-2-aot-cant-resolve-gendir-ngfactory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!