-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.js
More file actions
128 lines (106 loc) · 3.46 KB
/
index.js
File metadata and controls
128 lines (106 loc) · 3.46 KB
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
128
/* eslint-env node */
'use strict';
const Funnel = require('broccoli-funnel');
const VersionChecker = require('ember-cli-version-checker');
const log = require('debug')('ember-decorators:argument');
const FILTER_IMPORTS_LABEL = 'filter-imports:@ember-decorators/argument';
function isProductionEnv() {
return /production/.test(process.env.EMBER_ENV);
}
function addDefaults(options = {}) {
// Handle the old option, in case people forget to update it
if (options.disableCodeStripping) {
options.enableCodeStripping = true;
}
return Object.assign({ enableCodeStripping: true }, options);
}
module.exports = {
name: require('./package').name,
_getParentOptions() {
let options;
// The parent can either be an Addon or a Project. If it's an addon,
// we want to use the app instead. This public method probably wasn't meant
// for this, but it's named well enough that we can use it for this purpose.
if (this.parent && !this.parent.isEmberCLIProject) {
options = this.parent.options = this.parent.options || {};
} else {
options = this.app.options = this.app.options || {};
}
return options;
},
shouldStripAddon() {
return isProductionEnv() && this.addonOptions.enableCodeStripping;
},
/**
* Overwritten to remove implementation in Production builds, if the user does not
* disable stripping
*
* @param {BroccoliNode} tree
*/
treeForAddon(tree) {
const filteredTree = this.shouldStripAddon()
? new Funnel(tree, {
exclude: ['-private', 'types.js', 'index.js']
})
: tree;
return this._super.treeForAddon.call(this, filteredTree);
},
stripImports(babelOptions) {
const alreadyHasPlugin = babelOptions.plugins
.filter(definition => Array.isArray(definition))
.some(definition => definition[2] === FILTER_IMPORTS_LABEL);
if (!alreadyHasPlugin) {
babelOptions.plugins.push([
require.resolve('babel-plugin-filter-imports'),
{
imports: {
'@ember-decorators/argument': ['argument'],
'@ember-decorators/argument/types': [
'Any',
'arrayOf',
'optional',
'oneOf',
'shapeOf',
'unionOf',
'Action',
'ClassicAction',
'Element',
'Node'
]
}
},
'filter-imports:@ember-decorators/argument'
]);
}
},
included(app) {
this._super.included.apply(this, arguments);
let parentOptions = this._getParentOptions();
this.addonOptions = addDefaults(
parentOptions['@ember-decorators/argument']
);
log('Using options %o', this.addonOptions);
if (
isProductionEnv() &&
!this._registeredWithBabel &&
!this.addonOptions.disableCodeStripping
) {
let babelChecker = new VersionChecker(this.parent).for(
'ember-cli-babel',
'npm'
);
if (babelChecker.gte('7.0.0')) {
// Create babel options if they do not exist
parentOptions.babel = parentOptions.babel || {};
parentOptions.babel.plugins = parentOptions.babel.plugins || [];
this.stripImports(parentOptions.babel);
} else {
app.project.ui.writeWarnLine(
'@ember-decorators/argument: You are using an unsupported ember-cli-babel version, ' +
'decorators will not be stripped automatically'
);
}
this._registeredWithBabel = true;
}
}
};