2017-09-14 61 views
1

我在同一個項目中的不同模塊下的示例應用程序中有一組模塊中的組件。我有一個現有的JIT構建工作正常,我試圖轉換到AOT構建,我遇到問題。具體而言,/src/index.aot.ts (2,34): Cannot find module './aot/app/examples/app.module.ngfactory'Angular 2 AOT - 引用尚未創建的AppModuleNgFactory

如何設置webpack AotPlugin正確構建我的應用程序的AOT版本到本地dist目錄?

/src/index.aot.ts

在這個文件中,AppModuleNgFactory無法解決,因爲它不存在。目前沒有任何生成任何目錄,我不知道爲什麼。

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

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

import {enableProdMode} from '@angular/core'; 
import {AppModuleNgFactory} from '../aot/app/examples/app.module.ngfactory'; 
import {platformBrowser} from '@angular/platform-browser'; 

enableProdMode(); 
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory); 

/tsconfig.json

{ 
    "compilerOptions": { 
    "sourceMap": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "removeComments": false, 
    "noImplicitAny": false, 
    "typeRoots": [ 
     "node_modules/@types/" 
    ], 
    "target": "ES5", 
    "types": [ 
     "node", 
     "jasmine", 
     "es6-shim" 
    ] 
    }, 
    "compileOnSave": false, 
    "filesGlob": [ 
    "src/**/*.ts", 
    "src/**/*.tsx", 
    "!node_modules/**" 
    ], 
    "exclude": [ 
    "node_modules", 
    "dist/", 
    "**/*.spec.ts" 
    ], 
    "angularCompilerOptions": { 
    "genDir": "aot/", 
    "skipMetadataEmit": true 
    } 
} 

webpack-dist.conf

...imports  
module.exports = { 
    entry: { 
    app: './src/index.aot.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: './src/app/examples/index.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' 
    ] 
    } 
}; 

我缺少什麼?爲什麼我需要引用一個不存在於像index.aot.ts這樣的引導文件中的文件?

完整的錯誤輸出:

ERROR in ~/work/project/src/index.aot.ts (2,34): Cannot find module './aot/app/examples/app.module.ngfactory'. 

ERROR in Could not resolve "./src/app/examples/app.module" from "./src/app/examples/app.module". 

ERROR in ./src/index.aot.ts 
Module not found: Error: Can't resolve './aot/app/examples/app.module.ngfactory' in '~/work/project/src' 
@ ./src/index.aot.ts 4:29-79 

編輯:刪除platformBrowser()和生成的導入後,我到這個莫名其妙的錯誤:

ERROR in ./src/index.ts 
Module not found: Error: Can't resolve './../$$_gendir/src/app/examples/app.module.ts.ngfactory' 

回答

1

需要在main.ts中做下列事情:

platformBrowser().bootstrapModuleFactory(AppModuleNgFactory); 

你必須離開相同的引導代碼main.ts你有非AOT編譯:

platformBrowserDynamic().bootstrapModule(AppModule) 

角AOT裝載機將產生AOT編譯時代碼重構bootstrapModuleFactory。見here in the sources。重要的是skipCodeGeneration選項應該是false - 這是默認值。

+0

這可以消除1級的錯誤,並且引用的來源似乎表明這是正確的,但我仍然離開一步,原始錯誤之一仍然存在。 錯誤無法從「./src/app/examples/index.ts」解析「./src/app/examples/index.ts」。 錯誤在./src/index.ts中 找不到模塊:錯誤:無法解析'/ Users/a554173/work中的'./../aot/src/app/examples/index.ts.ngfactory'/angular2-student-loans-components/src' @ ./src/index.ts 8:27-82 –

+0

看起來我確實需要一個entryModule定義,儘管我不確定究竟是什麼問題。 –

+0

@StefanKendall,這個錯誤似乎超出了這個特定問題的範圍。您是否可以在顯示應用程序的項目結構的地方創建另一個問題?通常你不需要做任何特別的事情就可以用AOTPlugin獲得一個構建AOT。我通過創建新的angular-cli項目然後運行'ng eject'然後修改新的AOTPlugins選項(刪除skipCodeGeneration)然後運行'npm build'成功構建項目 –