首页 > 解决方案 > Rollupjs:忽略代码行

问题描述

我的 javascript 文件之一是使用 lodash 模板语法:

const deliveryClient = new DeliveryClient({
enablePreviewMode: <%= options.enablePreviewMode %>,
projectId: '<%= options.projectId %>',
previewApiKey: '<%= options.previewApiKey %>',
defaultLanguage: '<%= options.defaultLanguage %>',
enableAdvancedLogging: <%= options.enableAdvancedLogging %>,
baseUrl: '<%= options.baseUrl %>',
typeResolvers: typeResolvers
});

但是当我运行 rollup -c 时,我得到一个“意外令牌”错误。有没有办法告诉汇总忽略(只是把它放在输出文件中)一些代码行?

还是有其他/更好的方法来处理 RollupJS 中的 lodash 模板语法?

我只想将上面的代码片段放在我的最终输出中!

标签: lodashrollupjs

解决方案


我通过使用rollup-plugin-replace 插件修复了它。

在我的 javascript 中,我将代码更改为以下内容:

const deliveryClient = new DeliveryClient('KENTICOOPTIONS');

并在rollup.config.js我添加了具有以下配置的插件:

replace({
  include: 'lib/templates/plugin.template.js',
  KENTICOOPTIONS: '<%= serialize(options) %>'
})

所以这给出了最终输出:

const deliveryClient = new DeliveryClient('<%= serialize(options) %>');

这正是我需要的!


推荐阅读