msramalho / SigTools

📆 Sigarra Tools | An extension that makes the information system of the University of Porto slightly better.
https://chrome.google.com/webstore/detail/sigarra-to-calendar/piefgbacnljenipiifjopkfifeljjkme
Apache License 2.0
37 stars 0 forks source link

Using JavaScript Modules #92

Open fabiodrg opened 2 years ago

fabiodrg commented 2 years ago

I think it could be neat to adopt more modern development approaches, for instance, the use of modules. Modules are standard and browsers support them.

The problem with extensions is that you cannot load a module for content_scripts, background_scripts, and so on. Thus, the usage is restricted to HTML pages that can import a script as module rather than plain javascript: <script src="..." type="module">.

A workaround is using bundlers. We already have a base infrastructure for that with gulp. In another project, I am using rollup to bundle everything into one or more files that can work on browsers. I am still experimenting, but perhaps having a options.bundle.js, moodle.bundle.js, sigarra.bundle.js, etc can make sense to reduce the amount of code to load on different contexts. Another benefit is the possibility to import Node.js packages. Makes it a bit easier to manage dependencies and their version. Finally, manifest.js would be simpler, I think, and require less maintenance. Because instead of listing all javascript files to be loaded, you specify a single bundle file. The loads are managed at the source code using import statements, the bundler within gulp does the rest!