1

尝试将我的 angular2 应用程序与 webpack2 和 angular2-template-loader 捆绑在一起,但出现此错误:

ERROR in ./~/@angular/compiler/src/compile_metadata.js
Module not found: Error: Can't resolve './' in 'C:\Users\eagor\Documents\work\node_modules\@angular\compiler\src'
 @ ./~/@angular/compiler/src/compile_metadata.js 393:22-35
 @ ./~/@angular/compiler/index.js
 @ ./~/@angular/platform-browser-dynamic/src/platform-browser-dynamic.js
 @ ./~/@angular/platform-browser-dynamic/index.js
 @ ./src/login/index.js

没有angular2-template-loader它的bunldes就好了。

src/login/index.js:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { LoginModule } from './src/login.module';

document.addEventListener('DOMContentLoaded', function() {
    platformBrowserDynamic()
        .bootstrapModule(LoginModule);
});

src/login/src/login.module.js

export class LoginModule {
    // make empty for example.
}

webpack.config.js:

var path = require("path");

module.exports = {
    entry: {
        login: "./src/login/index.js"
    },
    output: {
        path: path.resolve(__dirname, "wwwroot/bundles"),
        filename: "[name].min.js"
    },
    module: {
        loaders: [
            {
                test: /\.js$/, exclude: /node_modules/,
                loader: "babel-loader"
            },

            // adding this loader breaks
            {
                test: /\.js$/,
                loaders: ['angular2-template-loader'],
                exclude: [/\.(spec)\.js$/]
            },

            {
                test: /\.(html|css)$/,
                loader: 'raw-loader'
            }
        ]
    }
};
4

1 回答 1

2

您应该排除该node_modules文件夹:

{
  test: /\.js$/,
  loaders: ['angular2-template-loader'],
  exclude: [/node_modules/, /\.(spec)\.js$/]
}

否则,webpack 还会尝试通过管道.js传输位于node_modules文件夹中某处的文件,这是您不想要的,并且会破坏您的情况。

于 2016-12-01T18:34:10.863 回答