UNPKG

4.93 kBJavaScriptView Raw
1const calculateCacheKeyForTree = require('calculate-cache-key-for-tree');
2const Funnel = require('broccoli-funnel');
3const merge = require('broccoli-merge-trees');
4const BroccoliDebug = require('broccoli-debug');
5const version = require('./create-version-module');
6const { isInstrumentedBuild } = require('./cli-flags');
7const rollupPrivateModule = require('./utilities/rollup-private-module');
8
9function isProductionEnv() {
10 let isProd = /production/.test(process.env.EMBER_ENV);
11 let isTest = process.env.EMBER_CLI_TEST_COMMAND;
12
13 return isProd && !isTest;
14}
15
16function addonBuildConfigForDataPackage(PackageName) {
17 return {
18 name: PackageName,
19
20 init() {
21 this._super.init && this._super.init.apply(this, arguments);
22 this._prodLikeWarning();
23 this.debugTree = BroccoliDebug.buildDebugCallback(`ember-data:${PackageName}`);
24 this.options = this.options || {};
25 },
26
27 _prodLikeWarning() {
28 let emberEnv = process.env.EMBER_ENV;
29 if (emberEnv !== 'production' && /production/.test(emberEnv)) {
30 this._warn(
31 `Production-like values for EMBER_ENV are deprecated (your EMBER_ENV is "${emberEnv}") and support will be removed in Ember Data 4.0.0. If using ember-cli-deploy, please configure your build using 'production'. Otherwise please set your EMBER_ENV to 'production' for production builds.`
32 );
33 }
34 },
35
36 _warn(message) {
37 let chalk = require('chalk');
38 let warning = chalk.yellow('WARNING: ' + message);
39
40 if (this.ui && this.ui.writeWarnLine) {
41 this.ui.writeWarnLine(message);
42 } else if (this.ui) {
43 this.ui.writeLine(warning);
44 } else {
45 // eslint-disable-next-line no-console
46 console.log(warning);
47 }
48 },
49
50 _suppressCircularDependencyWarnings(message, next) {
51 if (message.code !== 'CIRCULAR_DEPENDENCY') {
52 next(message);
53 }
54 },
55
56 getOutputDirForVersion() {
57 let VersionChecker = require('ember-cli-version-checker');
58 let checker = new VersionChecker(this);
59 let emberCli = checker.for('ember-cli', 'npm');
60
61 let requiresModulesDir = emberCli.satisfies('< 3.0.0');
62
63 return requiresModulesDir ? 'modules' : '';
64 },
65
66 isLocalBuild() {
67 let appName = this.parent.pkg.name;
68
69 return this.isDevelopingAddon() && appName === PackageName;
70 },
71
72 buildBabelOptions() {
73 let babelOptions = this.options.babel || {};
74 let existingPlugins = babelOptions.plugins || [];
75 let customPlugins = require('./stripped-build-plugins')(
76 process.env.EMBER_ENV,
77 this.isLocalBuild()
78 );
79 let plugins = existingPlugins.map(plugin => {
80 return Array.isArray(plugin) ? plugin : [plugin];
81 });
82 plugins = plugins.concat(customPlugins.plugins);
83
84 return {
85 loose: true,
86 plugins,
87 postTransformPlugins: customPlugins.postTransformPlugins,
88 exclude: ['transform-block-scoping', 'transform-typeof-symbol'],
89 };
90 },
91
92 _setupBabelOptions() {
93 if (this._hasSetupBabelOptions) {
94 return;
95 }
96
97 this.options.babel = this.buildBabelOptions();
98
99 this._hasSetupBabelOptions = true;
100 },
101
102 included() {
103 this._super.included.apply(this, arguments);
104
105 this._setupBabelOptions();
106 },
107
108 cacheKeyForTree(treeType) {
109 return calculateCacheKeyForTree(treeType, this);
110 },
111
112 externalDependenciesForPrivateModule() {
113 return [];
114 },
115
116 treeForAddon(tree) {
117 if (this.shouldRollupPrivate !== true) {
118 return this._super.treeForAddon.call(this, tree);
119 }
120
121 tree = this.debugTree(tree, 'input');
122 this._setupBabelOptions();
123
124 let babel = this.addons.find(addon => addon.name === 'ember-cli-babel');
125
126 let treeWithVersion = merge([
127 tree,
128 version(), // compile the VERSION into the build
129 ]);
130
131 let privateTree = rollupPrivateModule(tree, {
132 packageName: PackageName,
133 babelCompiler: babel,
134 babelOptions: this.options.babel,
135 onWarn: this._suppressCircularDependencyWarnings,
136 externalDependencies: this.externalDependenciesForPrivateModule(),
137 destDir: this.getOutputDirForVersion(),
138 });
139
140 let withoutPrivate = new Funnel(treeWithVersion, {
141 exclude: [
142 '-private',
143 isProductionEnv() && !isInstrumentedBuild() ? '-debug' : false,
144 ].filter(Boolean),
145
146 destDir: PackageName,
147 });
148
149 // use the default options
150 let publicTree = babel.transpileTree(this.debugTree(withoutPrivate, 'babel-public:input'));
151 publicTree = this.debugTree(publicTree, 'babel-public:output');
152
153 let destDir = this.getOutputDirForVersion();
154
155 publicTree = new Funnel(publicTree, { destDir });
156
157 return this.debugTree(merge([publicTree, privateTree]), 'final');
158 },
159 };
160}
161
162module.exports = addonBuildConfigForDataPackage;