@embroider/macros 1.10.0 → 1.10.1-unstable.1ecd57e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -1
- package/package.json +16 -5
- package/src/babel/get-config.js.map +1 -1
- package/src/babel/macros-babel-plugin.js +1 -5
- package/src/babel/macros-babel-plugin.js.map +1 -1
- package/src/babel/state.js.map +1 -1
- package/src/index.d.ts +39 -0
- package/src/index.js.map +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ A standardized solution for modifying your package's Javascript and Glimmer temp
|
|
|
6
6
|
|
|
7
7
|
Traditionally, Ember addons have a lot of power to run arbitrary code during the build process. This lets them do whatever they need to do, but it also makes them hard to statically analyze and makes them play badly with some tooling (like IDEs).
|
|
8
8
|
|
|
9
|
-
The [Embroider package spec](../../
|
|
9
|
+
The [Embroider package spec](../../docs/spec.md) proposes fixing this by making Ember addons much more static. But they will still need the ability to change themselves in certain ways at app compilation time. Hence this package.
|
|
10
10
|
|
|
11
11
|
This package works in both Embroider and Classical builds, so that addon authors can switch to this newer pattern without disruption.
|
|
12
12
|
|
|
@@ -212,6 +212,23 @@ if (macroCondition(isDevelopingApp()) {
|
|
|
212
212
|
|
|
213
213
|
Note that these can be used in combination - e.g. if you run tests in the production environment, `isTesting()` will be true, but `isDevelopingApp()` will be false.
|
|
214
214
|
|
|
215
|
+
## Glint usage
|
|
216
|
+
If you are using [Glint](https://typed-ember.gitbook.io/glint/) and `environment-ember-loose`, you can add all the macros to your app at once by adding
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import type { EmbroiderMacrosRegistry } from "@embroider/macros";
|
|
220
|
+
```
|
|
221
|
+
to your app's e.g. `types/glint.d.ts` file, and making sure your registry extends from EmbroiderMacrosRegistry:
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
declare module '@glint/environment-ember-loose/registry' {
|
|
225
|
+
export default interface Registry
|
|
226
|
+
extends EmbroiderMacrosRegistry {
|
|
227
|
+
// ...
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
215
232
|
## Real world examples
|
|
216
233
|
|
|
217
234
|
Below are a list of addons that have started using `@embroider/macros` so that you can get a feel for common use cases that can be solved via the macro system.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@embroider/macros",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.1-unstable.1ecd57e",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Standardized build-time macros for ember apps.",
|
|
6
6
|
"keywords": [
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"test": "jest"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@embroider/shared-internals": "2.0.
|
|
26
|
+
"@embroider/shared-internals": "2.0.1-unstable.1ecd57e",
|
|
27
27
|
"assert-never": "^1.2.1",
|
|
28
28
|
"babel-import-util": "^1.1.0",
|
|
29
29
|
"ember-cli-babel": "^7.26.6",
|
|
@@ -36,19 +36,30 @@
|
|
|
36
36
|
"@babel/core": "^7.14.5",
|
|
37
37
|
"@babel/plugin-transform-modules-amd": "^7.19.6",
|
|
38
38
|
"@babel/traverse": "^7.14.5",
|
|
39
|
-
"@embroider/core": "2.
|
|
39
|
+
"@embroider/core": "2.1.2-unstable.1ecd57e",
|
|
40
40
|
"@embroider/test-support": "0.36.0",
|
|
41
|
+
"@glint/template": "^1.0.0",
|
|
41
42
|
"@types/babel__core": "^7.1.14",
|
|
42
43
|
"@types/babel__generator": "^7.6.2",
|
|
43
44
|
"@types/babel__template": "^7.4.0",
|
|
45
|
+
"@types/babel__traverse": "^7.18.5",
|
|
46
|
+
"@types/lodash": "^4.14.170",
|
|
44
47
|
"@types/node": "^15.12.2",
|
|
45
48
|
"@types/resolve": "^1.20.0",
|
|
46
49
|
"@types/semver": "^7.3.6",
|
|
47
50
|
"babel-plugin-ember-template-compilation": "^2.0.0",
|
|
48
|
-
"code-equality-assertions": "^0.
|
|
49
|
-
"scenario-tester": "^2.
|
|
51
|
+
"code-equality-assertions": "^0.9.0",
|
|
52
|
+
"scenario-tester": "^2.1.2",
|
|
50
53
|
"typescript": "*"
|
|
51
54
|
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@glint/template": "^1.0.0"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"@glint/template": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
52
63
|
"engines": {
|
|
53
64
|
"node": "12.* || 14.* || >= 16"
|
|
54
65
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-config.js","sourceRoot":"","sources":["get-config.ts"],"names":[],"mappings":";;;;;;AAGA,oDAA4B;AAC5B,mDAAyF;AACzF,gEAAuC;AAMvC,SAAS,UAAU,CAAC,IAAgC,EAAE,KAAY,EAAE,IAAuB;IACzF,IAAI,WAA+B,CAAC;IACpC,IAAI,IAAI,KAAK,KAAK,EAAE;QAClB,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;YACpC,MAAM,IAAA,eAAK,EAAC,IAAI,EAAE,iDAAiD,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;SAClG;QACD,WAAW,GAAG,SAAS,CAAC;KACzB;SAAM,IAAI,IAAI,KAAK,SAAS,EAAE;QAC7B,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;YACpC,MAAM,IAAA,eAAK,EAAC,IAAI,EAAE,oDAAoD,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;SACrG;QACD,IAAI,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACzC,IAAI,WAAW,CAAC,IAAI,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AAW;IAC3C,IAAI,SAAS,GAAG,IAAI,yBAAS,CAAC,EAAE,UAAU,EAAE,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAErG,OAAO,IAAI,EAAE;QACX,IAAI,UAAU,GAAG,IAAI,CAAC,UAAW,CAAC;QAClC,IAAI,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;QAC5C,IAAI,CAAC,MAAM,CAAC,SAAS,IAAI,UAAU,CAAC,sBAAsB,EAAE,EAAE;YAC5D,OAAO,EAAE,IAAI,EAAE,MAAM,EAAG,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAqB,CAAC,KAAK,EAAE,CAAC;SAC9E;QACD,IAAI,GAAG,UAAU,CAAC;KACnB;AACH,CAAC;AAED,SAAgB,mBAAmB,CAAC,IAAqC,EAAE,KAAY,EAAE,OAAqB;IAC5G,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,GAAG;QAC3B,OAAO,CAAC,KAAK,CAAC,eAAe,CAC3B,IAAA,6BAAa,EAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,IAAI,CAAC,WAAW,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,OAAO,CAAC,CAC9F;KACF,CAAC;AACJ,CAAC;AAND,kDAMC","sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport State from './state';\nimport { PackageCache, Package } from '@embroider/shared-internals';\nimport error from './error';\nimport { Evaluator, assertArray, buildLiterals, ConfidentResult } from './evaluate-json';\nimport assertNever from 'assert-never';\nimport type * as Babel from '@babel/core';\nimport type { types as t } from '@babel/core';\n\nexport type Mode = 'own' | 'getGlobalConfig' | 'package';\n\nfunction getPackage(path: NodePath<t.CallExpression>, state: State, mode: 'own' | 'package'): { root: string } | null {\n let packageName: string | undefined;\n if (mode === 'own') {\n if (path.node.arguments.length !== 0) {\n throw error(path, `getOwnConfig takes zero arguments, you passed ${path.node.arguments.length}`);\n }\n packageName = undefined;\n } else if (mode === 'package') {\n if (path.node.arguments.length !== 1) {\n throw error(path, `getConfig takes exactly one argument, you passed ${path.node.arguments.length}`);\n }\n let packageNode = path.node.arguments[0];\n if (packageNode.type !== 'StringLiteral') {\n throw error(assertArray(path.get('arguments'))[0], `the argument to getConfig must be a string literal`);\n }\n packageName = packageNode.value;\n } else {\n assertNever(mode);\n }\n return targetPackage(state.sourceFile, packageName, state.packageCache);\n}\n\n// this evaluates to the actual value of the config. It can be used directly by the Evaluator.\nexport default function getConfig(path: NodePath<t.CallExpression>, state: State, mode: Mode) {\n let config: unknown | undefined;\n if (mode === 'getGlobalConfig') {\n return state.opts.globalConfig;\n }\n let pkg = getPackage(path, state, mode);\n if (pkg) {\n config = state.opts.userConfigs[pkg.root];\n }\n return config;\n}\n\n// this is the imperative version that's invoked directly by the babel visitor\n// when we encounter getConfig. It's implemented in terms of getConfig so we can\n// be sure we have the same semantics.\nexport function insertConfig(path: NodePath<t.CallExpression>, state: State, mode: Mode, context: typeof Babel) {\n if (state.opts.mode === 'compile-time') {\n let config = getConfig(path, state, mode);\n let collapsed = collapse(path, config);\n let literalResult = buildLiterals(collapsed.config, context);\n collapsed.path.replaceWith(literalResult);\n } else {\n if (mode === 'getGlobalConfig') {\n let callee = path.get('callee');\n callee.replaceWith(state.importUtil.import(callee, state.pathToOurAddon('runtime'), 'getGlobalConfig'));\n } else {\n let pkg = getPackage(path, state, mode);\n let pkgRoot;\n if (pkg) {\n pkgRoot = context.types.stringLiteral(pkg.root);\n } else {\n pkgRoot = context.types.identifier('undefined');\n }\n path.replaceWith(\n context.types.callExpression(state.importUtil.import(path, state.pathToOurAddon('runtime'), 'config'), [\n pkgRoot,\n ])\n );\n }\n }\n}\n\nfunction targetPackage(fromPath: string, packageName: string | undefined, packageCache: PackageCache): Package | null {\n let us = packageCache.ownerOfFile(fromPath);\n if (!us) {\n throw new Error(`unable to determine which npm package owns the file ${fromPath}`);\n }\n if (!packageName) {\n return us;\n }\n try {\n return packageCache.resolve(packageName, us);\n } catch (err) {\n return null;\n }\n}\n\nfunction collapse(path: NodePath, config: any) {\n let evaluator = new Evaluator({ knownPaths: new Map([[path, { confident: true, value: config }]]) });\n\n while (true) {\n let parentPath = path.parentPath!;\n let result = evaluator.evaluate(parentPath);\n if (!result.confident || parentPath.isAssignmentExpression()) {\n return { path, config: (evaluator.evaluate(path) as ConfidentResult).value };\n }\n path = parentPath;\n }\n}\n\nexport function inlineRuntimeConfig(path: NodePath<t.FunctionDeclaration>, state: State, context: typeof Babel) {\n path.get('body').node.body = [\n context.types.returnStatement(\n buildLiterals({ packages: state.opts.userConfigs, global: state.opts.globalConfig }, context)\n ),\n ];\n}\n"]}
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
9
5
|
}) : (function(o, m, k, k2) {
|
|
10
6
|
if (k2 === undefined) k2 = k;
|
|
11
7
|
o[k2] = m[k];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"macros-babel-plugin.js","sourceRoot":"","sources":["macros-babel-plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,kEAA2D;AAC3D,mCAA2C;AAC3C,6CAAwF;AACxF,qEAAyE;AACzE,iCAAgD;AAEhD,oDAA4B;AAC5B,8DAAqC;AACrC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qE;gBACrE,qCAAqC;gBACrC,OAAO;aACR;YAED,IACE,KAAK,CAAC,IAAI,CAAC,wBAAwB,KAAK,KAAK;gBAC7C,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS;gBAC5B,CAAC,KAAK,CAAC,iBAAiB,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;gBACvC,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC;gBACjC,KAAK,CAAC,aAAa,EAAE,CAAC,cAAc,EAAE,EACtC;gBACA,sEAAsE;gBACtE,uEAAuE;gBACvE,0DAA0D;gBAC1D,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;aACzE;QACH,CAAC;KACF,CAAC;IAEF,IAAK,OAAe,CAAC,KAAK,CAAC,wBAAwB,EAAE;QACnD,0EAA0E;QAC1E,4EAA4E;QAC5E,wCAAwC;QACvC,OAAe,CAAC,wBAAwB,GAAG;YAC1C,KAAK,CAAC,IAA0C,EAAE,KAAY;gBAC5D,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,cAAc,EAAE;oBACtC,IAAI,MAAM,GAAG,IAAI,yBAAS,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;oBACrD,IAAI,MAAM,CAAC,SAAS,EAAE;wBACpB,IAAI,CAAC,WAAW,CAAC,IAAA,6BAAa,EAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;qBACxD;iBACF;YACH,CAAC;SACF,CAAC;KACH;IAED,OAAO,EAAE,OAAO,EAAE,CAAC;AACrB,CAAC;AAhND,uBAgNC","sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport type { types as t } from '@babel/core';\nimport { PackageCache } from '@embroider/shared-internals';\nimport State, { initState } from './state';\nimport { inlineRuntimeConfig, insertConfig, Mode as GetConfigMode } from './get-config';\nimport macroCondition, { isMacroConditionPath } from './macro-condition';\nimport { isEachPath, insertEach } from './each';\n\nimport error from './error';\nimport failBuild from './fail-build';\nimport { Evaluator, buildLiterals } from './evaluate-json';\nimport type * as Babel from '@babel/core';\n\nexport default function main(context: typeof Babel): unknown {\n let t = context.types;\n let visitor = {\n Program: {\n enter(path: NodePath<t.Program>, state: State) {\n initState(t, path, state);\n\n state.packageCache = PackageCache.shared('embroider-stage3', state.opts.appPackageRoot);\n },\n exit(_: NodePath<t.Program>, state: State) {\n // @embroider/macros itself has no runtime behaviors and should always be removed\n state.importUtil.removeAllImports('@embroider/macros');\n for (let handler of state.jobs) {\n handler();\n }\n },\n },\n 'IfStatement|ConditionalExpression': {\n enter(path: NodePath<t.IfStatement | t.ConditionalExpression>, state: State) {\n if (isMacroConditionPath(path)) {\n state.calledIdentifiers.add(path.get('test').get('callee').node);\n macroCondition(path, state);\n }\n },\n },\n ForOfStatement: {\n enter(path: NodePath<t.ForOfStatement>, state: State) {\n if (isEachPath(path)) {\n state.calledIdentifiers.add(path.get('right').get('callee').node);\n insertEach(path, state, context);\n }\n },\n },\n FunctionDeclaration: {\n enter(path: NodePath<t.FunctionDeclaration>, state: State) {\n let id = path.get('id');\n if (id.isIdentifier() && id.node.name === 'initializeRuntimeMacrosConfig' && state.opts.mode === 'run-time') {\n let pkg = state.owningPackage();\n if (pkg && pkg.name === '@embroider/macros') {\n inlineRuntimeConfig(path, state, context);\n }\n }\n },\n },\n CallExpression: {\n enter(path: NodePath<t.CallExpression>, state: State) {\n let callee = path.get('callee');\n if (!callee.isIdentifier()) {\n return;\n }\n\n // failBuild is implemented for side-effect, not value, so it's not\n // handled by evaluateMacroCall.\n if (callee.referencesImport('@embroider/macros', 'failBuild')) {\n state.calledIdentifiers.add(callee.node);\n failBuild(path, state);\n return;\n }\n\n if (callee.referencesImport('@embroider/macros', 'importSync')) {\n // we handle importSync in the exit hook\n return;\n }\n\n // getOwnConfig/getGlobalConfig/getConfig needs special handling, so\n // even though it also emits values via evaluateMacroCall when they're\n // needed recursively by other macros, it has its own insertion-handling\n // code that we invoke here.\n //\n // The things that are special include:\n // - automatic collapsing of chained properties, etc\n // - these macros have runtime implementations sometimes, which changes\n // how we rewrite them\n let mode: GetConfigMode | false = callee.referencesImport('@embroider/macros', 'getOwnConfig')\n ? 'own'\n : callee.referencesImport('@embroider/macros', 'getGlobalConfig')\n ? 'getGlobalConfig'\n : callee.referencesImport('@embroider/macros', 'getConfig')\n ? 'package'\n : false;\n if (mode) {\n state.calledIdentifiers.add(callee.node);\n insertConfig(path, state, mode, context);\n return;\n }\n\n // isTesting can have a runtime implementation. At compile time it\n // instead falls through to evaluateMacroCall.\n if (callee.referencesImport('@embroider/macros', 'isTesting') && state.opts.mode === 'run-time') {\n state.calledIdentifiers.add(callee.node);\n callee.replaceWith(state.importUtil.import(callee, state.pathToOurAddon('runtime'), 'isTesting'));\n return;\n }\n\n let result = new Evaluator({ state }).evaluateMacroCall(path);\n if (result.confident) {\n state.calledIdentifiers.add(callee.node);\n path.replaceWith(buildLiterals(result.value, context));\n }\n },\n exit(path: NodePath<t.CallExpression>, state: State) {\n let callee = path.get('callee');\n if (!callee.isIdentifier()) {\n return;\n }\n // importSync doesn't evaluate to a static value, so it's implemented\n // directly here, not in evaluateMacroCall.\n // We intentionally do this on exit here, to allow other transforms to handle importSync before we do\n // For example ember-auto-import needs to do some custom transforms to enable use of dynamic template strings,\n // so its babel plugin needs to see and handle the importSync call first!\n if (callee.referencesImport('@embroider/macros', 'importSync')) {\n if (state.opts.importSyncImplementation === 'eager') {\n let specifier = path.node.arguments[0];\n if (specifier?.type !== 'StringLiteral') {\n throw new Error(`importSync eager mode doesn't implement non string literal arguments yet`);\n }\n path.replaceWith(state.importUtil.import(path, specifier.value, '*'));\n state.calledIdentifiers.add(callee.node);\n } else {\n if (path.scope.hasBinding('require')) {\n path.scope.rename('require');\n }\n let r = t.identifier('require');\n state.generatedRequires.add(r);\n path.replaceWith(\n t.callExpression(state.importUtil.import(path, state.pathToOurAddon('es-compat'), 'default', 'esc'), [\n t.callExpression(r, path.node.arguments),\n ])\n );\n }\n return;\n }\n },\n },\n ReferencedIdentifier(path: NodePath<t.Identifier>, state: State) {\n for (let candidate of [\n 'dependencySatisfies',\n 'moduleExists',\n 'getConfig',\n 'getOwnConfig',\n 'failBuild',\n // we cannot check importSync, as the babel transform runs on exit, so *after* this check\n // 'importSync',\n 'isDevelopingApp',\n 'isDevelopingThisPackage',\n 'isTesting',\n ]) {\n if (path.referencesImport('@embroider/macros', candidate) && !state.calledIdentifiers.has(path.node)) {\n throw error(path, `You can only use ${candidate} as a function call`);\n }\n }\n\n if (path.referencesImport('@embroider/macros', 'macroCondition') && !state.calledIdentifiers.has(path.node)) {\n throw error(path, `macroCondition can only be used as the predicate of an if statement or ternary expression`);\n }\n\n if (path.referencesImport('@embroider/macros', 'each') && !state.calledIdentifiers.has(path.node)) {\n throw error(\n path,\n `the each() macro can only be used within a for ... of statement, like: for (let x of each(thing)){}`\n );\n }\n\n if (state.opts.owningPackageRoot) {\n // there is only an owningPackageRoot when we are running inside a\n // classic ember-cli build. In the embroider stage3 build, there is no\n // owning package root because we're compiling *all* packages\n // simultaneously.\n //\n // given that we're inside classic ember-cli, stop here without trying\n // to rewrite bare `require`. It's not needed, because both our\n // `importSync` and any user-written bare `require` can both mean the\n // same thing: runtime AMD `require`.\n return;\n }\n\n if (\n state.opts.importSyncImplementation === 'cjs' &&\n path.node.name === 'require' &&\n !state.generatedRequires.has(path.node) &&\n !path.scope.hasBinding('require') &&\n state.owningPackage().isEmberPackage()\n ) {\n // Our importSync macro has been compiled to `require`. But we want to\n // distinguish that from any pre-existing, user-written `require` in an\n // Ember addon, which should retain its *runtime* meaning.\n path.replaceWith(t.memberExpression(t.identifier('window'), path.node));\n }\n },\n };\n\n if ((context as any).types.OptionalMemberExpression) {\n // our getConfig and getOwnConfig macros are supposed to be able to absorb\n // optional chaining. To make that work we need to see the optional chaining\n // before preset-env compiles them away.\n (visitor as any).OptionalMemberExpression = {\n enter(path: NodePath<t.OptionalMemberExpression>, state: State) {\n if (state.opts.mode === 'compile-time') {\n let result = new Evaluator({ state }).evaluate(path);\n if (result.confident) {\n path.replaceWith(buildLiterals(result.value, context));\n }\n }\n },\n };\n }\n\n return { visitor };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"macros-babel-plugin.js","sourceRoot":"","sources":["macros-babel-plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAEA,kEAA2D;AAC3D,mCAA2C;AAC3C,6CAAwF;AACxF,qEAAyE;AACzE,iCAAgD;AAEhD,oDAA4B;AAC5B,8DAAqC;AACrC,mDAA2D;AAG3D,SAAwB,IAAI,CAAC,OAAqB;IAChD,IAAI,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC;IACtB,IAAI,OAAO,GAAG;QACZ,OAAO,EAAE;YACP,KAAK,CAAC,IAAyB,EAAE,KAAY;gBAC3C,IAAA,iBAAS,EAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;gBAE1B,KAAK,CAAC,YAAY,GAAG,+BAAY,CAAC,MAAM,CAAC,kBAAkB,EAAE,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;YAC1F,CAAC;YACD,IAAI,CAAC,CAAsB,EAAE,KAAY;gBACvC,iFAAiF;gBACjF,KAAK,CAAC,UAAU,CAAC,gBAAgB,CAAC,mBAAmB,CAAC,CAAC;gBACvD,KAAK,IAAI,OAAO,IAAI,KAAK,CAAC,IAAI,EAAE;oBAC9B,OAAO,EAAE,CAAC;iBACX;YACH,CAAC;SACF;QACD,mCAAmC,EAAE;YACnC,KAAK,CAAC,IAAuD,EAAE,KAAY;gBACzE,IAAI,IAAA,sCAAoB,EAAC,IAAI,CAAC,EAAE;oBAC9B,KAAK,CAAC,iBAAiB,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC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IAAI,MAAM,GAAG,IAAI,yBAAS,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;oBACrD,IAAI,MAAM,CAAC,SAAS,EAAE;wBACpB,IAAI,CAAC,WAAW,CAAC,IAAA,6BAAa,EAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;qBACxD;iBACF;YACH,CAAC;SACF,CAAC;KACH;IAED,OAAO,EAAE,OAAO,EAAE,CAAC;AACrB,CAAC;AAhND,uBAgNC","sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport type { types as t } from '@babel/core';\nimport { PackageCache } from '@embroider/shared-internals';\nimport State, { initState } from './state';\nimport { inlineRuntimeConfig, insertConfig, Mode as GetConfigMode } from './get-config';\nimport macroCondition, { isMacroConditionPath } from './macro-condition';\nimport { isEachPath, insertEach } from './each';\n\nimport error from './error';\nimport failBuild from './fail-build';\nimport { Evaluator, buildLiterals } from './evaluate-json';\nimport type * as Babel from '@babel/core';\n\nexport default function main(context: typeof Babel): unknown {\n let t = context.types;\n let visitor = {\n Program: {\n enter(path: NodePath<t.Program>, state: State) {\n initState(t, path, state);\n\n state.packageCache = PackageCache.shared('embroider-stage3', state.opts.appPackageRoot);\n },\n exit(_: NodePath<t.Program>, state: State) {\n // @embroider/macros itself has no runtime behaviors and should always be removed\n state.importUtil.removeAllImports('@embroider/macros');\n for (let handler of state.jobs) {\n handler();\n }\n },\n },\n 'IfStatement|ConditionalExpression': {\n enter(path: NodePath<t.IfStatement | t.ConditionalExpression>, state: State) {\n if (isMacroConditionPath(path)) {\n state.calledIdentifiers.add(path.get('test').get('callee').node);\n macroCondition(path, state);\n }\n },\n },\n ForOfStatement: {\n enter(path: NodePath<t.ForOfStatement>, state: State) {\n if (isEachPath(path)) {\n state.calledIdentifiers.add(path.get('right').get('callee').node);\n insertEach(path, state, context);\n }\n },\n },\n FunctionDeclaration: {\n enter(path: NodePath<t.FunctionDeclaration>, state: State) {\n let id = path.get('id');\n if (id.isIdentifier() && id.node.name === 'initializeRuntimeMacrosConfig' && state.opts.mode === 'run-time') {\n let pkg = state.owningPackage();\n if (pkg && pkg.name === '@embroider/macros') {\n inlineRuntimeConfig(path, state, context);\n }\n }\n },\n },\n CallExpression: {\n enter(path: NodePath<t.CallExpression>, state: State) {\n let callee = path.get('callee');\n if (!callee.isIdentifier()) {\n return;\n }\n\n // failBuild is implemented for side-effect, not value, so it's not\n // handled by evaluateMacroCall.\n if (callee.referencesImport('@embroider/macros', 'failBuild')) {\n state.calledIdentifiers.add(callee.node);\n failBuild(path, state);\n return;\n }\n\n if (callee.referencesImport('@embroider/macros', 'importSync')) {\n // we handle importSync in the exit hook\n return;\n }\n\n // getOwnConfig/getGlobalConfig/getConfig needs special handling, so\n // even though it also emits values via evaluateMacroCall when they're\n // needed recursively by other macros, it has its own insertion-handling\n // code that we invoke here.\n //\n // The things that are special include:\n // - automatic collapsing of chained properties, etc\n // - these macros have runtime implementations sometimes, which changes\n // how we rewrite them\n let mode: GetConfigMode | false = callee.referencesImport('@embroider/macros', 'getOwnConfig')\n ? 'own'\n : callee.referencesImport('@embroider/macros', 'getGlobalConfig')\n ? 'getGlobalConfig'\n : callee.referencesImport('@embroider/macros', 'getConfig')\n ? 'package'\n : false;\n if (mode) {\n state.calledIdentifiers.add(callee.node);\n insertConfig(path, state, mode, context);\n return;\n }\n\n // isTesting can have a runtime implementation. At compile time it\n // instead falls through to evaluateMacroCall.\n if (callee.referencesImport('@embroider/macros', 'isTesting') && state.opts.mode === 'run-time') {\n state.calledIdentifiers.add(callee.node);\n callee.replaceWith(state.importUtil.import(callee, state.pathToOurAddon('runtime'), 'isTesting'));\n return;\n }\n\n let result = new Evaluator({ state }).evaluateMacroCall(path);\n if (result.confident) {\n state.calledIdentifiers.add(callee.node);\n path.replaceWith(buildLiterals(result.value, context));\n }\n },\n exit(path: NodePath<t.CallExpression>, state: State) {\n let callee = path.get('callee');\n if (!callee.isIdentifier()) {\n return;\n }\n // importSync doesn't evaluate to a static value, so it's implemented\n // directly here, not in evaluateMacroCall.\n // We intentionally do this on exit here, to allow other transforms to handle importSync before we do\n // For example ember-auto-import needs to do some custom transforms to enable use of dynamic template strings,\n // so its babel plugin needs to see and handle the importSync call first!\n if (callee.referencesImport('@embroider/macros', 'importSync')) {\n if (state.opts.importSyncImplementation === 'eager') {\n let specifier = path.node.arguments[0];\n if (specifier?.type !== 'StringLiteral') {\n throw new Error(`importSync eager mode doesn't implement non string literal arguments yet`);\n }\n path.replaceWith(state.importUtil.import(path, specifier.value, '*'));\n state.calledIdentifiers.add(callee.node);\n } else {\n if (path.scope.hasBinding('require')) {\n path.scope.rename('require');\n }\n let r = t.identifier('require');\n state.generatedRequires.add(r);\n path.replaceWith(\n t.callExpression(state.importUtil.import(path, state.pathToOurAddon('es-compat'), 'default', 'esc'), [\n t.callExpression(r, path.node.arguments),\n ])\n );\n }\n return;\n }\n },\n },\n ReferencedIdentifier(path: NodePath<t.Identifier>, state: State) {\n for (let candidate of [\n 'dependencySatisfies',\n 'moduleExists',\n 'getConfig',\n 'getOwnConfig',\n 'failBuild',\n // we cannot check importSync, as the babel transform runs on exit, so *after* this check\n // 'importSync',\n 'isDevelopingApp',\n 'isDevelopingThisPackage',\n 'isTesting',\n ]) {\n if (path.referencesImport('@embroider/macros', candidate) && !state.calledIdentifiers.has(path.node)) {\n throw error(path, `You can only use ${candidate} as a function call`);\n }\n }\n\n if (path.referencesImport('@embroider/macros', 'macroCondition') && !state.calledIdentifiers.has(path.node)) {\n throw error(path, `macroCondition can only be used as the predicate of an if statement or ternary expression`);\n }\n\n if (path.referencesImport('@embroider/macros', 'each') && !state.calledIdentifiers.has(path.node)) {\n throw error(\n path,\n `the each() macro can only be used within a for ... of statement, like: for (let x of each(thing)){}`\n );\n }\n\n if (state.opts.owningPackageRoot) {\n // there is only an owningPackageRoot when we are running inside a\n // classic ember-cli build. In the embroider stage3 build, there is no\n // owning package root because we're compiling *all* packages\n // simultaneously.\n //\n // given that we're inside classic ember-cli, stop here without trying\n // to rewrite bare `require`. It's not needed, because both our\n // `importSync` and any user-written bare `require` can both mean the\n // same thing: runtime AMD `require`.\n return;\n }\n\n if (\n state.opts.importSyncImplementation === 'cjs' &&\n path.node.name === 'require' &&\n !state.generatedRequires.has(path.node) &&\n !path.scope.hasBinding('require') &&\n state.owningPackage().isEmberPackage()\n ) {\n // Our importSync macro has been compiled to `require`. But we want to\n // distinguish that from any pre-existing, user-written `require` in an\n // Ember addon, which should retain its *runtime* meaning.\n path.replaceWith(t.memberExpression(t.identifier('window'), path.node));\n }\n },\n };\n\n if ((context as any).types.OptionalMemberExpression) {\n // our getConfig and getOwnConfig macros are supposed to be able to absorb\n // optional chaining. To make that work we need to see the optional chaining\n // before preset-env compiles them away.\n (visitor as any).OptionalMemberExpression = {\n enter(path: NodePath<t.OptionalMemberExpression>, state: State) {\n if (state.opts.mode === 'compile-time') {\n let result = new Evaluator({ state }).evaluate(path);\n if (result.confident) {\n path.replaceWith(buildLiterals(result.value, context));\n }\n }\n },\n };\n }\n\n return { visitor };\n}\n"]}
|
package/src/babel/state.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.js","sourceRoot":"","sources":["state.ts"],"names":[],"mappings":";;;;;;AACA,yEAAiD;AACjD,iEAA+C;AAC/C,+BAA8C;AAC9C,kEAAsF;AACtF,yDAA+C;AA8C/C,SAAgB,SAAS,CAAC,CAAqB,EAAE,IAAmC,EAAE,KAAY;IAChG,KAAK,CAAC,UAAU,GAAG,IAAI,8BAAU,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IAC3C,KAAK,CAAC,iBAAiB,GAAG,IAAI,GAAG,EAAE,CAAC;IACpC,KAAK,CAAC,IAAI,GAAG,EAAE,CAAC;IAChB,KAAK,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IAC1B,KAAK,CAAC,iBAAiB,GAAG,IAAI,GAAG,EAAE,CAAC;IACpC,KAAK,CAAC,Y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BAAiB,EAAE;QAChC,kEAAkE;QAClE,OAAO,IAAA,mCAAgB,EAAC,IAAA,cAAO,EAAC,IAAI,CAAC,UAAU,CAAC,EAAE,IAAA,WAAI,EAAC,gBAAgB,EAAE,UAAU,CAAC,CAAC,CAAC;KACvF;SAAM;QACL,sEAAsE;QACtE,aAAa;QACb,EAAE;QACF,qEAAqE;QACrE,yEAAyE;QACzE,4EAA4E;QAC5E,0EAA0E;QAC1E,eAAe;QACf,OAAO,qBAAqB,UAAU,EAAE,CAAC;KAC1C;AACH,CAAC;AAED,SAAS,aAAa;IACpB,IAAI,GAAG,GAAG,IAAI,CAAC,YAAY,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACzD,IAAI,CAAC,GAAG,EAAE;QACR,MAAM,IAAI,KAAK,CAAC,uDAAuD,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC;KAC3F;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,SAAS,SAAS,CAAc,IAAU;IACxC,IAAI,KAAK,GAAG,IAAI,CAAC;IACjB,OAAO,IAAA,uBAAa,EAAC,IAAI,EAAE,UAAU,KAAU;QAC7C,IAAI,KAAK,CAAC,iBAAiB,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;YACtC,IAAI,MAAM,GAAG,IAAA,mBAAe,EAAC,KAAK,CAAC,CAAC;YACpC,KAAK,CAAC,iBAAiB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YACpC,OAAO,MAAM,CAAC;SACf;IACH,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { NodePath, Node } from '@babel/traverse';\nimport cloneDeepWith from 'lodash/cloneDeepWith';\nimport lodashCloneDeep from 'lodash/cloneDeep';\nimport { join, dirname, resolve } from 'path';\nimport { explicitRelative, Package, PackageCache } from '@embroider/shared-internals';\nimport { ImportUtil } from 'babel-import-util';\nimport type * as Babel from '@babel/core';\n\nexport default interface State {\n importUtil: ImportUtil;\n generatedRequires: Set<Node>;\n removed: Set<Node>;\n calledIdentifiers: Set<Node>;\n jobs: (() => void)[];\n packageCache: PackageCache;\n sourceFile: string;\n pathToOurAddon(moduleName: string): string;\n owningPackage(): Package;\n cloneDeep(node: Node): Node;\n\n opts: {\n userConfigs: {\n [pkgRoot: string]: unknown;\n };\n globalConfig: {\n [key: string]: unknown;\n };\n // we set this when we're running inside classic ember-cli, because in that\n // case we don't have finer-grained info available about where the files\n // we're processing are globally located. When running in embroider, we\n // don't set this, because each file is visible at its full\n // globally-relevant path.\n owningPackageRoot: string | undefined;\n\n // list of packages that are under active development, represented by the\n // path to their package root directory\n isDevelopingPackageRoots: string[];\n\n // the package root directory of the app. Needed so that we can get\n // consistent answers to `isDevelopingApp` and `isDeveopingThisPackage`, as\n // well as consistent handling of Package devDependencies vs dependencies.\n appPackageRoot: string;\n\n embroiderMacrosConfigMarker: true;\n\n mode: 'compile-time' | 'run-time';\n\n importSyncImplementation: 'cjs' | 'eager';\n };\n}\n\nexport function initState(t: typeof Babel.types, path: NodePath<Babel.types.Program>, state: State) {\n state.importUtil = new ImportUtil(t, path);\n state.generatedRequires = new Set();\n state.jobs = [];\n state.removed = new Set();\n state.calledIdentifiers = new Set();\n state.packageCache = PackageCache.shared('embroider-stage3', state.opts.appPackageRoot);\n state.sourceFile = state.opts.owningPackageRoot || (path.hub as any).file.opts.filename;\n state.pathToOurAddon = pathToAddon;\n state.owningPackage = owningPackage;\n state.cloneDeep = cloneDeep;\n}\n\nconst runtimeAddonPath = resolve(join(__dirname, '..', 'addon'));\n\nfunction pathToAddon(this: State, moduleName: string): string {\n if (!this.opts.owningPackageRoot) {\n // running inside embroider, so make a relative path to the module\n return explicitRelative(dirname(this.sourceFile), join(runtimeAddonPath, moduleName));\n } else {\n // running inside a classic build, so use a classic-compatible runtime\n // specifier.\n //\n // CAUTION: the module we're pointing at here gets merged between all\n // present versions of @embroider/macros, and one will win. So if you are\n // introducing incompatible changes to its API, you need to change this name\n // (by tacking on a version number, etc) and rename the corresponding file\n // in ../addon.\n return `@embroider/macros/${moduleName}`;\n }\n}\n\nfunction owningPackage(this: State): Package {\n let pkg = this.packageCache.ownerOfFile(this.sourceFile);\n if (!pkg) {\n throw new Error(`unable to determine which npm package owns the file ${this.sourceFile}`);\n }\n return pkg;\n}\n\nfunction cloneDeep(this: State, node: Node): Node {\n let state = this;\n return cloneDeepWith(node, function (value: any) {\n if (state.generatedRequires.has(value)) {\n let cloned = lodashCloneDeep(value);\n state.generatedRequires.add(cloned);\n return cloned;\n }\n });\n}\n"]}
|
package/src/index.d.ts
CHANGED
|
@@ -9,3 +9,42 @@ export declare function isDevelopingApp(): boolean;
|
|
|
9
9
|
export declare function isTesting(): boolean;
|
|
10
10
|
export declare function failBuild(message: string): void;
|
|
11
11
|
export declare function moduleExists(packageName: string): boolean;
|
|
12
|
+
import type { HelperLike } from '@glint/template';
|
|
13
|
+
export interface EmbroiderMacrosRegistry {
|
|
14
|
+
macroGetOwnConfig: HelperLike<{
|
|
15
|
+
Args: {
|
|
16
|
+
Positional: [...keys: string[]];
|
|
17
|
+
};
|
|
18
|
+
Return: ReturnType<typeof getOwnConfig>;
|
|
19
|
+
}>;
|
|
20
|
+
macroGetConfig: HelperLike<{
|
|
21
|
+
Args: {
|
|
22
|
+
Positional: [packageName: string, ...keys: string[]];
|
|
23
|
+
};
|
|
24
|
+
Return: ReturnType<typeof getConfig>;
|
|
25
|
+
}>;
|
|
26
|
+
macroCondition: HelperLike<{
|
|
27
|
+
Args: {
|
|
28
|
+
Positional: [predicate: boolean];
|
|
29
|
+
};
|
|
30
|
+
Return: boolean;
|
|
31
|
+
}>;
|
|
32
|
+
macroDependencySatisfies: HelperLike<{
|
|
33
|
+
Args: {
|
|
34
|
+
Positional: Parameters<typeof dependencySatisfies>;
|
|
35
|
+
};
|
|
36
|
+
Return: ReturnType<typeof dependencySatisfies>;
|
|
37
|
+
}>;
|
|
38
|
+
macroMaybeAttrs: HelperLike<{
|
|
39
|
+
Args: {
|
|
40
|
+
Positional: [predicate: boolean, ...bareAttrs: unknown[]];
|
|
41
|
+
};
|
|
42
|
+
Return: void;
|
|
43
|
+
}>;
|
|
44
|
+
macroFailBuild: HelperLike<{
|
|
45
|
+
Args: {
|
|
46
|
+
Positional: Parameters<typeof failBuild>;
|
|
47
|
+
};
|
|
48
|
+
Return: ReturnType<typeof failBuild>;
|
|
49
|
+
}>;
|
|
50
|
+
}
|
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA,2BAA2B;;;AAE3B;;;;;;;;;;;;;;EAcE;AAEF,SAAgB,mBAAmB,CAAC,WAAmB,EAAE,WAAmB;IAC1E,MAAM,IAAI,IAAI,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AAC3C,CAAC;AAFD,kDAEC;AAED,SAAgB,cAAc,CAAC,SAAkB;IAC/C,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC;AAC5B,CAAC;AAFD,wCAEC;AAED,SAAgB,IAAI,CAAI,KAAU;IAChC,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;AACxB,CAAC;AAFD,oBAEC;AAED,4BAA4B;AAC5B,mFAAmF;AACnF,kDAAkD;AAClD,SAAgB,UAAU,CAAC,SAAiB;IAC1C,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC;AAC5B,CAAC;AAFD,gCAEC;AAED,SAAgB,SAAS,CAAI,WAAmB;IAC9C,MAAM,IAAI,IAAI,CAAC,WAAW,CAAC,CAAC;AAC9B,CAAC;AAFD,8BAEC;AAED,SAAgB,YAAY;IAC1B,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAFD,oCAEC;AAED,SAAgB,eAAe;IAC7B,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAFD,0CAEC;AAED,SAAgB,eAAe;IAC7B,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAFD,0CAEC;AAED,SAAgB,SAAS;IACvB,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAFD,8BAEC;AAED,SAAgB,SAAS,CAAC,OAAe;IACvC,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC;AAC1B,CAAC;AAFD,8BAEC;AAED,SAAgB,YAAY,CAAC,WAAmB;IAC9C,MAAM,IAAI,IAAI,CAAC,WAAW,CAAC,CAAC;AAC9B,CAAC;AAFD,oCAEC;AAED,MAAM,IAAK,SAAQ,KAAK;IAEtB,YAAY,GAAG,MAAa;QAC1B,KAAK,CACH,6HAA6H,CAC9H,CAAC;QACF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF","sourcesContent":["/* Macro Type Signatures */\n\n/*\n CAUTION: this code is not necessarily what you are actually running. In\n general, the macros are implemented at build time using babel, and so calls to\n these functions get compiled away before they ever run. However, this code is\n here because it provides types to typescript users of the macros.\n\n Some macros also have runtime implementations that are useful in development\n mode, in addition to their build-time implementations in babel. You can find\n the runtime implementations in runtime.ts.\n\n Having a runtime mode lets us do things like produce a single build in\n development that works for both fastboot and browser, using the macros to\n switch between modes. For production, you would switch to the build-time macro\n implementation to get two optimized builds instead.\n*/\n\nexport function dependencySatisfies(packageName: string, semverRange: string): boolean {\n throw new Oops(packageName, semverRange);\n}\n\nexport function macroCondition(predicate: boolean): boolean {\n throw new Oops(predicate);\n}\n\nexport function each<T>(array: T[]): T[] {\n throw new Oops(array);\n}\n\n// We would prefer to write:\n// export function importSync<T extends string>(specifier: T): typeof import(T) {\n// but TS doesn't seem to support that at present.\nexport function importSync(specifier: string): unknown {\n throw new Oops(specifier);\n}\n\nexport function getConfig<T>(packageName: string): T {\n throw new Oops(packageName);\n}\n\nexport function getOwnConfig<T>(): T {\n throw new Oops();\n}\n\nexport function getGlobalConfig<T>(): T {\n throw new Oops();\n}\n\nexport function isDevelopingApp(): boolean {\n throw new Oops();\n}\n\nexport function isTesting(): boolean {\n throw new Oops();\n}\n\nexport function failBuild(message: string): void {\n throw new Oops(message);\n}\n\nexport function moduleExists(packageName: string): boolean {\n throw new Oops(packageName);\n}\n\nclass Oops extends Error {\n params: any[];\n constructor(...params: any[]) {\n super(\n `this method is really implemented at compile time via a babel plugin. If you're seeing this exception, something went wrong`\n );\n this.params = params;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA,2BAA2B;;;AAE3B;;;;;;;;;;;;;;EAcE;AAEF,SAAgB,mBAAmB,CAAC,WAAmB,EAAE,WAAmB;IAC1E,MAAM,IAAI,IAAI,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AAC3C,CAAC;AAFD,kDAEC;AAED,SAAgB,cAAc,CAAC,SAAkB;IAC/C,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC;AAC5B,CAAC;AAFD,wCAEC;AAED,SAAgB,IAAI,CAAI,KAAU;IAChC,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;AACxB,CAAC;AAFD,oBAEC;AAED,4BAA4B;AAC5B,mFAAmF;AACnF,kDAAkD;AAClD,SAAgB,UAAU,CAAC,SAAiB;IAC1C,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC;AAC5B,CAAC;AAFD,gCAEC;AAED,SAAgB,SAAS,CAAI,WAAmB;IAC9C,MAAM,IAAI,IAAI,CAAC,WAAW,CAAC,CAAC;AAC9B,CAAC;AAFD,8BAEC;AAED,SAAgB,YAAY;IAC1B,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAFD,oCAEC;AAED,SAAgB,eAAe;IAC7B,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAFD,0CAEC;AAED,SAAgB,eAAe;IAC7B,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAFD,0CAEC;AAED,SAAgB,SAAS;IACvB,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAFD,8BAEC;AAED,SAAgB,SAAS,CAAC,OAAe;IACvC,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC;AAC1B,CAAC;AAFD,8BAEC;AAED,SAAgB,YAAY,CAAC,WAAmB;IAC9C,MAAM,IAAI,IAAI,CAAC,WAAW,CAAC,CAAC;AAC9B,CAAC;AAFD,oCAEC;AAED,MAAM,IAAK,SAAQ,KAAK;IAEtB,YAAY,GAAG,MAAa;QAC1B,KAAK,CACH,6HAA6H,CAC9H,CAAC;QACF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF","sourcesContent":["/* Macro Type Signatures */\n\n/*\n CAUTION: this code is not necessarily what you are actually running. In\n general, the macros are implemented at build time using babel, and so calls to\n these functions get compiled away before they ever run. However, this code is\n here because it provides types to typescript users of the macros.\n\n Some macros also have runtime implementations that are useful in development\n mode, in addition to their build-time implementations in babel. You can find\n the runtime implementations in runtime.ts.\n\n Having a runtime mode lets us do things like produce a single build in\n development that works for both fastboot and browser, using the macros to\n switch between modes. For production, you would switch to the build-time macro\n implementation to get two optimized builds instead.\n*/\n\nexport function dependencySatisfies(packageName: string, semverRange: string): boolean {\n throw new Oops(packageName, semverRange);\n}\n\nexport function macroCondition(predicate: boolean): boolean {\n throw new Oops(predicate);\n}\n\nexport function each<T>(array: T[]): T[] {\n throw new Oops(array);\n}\n\n// We would prefer to write:\n// export function importSync<T extends string>(specifier: T): typeof import(T) {\n// but TS doesn't seem to support that at present.\nexport function importSync(specifier: string): unknown {\n throw new Oops(specifier);\n}\n\nexport function getConfig<T>(packageName: string): T {\n throw new Oops(packageName);\n}\n\nexport function getOwnConfig<T>(): T {\n throw new Oops();\n}\n\nexport function getGlobalConfig<T>(): T {\n throw new Oops();\n}\n\nexport function isDevelopingApp(): boolean {\n throw new Oops();\n}\n\nexport function isTesting(): boolean {\n throw new Oops();\n}\n\nexport function failBuild(message: string): void {\n throw new Oops(message);\n}\n\nexport function moduleExists(packageName: string): boolean {\n throw new Oops(packageName);\n}\n\nclass Oops extends Error {\n params: any[];\n constructor(...params: any[]) {\n super(\n `this method is really implemented at compile time via a babel plugin. If you're seeing this exception, something went wrong`\n );\n this.params = params;\n }\n}\n\nimport type { HelperLike } from '@glint/template';\n\nexport interface EmbroiderMacrosRegistry {\n macroGetOwnConfig: HelperLike<{\n Args: { Positional: [...keys: string[]] };\n Return: ReturnType<typeof getOwnConfig>;\n }>;\n macroGetConfig: HelperLike<{\n Args: { Positional: [packageName: string, ...keys: string[]] };\n Return: ReturnType<typeof getConfig>;\n }>;\n macroCondition: HelperLike<{\n Args: { Positional: [predicate: boolean] };\n Return: boolean;\n }>;\n macroDependencySatisfies: HelperLike<{\n Args: { Positional: Parameters<typeof dependencySatisfies> };\n Return: ReturnType<typeof dependencySatisfies>;\n }>;\n macroMaybeAttrs: HelperLike<{\n Args: { Positional: [predicate: boolean, ...bareAttrs: unknown[]] };\n Return: void;\n }>;\n macroFailBuild: HelperLike<{\n Args: { Positional: Parameters<typeof failBuild> };\n Return: ReturnType<typeof failBuild>;\n }>;\n}\n"]}
|