Typescript 网页包ts加载程序:更改tsconfig文件名
我有两个tsconfig,一个用于开发构建,一个用于产品构建。Typescript 网页包ts加载程序:更改tsconfig文件名,typescript,webpack,tsconfig,ts-loader,Typescript,Webpack,Tsconfig,Ts Loader,我有两个tsconfig,一个用于开发构建,一个用于产品构建。 我选择带有-p标志的tsconfig: tsc-p dev.tsconfig.json Ts loader正在查找tsconfig.json文件。如何使用ts加载程序指定另一个文件名 module.exports = { entry : __dirname + "/src/app/main.ts", output : { path : __dirname + "/dist", file
我选择带有
-p
标志的tsconfig:tsc-p dev.tsconfig.json
Ts loader正在查找tsconfig.json文件。如何使用ts加载程序指定另一个文件名
module.exports = {
entry : __dirname + "/src/app/main.ts",
output : {
path : __dirname + "/dist",
filename : "bundle.js"
},
resolve : {
extensions : ["", ".webpack.js", ".web.js", ".js", ".ts"]
},
module: {
loaders: [
{ test: /\.ts?$/, loader: "ts" }
]
}
};
更新(第4页):如评论中所述,语法已更改,如中所述
Webpack使我们能够为每个加载程序添加自定义选项。描述了所有
ts加载器配置属性
configFileName
是您要查找的属性。应该是这样的
module.exports = {
entry : __dirname + "/src/app/main.ts",
output : {
path : __dirname + "/dist",
filename : "bundle.js"
},
resolve : {
extensions : ["", ".webpack.js", ".web.js", ".js", ".ts"]
},
module: {
loaders: [
{ test: /\.ts?$/, loader: "ts" }
]
},
ts: {
configFileName : 'dev.tsconfig.json'
}
};
2017年9月6日更新
configFileName
已被弃用,取而代之的是configFile
-尝试在加载程序名称后使用configFile作为querystring。这是和
以下是如何指定2017年12月起的configFile
(以前的configFileName
,如Frank所述)(网页3.10.0)
网页包配置
注意:,Webpack一直在使用模块.rules
而不是模块.loaders
,并且不推荐使用查询参数而使用选项
对象
module.exports = {
entry: {
"./build/bundle" : "./src/startup/Entry.ts"
},
output: {
filename: '[name].js',
libraryTarget: 'this'
},
devtool: 'source-map',
resolve: {
modules: [".", "node_modules"],
extensions: [".js", ".webpack.js", ".web.js", ".d.ts", ".ts", ".tsx"]
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: [/node_modules/],
loader: "ts-loader",
options: {
configFile: "webpack_configs/tsconfig.webpack.json"
}
}
]
},
plugins: []
};
目录结构
我的目录结构的根目录如下所示:
webpack.config.js
webpack_configs/tsconfig.webpack.json
tsconfig.json
package.json
src/*
test/*
node_modules/*
build/*
。。。其中,*
表示多个文件
类型脚本配置
tsconfig.webpack.json
本身通过排除test
文件夹扩展了我的通用tsconfig.json
:
{
"extends": "../tsconfig",
"exclude": [
"node_modules",
"test"
]
}
。。。但是,您不需要有两个网页包配置就可以从configFile
设置中获益;您可以简单地使用它从自定义位置将您的单数tsconfig.json
作为目标。在Webpack 4中,您需要将选项指定到use
对象中:
use: [{
loader: 'ts-loader',
options: {
configFile: "tsconfig.webpack.json"
}
}]
完成网页包配置:
var path = require('path');
module.exports = {
entry: path.resolve(__dirname, 'src') + '/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'your-library-name.js',
},
module: {
rules: [
{
test: /\.ts$/,
use: [{
loader: 'ts-loader',
options: {
configFile: "tsconfig.webpack.json"
}
}],
exclude: /node_modules/,
}
]
},
mode: 'development',
resolve: {
extensions: ['.js', '.ts']
},
devtool: false
};
更像如下:
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {configFile: 'tsconfig.webpack.json'}
}
完美!因此,我将这样做:module.exports=env=>({ts:{configFileName:env.build==='dev'?'dev.tsconfig.json':'acc.tsconfig.json'}})
。文档中不再提到ts.configFileName
选项该选项不再有效,请参阅不确定他们是否更改了该选项,但该选项的名称为:configFile
:。答案中建议的名称使webpack抛出错误。我没有足够的代表发表评论,因此很抱歉将此添加为答案。显然有人不喜欢那样这不再适用于Webpack4。请参阅本页我的答案中的解决方案。太棒了,谢谢!还请注意,对于其他仍在挣扎的用户,如果您提供了tsconfig的相对路径,则ts loader文档中的“它将相对于相应的.ts条目文件进行解析。”
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {configFile: 'tsconfig.webpack.json'}
}