nippur72 / ifdef-loader

Webpack loader for JavaScript/TypeScript conditional compilation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to config for .ts file?

TerryChan opened this issue · comments

There are config webpack example for .tsx file in the Readme, and it worked for .tsx file in my project, but it's not work for .ts file when I add #if comment in .ts file.
The code below is my webpack config, it used awesome-typescript-loader for .tsx file, and added resolve for .ts, .tsx file:

  module: {
    rules: [
      {
        test: /\.tsx?$/,
        include: path.resolve(__dirname, 'src'),
        use: [
          {
            loader: 'awesome-typescript-loader',
            options: {
                  useBabel: true,
                  useCache: true,
                },
          },
          { loader: 'ifdef-loader', options: opts },
        ],
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.js', '.ts', '.tsx'],
    modules: [path.resolve(__dirname, 'node_modules/'), 'node_modules'],
  },

Anyone know how to make it work for .ts file?

it should work with .ts files as well, the regex /\.tsx?$/ in line 4 matches both .tsx and .ts.

Also .tsx is a superset of .ts, so what works for it works also for .ts.

Yes, you are right, it also works for .ts file. And it's working well in my project now.
It was my mistake, I tested in the file that out of the include field, which means out of the src folder.
Thanks for your reply.