UNPKG

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