Skip to content

Commit

Permalink
Replace require with importSync to make it Embroider friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
achambers committed Nov 13, 2023
1 parent cf60ca0 commit 71ef079
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
25 changes: 13 additions & 12 deletions addon/builder/fixture-builder-factory.js
Original file line number Diff line number Diff line change
@@ -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('active-model-adapter').default;
}

export default class {
Expand Down
28 changes: 14 additions & 14 deletions addon/converter/fixture-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
13 changes: 7 additions & 6 deletions addon/factory-guy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 71ef079

Please sign in to comment.