最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Rollup generates empty bundle - Stack Overflow

programmeradmin4浏览0评论

I would like to bundle a .js file with rollup that has only a class definition in it. But rollup creates only an empty bundle-file. This changes when I add some code outside the class Definition. This creates an empty bundle:

class MyElement extends HTMLElement{
    constructor() {...}
    ...
}

And this creates a filled bundle:

class MyElement extends HTMLElement{
    constructor() {...}
    ...
}

customElements.define('my-element', MyElement);

But I don't want to have the ...define() in that file. Is there a way to convice rollup.js to just bundle the class-definition?

I would like to bundle a .js file with rollup that has only a class definition in it. But rollup creates only an empty bundle-file. This changes when I add some code outside the class Definition. This creates an empty bundle:

class MyElement extends HTMLElement{
    constructor() {...}
    ...
}

And this creates a filled bundle:

class MyElement extends HTMLElement{
    constructor() {...}
    ...
}

customElements.define('my-element', MyElement);

But I don't want to have the ...define() in that file. Is there a way to convice rollup.js to just bundle the class-definition?

Share Improve this question edited Apr 4, 2017 at 5:43 Cœur 38.7k26 gold badges203 silver badges277 bronze badges asked Apr 1, 2017 at 9:10 treenotreeno 2,6002 gold badges23 silver badges39 bronze badges 1
  • make sure you export your ponent – Amir Rezvani Commented Jan 19, 2024 at 20:35
Add a ment  | 

2 Answers 2

Reset to default 12

Old topic but can be useful for those who are researching: for me it worked:

change the file tsconfig.json

  "emitDeclarationOnly": false,

You have a module that defines a class in its local scope but doesn't do anything with it - neither export it nor use it to perform a side effect like passing it to define. It's dead code - and that will be stripped by rollup. You'll likely want to use

export default class MyElement extends HTMLElement { /*
^^^^^^^^^^^^^^ */
    constructor() { … }
    …
}

which can be bundled to something that still exports the class so that it is usable elsewhere.

发布评论

评论列表(0)

  1. 暂无评论