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')(process.env.EMBER_ENV, this.isLocalBuild());
76 let plugins = existingPlugins.map(plugin => {
77 return Array.isArray(plugin) ? plugin : [plugin];
78 });
79 plugins = plugins.concat(customPlugins.plugins).concat(require('./debug-macros')(process.env.EMBER_ENV));
80
81 return {
82 loose: true,
83 plugins,
84 postTransformPlugins: customPlugins.postTransformPlugins,
85 exclude: ['transform-block-scoping', 'transform-typeof-symbol'],
86 };
87 },
88
89 _setupBabelOptions() {
90 if (this._hasSetupBabelOptions) {
91 return;
92 }
93
94 this.options.babel = this.buildBabelOptions();
95
96 this._hasSetupBabelOptions = true;
97 },
98
99 included() {
100 this._super.included.apply(this, arguments);
101
102 this._setupBabelOptions();
103 },
104
105 cacheKeyForTree(treeType) {
106 return calculateCacheKeyForTree(treeType, this);
107 },
108
109 externalDependenciesForPrivateModule() {
110 return [];
111 },
112
113 treeForAddon(tree) {
114 if (this.shouldRollupPrivate !== true) {
115 return this._super.treeForAddon.call(this, tree);
116 }
117
118 tree = this.debugTree(tree, 'input');
119 this._setupBabelOptions();
120
121 let babel = this.addons.find(addon => addon.name === 'ember-cli-babel');
122
123 let treeWithVersion = merge([
124 tree,
125 version(), // compile the VERSION into the build
126 ]);
127
128 let privateTree = rollupPrivateModule(tree, {
129 packageName: PackageName,
130 babelCompiler: babel,
131 babelOptions: this.options.babel,
132 onWarn: this._suppressCircularDependencyWarnings,
133 externalDependencies: this.externalDependenciesForPrivateModule(),
134 destDir: this.getOutputDirForVersion(),
135 });
136
137 let withoutPrivate = new Funnel(treeWithVersion, {
138 exclude: ['-private', isProductionEnv() && !isInstrumentedBuild() ? '-debug' : false].filter(Boolean),
139
140 destDir: PackageName,
141 });
142
143 // use the default options
144 let publicTree = babel.transpileTree(this.debugTree(withoutPrivate, 'babel-public:input'));
145 publicTree = this.debugTree(publicTree, 'babel-public:output');
146
147 let destDir = this.getOutputDirForVersion();
148
149 publicTree = new Funnel(publicTree, { destDir });
150
151 return this.debugTree(merge([publicTree, privateTree]), 'final');
152 },
153 };
154}
155
156module.exports = addonBuildConfigForDataPackage;