Webpack并非所有源图都显示打字稿

时间:2016-11-12 14:05:39

标签: javascript angular typescript webpack

所以,我的项目结构如下:

Root
|   index.html
|   package.json
|   tree.txt
|   tsconfig.json
|   webpack.config.js
|   
+---.vscode
|       settings.json
|       tasks.json
|       
+---app
|   |   main.js
|   |   main.js.map
|   |   main.ts
|   |   test.js
|   |   test.js.map
|   |   test.ts
|   |   
|   +---components
|   |       app.component.js
|   |       app.component.js.map
|   |       app.component.ts
|   |       
|   \---modules
|           app.module.js
|           app.module.js.map
|           app.module.ts
|           
\---node_modules
    |
    *SNIP*

我的问题是只有我的main.ts源图在chrome dev工具中显示为实际的打字稿,app.module,app.component和test显示为已编译的javascript。

我设法找到的有关webpack打字稿的任何其他问题都是特定于babel加载程序,根本没有生成断点或源图的问题。

我的tsconfig:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "watch": true,
        "compileOnSave": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false
    },
    "exclude": [
        "node_modules",
        "typings/browser",
        "typings/browser.d.ts"
    ],
    "atom": {
        "rewriteTsconfig": false
    },
    "typeRoots": [
        "./node_modules/@types"
    ],
    "types": [
        "core-js"
    ]
}

我的webpack.config.js:

const config = {
    entry: {
        app: "./app/main.ts"
    },
    output: {
        filename: "./bundle.js",
        path: "./"
    },
    devtool: "source-map",
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loaders: ["awesome-typescript-loader", "angular2-template-loader"]
            }
        ]
    },
    resolve: {
        extentions: ["", ".ts", ".js"]
    }
}

module.exports = config;

主要的Chrome开发工具输出:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './modules/app.module';
import { test } from "./test";
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
alert(test.msg)


// WEBPACK FOOTER //
// ./~/angular2-template-loader!./app/main.ts

适用于test.js的Chrome开发工具输出:

"use strict";
var test = (function () {
    function test() {
    }
    test.msg = "test";
    return test;
}());
exports.test = test;
//# sourceMappingURL=test.js.map


//////////////////
// WEBPACK FOOTER
// ./app/test.js
// module id = 25
// module chunks = 0

所有单独的.map文件源都指向脚本的ts版本。

webpack-dev-server命令输出:

webpack-dev-server -d
 http://localhost:8080/webpack-dev-server/
webpack result is served from /
content is served from C:\Users\Joseph Goodley\Desktop\stockmonitor\frontend
Hash: b3331cfe915060534a0f
Version: webpack 1.13.3
Time: 7618ms
          Asset     Size  Chunks             Chunk Names
    ./bundle.js  1.54 MB       0  [emitted]  app
./bundle.js.map  1.75 MB       0  [emitted]  app
chunk    {0} ./bundle.js, ./bundle.js.map (app) 1.49 MB [rendered]
    [0] ./app/main.ts 326 bytes {0} [built]
    [1] ./~/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js 6.89 kB {0} [built]
    [2] ./~/@angular/compiler/bundles/compiler.umd.js 825 kB {0} [built]
    [3] ./~/@angular/core/bundles/core.umd.js 386 kB {0} [built]
    [4] ./~/rxjs/Subject.js 5.33 kB {0} [built]
    [5] ./~/rxjs/Observable.js 6.04 kB {0} [built]
    [6] ./~/rxjs/util/root.js 471 bytes {0} [built]
    [7] ./~/rxjs/util/toSubscriber.js 707 bytes {0} [built]
    [8] ./~/rxjs/Subscriber.js 8.87 kB {0} [built]
    [9] ./~/rxjs/util/isFunction.js 148 bytes {0} [built]
   [10] ./~/rxjs/Subscription.js 5.99 kB {0} [built]
   [11] ./~/rxjs/util/isArray.js 146 bytes {0} [built]
   [12] ./~/rxjs/util/isObject.js 151 bytes {0} [built]
   [13] ./~/rxjs/util/tryCatch.js 422 bytes {0} [built]
   [14] ./~/rxjs/util/errorObject.js 177 bytes {0} [built]
   [15] ./~/rxjs/util/UnsubscriptionError.js 1.07 kB {0} [built]
   [16] ./~/rxjs/Observer.js 193 bytes {0} [built]
   [17] ./~/rxjs/symbol/rxSubscriber.js 270 bytes {0} [built]
   [18] ./~/rxjs/symbol/observable.js 631 bytes {0} [built]
   [19] ./~/rxjs/util/ObjectUnsubscribedError.js 955 bytes {0} [built]
   [20] ./~/rxjs/SubjectSubscription.js 1.4 kB {0} [built]
   [21] ./~/@angular/platform-browser/bundles/platform-browser.umd.js 120 kB {0} [built]
   [22] ./~/@angular/common/bundles/common.umd.js 121 kB {0} [built]
   [23] ./app/modules/app.module.js 1.42 kB {0} [built]
   [24] ./app/components/app.component.js 1.22 kB {0} [built]
   [25] ./app/test.js 175 bytes {0} [built]
webpack: bundle is now VALID.

我注意到它在[23-24]上构建了js文件,但是我不知道为什么,另外只运行webpack --debug会生成一个bundle.js.map文件,其源代码指向也是js文件。

这个问题已经困扰了我很长一段时间,所以如果它很简单,我道歉。

感谢您的帮助!

更新

问题可能是我在网络包装之前编译了ts吗?我试图在没有编译文件的情况下运行webpack,但现在找不到我的ts文件:

./app/main.ts
Module not found: Error: Cannot resolve 'file' or 'directory' ./modules/app.module in C:\--\app
resolve file
  C:\--\app\modules\app.module doesn't exist
  C:\--\app\modules\app.module.webpack.js doesn't exist
  C:\--\app\modules\app.module.web.js doesn't exist
  C:\--\app\modules\app.module.js doesn't exist
  C:\--\app\modules\app.module.json doesn't exist
resolve directory
  C:\--\app\modules\app.module doesn't exist (directory default file)
  C:\--\app\modules\app.module\package.json doesn't exist (directory description file)
[C:\--\app\modules\app.module]
[C:\--\app\modules\app.module.webpack.js]
[C:\--\app\modules\app.module.web.js]
[C:\--\app\modules\app.module.js]
[C:\--\app\modules\app.module.json]
 @ ./app/main.ts 3:19-50

为什么不解析我的.ts文件?

2 个答案:

答案 0 :(得分:1)

发现了这个问题。

在我的package.config “扩展名”内应该是“扩展程序”,遗憾的是我没有错误指示语法问题。

答案 1 :(得分:0)

正如您所说,错误在webpack.config.js内。从Webpack 2开始,当您有一个无效的配置对象时,它会给出错误消息,因此升级Webpack版本将帮助您将来避免这些问题。

如果您不想升级webpack,可以使用此软件包自动验证您的配置:https://github.com/js-dxtools/webpack-validator