-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
105 lines (92 loc) · 3.55 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
'use strict';
const path = require('path');
const Funnel = require('broccoli-funnel');
const mergeTrees = require('broccoli-merge-trees');
module.exports = {
name: 'ember-cli-prop-types',
/**
* By default we strip out the call to check prop types in prod builds
*/
addonOptions: {
compress: true,
getDefaultProps: true
},
/**
* Import prop-types package from /vendor (See treeForVendor for package Funnel
* details). Configure UglifyJS for prod builds.
* @method included
* @param {Object} app Parent app or addon
* @return {Object} Parent application
*/
included(app) {
this._super.included.apply(this, arguments);
const vendor = this.treePaths.vendor;
this.env = process.env.EMBER_ENV || 'development';
/**
* Attach babel config options before the addon tree is transpiled.
* Use babel plugin to replace inline variables to determine whether or not to
* check prop types in development/production and support the getDefaultProps
* function.
*/
this.options.babel = {
plugins: [
['inline-replace-variables', {
"NODE_ENV": this.env,
"INCLUDE_GET_DEFAULT_PROPS": this.addonOptions.getDefaultProps
}]
]
};
// Find the parent app by crawling addon tree
while (typeof app.import !== 'function' && app.app) {
app = app.app;
}
// Check for configurations specified by consuming app and fall back to addon
// defaults using Object.assign
app.options = app.options || {};
app.options.emberCliPropTypes = app.options.emberCliPropTypes || {};
let addonOptions = Object.assign(this.addonOptions, app.options.emberCliPropTypes)
this.addonOptions = addonOptions;
// In production strip out features that are disabled
if (this.env === 'production' && addonOptions.compress) {
app.options.minifyJS = app.options.minifyJS || {};
app.options.minifyJS.options = app.options.minifyJS.options || {};
let minifyOpts = app.options.minifyJS.options;
minifyOpts.enabled = true; // If you want to remove unreachable code, uglify must be enabled
minifyOpts.compress = minifyOpts.compress || {};
minifyOpts.compress.dead_code = true; // Prunes dead code
minifyOpts.compress.global_defs = minifyOpts.compress.global_defs || {};
minifyOpts.compress.global_defs.NODE_ENV = this.env;
minifyOpts.compress.global_defs.INCLUDE_GET_DEFAULT_PROPS = addonOptions.getDefaultProps;
}
// Import the prop-types library only in dev builds. In prod builds import the
// prod shims so import statements don't throw
if (this.env !== 'production') {
app.import(`${vendor}/prop-types/prop-types.js`, {
using: [
{ transformation: 'amd', as: 'prop-types' }
]
});
} else {
app.import(`${vendor}/prop-types-production-shims.js`);
}
return app;
},
/**
* Package must be pulled into the /vendor directory or CLI will fail to app.import
* it. Use a Funnel to move package from node_modules to /vendor
* @method treeForVendor
* @param {Array} vendorTree Broccoli vendor tree
* @return {Array} Broccoli vendor tree
*/
treeForVendor(vendorTree) {
if (this.env === 'production') { return vendorTree; }
const tree = [];
if (vendorTree) { tree.push(vendorTree); }
// require.resolve always returns the correct path to the prop-types node_module
tree.push(new Funnel(path.dirname(require.resolve('prop-types')), {
destDir: 'prop-types',
include: [new RegExp(/\.js$/)]
}));
return mergeTrees(tree);
}
};