Skip to content

Commit

Permalink
fix: embroider compat (#8427)
Browse files Browse the repository at this point in the history
* fix: embroider compat

* fix

* fix unary

* fix flag usages

* fix usage

* fix lint
  • Loading branch information
runspired authored Feb 25, 2023
1 parent d9ac247 commit e0f92a2
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const { ImportUtil } = require('babel-import-util');

function parentIsUnary(node) {
if (node.parent.type === 'UnaryExpression' && node.parent.operator === '!') {
return true;
}
return false;
}

module.exports = function (babel) {
const { types: t } = babel;

Expand All @@ -18,12 +25,22 @@ module.exports = function (babel) {
let localBindingName = specifier.node.local.name;
let binding = specifier.scope.getBinding(localBindingName);
binding.referencePaths.forEach((p) => {
p.replaceWith(
// t.callExpression(state.importer.import(p, '@embroider/macros', 'macroCondition'), [
t.callExpression(state.importer.import(p, '@embroider/macros', 'moduleExists'), [
t.stringLiteral(replacements[name]),
let negateStatement = false;
let node = p;
if (parentIsUnary(p)) {
negateStatement = true;
node = p.parentPath;
}

const exp = t.callExpression(state.importer.import(p, '@embroider/macros', 'dependencySatisfies'), [
t.stringLiteral(replacements[name]),
t.stringLiteral('*'),
]);

node.replaceWith(
t.callExpression(state.importer.import(p, '@embroider/macros', 'macroCondition'), [
negateStatement ? t.unaryExpression('!', exp) : exp,
])
// ])
);
});
specifier.scope.removeOwnBinding(localBindingName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ module.exports = function (babel) {
// if (LOG_FOO)
// =>
// if (macroCondition(getOwnConfig().debug.LOG_FOO))
//t.callExpression(state.importer.import(p, '@embroider/macros', 'macroCondition'), [
negateStatement ? t.unaryExpression('!', getConfig) : getConfig
//])
t.callExpression(state.importer.import(p, '@embroider/macros', 'macroCondition'), [
negateStatement ? t.unaryExpression('!', getConfig) : getConfig,
])
);
});
specifier.scope.removeOwnBinding(localBindingName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ export function getModelFactory(store: Store, cache, normalizedModelName: string
let owner: any = getOwner(store);
factory = owner.factoryFor(`model:${normalizedModelName}`);

if (!factory && HAS_MODEL_PACKAGE) {
//Support looking up mixins as base types for polymorphic relationships
factory = _modelForMixin(store, normalizedModelName);
if (HAS_MODEL_PACKAGE) {
if (!factory) {
//Support looking up mixins as base types for polymorphic relationships
factory = _modelForMixin(store, normalizedModelName);
}
}

if (!factory) {
Expand Down
18 changes: 10 additions & 8 deletions packages/store/addon/-private/network/fetch-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,16 @@ export default class FetchManager {
const recordData = store._instanceCache.peek({ identifier, bucket: 'recordData' });
if (!recordData || recordData.isEmpty(identifier) || isLoading) {
let isReleasable = true;
if (!recordData && HAS_RECORD_DATA_PACKAGE) {
const graphFor = (
importSync('@ember-data/record-data/-private') as typeof import('@ember-data/record-data/-private')
).graphFor;
const graph = graphFor(store);
isReleasable = graph.isReleasable(identifier);
if (!isReleasable) {
graph.unload(identifier, true);
if (HAS_RECORD_DATA_PACKAGE) {
if (!recordData) {
const graphFor = (
importSync('@ember-data/record-data/-private') as typeof import('@ember-data/record-data/-private')
).graphFor;
const graph = graphFor(store);
isReleasable = graph.isReleasable(identifier);
if (!isReleasable) {
graph.unload(identifier, true);
}
}
}
if (recordData || isReleasable) {
Expand Down
21 changes: 10 additions & 11 deletions packages/store/addon/-private/store-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,12 @@ class Store extends Service {
* @public
*/
getSchemaDefinitionService(): SchemaDefinitionService {
if (HAS_MODEL_PACKAGE && !this._schemaDefinitionService) {
// it is potentially a mistake for the RFC to have not enabled chaining these services, though highlander rule is nice.
// what ember-m3 did via private API to allow both worlds to interop would be much much harder using this.
this._schemaDefinitionService = new DSModelSchemaDefinitionService(this);
if (HAS_MODEL_PACKAGE) {
if (!this._schemaDefinitionService) {
// it is potentially a mistake for the RFC to have not enabled chaining these services, though highlander rule is nice.
// what ember-m3 did via private API to allow both worlds to interop would be much much harder using this.
this._schemaDefinitionService = new DSModelSchemaDefinitionService(this);
}
}
assert(
`You must registerSchemaDefinitionService with the store to use custom model classes`,
Expand Down Expand Up @@ -2692,13 +2694,10 @@ export function assertIdentifierHasId(
}

function isDSModel(record: RecordInstance | null): record is DSModel {
return (
HAS_MODEL_PACKAGE &&
!!record &&
'constructor' in record &&
'isModel' in record.constructor &&
record.constructor.isModel === true
);
if (!HAS_MODEL_PACKAGE) {
return false;
}
return !!record && 'constructor' in record && 'isModel' in record.constructor && record.constructor.isModel === true;
}

type AdapterErrors = Error & { errors?: unknown[]; isAdapterError?: true; code?: string };
Expand Down
5 changes: 5 additions & 0 deletions tests/embroider-basic-compat/app/models/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Model, { attr } from '@ember-data/model';

export default class User extends Model {
@attr declare name: string;
}
23 changes: 23 additions & 0 deletions tests/embroider-basic-compat/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,28 @@ module.exports = function (defaults) {
package: 'qunit',
},
],
compatAdapters: new Map([
['@ember-data/store', null],
['@ember-data/record-data', null],
['@ember-data/serializer', null],
['@ember-data/adapter', null],
['@ember-data/model', null],
['@ember-data/debug', null],
['@ember-data/tracking', null],
['@ember-data/request', null],
['@ember-data/private-build-infra', null],
['@ember-data/canary-features', null],
['ember-data', null],
]),
packageRules: [
{
package: '@ember-data/store',
addonModules: {
'-private.js': {
dependsOnModules: ['@ember-data/json-api/-private'],
},
},
},
],
});
};
9 changes: 9 additions & 0 deletions tests/embroider-basic-compat/tests/acceptance/visit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ module('it works', function (hooks) {
await visit('/');
assert.ok('it works!');
});

test('we can use the store', async function (assert) {
const { owner } = this;
const store = owner.lookup('service:store');

const record = store.createRecord('user', { name: 'Chris' });

assert.strictEqual(record.name, 'Chris', 'correct name');
});
});

0 comments on commit e0f92a2

Please sign in to comment.