简体   繁体   English

Webpack输出带有CamelCase和脊柱案例名称的UMD库

[英]Webpack output UMD library with CamelCase and spinal-case name

How do I configure Webpack to create UMD bundle, so that package name for AMD and CommonJS will be named in lowercase (spinal-case), and for global context will have CamelCase name? 如何配置Webpack来创建UMD捆绑包,以便AMD和CommonJS的包名称将以小写字母(小写字母)命名,而全局上下文的名称将为CamelCase名称?

For example I expect my bundle to start with 例如,我希望我的捆绑包以

(function webpackUniversalModuleDefinition(root, factory) {
    if(typeof exports === 'object' && typeof module === 'object')
        module.exports = factory(require("dependency"));
    else if(typeof define === 'function' && define.amd)
        define("my-library", ["dependency"], factory);           // spinal-case
    else if(typeof exports === 'object')
        exports["my-library"] = factory(require("dependency"));  // spinal-case
    else
        root["MyLibrary"] = factory(root["dependency"]);         // CamelCase
})...

This is how it looks in ReactDOM: 这是在ReactDOM中的样子:

(function(f) {
  // CommonJS
  if (typeof exports === "object" && typeof module !== "undefined") {
    module.exports = f(require('react'));

  // RequireJS
  } else if (typeof define === "function" && define.amd) {
    define(['react'], f);

  // <script>
  } else {
    var g;
    if (typeof window !== "undefined") {
      g = window;
    } else if (typeof global !== "undefined") {
      g = global;
    } else if (typeof self !== "undefined") {
      g = self;
    } else {
      // works providing we're not in "use strict";
      // needed for Java 8 Nashorn
      // see https://github.com/facebook/react/issues/3037
      g = this;
    }
    g.ReactDOM = f(g.React);
  }
})(function(React)...

You also should have the property umdNamedDefine set to true . 您还应该将umdNamedDefine属性设置为true This is what my output block commonly looks like when I do a UMD wrapped library. 这是我做UMD包装的库时通常的output块。 I will use an example from an npm package that I currently have as it will be the most illustrative: 我将使用当前拥有的一个npm包中的示例,因为这将是最说明性的:

output: {
    path: './lib',
    filename: 'dom-regex.js',
    library: 'DomRegex',
    libraryTarget: 'umd',
    umdNamedDefine: true
},

Given that my npm package is named dom-regex in the package.json , this allows me to use my packages in the following ways: 鉴于我的npm软件包在package.json名为dom-regex ,这使我可以通过以下方式使用我的软件包:

ES6: ES6:

import DomRegex from 'dom-regex';

RequireJS 需求JS

var DomRegex = require('dom-regex');

On the window: (if the file is exposed directly to the window) 在窗口上:(如果文件直接显示在窗口上)

window.DomRegex

What the name is exported as in the UMD wrapped section isn't as important as what you name it when you import it. 在UMD包装部分中导出的名称并不像在导入时命名的名称那么重要。 Using dom-regex as an example, even if I had it locally in my project (not in node_modules), and the filename was dom-regex.js , I could still do all the previous examples, because it is UMD wrapped: dom-regex为例,即使我在项目本地(而不是在node_modules中),并且文件名是dom-regex.js ,我仍然可以做所有前面的示例,因为它是UMD包装的:

You're welcome to explore the repo for dom-regex and test download the package to see how this works. 欢迎您浏览dom-regex的仓库,并测试下载该软件包以了解其工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM