在脚本中使用webpack 2.2.1解析路径中的ts-loader

时间:2017-02-15 16:27:00

标签: javascript node.js webpack gulp webpack-2

鉴于以下gulp任务。 我得到错误输出

  

找不到模块:错误:无法解析'wwwroot / js / admin'中的'app.ts'

gulp.task("admin:js", function (done) {
    module.exports = {
        context: "wwwroot/js/admin",
        entry: ["app.ts", "zippy.ts"],
        output: {
            filename: "admin.js"
        },
        devtool: "source-map",
        module: {
            rules: [
              { test: /\.ts$/, use: 'ts-loader' }
            ]
        }
    };
    webpack(module.exports).run(onBuild(done));
});

目录结构

wwwroot\js\admin\app.ts
wwwroot\js\admin\zippy.ts

我收到此错误:

Version: webpack 2.2.1
Time: 31ms
       Asset     Size  Chunks             Chunk Names
    admin.js  2.87 kB       0  [emitted]  main
admin.js.map  2.61 kB       0  [emitted]  main
chunk    {0} admin.js, admin.js.map (main) 40 bytes [entry] [rendered]
    [0] multi app.ts zippy.ts 40 bytes {0} [built]

ERROR in multi app.ts zippy.ts
Module not found: Error: Can't resolve 'app.ts' in 'wwwroot/js/admin'
 @ multi app.ts zippy.ts

ERROR in multi app.ts zippy.ts
Module not found: Error: Can't resolve 'zippy.ts' in 'wwwroot/js/admin'
 @ multi app.ts zippy.ts

这是一个路径问题,就好像我从命令行执行webpack,在wwwroot\js\admin目录中,没有上下文路径,文件被正确捆绑,一切正常。

当我使用API​​脚本并从项目根目录执行时(相对于.\wwwroot\js\admin),我得到了错误输出。

1 个答案:

答案 0 :(得分:0)

webpack在 context 配置条目中不支持使用相对路径

按以下方式更改context

module.exports = {
    context: __dirname + "/wwwroot/js/admin",
    ...

更正了输出。

Version: webpack 2.2.1
Time: 2337ms
       Asset     Size  Chunks                    Chunk Names
    admin.js  1.24 MB       0  [emitted]  [big]  main
admin.js.map  1.48 MB       0  [emitted]         main
chunk    {0} admin.js, admin.js.map (main) 1.24 MB [entry] [rendered]
    [0] ./~/angular/index.js 48 bytes {0} [built]
    [1] ./wwwroot/js/admin/app.ts 408 bytes {0} [built]
    [2] ./wwwroot/js/admin/zippy.ts 1.16 kB {0} [built]
    [3] ./~/angular/angular.js 1.24 MB {0} [built]
    [4] multi ./app.ts ./zippy.ts 40 bytes {0} [built]
[10:30:37] Finished 'admin:js' after 2.43 s