Discrepancy with es modules / duplicated multiple entries
See original GitHub issue🐛 bug report
Take index.html
:
<script src="./x.js"></script>
<script src="./x.js"></script>
// x.js
import './y.js'
// y.js
console.log('foo')
If we build it with parcel parcel serve --no-hmr index.html
, it includes/runs y.js
two times.
Same time, if we use native ES modules:
<script>
var parcelRequire // hello #3545
</script>
<script type="module" src="./x.js"></script>
<script type="module" src="./x.js"></script>
The y.js
is run once.
🤔 Expected Behavior
y.js
should be run once.
😯 Current Behavior
Runs y.js
multiple times.
💁 Possible Solution
Same as browserify - it handles that fine. Should be code-splitting engaged or something, to avoid dupes in output. I wouldn’t bundle per-entry, instead I’d split out shared imports and reuse them.
🔦 Context
spect enables aspect-oriented approach to web-sites, so that for example material-design
components are initialized separately from main application logic, and separate from “decorative” logic (and separately from i18n, dataschema etc).
💻 Code Sample
For example, that spect app logic can be loaded each by own entry:
<script src="./materialize.js"></script>
<script src="./app.js"></script>
<script src="./decorations.js"></script>
<script src="./i18n.js"></script>
<script src="./microdata.js"></script>
Since each that entry includes spect
internally:
// materialize.js
import { use } from 'spect'
import { MDLFab } from '@material/fab'
use('.mdl-fab', el => el.mdlComponent = new MDLFab(el))
// app.js
import { on } from 'spect'
on('.app-form', 'submit', e => {
// ...send form
})
....
That results in duplication of spect
imports N times - per each entry.
🌍 Your Environment
Software | Version(s) |
---|---|
Parcel | 1.12.3 |
Node | - |
npm/Yarn | ∀ |
Operating System | ∀ |
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (4 by maintainers)
@felipemullen Module hoisting with multiple scripts
isn’t supported by Parcel, mainly because few people have requested that and putting
import "run.js"; import "run2.js";
in anindex.js
file works as expected in this case. (unless you have some reasons against this?) Without es modules, we would even have to pollute the global namespace to access the modules in the first script (soC
andD
in your case).Ah, I see. That’s a pity. For context, I’ll explain the scenario, maybe it will help others who come across this situation:
I am working on an enterprise level angular 1.6 project, and I have been slowly modernizing it where I can (The goal is to move away from angular in the long run)
The issue came up because multiple angular services (in separate files) make use of functionality that was extracted into a module, resulting in the complete duplication of the module in each service.
I understand now, thank you for the clarification. I will find a way around it 🐒
Parcel is a great project, many thanks to everyone behind it!