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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:59:34  来源:igfitidea点击:

Webpack ts-loader : change tsconfig filename

typescriptwebpacktsconfigts-loader

提问by gr3g

I have 2 tsconfigs, one for my dev build and one for my prod build.
I choose the tsconfig with the -pflag :
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-loaderconfiguration properties are described here.

Webpack 使我们能够为每个加载器添加自定义选项。此处ts-loader描述所有配置属性。

configFileNameis 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.rulesrather than module.loaders, and has deprecated use of query params in favour of an optionsobject.

注意:从版本 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.jsonitself just extends my generic tsconfig.jsonby excluding the testfolder:

tsconfig.webpack.json本身只是tsconfig.json通过排除test文件夹来扩展我的泛型:

{
    "extends": "../tsconfig",
     "exclude": [
       "node_modules",
       "test"
     ]
}

... But you needn't have two webpack configs to benefit from the configFilesetting; you could simply use it to target your singular tsconfig.jsonfrom a custom location.

...但是你不需要有两个 webpack 配置才能从configFile设置中受益;您可以简单地使用它tsconfig.json从自定义位置定位您的单数。

回答by mvermand

In Webpack 4 you need to specify the options into a useobject:

在 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

Update @ September 6, 2017

2017 年 9 月 6 日更新

configFileNamehas been deprecated in favor of configFile- Source

configFileName已被弃用,取而代之的是configFile-来源

回答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" }
     ]
   },
};