UNPKG

6.61 kBJavaScriptView Raw
1const semver = require("semver");
2const resolvePackagePath = require("resolve-package-path");
3
4function _getDebugMacroPlugins(appRoot) {
5 const isProduction = process.env.EMBER_ENV === "production";
6 const isDebug = !isProduction;
7
8 const emberDebugOptions = {
9 flags: [
10 {
11 source: "@glimmer/env",
12 flags: { DEBUG: isDebug, CI: !!process.env.CI },
13 },
14 ],
15
16 debugTools: {
17 isDebug,
18 source: "@ember/debug",
19 assertPredicateIndex: 1,
20 },
21 };
22
23 const emberApplicationDeprecationsOptions = {
24 // deprecated import path https://github.com/emberjs/ember.js/pull/17926#issuecomment-484987305
25 externalizeHelpers: {
26 global: "Ember",
27 },
28
29 debugTools: {
30 isDebug,
31 source: "@ember/application/deprecations",
32 assertPredicateIndex: 1,
33 },
34 };
35
36 if (_emberVersionRequiresModulesAPIPolyfill(appRoot)) {
37 emberDebugOptions.externalizeHelpers = {
38 global: "Ember",
39 };
40 emberApplicationDeprecationsOptions.externalizeHelpers = {
41 global: "Ember",
42 };
43 } else {
44 emberDebugOptions.externalizeHelpers = {
45 module: "@ember/debug",
46 };
47 emberApplicationDeprecationsOptions.externalizeHelpers = {
48 module: "@ember/application/deprecations",
49 };
50 }
51
52 return [
53 [
54 require.resolve("babel-plugin-debug-macros"),
55 emberDebugOptions,
56 "@ember/debug stripping",
57 ],
58 [
59 require.resolve("babel-plugin-debug-macros"),
60 emberApplicationDeprecationsOptions,
61 "@ember/application/deprecations stripping",
62 ],
63 ];
64}
65
66function _emberVersionRequiresModulesAPIPolyfill(appRoot) {
67 let packagePath = resolvePackagePath("ember-source", appRoot);
68 if (packagePath === null) {
69 return true;
70 }
71
72 let pkg = require(packagePath);
73 return pkg && semver.lt(pkg.version, "3.27.0-alpha.1");
74}
75
76function _getEmberModulesAPIPolyfill(appRoot, config) {
77 if (config.disableEmberModulesAPIPolyfill) {
78 return;
79 }
80
81 if (_emberVersionRequiresModulesAPIPolyfill(appRoot)) {
82 const ignore = _getEmberModulesAPIIgnore(appRoot, config);
83
84 return [
85 [require.resolve("babel-plugin-ember-modules-api-polyfill"), { ignore }],
86 ];
87 }
88}
89
90function _shouldIgnoreEmberString(appRoot) {
91 return resolvePackagePath("@ember/string", appRoot) !== null;
92}
93
94function _shouldIgnoreJQuery(appRoot) {
95 let packagePath = resolvePackagePath("@ember/jquery", appRoot);
96 if (packagePath === null) {
97 return true;
98 }
99 let pkg = require(packagePath);
100 return pkg && semver.gt(pkg.version, "0.6.0");
101}
102
103function _emberDataVersionRequiresPackagesPolyfill(appRoot) {
104 let packagePath = resolvePackagePath("ember-data", appRoot);
105 if (packagePath === null) {
106 return false;
107 }
108 let pkg = require(packagePath);
109 return pkg && semver.lt(pkg.version, "3.12.0-alpha.0");
110}
111
112function _getEmberModulesAPIIgnore(appRoot, config) {
113 const ignore = {
114 "@ember/debug": ["assert", "deprecate", "warn"],
115 "@ember/application/deprecations": ["deprecate"],
116 };
117
118 if (config.shouldIgnoreEmberString || _shouldIgnoreEmberString(appRoot)) {
119 ignore["@ember/string"] = [
120 "fmt",
121 "loc",
122 "w",
123 "decamelize",
124 "dasherize",
125 "camelize",
126 "classify",
127 "underscore",
128 "capitalize",
129 "setStrings",
130 "getStrings",
131 "getString",
132 ];
133 }
134 if (config.shouldIgnoreJQuery || _shouldIgnoreJQuery(appRoot)) {
135 ignore["jquery"] = ["default"];
136 }
137
138 return ignore;
139}
140
141function _getEmberDataPackagesPolyfill(appRoot, config) {
142 if (config.emberDataVersionRequiresPackagesPolyfill) {
143 return [[require.resolve("babel-plugin-ember-data-packages-polyfill")]];
144 }
145 return _emberDataVersionRequiresPackagesPolyfill(appRoot);
146}
147
148function _getModuleResolutionPlugins(config) {
149 if (!config.disableModuleResolution) {
150 const resolvePath = require("../lib/relative-module-paths")
151 .resolveRelativeModulePath;
152 return [
153 [require.resolve("babel-plugin-module-resolver"), { resolvePath }],
154 [
155 require.resolve("@babel/plugin-transform-modules-amd"),
156 { noInterop: true },
157 ],
158 ];
159 }
160}
161
162function _getProposalDecoratorsAndClassPlugins(config) {
163 if (!config.shouldIgnoreDecoratorAndClassPlugins) {
164 return [
165 /**
166 * Required for apps to use `@ember/template-compliation`
167 * for transforming templates from babel-plugin-ember-template-compilation
168 * which shipped in ember-cli-htmlbars 6.2
169 *
170 * Normally this plugin wouldn't be required because the feature has shipped
171 * in all browsers, but because we have legacy decorators, and
172 * legacy decorators do not support parsing the static block syntax,
173 * we need to compile it away.
174 */
175 ["@babel/plugin-transform-class-static-block"],
176 ["@babel/plugin-proposal-decorators", { legacy: true }],
177 ["@babel/plugin-proposal-class-properties"],
178 ];
179 }
180}
181
182/**
183 * This function allows returns all the required Ember specific babel plugins for the app to transpile correctly.
184 * As the first argument, you need to pass in the appRoot (which is usually the __dirname).
185 * As the second argument, which is optional, you can choose to turn the switch on and off of several plugins that this function returns.
186 * **List of supported configs**
187 * {
188 * disableModuleResolution: boolean, // determines if you want the module resolution enabled
189 * emberDataVersionRequiresPackagesPolyfill: boolean, // enable ember data's polyfill
190 * shouldIgnoreJQuery: boolean, // ignore jQuery
191 * shouldIgnoreEmberString: boolean, // ignore ember string
192 * shouldIgnoreDecoratorAndClassPlugins: boolean, // disable decorator plugins
193 * disableEmberModulesAPIPolyfill: boolean, // disable ember modules API polyfill
194 * }
195 * @param {string} appRoot - root directory of your project
196 * @param {object} config - config options to finetune the plugins
197 */
198module.exports = function (appRoot, config = {}) {
199 return []
200 .concat(
201 _getProposalDecoratorsAndClassPlugins(config),
202 _getDebugMacroPlugins(appRoot),
203 _getEmberModulesAPIPolyfill(appRoot, config),
204 _getEmberDataPackagesPolyfill(appRoot, config),
205 _getModuleResolutionPlugins(config)
206 )
207 .filter(Boolean);
208};
209
210module.exports.getDebugMacroPlugins = _getDebugMacroPlugins;
211module.exports.getEmberModulesAPIPolyfill = _getEmberModulesAPIPolyfill;
212module.exports.getEmberDataPackagesPolyfill = _getEmberDataPackagesPolyfill;
213module.exports.getModuleResolutionPlugins = _getModuleResolutionPlugins;
214module.exports.getProposalDecoratorsAndClassPlugins = _getProposalDecoratorsAndClassPlugins;