打包一个library

135 阅读1分钟
//核心代码
const path = require('path')
const HtmlWebpackplugin = require('html-webpack-plugin')
module.exports = {
  mode: 'production',
  entry: './src/index.js',
  // 不打包lodash,让业务代码加载lodash
  externals: ['lodash'],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'library.js',
    // 往全局注入library变量
    library: 'library',
    // 配置可支持的语法/import/require
    libraryTarget: 'umd',
  },
  plugins: [
    new HtmlWebpackplugin({
      template: './src/index.html'
    }),
  ],
  devServer: {
    contentBase: './dist',
    port: 3000,
    open: true,
    hot: true
  }
}
//在package.json中
"main": "./dist/library.js",
//在npm库里面发布代码