是否可以在打字稿中编写 webpack 配置? [英] Is it possible to write webpack config in typescript?

查看:36
本文介绍了是否可以在打字稿中编写 webpack 配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在搜索时看到有 webpack 的类型.所以看起来我可以在打字稿中编写 webpack.config.js ?但是我该怎么做呢?

I saw, when searching, that there are typings for webpack. So it seems I can write webpack.config.js in typescript? But how can I do that?

推荐答案

你可以使用 TS 作为你的配置文件 (webpack.config.ts)

You can use TS as your config file (webpack.config.ts)

对此有明确的线索,请参阅 源代码

There is a clear clue for that, see Source Code

使用的 ** interpret** 模块有一个扩展文件及其加载器的列表.

The ** interpret** module used there is a list of extension files and their loaders.

在突出显示的代码中,webpack 为默认文件生成所有可能的扩展名的数组.

In the highlighted code webpack generates an array of all possible extension for the default files.

例如,给 webpack.config 你会得到一个数组

For example, give webpack.config you will get an array with

  • webpack.config.ts
  • webpack.config.js
  • ......等等

为此,您需要安装支持加载扩展程序的软件包之一.

For this to work you need to install one of the packages that support loading your extension.

例如,TS 有一些节点包,使您能够require('something.ts'),并且该包将完成这项工作.

For example, TS has some node packages that enable you to require('something.ts') and the package will do the work.

interpret 包声明需要这些包之一

The interpret package states that one of these packages is required

ts-node、typescript-node、typescript-register、typescript-require

ts-node, typescript-node, typescript-register, typescript-require

所以 npm/yarn ts-node 然后只需放置一个 webpack.config.ts 文件,它就会正常工作!

So npm/yarn ts-node then just put a webpack.config.ts file and it will just work!

编辑:Webpack 文档现在有关于配置的专门部分语言,包括 TypeScript、CoffeeScript 和 Babel &JSX

EDIT: Webpack documentation now has dedicated section on configuration languages which includes TypeScript, CoffeeScript and Babel & JSX

这篇关于是否可以在打字稿中编写 webpack 配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆