Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove some usage of runtime require #2297

Merged
merged 3 commits into from
Jan 7, 2022
Merged

Conversation

ef4
Copy link
Contributor

@ef4 ef4 commented Jan 5, 2022

require a pre-ES-module feature that continues to work in Ember but is fragile and unreliable under ES-module-first build tooling like Embroider.

This PR replaces two uses with embroider macros instead, which are designed to allow dynamism without sacrificing static analysis.

`require` a pre-ES-module feature that continues to work in Ember but is fragile and unreliable under ES-module-first build tooling like Embroider.

This PR replaces two uses with embroider macros instead, which are designed to allow dynamism without sacrificing static analysis.

/**
@hide
*/
export const hasEmberData = _hasEmberData();
export const hasEmberData = dependencySatisfies('ember-data', '*');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I never liked the way it checked before.

@@ -1,4 +1,4 @@
import require from 'require';
import { importSync, dependencySatisfies, isTesting } from '@embroider/macros';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This entire file should be done away with in the next version

package.json Outdated
Comment on lines 54 to 65
"peerDependencies": {
"@ember/test-helpers": "*",
"ember-qunit": "*"
},
"peerDependenciesMeta": {
"@ember/test-helpers": {
"optional": true
},
"ember-qunit": {
"optional": true
}
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate on why these are here. They are listed under devDependancies. Why are these two peer and optional and not the rest? Trying to understand if I should be doing something like this in other addons.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dependencySatisfies follows the real NPM package resolving rules.

The rule is that a package looks in its own node_modules folder and then proceeds to look up the filesystem for higher node_modules folders.

An app will often accidentally allow ember-cli-mirage to resolve the app's copy of ember-qunit:

/the-app
└─ node_modules
    └── ember-qunit
    └── ember-cli-mirage

But this is not guaranteed. For example, if the app lives in a monorepo with many projects, some dependencies may be shared at the top level and others may not:

/the-monorepo
└─ node_modules
│  └── ember-cli-mirage
└── app1
│  └── node_modules
|    └── ember-qunit

In this case, app1 can see both addons, but ember-cli-mirage cannot resolve ember-qunit, so dependencySatisfies would return false.

NPM's solution to this problem is peerDependencies. They declare that you must see the same copy as your parent package. With a peerDependency declaration, NPM would ensure that ember-cli-mirage and ember-qunit are always located so that ember-cli-mirage can see the correct copy of ember-qunit.

Usually peerDependencies also appear in devDependencies. That's because they're simultaneously dependencies of the dummy app and peerDependencies of the addon.

I marked these as optional peer dependencies because the existing code also treats them as optional. By declaring these optional peer deps, we're telling NPM that if the app has these packages, we need to be able to see them too, but if it doesn't have them that's OK.

Copy link
Collaborator

@cah-brian-gantzler cah-brian-gantzler Jan 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew most of what you said, what I missed was the dependencySatisfies is why you did this. @ember/test-helpers and ember-data appears in dependencySatisfies.

I had wanted to make ember-qunit a peerDependancy much like ember-qunit made qunit a peerDependancy. In your example of the mono-repo, should not every devDependancy be listed as a peer because we cant predict how the end consumer will structure their repo?

Also should ember-data be listed as a peerDependancy since it is directly referred to in a dependencySatisfies?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it would be correct to add ember-data to peerDeps too. Done.

@SergeAstapov
Copy link
Collaborator

SergeAstapov commented Jan 5, 2022

@ef4 one more thing:

ember-cli-mirage still supports "node": ">= 10.*" whereas @embroider/macros has dropped Node.js 10 support in v0.42.0

@ef4 can we use "@embroider/macros": "^0.41.0" for the time being and bump to the latest version once we release ember-cli-mirage@3.0.0?

@ef4
Copy link
Contributor Author

ef4 commented Jan 7, 2022

Yes, I downgraded back to the older embroider/macros for Node 10 support.

@SergeAstapov
Copy link
Collaborator

@ef4 Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants