s-panferov / awesome-typescript-loader

Awesome TypeScript loader for webpack
Other
2.35k stars 179 forks source link

Finding config file in a directory of the webpack entry resource #623

Open maxxporoshin opened 5 years ago

maxxporoshin commented 5 years ago

Hello guys!

Switched from ts-loader and run into inconvenience with typescript config files.

The problem is that config file is searched in the root directory for each entry despite its path.

The question is is it possible to make awesome-typescript-loader search config in the resource directory like ts-loader does?

Example:

Some directories in the project have their own tsconfig.json and each of them has corresponding entry point in the webpack config (or configs). To make awesome-typescript-loader use the right config, you have to specify config path for every entry which often means duplication of module/module.rules options.

Project structure:

dir1
--index1.ts
--tsconfig.json
dir2
--index2.ts
--tsconfig.json
index.ts
tsconfig.json
webpack.config.js

webpack.config.js:

module.exports = [{
    entry: { index: './index.ts' },
    module: {
         rules: [{
            test: /\.ts$/,
            loader: 'awesome-typescript-loader',
            options: {
                configFileName: './tsconfig.json',
            }
         }, ...]
    }
    ...
}, {
    entry: { index1: './dir1/index1.ts' },
    module: {
         rules: [{
            test: /\.ts$/,
            loader: 'awesome-typescript-loader',
            options: {
                configFileName: './dir1/tsconfig.json',
            }
         }, ...]
    }
    ...
}, {
    entry: { index2: './dir2/index2.ts' },
    module: {
         rules: [{
            test: /\.ts$/,
            loader: 'awesome-typescript-loader',
            options: {
                configFileName: './dir2/tsconfig.json',
            }
         }, ...]
    }
    ...
}];

Technically, this can be done with something like changing line

configFilePath = tsImpl.findConfigFile(context, tsImpl.sys.fileExists)

to

configFilePath = tsImpl.findConfigFile(webpack.resourcePath, tsImpl.sys.fileExists)

but is there any reason why this cannot / not intended to be done? I can open a PR myself if it's fine.

MacKentoch commented 5 years ago

Having same question.

My project sructure would be

front
--index1.ts
--tsconfig.json
server
--index2.ts
--tsconfig.json
index.ts

webpack.config.js

Trying to use configFileName but it does not work:

plugins: [
    new TsConfigPathsPlugin({
      configFileName: path.join(__dirname, '/front/tsconfig.json'),
    }),
...