question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mischniccommented, Dec 2, 2019

@felipemullen Module hoisting with multiple scripts

    <script src="run.js"></script>
    <script src="run2.js"></script>

isn’t supported by Parcel, mainly because few people have requested that and putting import "run.js"; import "run2.js"; in an index.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 (so C and D in your case).

0reactions
felipemullencommented, Dec 2, 2019

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicate modules - NOT solvable by npm dedupe #5593
Two identical sub-dependencies are being included twice in the build. If the current behavior is a bug, please provide the steps to reproduce....
Read more >
Duplicate modules of same version in webpack build
Here are some steps that can help you reduce duplicated packages in your webpack build. Let's take your example where lodash is bundled...
Read more >
Understanding Modules and Import and Export Statements in ...
In this tutorial, you will learn what a JavaScript module is and how to use import and export to organize your code. Modular...
Read more >
CJS vs ESM - Andrea Giammarchi - Medium
The biggest difference between CJS and ESM, is that the former, filesystem constrained, understands some file content based on its extension, ...
Read more >
A Practical guide to ES6 modules - freeCodeCamp
In this article, we'll create a simple dashboard using ES6 modules, and then present optimization techniques for improving the folder ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found