作者:PHP猎人 | 来源:互联网 | 2023-09-15 16:48
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 个解决方案