1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 |
|
5 | const FilterImports = require.resolve('babel-plugin-filter-imports');
|
6 | const FeatureFlags = require.resolve('babel-plugin-feature-flags');
|
7 | const StripClassCallCheck = require.resolve('babel6-plugin-strip-class-callcheck');
|
8 | const StripFilteredImports = require.resolve('./transforms/babel-plugin-remove-imports');
|
9 | const TransformBlockScoping = require.resolve('@babel/plugin-transform-block-scoping');
|
10 | const { isInstrumentedBuild, wantsEnabledFeatures, getManuallyEnabledFeatures } = require('./cli-flags');
|
11 |
|
12 | function 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 |
|
22 | function isProduction(environment) {
|
23 | return /production/.test(environment);
|
24 | }
|
25 |
|
26 | module.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 |
|
40 |
|
41 |
|
42 |
|
43 | if (manuallyEnabled[flag]) {
|
44 | if (state === true) {
|
45 |
|
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 |
|
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 |
|
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 |
|
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 | };
|