Webpack 将 ts 文件的导出编译为空

我的 index.ts 文件中只有一行。


export * from "./foo"

foo.ts 文件中也只有一行。


export const foo = ()=> 'bar'

我只使用“npx webpack-cli init”中的默认配置。仅编辑“模式”和“输出”。


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"

}

这是我的 tsconfig.js


{

  "compilerOptions": {

    "allowSyntheticDefaultImports": false,

    "noImplicitAny": true,

    "module": "es6",

    "target": "es5",

    "allowJs": false,

    "sourceMap": true

  }

}

这是我的 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"

  }

}

当我运行构建时使用这些


"build": "webpack"

我在“dist/index.js”中得到了一个空的index.js。我缺少什么?


哔哔one
浏览 160回答 1
1回答

肥皂起泡泡

我认为问题在于删除未使用的webpack. 请记住,除非传递给它的输入代码的样式必须与配置的样式相同,否则能够webpack执行此操作。esmmodule: "es6"如果导出为库,您可能必须通过指定目标来告诉 webpack 构建为库,甚至可以添加名称:webpack.config.js{  output: {    library: "yourLibName",    libraryTarget: "umd",    filename: "index.js"  },}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript