热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在rollupjs中禁用树抖动-HowtodisableTreeshakinginrollupjs

IamtryingtobundletogetherseveraljavascriptfilesusingRollUp.jsbutwhenIdo,theclassest

I am trying to bundle together several Javascript files using RollUp.js but when I do, the classes that aren't used get removed. This process is called tree shaking and I want to disable it.

我试图使用RollUp.js将几个Javascript文件捆绑在一起,但是当我这样做时,未使用的类将被删除。这个过程称为树摇动,我想禁用它。

I have found this but it doesn't seem to have any effect.

我发现了这个,但它似乎没有任何影响。

// rollup.config.js

let cOnfiguration= {
  output: {
    format: 'es',
  },
  name: 'namename',
  input: './main.js',
  treeshake: false, // <-- disabling tree shaking?
};

export default configuration;

I added treeshake: false to the configuration, but it doesn't seem to have any effect. Is this supposed to be placed somewhere else?

我在配置中添加了treeshake:false,但它似乎没有任何效果。这应该放在别的地方吗?

Here are the files I am trying to roll up.

这是我试图卷起的文件。

// Base.js
export default class Base {
  aMethod() {
    return "Hello";
  }
}

// main.js 
import Base from './Base.js';

So with this set up, I call rollup --config and it produces something empty. So clearly, tree shaking is happening and it is removing the Base class even though I imported it.

所以通过这个设置,我调用rollup --config并产生一些空的东西。很明显,树摇动正在发生,即使我导入它也正在删除Base类。

So far the only workaround I've found is to actually create an instance of the class, which is undesirable.

到目前为止,我发现的唯一解决方法是实际创建一个类的实例,这是不可取的。

// main.js
import Base from './Base.js';

export default function () {
    {   
        new Base();
    }
}

My goal is to use the bundled Javascript file with JSContext. It will take in the Javascript as a string and then from there, I'd invoke methods as needed.

我的目标是使用JSContext捆绑的Javascript文件。它将Javascript作为一个字符串,然后从那里,我会根据需要调用方法。

// suppose rollup.js produces a file called "product.js"

let s = String(contentsOfFile: "path/to/product.js")
let cOntext= JSContext()!
context.evaluateScript(s)

context.evaluateScript("var b = new Base()")
context.evaluateScript("b.aMethod()")

But because of the tree shaking the Base class never gets placed in product.js

但由于树摇动,Base类永远不会放在product.js中

Is there a way to disable tree shaking?

有没有办法禁用树摇动?

I've included a sample project for this.

我已经为此包含了一个示例项目。

1 个解决方案

#1


1  

Your entry file — main.js — needs to export any classes or other values that need to be accessible to the outside world:

您的条目文件 - main.js - 需要导出需要外部访问的任何类或其他值:

// main.js
import Base from './Base.js';
import SubB from './SubB.js';
import SubA from './SubA.js';

export { Base, SubA, SubB };

推荐阅读
author-avatar
PHP猎人
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有