From 25206e24db6e81389c44eb8933d240f471b9abb0 Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Sat, 25 Feb 2023 00:44:27 -0800 Subject: [PATCH 1/6] fix: embroider compat --- .../index.js | 9 ++--- .../index.js | 6 ++-- .../embroider-basic-compat/app/models/user.ts | 5 +++ .../embroider-basic-compat/ember-cli-build.js | 23 ++++++++++++ tests/embroider-basic-compat/package.json | 35 +++++++++---------- .../tests/acceptance/visit-test.js | 9 +++++ 6 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 tests/embroider-basic-compat/app/models/user.ts diff --git a/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js b/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js index cfad6bf073c..eb429eb1094 100644 --- a/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js +++ b/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js @@ -19,11 +19,12 @@ module.exports = function (babel) { 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]), + t.callExpression(state.importer.import(p, '@embroider/macros', 'macroCondition'), [ + t.callExpression(state.importer.import(p, '@embroider/macros', 'dependencySatisfies'), [ + t.stringLiteral(replacements[name]), + t.stringLiteral('*'), + ]), ]) - // ]) ); }); specifier.scope.removeOwnBinding(localBindingName); diff --git a/packages/private-build-infra/src/transforms/babel-plugin-transform-has-debug-package/index.js b/packages/private-build-infra/src/transforms/babel-plugin-transform-has-debug-package/index.js index 3b30068e84a..4abd10eb752 100644 --- a/packages/private-build-infra/src/transforms/babel-plugin-transform-has-debug-package/index.js +++ b/packages/private-build-infra/src/transforms/babel-plugin-transform-has-debug-package/index.js @@ -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); diff --git a/tests/embroider-basic-compat/app/models/user.ts b/tests/embroider-basic-compat/app/models/user.ts new file mode 100644 index 00000000000..33d0be8ad43 --- /dev/null +++ b/tests/embroider-basic-compat/app/models/user.ts @@ -0,0 +1,5 @@ +import Model, { attr } from '@ember-data/model'; + +export default class User extends Model { + @attr declare name: string; +} diff --git a/tests/embroider-basic-compat/ember-cli-build.js b/tests/embroider-basic-compat/ember-cli-build.js index 9b3fd539c72..b8c2a8e1c01 100644 --- a/tests/embroider-basic-compat/ember-cli-build.js +++ b/tests/embroider-basic-compat/ember-cli-build.js @@ -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'], + }, + }, + }, + ], }); }; diff --git a/tests/embroider-basic-compat/package.json b/tests/embroider-basic-compat/package.json index 4761e88bf96..c94dadb0d0c 100644 --- a/tests/embroider-basic-compat/package.json +++ b/tests/embroider-basic-compat/package.json @@ -1,6 +1,6 @@ { "name": "embroider-basic-compat", - "version": "4.11.0", + "version": "4.12.0-alpha.0", "private": true, "description": "Small description for embroider-basic-compat goes here", "repository": { @@ -22,8 +22,8 @@ "test": "ember test --test-port=0" }, "dependencies": { - "ember-auto-import": "^2.4.3", - "ember-data": "workspace:4.11.0", + "ember-auto-import": "^2.6.0", + "ember-data": "workspace:4.12.0-alpha.0", "@ember/string": "^3.0.1", "ember-inflector": "^4.0.2" }, @@ -36,43 +36,42 @@ } }, "devDependencies": { - "@babel/core": "^7.19.3", - "@babel/runtime": "^7.19.3", - "@ember-data/unpublished-test-infra": "workspace:4.11.0", + "@babel/core": "^7.21.0", + "@babel/runtime": "^7.21.0", + "@ember-data/unpublished-test-infra": "workspace:4.12.0-alpha.0", "@ember/optional-features": "^2.0.0", - "@ember/test-helpers": "~2.8.1", + "@ember/test-helpers": "^2.9.3", "@embroider/compat": "^1.9.0", "@embroider/core": "^1.9.0", "@embroider/webpack": "^1.9.0", "@glimmer/component": "^1.1.2", "@glimmer/tracking": "^1.1.2", - "@types/ember": "^4.0.1", + "@types/ember": "^4.0.3", "@types/ember-qunit": "^5.0.2", "@types/ember-testing-helpers": "^0.0.4", - "@types/ember__test-helpers": "^2.8.1", "@types/rsvp": "^4.0.4", "broccoli-asset-rev": "^3.0.0", - "ember-cli": "~4.8.0", + "ember-cli": "~4.10.0", "ember-cli-app-version": "^5.0.0", "ember-cli-babel": "^7.26.11", "ember-cli-dependency-checker": "^3.3.1", "ember-cli-fastboot": "^3.3.2", "ember-cli-fastboot-testing": "^0.6.0", - "ember-cli-htmlbars": "^6.1.1", + "ember-cli-htmlbars": "^6.2.0", "ember-cli-inject-live-reload": "^2.1.0", "ember-export-application-global": "^2.0.1", "ember-fetch": "^8.1.2", "ember-load-initializers": "^2.1.2", "ember-maybe-import-regenerator": "^1.0.0", - "ember-qunit": "^6.0.0", - "ember-resolver": "^8.0.3", + "ember-qunit": "^6.1.1", + "ember-resolver": "^10.0.0", "ember-simple-tree": "^0.8.3", - "ember-source": "~4.8.0", + "ember-source": "~4.11.0", "loader.js": "^4.7.0", - "qunit": "^2.19.2", + "qunit": "^2.19.4", "qunit-dom": "^2.0.0", - "typescript": "~4.8.4", - "webpack": "^5.74.0" + "typescript": "~4.9.5", + "webpack": "^5.75.0" }, "engines": { "node": "^14.8.0 || 16.* || >= 18.*" @@ -86,5 +85,5 @@ "volta": { "extends": "../../package.json" }, - "packageManager": "pnpm@7.15.0" + "packageManager": "pnpm@7.27.1" } \ No newline at end of file diff --git a/tests/embroider-basic-compat/tests/acceptance/visit-test.js b/tests/embroider-basic-compat/tests/acceptance/visit-test.js index 7cfc80cef80..f09ab7273ed 100644 --- a/tests/embroider-basic-compat/tests/acceptance/visit-test.js +++ b/tests/embroider-basic-compat/tests/acceptance/visit-test.js @@ -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'); + }); }); From 697bdfd9e3a7e2ecf033b9206c8da46059186e98 Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Sat, 25 Feb 2023 00:50:31 -0800 Subject: [PATCH 2/6] fix --- tests/embroider-basic-compat/package.json | 35 ++++++++++++----------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/tests/embroider-basic-compat/package.json b/tests/embroider-basic-compat/package.json index c94dadb0d0c..4761e88bf96 100644 --- a/tests/embroider-basic-compat/package.json +++ b/tests/embroider-basic-compat/package.json @@ -1,6 +1,6 @@ { "name": "embroider-basic-compat", - "version": "4.12.0-alpha.0", + "version": "4.11.0", "private": true, "description": "Small description for embroider-basic-compat goes here", "repository": { @@ -22,8 +22,8 @@ "test": "ember test --test-port=0" }, "dependencies": { - "ember-auto-import": "^2.6.0", - "ember-data": "workspace:4.12.0-alpha.0", + "ember-auto-import": "^2.4.3", + "ember-data": "workspace:4.11.0", "@ember/string": "^3.0.1", "ember-inflector": "^4.0.2" }, @@ -36,42 +36,43 @@ } }, "devDependencies": { - "@babel/core": "^7.21.0", - "@babel/runtime": "^7.21.0", - "@ember-data/unpublished-test-infra": "workspace:4.12.0-alpha.0", + "@babel/core": "^7.19.3", + "@babel/runtime": "^7.19.3", + "@ember-data/unpublished-test-infra": "workspace:4.11.0", "@ember/optional-features": "^2.0.0", - "@ember/test-helpers": "^2.9.3", + "@ember/test-helpers": "~2.8.1", "@embroider/compat": "^1.9.0", "@embroider/core": "^1.9.0", "@embroider/webpack": "^1.9.0", "@glimmer/component": "^1.1.2", "@glimmer/tracking": "^1.1.2", - "@types/ember": "^4.0.3", + "@types/ember": "^4.0.1", "@types/ember-qunit": "^5.0.2", "@types/ember-testing-helpers": "^0.0.4", + "@types/ember__test-helpers": "^2.8.1", "@types/rsvp": "^4.0.4", "broccoli-asset-rev": "^3.0.0", - "ember-cli": "~4.10.0", + "ember-cli": "~4.8.0", "ember-cli-app-version": "^5.0.0", "ember-cli-babel": "^7.26.11", "ember-cli-dependency-checker": "^3.3.1", "ember-cli-fastboot": "^3.3.2", "ember-cli-fastboot-testing": "^0.6.0", - "ember-cli-htmlbars": "^6.2.0", + "ember-cli-htmlbars": "^6.1.1", "ember-cli-inject-live-reload": "^2.1.0", "ember-export-application-global": "^2.0.1", "ember-fetch": "^8.1.2", "ember-load-initializers": "^2.1.2", "ember-maybe-import-regenerator": "^1.0.0", - "ember-qunit": "^6.1.1", - "ember-resolver": "^10.0.0", + "ember-qunit": "^6.0.0", + "ember-resolver": "^8.0.3", "ember-simple-tree": "^0.8.3", - "ember-source": "~4.11.0", + "ember-source": "~4.8.0", "loader.js": "^4.7.0", - "qunit": "^2.19.4", + "qunit": "^2.19.2", "qunit-dom": "^2.0.0", - "typescript": "~4.9.5", - "webpack": "^5.75.0" + "typescript": "~4.8.4", + "webpack": "^5.74.0" }, "engines": { "node": "^14.8.0 || 16.* || >= 18.*" @@ -85,5 +86,5 @@ "volta": { "extends": "../../package.json" }, - "packageManager": "pnpm@7.27.1" + "packageManager": "pnpm@7.15.0" } \ No newline at end of file From d372c16b6114494f39c50a51fdf3e7ac053dcae9 Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Sat, 25 Feb 2023 00:55:19 -0800 Subject: [PATCH 3/6] fix unary --- .../index.js | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js b/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js index eb429eb1094..8f59ca2a4bd 100644 --- a/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js +++ b/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js @@ -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; @@ -18,12 +25,21 @@ module.exports = function (babel) { let localBindingName = specifier.node.local.name; let binding = specifier.scope.getBinding(localBindingName); binding.referencePaths.forEach((p) => { - p.replaceWith( + 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'), [ - t.callExpression(state.importer.import(p, '@embroider/macros', 'dependencySatisfies'), [ - t.stringLiteral(replacements[name]), - t.stringLiteral('*'), - ]), + negateStatement ? t.unaryExpression('!', exp) : exp ]) ); }); From 6814f74f1f185e57f34b61d939adca574eec505b Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Sat, 25 Feb 2023 01:00:12 -0800 Subject: [PATCH 4/6] fix flag usages --- .../schema-definition-service.ts | 9 ++++++--- .../addon/-private/network/fetch-manager.ts | 18 ++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/store/addon/-private/legacy-model-support/schema-definition-service.ts b/packages/store/addon/-private/legacy-model-support/schema-definition-service.ts index 9478dd06314..f8aeda54e05 100644 --- a/packages/store/addon/-private/legacy-model-support/schema-definition-service.ts +++ b/packages/store/addon/-private/legacy-model-support/schema-definition-service.ts @@ -122,11 +122,14 @@ 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) { // we don't cache misses in case someone wants to register a missing model return null; diff --git a/packages/store/addon/-private/network/fetch-manager.ts b/packages/store/addon/-private/network/fetch-manager.ts index aa9af2bf52f..cda841ffa13 100644 --- a/packages/store/addon/-private/network/fetch-manager.ts +++ b/packages/store/addon/-private/network/fetch-manager.ts @@ -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) { From 843e7d69acb298bfc07e6b7aa9046343a163c66f Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Sat, 25 Feb 2023 01:04:32 -0800 Subject: [PATCH 5/6] fix usage --- packages/store/addon/-private/store-service.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/store/addon/-private/store-service.ts b/packages/store/addon/-private/store-service.ts index bdd68fe05b5..e2bb3ac15c1 100644 --- a/packages/store/addon/-private/store-service.ts +++ b/packages/store/addon/-private/store-service.ts @@ -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`, @@ -2692,8 +2694,10 @@ export function assertIdentifierHasId( } function isDSModel(record: RecordInstance | null): record is DSModel { + if (!HAS_MODEL_PACKAGE) { + return false; + } return ( - HAS_MODEL_PACKAGE && !!record && 'constructor' in record && 'isModel' in record.constructor && From 7108b19df6ef58b23919e41abf195c6cd04b5bed Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Sat, 25 Feb 2023 01:07:33 -0800 Subject: [PATCH 6/6] fix lint --- .../index.js | 2 +- .../legacy-model-support/schema-definition-service.ts | 1 - packages/store/addon/-private/store-service.ts | 7 +------ 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js b/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js index 8f59ca2a4bd..4a678c55956 100644 --- a/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js +++ b/packages/private-build-infra/src/transforms/babel-plugin-convert-existence-checks-to-macros/index.js @@ -39,7 +39,7 @@ module.exports = function (babel) { node.replaceWith( t.callExpression(state.importer.import(p, '@embroider/macros', 'macroCondition'), [ - negateStatement ? t.unaryExpression('!', exp) : exp + negateStatement ? t.unaryExpression('!', exp) : exp, ]) ); }); diff --git a/packages/store/addon/-private/legacy-model-support/schema-definition-service.ts b/packages/store/addon/-private/legacy-model-support/schema-definition-service.ts index f8aeda54e05..b8f0fab8564 100644 --- a/packages/store/addon/-private/legacy-model-support/schema-definition-service.ts +++ b/packages/store/addon/-private/legacy-model-support/schema-definition-service.ts @@ -129,7 +129,6 @@ export function getModelFactory(store: Store, cache, normalizedModelName: string } } - if (!factory) { // we don't cache misses in case someone wants to register a missing model return null; diff --git a/packages/store/addon/-private/store-service.ts b/packages/store/addon/-private/store-service.ts index e2bb3ac15c1..453c4de19c6 100644 --- a/packages/store/addon/-private/store-service.ts +++ b/packages/store/addon/-private/store-service.ts @@ -2697,12 +2697,7 @@ function isDSModel(record: RecordInstance | null): record is DSModel { if (!HAS_MODEL_PACKAGE) { return false; } - return ( - !!record && - 'constructor' in record && - 'isModel' in record.constructor && - record.constructor.isModel === true - ); + return !!record && 'constructor' in record && 'isModel' in record.constructor && record.constructor.isModel === true; } type AdapterErrors = Error & { errors?: unknown[]; isAdapterError?: true; code?: string };