-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Closed
Labels
Description
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 import
s required by tree shaking.
If I'm correct, something like this should be used for tree shaking:
npm install babel-core babel-preset-es2015
- Add
.babelrc
with
{
"presets": [
[
"es2015",
{
"modules": false
}
]
]
}
tsconfig.json
should be set toes6
{
"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"
.
- 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
)
edo-zhouim-danwu, queq1890 and edo-zhou