Skip to content

Typescript + Tree shaking configuration #995

@dmitriid

Description

@dmitriid

Relevant page: https://webpack.js.org/guides/webpack-and-typescript/

If I understand this correctly, this setup will not trigger webpack's tree shaking algorithm, because the loader will produce a CommonJS-stye require, not static imports required by tree shaking.

If I'm correct, something like this should be used for tree shaking:


  1. npm install babel-core babel-preset-es2015
  2. Add .babelrc with
{
  "presets": [
    [
      "es2015",
      {
        "modules": false
      }
    ]
  ]
}
  1. tsconfig.json should be set to es6
{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "es6",
    "target": "es6",
    "jsx": "react",
    "allowJs": true
  }
}

If TS compiler complain about modules not being found, try adding "moduleResolution": "node" to "compilerOptions".

  1. webpack.config should invoke both loaders:
module.exports = {
 entry: './index.ts',
 output: {
   filename: 'bundle.js',
   path: __dirname
 },
 module: {
   rules: [
     {
       test: /\.tsx?$/,
       use: ['babel-loader', 'ts-loader']
       exclude: /node_modules/,
     },
   ]
 },
 resolve: {
   extensions: [".tsx", ".ts", ".js"]
 },
};

(Note, loaders should be replaced by use)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions