Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack to umd package #72

Open
hsipeng opened this issue Sep 7, 2018 · 0 comments
Open

webpack to umd package #72

hsipeng opened this issue Sep 7, 2018 · 0 comments
Labels

Comments

@hsipeng
Copy link
Owner

hsipeng commented Sep 7, 2018

build to umd which one can AMD or commons use require or import .

  • index.js
import _ from 'lodash';
import numRef from './ref.json';

export function numToWord(num) {
  return _.reduce(numRef, (accum, ref) => {
    return ref.num === num ? ref.word : accum;
  }, '');
};

export function wordToNum(word) {
  return _.reduce(numRef, (accum, ref) => {
    return ref.word === word && word.toLowerCase() ? ref.num : accum;
  }, -1);
};
  • package.json
{
  "name": "webpack-numbers",
  "version": "1.0.0",
  "main": "dist/webpack-numbers.js", // 生成包路径
  "module": "src/index.js", // 用于支持es6的浏览器实验性选项
  "license": "MIT",
  "devDependencies": {
    "lodash": "^4.17.10",
    "webpack": "^4.17.2",
    "webpack-cli": "^3.1.0"
  }
}
  • webpack.config.js
var path = require("path");

module.exports = {
  entry: "./src/index.js",
  mode: "production", // 生产用
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "webpack-numbers.js",// 最后生成的包文件名
    library: "webpackNumbers", // 用于
    libraryTarget: "umd" //指定打包类型
  },
  externals: { // 以来第三方包
    lodash: {
      commonjs: "lodash",
      commonjs2: "lodash",
      amd: "lodash",
      root: "_"
    }
  }
};
@hsipeng hsipeng added the Webpack label Sep 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant