From 57b2d2b0807b5dac10492951f969cfe0933fa0f6 Mon Sep 17 00:00:00 2001 From: Aaron Chambers Date: Wed, 8 Nov 2023 09:30:52 +0000 Subject: [PATCH] Replace require with @embroider/macros In order to place nicely with Embroider, we need to remove window.require. Instead we can use `dependencySatisfies` and `importSync`. --- addon/utils/utils.js | 17 +++++++++-------- package.json | 9 +++++++++ yarn.lock | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/addon/utils/utils.js b/addon/utils/utils.js index f6fecc71..633e17d6 100644 --- a/addon/utils/utils.js +++ b/addon/utils/utils.js @@ -4,16 +4,17 @@ import { isHTMLSafe } from '@ember/template'; import EmberObject from '@ember/object'; import { typeOf } from '@ember/utils'; import { A as emberArray, isArray } from '@ember/array'; -import require from 'require'; - -function requireModule(module, exportName = 'default') { - if (require.has(module)) { - return require(module)[exportName]; - } +import { + macroCondition, + dependencySatisfies, + importSync, +} from '@embroider/macros'; + +let DS; +if (macroCondition(dependencySatisfies('ember-data', '*'))) { + DS = importSync('ember-data').default; } -const DS = requireModule('ember-data'); - export { getDependentKeys, isDescriptor } from '../-private/ember-internals'; export function unwrapString(s) { diff --git a/package.json b/package.json index 6052ace4..2dc4d049 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "test:ember-compatibility": "ember try:each" }, "dependencies": { + "@embroider/macros": "^1.13.2", "ember-cli-babel": "^7.26.11", "ember-validators": "^4.1.1" }, @@ -110,6 +111,14 @@ "peerDependencies": { "ember-source": "^4.0.0" }, + "peerDependencies": { + "ember-data": "*" + }, + "peerDependenciesMeta": { + "ember-data": { + "optional": true + } + }, "engines": { "node": "14.* || 16.* || >= 18" }, diff --git a/yarn.lock b/yarn.lock index eb2e2918..317f6127 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1510,6 +1510,20 @@ resolve "^1.20.0" semver "^7.3.2" +"@embroider/macros@^1.13.2": + version "1.13.2" + resolved "https://registry.yarnpkg.com/@embroider/macros/-/macros-1.13.2.tgz#07dda11313a2539f403404881b729e622a80ca17" + integrity sha512-AUgJ71xG8kjuTx8XB1AQNBiebJuXRfhcHr318dCwnQz9VRXdYSnEEqf38XRvGYIoCvIyn/3c72LrSwzaJqknOA== + dependencies: + "@embroider/shared-internals" "2.5.0" + assert-never "^1.2.1" + babel-import-util "^2.0.0" + ember-cli-babel "^7.26.6" + find-up "^5.0.0" + lodash "^4.17.21" + resolve "^1.20.0" + semver "^7.3.2" + "@embroider/shared-internals@2.1.0", "@embroider/shared-internals@^2.0.0": version "2.1.0" resolved "https://registry.yarnpkg.com/@embroider/shared-internals/-/shared-internals-2.1.0.tgz#4da79fe8b26e4b94819b8313c51b5b24a68c617b" @@ -1524,6 +1538,21 @@ semver "^7.3.5" typescript-memoize "^1.0.1" +"@embroider/shared-internals@2.5.0": + version "2.5.0" + resolved "https://registry.yarnpkg.com/@embroider/shared-internals/-/shared-internals-2.5.0.tgz#4a0b5127c589718fae60fc22f81374ed558b944a" + integrity sha512-7qzrb7GVIyNqeY0umxoeIvjDC+ay1b+wb2yCVuYTUYrFfLAkLEy9FNI3iWCi3RdQ9OFjgcAxAnwsAiPIMZZ3pQ== + dependencies: + babel-import-util "^2.0.0" + debug "^4.3.2" + ember-rfc176-data "^0.3.17" + fs-extra "^9.1.0" + js-string-escape "^1.0.1" + lodash "^4.17.21" + resolve-package-path "^4.0.1" + semver "^7.3.5" + typescript-memoize "^1.0.1" + "@embroider/test-setup@^2.1.1": version "2.1.1" resolved "https://registry.yarnpkg.com/@embroider/test-setup/-/test-setup-2.1.1.tgz#1cf613f479ed120fdc5d71cb834c8fb71514cce1" @@ -2913,6 +2942,11 @@ babel-import-util@^1.1.0, babel-import-util@^1.2.2, babel-import-util@^1.3.0: resolved "https://registry.yarnpkg.com/babel-import-util/-/babel-import-util-1.3.0.tgz#dc9251ea39a7747bd586c1c13b8d785a42797f8e" integrity sha512-PPzUT17eAI18zn6ek1R3sB4Krc/MbnmT1MkZQFmyhjoaEGBVwNABhfVU9+EKcDSKrrOm9OIpGhjxukx1GCiy1g== +babel-import-util@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/babel-import-util/-/babel-import-util-2.0.1.tgz#263a2963ee9208428c04f05326c6ea32b2206ac6" + integrity sha512-N1ZfNprtf/37x0R05J0QCW/9pCAcuI+bjZIK9tlu0JEkwEST7ssdD++gxHRbD58AiG5QE5OuNYhRoEFsc1wESw== + babel-loader@^8.0.6: version "8.2.5" resolved "https://registry.yarnpkg.com/babel-loader/-/babel-loader-8.2.5.tgz#d45f585e654d5a5d90f5350a779d7647c5ed512e"