From a27d7f81bc7966e53c73819bc9a2e2a915d4ff1c Mon Sep 17 00:00:00 2001 From: Aaron Chambers Date: Mon, 13 Nov 2023 11:15:33 +0000 Subject: [PATCH] Replace require with importSync to make it Embroider friendly --- addon/converter/fixture-converter.js | 26 ++++++++++++-------------- addon/factory-guy.js | 13 +++++++------ 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/addon/converter/fixture-converter.js b/addon/converter/fixture-converter.js index b1f1a4fe..1cd30bc9 100644 --- a/addon/converter/fixture-converter.js +++ b/addon/converter/fixture-converter.js @@ -3,19 +3,17 @@ import { entries } from '../utils/helper-functions'; import { typeOf } from '@ember/utils'; import { getOwner } from '@ember/application'; import { camelize } from '@ember/string'; -import require from 'require'; +import { + macroCondition, + dependencySatisfies, + importSync, +} from '@embroider/macros'; let Fragment; let FragmentArray; -try { - let MF = require('ember-data-model-fragments/fragment'); - let MFA = require('ember-data-model-fragments/array/fragment'); - Fragment = MF && MF.default; - FragmentArray = MFA && MFA.default; -} catch (e) { - // create empty constructors - Fragment = function Fragment() {}; - FragmentArray = function FragmentArray() {}; +if (macroCondition(dependencySatisfies('ember-data-model-fragments', '*'))) { + Fragment = importSync('ember-data-model-fragments/fragment').default; + FragmentArray = importSync('ember-data-model-fragments/array/fragment').default; } /** @@ -203,8 +201,8 @@ export default class FixtureConverter { // If passed Fragments or FragmentArrays we must transform them to their serialized form before we can push them into the Store if ( - attributeValueInFixture instanceof Fragment || - attributeValueInFixture instanceof FragmentArray + (Fragment && attributeValueInFixture instanceof Fragment) || + (FragmentArray && attributeValueInFixture instanceof FragmentArray) ) { fixture[attributeKey] = this.normalizeModelFragments( attributeValueInFixture @@ -230,13 +228,13 @@ export default class FixtureConverter { } normalizeModelFragments(attributeValueInFixture) { - if (attributeValueInFixture instanceof Fragment) { + if (Fragment && attributeValueInFixture instanceof Fragment) { return this.store.normalize( attributeValueInFixture.constructor.modelName, attributeValueInFixture.serialize() ).data.attributes; } - if (attributeValueInFixture instanceof FragmentArray) { + if (FragmentArray && attributeValueInFixture instanceof FragmentArray) { return attributeValueInFixture .serialize() .map( diff --git a/addon/factory-guy.js b/addon/factory-guy.js index eaca5833..98f9a17f 100644 --- a/addon/factory-guy.js +++ b/addon/factory-guy.js @@ -3,19 +3,20 @@ import { assert } from '@ember/debug'; import { isPresent, typeOf } from '@ember/utils'; import { join } from '@ember/runloop'; import { A } from '@ember/array'; -import require from 'require'; import ModelDefinition from './model-definition'; import FixtureBuilderFactory from './builder/fixture-builder-factory'; import RequestManager from './mocks/request-manager'; +import { + macroCondition, + dependencySatisfies, + importSync, +} from '@embroider/macros'; let modelDefinitions = {}; let Fragment; -try { - let MF = require('ember-data-model-fragments'); - Fragment = MF && MF.default.Fragment; -} catch (e) { - // do nothing +if (macroCondition(dependencySatisfies('ember-data-model-fragments', '*'))) { + Fragment = importSync('ember-data-model-fragments').default.Fragment; } class FactoryGuy {