Skip to content

Commit 2c578a6

Browse files
authored
Merge pull request #96 from ember-cli/embroider-macros
Add a mode that converts to @embroider/macros
2 parents daf31bf + 14379e3 commit 2c578a6

File tree

13 files changed

+142
-22
lines changed

13 files changed

+142
-22
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { assert } from '@ember/debug';
2+
import { isDevelopingApp } from '@embroider/macros';
3+
4+
isDevelopingApp() && !(() => true )() && assert('This is an assertion', (() => true )());
5+
isDevelopingApp() && !false && assert('This is an assertion 2', false);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { assert } from '@ember/debug';
2+
3+
assert('This is an assertion', (() => true )());
4+
assert('This is an assertion 2', false);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { deprecate } from '@ember/debug';
2+
import { isDevelopingApp } from '@embroider/macros';
3+
4+
isDevelopingApp() &&
5+
!false &&
6+
deprecate('This is deprecated', false, {
7+
until: '3.0.0',
8+
id: 'a-thing',
9+
url: 'http://example.com',
10+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { deprecate } from '@ember/debug';
2+
3+
deprecate('This is deprecated', false, {
4+
until: '3.0.0',
5+
id: 'a-thing',
6+
url: 'http://example.com'
7+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { isDevelopingApp } from '@embroider/macros';
2+
3+
if (isDevelopingApp()) {
4+
console.log('stuff');
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { DEBUG } from '@glimmer/env';
2+
3+
if (DEBUG) {
4+
console.log('stuff');
5+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
]
3434
},
3535
"dependencies": {
36+
"babel-import-util": "^2.0.2",
3637
"semver": "^7.6.0"
3738
},
3839
"devDependencies": {

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import Macros from './utils/macros';
33
import { UserOptions, NormalizedOptions, normalizeOptions } from './utils/normalize-options';
44
import * as Babel from '@babel/core';
55
import type { types as t } from '@babel/core';
6+
import { ImportUtil } from 'babel-import-util';
67

78
interface State {
89
opts: NormalizedOptions;
910
macroBuilder: Macros;
11+
util: ImportUtil;
1012
}
1113

1214
export default function macros(babel: typeof Babel): Babel.PluginObj<State> {
@@ -45,7 +47,12 @@ export default function macros(babel: typeof Babel): Babel.PluginObj<State> {
4547
let binding = path.scope.getBinding(localBindingName)!;
4648

4749
binding.referencePaths.forEach((p) => {
48-
p.replaceWith(buildIdentifier(flagValue, flagName));
50+
if (flagValue === '@embroider/macros') {
51+
p.replaceWith(t.callExpression(state.util.import(p, "@embroider/macros", "isDevelopingApp"), []))
52+
p.scope.crawl();
53+
} else {
54+
p.replaceWith(buildIdentifier(flagValue, flagName));
55+
}
4956
});
5057

5158
path.remove();
@@ -70,7 +77,8 @@ export default function macros(babel: typeof Babel): Babel.PluginObj<State> {
7077
// most of our plugin declares state.opts as already being normalized.
7178
// This is the spot where we force it become so.
7279
state.opts = normalizeOptions(state.opts as unknown as UserOptions);
73-
this.macroBuilder = new Macros(babel, state.opts);
80+
state.util = new ImportUtil(t, path)
81+
this.macroBuilder = new Macros(babel, state.opts, state.util);
7482

7583
let body = path.get('body');
7684

src/utils/builder.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type * as Babel from '@babel/core';
2-
import type { types as t } from '@babel/core';
3-
import type { NodePath } from '@babel/core';
2+
import type { NodePath, types as t } from '@babel/core';
43
import { CallIdentifierExpression, CallStatementPath } from './babel-type-helpers';
4+
import { ImportUtil } from 'babel-import-util';
55

66
export interface Options {
77
module: boolean | undefined;
88
global: string | undefined;
99
assertPredicateIndex: number | undefined;
10-
isDebug: boolean;
10+
isDebug: boolean | "@embroider/macros";
1111
}
1212

1313
interface MacroExpressionOpts {
@@ -27,18 +27,21 @@ export default class Builder {
2727
private module: boolean | undefined;
2828
private global: string | undefined;
2929
private assertPredicateIndex: number | undefined;
30-
private isDebug: boolean;
30+
private isDebug: boolean | '@embroider/macros';
31+
private util: ImportUtil;
3132

3233
private expressions: [CallStatementPath, (debugIdentifier: t.Expression) => t.Expression][] = [];
3334

3435
constructor(
3536
readonly t: typeof Babel.types,
37+
util: ImportUtil,
3638
options: Options
3739
) {
3840
this.module = options.module;
3941
this.global = options.global;
4042
this.assertPredicateIndex = options.assertPredicateIndex;
4143
this.isDebug = options.isDebug;
44+
this.util = util;
4245
}
4346

4447
/**
@@ -212,12 +215,24 @@ export default class Builder {
212215
*/
213216
expandMacros() {
214217
let t = this.t;
215-
let flag = t.booleanLiteral(this.isDebug);
216218
for (let i = 0; i < this.expressions.length; i++) {
217219
let expression = this.expressions[i];
218220
let exp = expression[0];
221+
let flag = this._debugExpression(exp);
219222
let logicalExp = expression[1];
220223
exp.replaceWith(t.parenthesizedExpression(logicalExp(flag)));
224+
exp.scope.crawl();
225+
}
226+
}
227+
228+
_debugExpression(target: NodePath) {
229+
if (typeof this.isDebug === 'boolean') {
230+
return this.t.booleanLiteral(this.isDebug);
231+
} else {
232+
return this.t.callExpression(
233+
this.util.import(target, '@embroider/macros', 'isDevelopingApp'),
234+
[]
235+
);
221236
}
222237
}
223238

0 commit comments

Comments
 (0)