【问题标题】:Svelte Rollup [export name] is not exported by node_modules/Svelte Rollup [export name] 未由 node_modules/ 导出
【发布时间】:2020-05-30 22:58:45
【问题描述】:

我正在尝试从 hyphen 导入一个模块,如下所示:import { hyphenateHTMLSync } from "hyphen/fr"; 在 Svelte 模块的脚本标记中,但我从汇总中得到 Error: 'hyphenateHTMLSync' is not exported by node_modules/hyphen/fr/index.js

问题中的模块文件如下所示:

node_modules/hyphen/fr/index.js

module.exports = require("../export-contract.js")(
    require("../patterns/fr.js")
);

node_modules/hyphen/export-contract.js

var createHyphenator = require("./hyphen.js");

module.exports = function (patterns) {
  return {
    hyphenate: createHyphenator(patterns, { async: true }),
    hyphenateHTML: createHyphenator(patterns, { async: true, html: true }),
    hyphenateHTMLSync: createHyphenator(patterns, { html: true }),
    hyphenateSync: createHyphenator(patterns),
    patterns: patterns
  };
};

而 hyphen.js 包含创建连字符的函数。

我对 Rollup、Svelte 甚至 Node 的了解还不够,不知道如何解决这个问题。

【问题讨论】:

    标签: javascript node.js svelte rollup


    【解决方案1】:

    Rollup 需要额外的插件(@rollup/plugin-node-resolve@rollup/plugin-commonjs)来处理 CommonJS 模块,如 here 所述。

    使用这两个插件的一个非常基本的汇总示例配置是here

    在您的特定用例中,如果您在使用基本配置时仍然遇到问题,您可能需要深入研究 commonjs 插件的 dynamicRequireTargets 选项。

    【讨论】:

    • 两个插件都已经在我的汇总配置文件中。我使用了Svelte template from degit,它们已预先配置。我尝试使用 CommonJS 的 dynamicRequireTargets 参数,添加插件的 2 个核心组件,但我仍然遇到同样的问题。不确定我是否对 dynamicRequireTargets 做错了什么。 commonjs( { dynamicRequireTargets: [ 'node_modules/hyphen/index.js', 'node_modules/hyphen/hyphen.js' ] } )
    • 为了安心,我会添加动态导入中涉及的所有模块,即commonjs({ dynamicRequireTargets: [ 'node_modules/hyphen/export-contract.js', 'node_modules/hyphen/hyphen.js', 'node_modules/hyphen/patterns/fr.js' ] })。如果这不起作用,那么您可能必须找到另一种方法,即通过从patterns/fr.jshyphen.js 导入并手动调用createHyphenator(您可以在单独的 JS 文件中完成所有这些操作,导出结果函数调用,并将其导入项目中需要的任何位置)。
    • 我最终完成了最后一部分,导入 createHyphenator 和模式并导出。感谢您的帮助!
    猜你喜欢
    • 2022-01-02
    • 2021-05-31
    • 2021-10-21
    • 2021-03-13
    • 2021-12-14
    • 2019-10-22
    • 2021-12-14
    • 2020-10-20
    • 2021-12-25
    相关资源
    最近更新 更多