简体   繁体   English

配置 WebPack 为 Typescript 导入

[英]Configuring WebPack for Typescript with import

The tutorial of webpack configuration for typescript shows something like this: typescript 的 webpack 配置教程显示如下:

const path = require('path');

module.exports = { ... }

Would it not be better to use it as es module and configure it with eg imports.将它用作 es 模块并使用 eg imports 配置它不是更好吗? Or is there a reason why it is configured like above?或者有没有像上面那样配置的原因? I cant find any example where it is configured like this:我找不到任何配置如下的示例:

import webpack from "webpack";
import path from "path";

export default () => { ... }

TypeScript and the newer ES standard are supersets of normal JavaScript. Writing config files using widely supported syntax and features makes it more widely available and acceptable without requiring additional setup. TypeScript 和较新的 ES 标准是普通 JavaScript 的超集。使用广泛支持的语法和功能编写配置文件使其更广泛可用和可接受,而无需额外设置。

TypeScript is a better practice in some ways, but you need to introduce extra dependencies and configuration to use it, in some organizations you don't even have that freedom. TypeScript 在某些方面是一种更好的做法,但您需要引入额外的依赖项和配置才能使用它,在某些组织中您甚至没有这种自由。 Similar to ES, Node.js didn't have native support for mjs until v12.和ES类似,Node.js直到v12才原生支持mjs。

The good news is that Webpack also supports writing configurations in multiple languages including TypeScript, see https://webpack.js.org/configuration/configuration-languages/好消息是Webpack还支持包括TypeScript在内的多种语言编写配置,参见https://webpack.js.org/configuration/configuration-languages/

It's also available to get features like IntelliSense by using TypeScript JSDoc annotations if for some reason you can't write TypeScript directly:如果由于某种原因您不能直接编写 TypeScript,也可以通过使用 TypeScript JSDoc 注释来获取 IntelliSense 等功能:

/** @type { import('webpack').Configuration } */
const config = {...};
module.exports = config;

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

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