Webpack compile export from ts file into nothing

There is only one line in my index.ts file.

export * from "./foo"

There is only one line in foo.ts file too.

export const foo = ()=> 'bar'

I’m only using default config from “npx webpack-cli init”. Only “mode” and “output” are edited.

const TerserPlugin = require('terser-webpack-plugin');


module.exports = {
  mode: 'production',
  entry: './src/index.ts',
  output:{
    filename:'index.js'
  },
  plugins: [new webpack.ProgressPlugin()],

  module: {
    rules: [{
      test: /.(ts|tsx)$/,
      loader: 'ts-loader',
      include: [path.resolve(__dirname, 'src')],
      exclude: [/node_modules/],
      options:{
        transpileOnly: true
      }
    }]
  },

  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },

  optimization: {
    minimizer: [new TerserPlugin()],

    splitChunks: {
      cacheGroups: {
        vendors: {
          priority: -10,
          test: /[\/]node_modules[\/]/
        }
      },

      chunks: 'async',
      minChunks: 1,
      minSize: 30000,
      name: false
    }
  },
  target:"web"
}

And here is my tsconfig.js

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": false,
    "noImplicitAny": true,
    "module": "es6",
    "target": "es5",
    "allowJs": false,
    "sourceMap": true
  }
}

Here is my package.json

{
  "name": "npm",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "build": "webpack"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@webpack-cli/init": "^1.0.3",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "terser-webpack-plugin": "^5.0.3",
    "ts-loader": "^8.0.12",
    "typescript": "^4.1.2",
    "webpack": "^5.10.0",
    "webpack-cli": "^4.2.0"
  }
}

With these when I run build

"build": "webpack"

I got an empty index.js in “dist/index.js”. What am I missing?

Answer

I think the problem is here all about to remove unused export of webpack. Keep in mind that webpack is able to do this unless the input code passed to it must be esm style as same as you’ve done by configured module: "es6".

In case of exporting as library, you might have to tell webpack to build as library by specifying target or even could add a name:

webpack.config.js

{
  output: {
    library: "yourLibName",
    libraryTarget: "umd",
    filename: "index.js"
  },
}