# 未归档内容

# 编译成模块

说明:

  • 该配置用于开发模块

目录:

my-module/
  src/
    index.ts
  dist/
    index.esm.js
    index.cjs.js
    index.d.ts
    index.d.map.ts
  package.json
  tsconfig.json

配置:

  • webpack.config.js:

    module.exports = {
      experiments: {
        outputModule: true,
      },
    
      entry: {
        'index.esm': {
          import: './src/index.ts',
          library: {
            type: 'module',
          },
        },
        'index.cjs': {
          import: './src/index.ts',
          library: {
            type: 'commonjs2',
          },
        }
      },
    
      output: {
        clean: true,
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'),
      },
    };
    
  • package.json:

    {
      "main": "dist/index.cjs.js",
      "module": "dist/index.esm.js",
      "types": "dist/index.d.ts",
    }
    
  • tsconfig.json:

    {
      "compilerOptions": {
        "outDir": "dist",
        "declaration": true,
        "emitDeclarationOnly": false,
        "declarationMap": true,
      },
    }
    

# 参考

本章目录