-
Notifications
You must be signed in to change notification settings - Fork 41
/
index.js
127 lines (106 loc) · 4.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
/* eslint-env node */
const VersionChecker = require('ember-cli-version-checker');
const cacheKeyForTree = require('calculate-cache-key-for-tree');
module.exports = {
name: 'ember-test-selectors',
_assignOptions(app) {
let ui = app.project.ui;
let appOptions = app.options || {};
let addonOptions = appOptions['ember-test-selectors'] || {};
if (addonOptions.environments) {
ui.writeDeprecateLine(
'The "environments" option in "ember-test-selectors" has been replaced ' +
'with the "strip" option. Use e.g. "strip: EmberApp.env() === \'production\'" instead to ' +
'recreate the old behavior.',
false
);
this._stripTestSelectors = addonOptions.environments.indexOf(app.env) !== -1;
} else if ('strip' in addonOptions) {
this._stripTestSelectors = addonOptions.strip;
} else {
this._stripTestSelectors = !app.tests;
}
if ('patchClassicComponent' in addonOptions) {
ui.writeDeprecateLine(
'[ember-test-selectors] The `patchClassicComponent` option is obsolete. ' +
'You can remove it from your `ember-cli-build.js` file.',
false
);
}
},
_setupPreprocessorRegistry(registry) {
if (this._stripTestSelectors) {
let plugin = this._buildStripPlugin();
plugin.parallelBabel = {
requireFile: __filename,
buildUsing: '_buildStripPlugin',
params: {},
};
registry.add('htmlbars-ast-plugin', plugin);
}
},
included(appOrParent) {
this._super.included.apply(this, arguments);
let host = this._findHost();
this._assignOptions(host);
// we can't use the setupPreprocessorRegistry() hook as it is called too
// early and we do not have reliable access to `app.tests` there yet
this._setupPreprocessorRegistry(appOrParent.registry);
// add the StripDataTestPropertiesPlugin to the list of plugins used by
// the `ember-cli-babel` addon
if (this._stripTestSelectors && !this._registeredWithBabel) {
let checker = new VersionChecker(this.parent).for('ember-cli-babel', 'npm');
appOrParent.options = appOrParent.options || {};
if (checker.satisfies('^6.0.0-beta.1')) {
appOrParent.options.babel6 = appOrParent.options.babel6 || {};
appOrParent.options.babel6.plugins = appOrParent.options.babel6.plugins || [];
appOrParent.options.babel6.plugins.push(
require.resolve('./strip-data-test-properties-plugin6')
);
} else if (checker.satisfies('^7.0.0') || checker.satisfies('^8.0.0')) {
appOrParent.options.babel = appOrParent.options.babel || {};
appOrParent.options.babel.plugins = appOrParent.options.babel.plugins || [];
appOrParent.options.babel.plugins.push(
require.resolve('./strip-data-test-properties-plugin6')
);
} else {
this.ui.writeWarnLine(
'ember-test-selectors: You are using an unsupported ember-cli-babel version. data-test ' +
'properties are not automatically stripped from your JS code.'
);
}
this._registeredWithBabel = true;
}
},
cacheKeyForTree(treeType) {
// ensure our treeForAddon is memoized
if (treeType === 'addon') {
return cacheKeyForTree('addon', this, [this._stripTestSelectors]);
} else {
return cacheKeyForTree(treeType, this);
}
},
preprocessTree(type, tree) {
// remove the unit tests if we're testing ourself and are in strip mode.
// we do this because these tests depend on the "addon" and "app" folders being available,
// which is not the case if they are stripped out of the build.
if (
type === 'test' &&
this._stripTestSelectors &&
this.project.name() === 'ember-test-selectors'
) {
tree = require('broccoli-stew').rm(tree, 'dummy/tests/unit/**/*.js');
}
return tree;
},
_buildStripPlugin() {
let StripTestSelectorsTransform = require('./strip-test-selectors');
return {
name: 'strip-test-selectors',
plugin: StripTestSelectorsTransform,
baseDir: StripTestSelectorsTransform.baseDir,
cacheKey: StripTestSelectorsTransform.cacheKey,
};
},
};