简体   繁体   English

Webpack + TypeScript +模块加载

[英]Webpack + TypeScript + Module Loading

Basically, module loading is a pain in JavaScript right now sigh ... 基本上,模块加载是JavaScript的痛苦,现在叹息 ......

So I have a TypeScript application I'd like to be compiled with webpack. 所以我有一个TypeScript应用程序,我想用webpack编译。 The issue is that my editor (vscode) seems to expect that modules are imported without extensions. 问题是我的编辑器(vscode)似乎期望导入的模块没有扩展。 For example: 例如:

import {IServer} from "../server/server";

In webpack, I can only get this to work if I include an extension. 在webpack中,如果我包含扩展名,我只能使用它。 If I include an extension (ie "../server/server.ts") it builds in webpack, but the editor doesn't recognize it and throws an error that the module wasn't found. 如果我包含一个扩展名(即“../server/server.ts”),它会在webpack中构建,但编辑器无法识别它并抛出一个错误,表明找不到该模块。 If I omit an extension (ie "../server/server"), webpack won't load it (it says it can't find the module "../server/server"), but the editor will load it for purposes of code-completion, etc. 如果我省略一个扩展名(即“../server/server”),webpack 将不会加载它(它说它找不到模块“../server/server”),但编辑器会加载它代码完成等目的

This leads me to believe that importing modules in TypeScript are expected to be done without extensions, whereas in the webpack ecosystem, they are required (which makes sense with how loaders work, etc). 这让我相信,TypeScript中的导入模块预计会在没有扩展的情况下完成,而在webpack生态系统中,它们是必需的(这对于加载器的工作方式等有意义)。

This leaves a very bad taste in my mouth. 这在我的嘴里留下了非常糟糕的味道。 My question here is: is my conclusion correct? 我的问题是:我的结论是否正确? Do I have to trade off between the build system or the editor? 我是否需要在构建系统或编辑器之间进行权衡? Or am I missing something? 或者我错过了什么? Is it possible to have the webpack typescript loader silently add in the extensions so the modules are properly recognized by webpack during the build? 是否可以让webpack打字机加载程序在扩展中静默添加,以便在构建期间webpack正确识别模块?

I have the following webpack.config.js file: 我有以下webpack.config.js文件:

module.exports = [
    {
        name: "Server",
        entry: "./src/server/server.ts",
        output: {
            filename: "./server/server.js"
        },
        target: "node",
        resolve: [".ts", ".js"],
        module: {
            loaders: [
                { test: /\.ts$/, loader: 'ts-loader' }
            ]
        }
    },
    {
        name: "Client",
        entry: "./src/client/scripts/client.ts",
        output: {
            filename: "./public/scripts/client.js"
        },
        resolve: ["", ".ts", ".js", ".less"],
        module: {
            loaders: [
                { test: /\.ts$/, loader: 'ts-loader' },
                { test: /\.less$/, loader: "style!css!less" },
                { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&minetype=application/font-woff" },
                { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" },
                { test: /\.png$/, loader: "url-loader?mimetype=image/png" }
            ]
        }
    }
];

And the following tsconfig file: 以下tsconfig文件:

{
    "compilerOptions": {
        "target": "ES5",
        "module": "commonjs",
        "jsx": "react"
    },

    "files": [
        "src/shared/typings/tsd.d.ts",
        "src/shared/typings/webpack.d.ts"
    ]
}

And I'm using the following packages in node: 我在节点中使用以下包:

"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"css-loader": "^0.23.0",
"file-loader": "^0.8.5",
"less": "^2.5.3",
"less-loader": "^2.2.1",
"react-hot-loader": "^1.3.0",
"style-loader": "^0.13.0",
"ts-loader": "^0.7.1",
"typescript": "^1.6.2",
"url-loader": "^0.5.7",
"webpack-dev-server": "^1.12.1",
"moment": "^2.10.6"

is my conclusion correct 我的结论是正确的

No. Extensions should definitely not be there. 不,扩展名绝对不应该存在。

Fix 固定

resolve: [".ts", ".js"],

Should be : 应该 :

resolve: {
    extensions: ['.ts', '.js']
},

When in doubt, check the tests : https://github.com/TypeStrong/ts-loader/tree/master/test 如有疑问,请检查测试: https//github.com/TypeStrong/ts-loader/tree/master/test

in webpack, I can only get this to work if I include an extension. 在webpack中,如果我包含扩展名,我只能使用它。

Definitely not needed. 绝对不需要。

whereas in the webpack ecosystem, they are required 而在webpack生态系统中,它们是必需的

No again. 再也没有。 They are not required. 它们不是必需的。

entry: "./src/server/server.ts", 条目:“./ src / server / server.ts”,

This should be entry: "./src/server/server" . 这应该是entry: "./src/server/server" Also checkout the extensive list of tests https://github.com/TypeStrong/ts-loader/tree/master/test 还要查看广泛的测试列表https://github.com/TypeStrong/ts-loader/tree/master/test

I overcame all the compiling issues and created a project template here, https://github.com/c9s/ts-webpack 我克服了所有的编译问题并在这里创建了一个项目模板, https://github.com/c9s/ts-webpack

You can just clone the project to start without fighting with the config files... 你可以克隆项目开始而不用配置文件打架......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM