Angular 2 Webpack AOT无法解析条目模块

Angular 2 Webpack AOT无法解析条目模块

问题描述:

我想安装一个使用Angular 2和Webpack的AOT构建(JIT构建工作正常)。每当我试图建立它,我得到ERROR in Could not resolve "src/app/app.module" from "src/app/app.module"。我试过除了AotPlugin之外,还移除了webpack插件,为AotPlugin选项使用了相对/绝对路径的每个组合,所有都没有结果。我使用的是webpack 2.2.1,typescript 2.0.10,@ ngtools/webpack 1.2.9和节点版本6.9.4。任何帮助将是惊人的,谢谢!从webpack.configAngular 2 Webpack AOT无法解析条目模块

摘录:

module: { 
    rules: [ 
    { test: /\ts$/, loader: @'ngtools/webpack } 
    ] 
} 

plugins: [ 
    new webpack.NoEmitOnErrorsPlugin(), 
    new AotPlugin({ 
    tsConfigPath: './tsconfig.json', 
    entryModule: 'src/app/app.module#AppModule', 
    mainPath: 'src/main' 
    }), 
    new webpack.optimize.UglifyJsPlugin({ 
    beautify: false, 
    mangle: { 
     screw_ie8: true, 
     keep_fnames: true 
    }, 
    compress: { 
     warnings: false, 
     screw_ie8: true 
    }, 
    comments: false 
    }), 
    new ExtractTextPlugin('[name].[hash].css'), 
    new webpack.DefinePlugin({ 
    'process.env': { 
    'ENV': JSON.stringify(ENV) 
    } 
    }) 
] 

tsconfig.json

{ 
    "compilerOptions": { 
    "baseUrl": "src", 
    "target": "es5", 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "sourceMap": false, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "removeComments": false, 
    "noImplicitAny": true, 
    "suppressImplicitAnyIndexErrors": true 
    }, 
    "compileOnSave": false 
} 
+0

尝试使用相对路径为:**/src目录/应用/ app.module ** – Fals

+0

虽然这并没有直接工作这个问题肯定与路径有关。我详细解答了这个问题的答案。 – peaches

回答我的问题,一定有一个路径问题。我一直在使用辅助函数来处理路径,由于某种原因,在这种情况下并没有。

辅助函数:

var path = require('path'); 

var _root = path.resolve(__dirname, '..'); 
function root(args) { 
    args = Array.prototype.slice.call(arguments, 0); 
    return path.join.apply(path, [_root].concat(args)); 
} 

使用:

entryModule: root('src', 'app', 'app.module#AppModule') 
+0

你救了我!出于某种原因,Webpack配置不喜欢相对路径。我使用了path.resolve(__ dirname,'path') – light24bulbs