简体   繁体   English

如何从与 Rollup 捆绑的库中导入命名导出到 HTML 页面?

[英]How do I import named exports from a library bundled with Rollup into a HTML page?

I'm probably just incorrect about my assumptions of how this should work, so I would appreciate it if somebody can either tell me what to change in my Rollup config or how I should use the resulting bundle.我对这应该如何工作的假设可能只是不正确,所以如果有人能告诉我在我的 Rollup 配置中更改什么或我应该如何使用生成的包,我将不胜感激。

Let me set the scene.让我来布景。 I'm writing a library, bundling it with Rollup, and then want to write some HTML page where I import a named export from that libray.我正在编写一个库,将它与 Rollup 捆绑在一起,然后想编写一些 HTML 页面,在其中我从该库中导入一个命名导出。 It keeps telling me that it can't find the named export.它一直告诉我它找不到命名的导出。

For your convenience, I have boiled the issue down to this minimal example:为了您的方便,我将问题归结为这个最小的例子:

I have The following files in my project folder:我的项目文件夹中有以下文件:

  • min-example.js : min-example.js
export function minExample(){
  return "can you find me?";
}
  • package.json : package.json :
{
  "name": "min-example",
  "version": "1.0.0",
  "description": "A minimal example of an issue with rollup",
  "scripts": {
    "build": "rollup -c"
  },
  "author": "nvcleemp",
  "license": "ISC",
  "devDependencies": {
    "rollup": "^2.58.0"
  }
}
  • rollup.config.js
export default [
  {
    input: "min-example.js",
    output: {
      file: `min-example.bundle.js`,
      name: "min-example",
      format: "umd"
    }
  }
];

If I run npm run build , then this creates the file min-example.bundle.js :如果我运行npm run build ,那么这将创建文件min-example.bundle.js

(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["min-example"] = {}));
})(this, (function (exports) { 'use strict';

  function minExample(){
    return "can you find me?";
  }

  exports.minExample = minExample;

  Object.defineProperty(exports, '__esModule', { value: true });

}));

I would have expected that I could use this file in the following manner:我原以为可以通过以下方式使用此文件:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Minimum example</title>
</head>

<body>
        <script type="module">
        import { minExample } from './min-example.bundle.js';

        console.log(minExample());
    </script>
</body>

</html>

As I said above however, this complains that it can't find the named export minExample .然而,正如我上面所说,这抱怨它找不到命名的 export minExample

Note that it does work without using Rollup:请注意,它确实可以在不使用 Rollup 的情况下工作:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Minimum example</title>
</head>

<body>
        <script type="module">
        import { minExample } from './min-example.js';

        console.log(minExample());
    </script>
</body>

</html>

OK, a bit more trial-and-error let to a solution.好的,多一点试错让解决方案。 Apparently the output format should be es instead of umd , so the file rollup.config.js had to be changed to显然输出格式应该是es而不是umd ,因此文件rollup.config.js必须更改为

export default [
  {
    input: "min-example.js",
    output: {
      file: `min-example.bundle.js`,
      name: "min-example",
      format: "es"
    }
  }
];

So I was indeed under the false impression that umd just combined es and cjs .所以我确实有一种错误的印象,即umd只是结合了escjs

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

相关问题 我将如何导入这些es6导出? 它们是导出文件还是默认文件? - How would I import these es6 exports? Are they named exports or default? 如何使用汇总导入内容 - How do I import something with rollup 使用 Rollup 混合默认导出和命名导出 - Mixing default and named exports with Rollup 使用CommonJS进行汇总,通过树状结构进行进出口 - Rollup with CommonJS, import and exports with treeshaking 我正在尝试导出一个函数以导入到另一个 javascript 页面,但无法弄清楚如何使用导出/导入来做到这一点 - i am trying to export a function to import into another javascript page but can not figure out how to do it with exports/imports 如何通过 module.exports 导出这个名为 function 的文件? - How do I export this named function through module.exports? 导入动态命名的导出 - Import dynamically named exports 捆绑的转口在图书馆树中是否摇动了? - Are bundled re-exports in a library tree shaken? 如何使用 Jest 模拟外部库的命名导出构造函数和函数? - How do you mock a named exports constructor and functions of an external library with Jest? JS - 我可以导入具有多个文件的同名导出吗? - JS - Can I import same named exports with multiple files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM