webpack 构建 ts 时,输出为空(只有一行 require)

242 阅读1分钟

原因:ts 编写的是 vscode 扩展,入口文件属于模块。所以需要在 webpack 中指定 libraryTarget: 'commonjs' 。不过这个配置项未来可能会被废弃,建议使用 output.library.type="commonjs" 代替。

完整的 webpack.config.js 文件如下:

const path = require("path");

module.exports = {
  entry: "./src/index.ts",
  externals: {
    vscode: "commonjs vscode",
  },
  output: {
    filename: "index.js",
    path: path.resolve(__dirname, "dist"),
    library: {
      type: "commonjs",
    },
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        exclude: /node_modules/,
        use: ["ts-loader"],
      },
    ],
  },
};