Hi again! Back with another question about the best way to import / manage dependencies, after reading the docs and hitting a wall.
Ultimately, I am unclear about when (in an Embroider, Webpack, ember-auto-import
world) I should add a package to:
- an App;
- an Addon’s dependencies; or
- an Addon’s Blueprint for installation in the parent App.
Some background:
I have an Addon running on Ember 3.28. It is embroiderOptimized
, and has ember-auto-import@2
in dependencies and webpack@5
in devDependencies.
I have an Embroider enabled App with my Addon installed. It’s on the same version of Ember, and has ember-auto-import@2
and webpack@5
installed in devDependencies.
My project uses Lodash, via lodash-es
so I can take advantage of some tree-shaking magic.
Both Addon and App have their own, independent needs for Lodash.
Initially, I had the package installed in the Addon and the App dependencies, and just assumed that Embroider / Webpack would ensure only one copy ended up in the build. When I look at the Bundle Analyser I can see two copies of lodash-es
in the same chunk. I also found that all the Lodash modules were included, not just the handful I have imported, via:
import { sortBy } from 'lodash-es';
My immediate priority was the duplicated packages. I moved Lodash into the Addon’s devDependencies and used a Blueprint to include the package in the App via addPackagesToProject
. When I serve the app I get errors indicating that Lodash is missing:
"Could not find module `lodash-es` imported from `(require)`
The above should work, right? Either way, my Addon’s tests fail because they expect lodash-es
to be in dependencies:
my-addon tried to import "lodash-es" in "my-addon/components/some-component.js" from addon code, but "lodash-es" is a devDependency. You may need to move it into dependencies.
I’m back to having duplicate copes of the package in the App build.
In this scenario (one package being shared by the App and Addon, for their own reasons) what is the correct way to manage the dependency?
Regarding tree-shaking the unused loads-es
modules , what’s the correct way to import?
As always, thank you