Skip to content

Commit

Permalink
[CHORE] add package flag infra
Browse files Browse the repository at this point in the history
convert to flag infra

dont ship flag code to production
  • Loading branch information
runspired committed Nov 14, 2019
1 parent 8d79079 commit fa4c9fa
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 10 deletions.
32 changes: 31 additions & 1 deletion packages/canary-features/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
module.exports = { name: require('./package').name };
function getApp(addon) {
while (addon && !addon.app) {
addon = addon.parent;
}
if (!addon) {
throw new Error(`Unable to find the parent application`);
}
return addon.app;
}

module.exports = {
name: require('./package').name,
treeFor() {
// Nested addons don't call isEnabled automatically,
// So this ensures that we return empty trees whenever
// we are not enabled.
if (this.isEnabled()) {
return this._super.treeFor.call(this, ...arguments);
}
},
isEnabled() {
if (this.__isEnabled !== null) {
return this.__isEnabled;
}
const env = getApp(this).env;

this.__isEnabled = env !== 'production';

return this.__isEnabled;
},
};
9 changes: 9 additions & 0 deletions packages/private-build-infra/addon/available-packages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
HAS_EMBER_DATA_PACKAGE: 'ember-data',
HAS_STORE_PACKAGE: '@ember-data/store',
HAS_MODEL_PACKAGE: '@ember-data/model',
HAS_RECORD_DATA_PACKAGE: '@ember-data/record-data',
HAS_ADAPTER_PACKAGE: '@ember-data/adapter',
HAS_SERIALIZER_PACKAGE: '@ember-data/serializer',
HAS_DEBUG_PACKAGE: '@ember-data/debug',
};
22 changes: 22 additions & 0 deletions packages/private-build-infra/addon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
@module @ember-data/private-build-infra
*/

import { has } from 'require';
import POSSIBLE_PACKAGES from './available-packages';

type PackageSettings = Record<keyof typeof POSSIBLE_PACKAGES, boolean>;

const PACKAGES = Object.keys(POSSIBLE_PACKAGES).reduce((obj, name) => {
const NAME = POSSIBLE_PACKAGES[name];
obj[NAME] = has(name) || false;
return obj;
}, {}) as PackageSettings;

export const HAS_EMBER_DATA_PACKAGE = PACKAGES.HAS_EMBER_DATA_PACKAGE;
export const HAS_STORE_PACKAGE = PACKAGES.HAS_STORE_PACKAGE;
export const HAS_MODEL_PACKAGE = PACKAGES.HAS_MODEL_PACKAGE;
export const HAS_ADAPTER_PACKAGE = PACKAGES.HAS_ADAPTER_PACKAGE;
export const HAS_SERIALIZER_PACKAGE = PACKAGES.HAS_SERIALIZER_PACKAGE;
export const HAS_DEBUG_PACKAGE = PACKAGES.HAS_DEBUG_PACKAGE;
export const HAS_RECORD_DATA_PACKAGE = PACKAGES.HAS_RECORD_DATA_PACKAGE;
31 changes: 31 additions & 0 deletions packages/private-build-infra/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function getApp(addon) {
while (addon && !addon.app) {
addon = addon.parent;
}
if (!addon) {
throw new Error(`Unable to find the parent application`);
}
return addon.app;
}

module.exports = {
name: require('./package').name,
treeFor() {
// Nested addons don't call isEnabled automatically,
// So this ensures that we return empty trees whenever
// we are not enabled.
if (this.isEnabled()) {
return this._super.treeFor.call(this, ...arguments);
}
},
isEnabled() {
if (this.__isEnabled !== null) {
return this.__isEnabled;
}
const env = getApp(this).env;

this.__isEnabled = env !== 'production';

return this.__isEnabled;
},
};
6 changes: 5 additions & 1 deletion packages/private-build-infra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "@ember-data/private-build-infra",
"version": "3.16.0-alpha.0",
"description": "The default blueprint for ember-data private packages.",
"keywords": [],
"keywords": [
"ember-addon"
],
"repository": "",
"license": "MIT",
"author": "",
Expand All @@ -14,6 +16,8 @@
"test:node": "mocha"
},
"dependencies": {
"ember-cli-babel": "^7.12.0",
"ember-cli-typescript": "^3.1.1",
"@babel/plugin-transform-block-scoping": "^7.6.3",
"@ember-data/canary-features": "3.16.0-alpha.0",
"@ember/edition-utils": "^1.1.1",
Expand Down
18 changes: 16 additions & 2 deletions packages/private-build-infra/src/debug-macros.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

const FEATURES = require('./features');
const PACKAGES = require('./packages');

module.exports = function debugMacros(environment) {
module.exports = function debugMacros() {
const debugMacrosPath = require.resolve('babel-plugin-debug-macros');
let plugins = [
[
require.resolve('babel-plugin-debug-macros'),
debugMacrosPath,
{
flags: [
{
Expand All @@ -16,6 +18,18 @@ module.exports = function debugMacros(environment) {
},
'@ember-data/canary-features-stripping',
],
[
debugMacrosPath,
{
flags: [
{
source: '@ember-data/private-build-infra',
flags: PACKAGES,
},
],
},
'@ember-data/optional-packages-stripping',
],
];

return plugins;
Expand Down
25 changes: 25 additions & 0 deletions packages/private-build-infra/src/packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const requireEsm = require('esm')(module);

function getPackages() {
const { default: POSSIBLE_PACKAGES } = requireEsm('@ember-data/private-build-infra/addon/available-packages.ts');
const flags = {};

Object.keys(POSSIBLE_PACKAGES).forEach(flag => {
const packageName = POSSIBLE_PACKAGES[flag];
let hasPackage = false;
try {
require.resolve(packageName);
hasPackage = true;
} catch (e) {
hasPackage = false;
}

flags[flag] = hasPackage;
});

return flags;
}

module.exports = getPackages();
2 changes: 1 addition & 1 deletion packages/private-build-infra/src/stripped-build-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function isProduction(environment) {

module.exports = function(environment) {
let plugins = [];
const DebugMacros = require('./debug-macros')(environment);
const DebugMacros = require('./debug-macros')();
let filteredImports = {};
let postTransformPlugins = [];

Expand Down
5 changes: 2 additions & 3 deletions packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import {
REQUEST_SERVICE,
CUSTOM_MODEL_CLASS,
} from '@ember-data/canary-features';
import { HAS_ADAPTER_PACKAGE, HAS_MODEL_PACKAGE, HAS_SERIALIZER_PACKAGE } from '@ember-data/private-build-infra';

import { Record } from '../ts-interfaces/record';
import { JsonApiRelationship } from '../ts-interfaces/record-data-json-api';
import { ResourceIdentifierObject } from '../ts-interfaces/ember-data-json-api';
Expand Down Expand Up @@ -100,9 +102,6 @@ type PendingSaveItem = {
let globalClientIdCounter = 1;

const HAS_DS_META_PACKAGE = has('ember-data');
const HAS_SERIALIZER_PACKAGE = has('@ember-data/serializer');
const HAS_ADAPTER_PACKAGE = has('@ember-data/adapter');
const HAS_MODEL_PACKAGE = has('@ember-data/model');
let _Model;
function getModel() {
if (HAS_MODEL_PACKAGE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { get } from '@ember/object';
import { getOwner } from '@ember/application';
import normalizeModelName from './normalize-model-name';
import { RelationshipsSchema, AttributesSchema } from '../ts-interfaces/record-data-schemas';
import require, { has } from 'require';
import require from 'require';
import CoreStore from './core-store';
import { HAS_MODEL_PACKAGE } from '@ember-data/private-build-infra';
type Model = import('@ember-data/model').default;

const HAS_MODEL_PACKAGE = has('@ember-data/model');
let _Model;
function getModel() {
if (HAS_MODEL_PACKAGE) {
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"@ember-data/record-data/*": ["packages/record-data/addon/*"],
"@ember-data/canary-features": ["packages/canary-features/addon"],
"@ember-data/canary-features/*": ["packages/canary-features/addon/*"],
"@ember-data/private-build-infra": ["packages/private-build-infra/addon"],
"@ember-data/private-build-infra/*": ["packages/private-build-infra/addon/*"],
"fastboot-test-app/tests/*": ["tests/*"],
"fastboot-test-app/*": ["app/*"],
"*": ["packages/store/types/*", "packages/record-data/types/*", "packages/fastboot-test-app/types/*"]
Expand Down

0 comments on commit fa4c9fa

Please sign in to comment.