1. 程式人生 > 其它 >webpack-自定義library

webpack-自定義library

技術標籤:webpack-配置webpackjavascript

  • package.json
{
  "name": "library",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/library.js",
  "scripts": {
    "build": "webpack"
  },
  "author": "Xavier",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.12.10",
    "@babel/plugin-transform-runtime": "^7.12.10",
    "@babel/preset-env": "^7.12.11",
    "babel-loader": "^8.2.2",
    "clean-webpack-plugin": "^3.0.0",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12"
  },
  "dependencies": {
    "@babel/runtime": "^7.12.5",
    "@babel/runtime-corejs3": "^7.12.5"
  }
}
  • webpack.config.js
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
  mode: 'production',
  devtool: 'cheap-module-source-map',
  entry: {
    index: './src/index.js'
  },
  // externals: 'lodash',
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'storageObj.js',
    library: 'storageObj',
    libraryTarget: 'umd'
  },
  plugins: [
    new CleanWebpackPlugin()
  ]
}
  • .babelrc
{
  "presets": [
    "@babel/preset-env"
  ],
  "plugins": [
    [
      "@babel/plugin-transform-runtime",
      {
        "corejs": 3,
        "helpers": true,
        "regenerator": true,
        "useESModules": false
      }
    ]
  ]
}
  • index.js
import { hl } from './public/HappyLocalStorage.js';
import { hc } from './public/HappyCookie.js';
export {
  hl, hc
}
  • 專案結構圖
    在這裡插入圖片描述
  • 專案地址: 碼雲.