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 7d262f4 commit a27d7f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
26 changes: 12 additions & 14 deletions addon/converter/fixture-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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(
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 a27d7f8

Please sign in to comment.