UNPKG

3.49 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs');
4
5const FilterImports = require.resolve('babel-plugin-filter-imports');
6const FeatureFlags = require.resolve('babel-plugin-feature-flags');
7const StripClassCallCheck = require.resolve('babel6-plugin-strip-class-callcheck');
8const StripFilteredImports = require.resolve('./transforms/babel-plugin-remove-imports');
9const TransformBlockScoping = require.resolve('@babel/plugin-transform-block-scoping');
10const { isInstrumentedBuild, wantsEnabledFeatures, getManuallyEnabledFeatures } = require('./cli-flags');
11
12function uniqueAdd(obj, key, values) {
13 const a = (obj[key] = obj[key] || []);
14
15 for (let i = 0; i < values.length; i++) {
16 if (a.indexOf(values[i]) === -1) {
17 a.push(values[i]);
18 }
19 }
20}
21
22function isProduction(environment) {
23 return /production/.test(environment);
24}
25
26module.exports = function(environment, isLocalBuild) {
27 let flagsJsonPath = __dirname + '/../config/in-progress-features.json';
28 let flagsJson = fs.readFileSync(flagsJsonPath, { encoding: 'utf8' });
29 let inProgressFlags = JSON.parse(flagsJson);
30 let filteredImports = {};
31 let allowInProgressFeatures = isLocalBuild === true && wantsEnabledFeatures();
32 let manuallyEnabled = isLocalBuild ? getManuallyEnabledFeatures() : {};
33 let enabledFlags = [];
34 let allFlags = Object.assign({}, inProgressFlags, manuallyEnabled);
35
36 for (let flag in allFlags) {
37 let state = inProgressFlags[flag];
38 /*
39 Default anything not `true` (e.g. `null`) feature to `false`
40 unless this is a local build and we've set the flags
41 to enable something.
42 */
43 if (manuallyEnabled[flag]) {
44 if (state === true) {
45 // eslint-disable-next-line no-console
46 console.warn('You specified the in-progress-feature "' + flag + '" but it was already active');
47 } else if (state === undefined) {
48 throw new Error('You specified the in-progress-feature "' + flag + '" but no such flag exists!');
49 } else {
50 // eslint-disable-next-line no-console
51 console.warn('Manually Actived in-progress-feature: ' + flag);
52 }
53 inProgressFlags[flag] = true;
54 } else if (allowInProgressFeatures && state === null) {
55 enabledFlags.push(flag);
56 inProgressFlags[flag] = true;
57 } else if (state !== true) {
58 inProgressFlags[flag] = false;
59 }
60 }
61
62 if (allowInProgressFeatures) {
63 if (enabledFlags.length) {
64 // eslint-disable-next-line no-console
65 console.warn(
66 'Enabled the following in-progress-features that specified `null` as their state for this build: ["' +
67 enabledFlags.join(', ') +
68 '"]'
69 );
70 } else {
71 // eslint-disable-next-line no-console
72 console.warn(
73 'Attempted to enable all in-progress-features that specify `null` as their state for this build, but there were none.'
74 );
75 }
76 }
77
78 let postTransformPlugins = [];
79 let plugins = [
80 [
81 FeatureFlags,
82 {
83 import: { module: 'ember-data/-private/features' },
84 features: inProgressFlags,
85 },
86 ],
87 ];
88
89 if (isProduction(environment) || isInstrumentedBuild()) {
90 postTransformPlugins.push([StripClassCallCheck]);
91 uniqueAdd(filteredImports, 'ember-data/-debug', ['assertPolymorphicType']);
92 }
93
94 plugins.push(
95 [FilterImports, { imports: filteredImports }],
96 [StripFilteredImports, filteredImports],
97 [TransformBlockScoping, { throwIfClosureRequired: true }]
98 );
99
100 return { plugins, postTransformPlugins };
101};