typescript Webpack ts-loader :更改 tsconfig 文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40426378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Webpack ts-loader : change tsconfig filename
提问by gr3g
I have 2 tsconfigs, one for my dev build and one for my prod build.
I choose the tsconfig with the -p
flag :tsc -p dev.tsconfig.json
我有 2 个 tsconfigs,一个用于我的开发构建,一个用于我的生产构建。
我选择带有-p
标志的 tsconfig :tsc -p dev.tsconfig.json
Ts-loader is looking for a tsconfig.json file. How can I specify another filename with the ts-loader?
Ts-loader 正在寻找 tsconfig.json 文件。如何使用 ts-loader 指定另一个文件名?
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" }
]
}
};
采纳答案by goenning
Webpack enables us to add custom options for each loader. All ts-loader
configuration properties are described here.
Webpack 使我们能够为每个加载器添加自定义选项。此处ts-loader
描述了所有配置属性。
configFileName
is the property you are looking for. It should be like this.
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'
}
};
回答by Jamie Birch
Here is how to specify configFile
(formerly configFileName
, as Frank notes), as of December 2017 (Webpack 3.10.0).
以下是截至 2017 年 12 月 (Webpack 3.10.0) 的指定方法configFile
(原为configFileName
,如 Frank 所注)。
Webpack config
网络包配置
Note:Since version 2, Webpack has been using module.rules
rather than module.loaders
, and has deprecated use of query params in favour of an options
object.
注意:从版本 2 开始,Webpack 一直使用module.rules
而不是module.loaders
,并且已经弃用查询参数而支持options
对象。
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: []
};
Directory structure
目录结构
The root of my directory structure looks like:
我的目录结构的根目录如下:
webpack.config.js
webpack_configs/tsconfig.webpack.json
tsconfig.json
package.json
src/*
test/*
node_modules/*
build/*
... Where *
indicates multiple files.
... 其中*
表示多个文件。
Typescript config
打字稿配置
tsconfig.webpack.json
itself just extends my generic tsconfig.json
by excluding the test
folder:
tsconfig.webpack.json
本身只是tsconfig.json
通过排除test
文件夹来扩展我的泛型:
{
"extends": "../tsconfig",
"exclude": [
"node_modules",
"test"
]
}
... But you needn't have two webpack configs to benefit from the configFile
setting; you could simply use it to target your singular tsconfig.json
from a custom location.
...但是你不需要有两个 webpack 配置才能从configFile
设置中受益;您可以简单地使用它tsconfig.json
从自定义位置定位您的单数。
回答by mvermand
In Webpack 4 you need to specify the options into a use
object:
在 Webpack 4 中,您需要将选项指定到一个use
对象中:
use: [{
loader: 'ts-loader',
options: {
configFile: "tsconfig.webpack.json"
}
}]
Complete Webpack config:
完整的 Webpack 配置:
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
};
回答by Frank Stallone III
回答by jainmitesh09
More like below:
更像下面:
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {configFile: 'tsconfig.webpack.json'}
}
https://www.npmjs.com/package/ts-loader#configfile-string-defaulttsconfigjson
https://www.npmjs.com/package/ts-loader#configfile-string-defaulttsconfigjson
回答by Premchandra Singh
Try using configFileafter the loader name as querystring. Here is webpack configand ts-loader's config
尝试在加载程序名称后使用configFile作为查询字符串。这是webpack 配置和ts-loader的配置
module.exports = {
entry : "./main.ts",
output : {
path : __dirname + "/dist",
filename : "bundle.js"
},
resolve : {
extensions : ["", ".js", ".ts"]
},
module: {
loaders: [
{ test: /\.ts?$/, loader: "ts-loader?configFile=src/tsconfig.server.json" }
]
},
};