From 96c2771b7ed2612363dafa7c8433ae08c5552dbd 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/builder/fixture-builder-factory.js | 25 +++++++++++---------- addon/converter/fixture-converter.js | 28 ++++++++++++------------ addon/factory-guy.js | 13 ++++++----- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/addon/builder/fixture-builder-factory.js b/addon/builder/fixture-builder-factory.js index 87876544..5c4ff76c 100644 --- a/addon/builder/fixture-builder-factory.js +++ b/addon/builder/fixture-builder-factory.js @@ -1,26 +1,27 @@ import JSONSerializer from '@ember-data/serializer/json'; import RESTSerializer from '@ember-data/serializer/rest'; import JSONAPISerializer from '@ember-data/serializer/json-api'; -import require from 'require'; import JSONAPIFixtureBuilder from './jsonapi-fixture-builder'; import RESTFixtureBuilder from './rest-fixture-builder'; import JSONFixtureBuilder from './json-fixture-builder'; import DRFFixtureBuilder from './drf-fixture-builder'; import ActiveModelFixtureBuilder from './active-model-fixture-builder'; +import { + macroCondition, + dependencySatisfies, + importSync, +} from '@embroider/macros'; -let ActiveModelSerializer, DjangoSerializer; -try { - let activeModel = require('active-model-adapter'); - ActiveModelSerializer = activeModel.ActiveModelSerializer; -} catch (e) { - // do nothing +let ActiveModelSerializer; +if (macroCondition(dependencySatisfies('active-model-adapter', '*'))) { + ActiveModelSerializer = importSync( + 'active-model-adapter' + ).ActiveModelSerializer; } -try { - let drf = require('ember-django-adapter/serializers/drf'); - DjangoSerializer = drf && drf.default; -} catch (e) { - // do nothing +let DjangoSerializer; +if (macroCondition(dependencySatisfies('ember-django-adapter', '*'))) { + DjangoSerializer = importSync('ember-django-adapter/serializers/drf').default; } export default class { diff --git a/addon/converter/fixture-converter.js b/addon/converter/fixture-converter.js index b1f1a4fe..17428ab7 100644 --- a/addon/converter/fixture-converter.js +++ b/addon/converter/fixture-converter.js @@ -3,19 +3,19 @@ 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 +203,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 +230,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 {