@embroider/macros 1.17.0-alpha.4 → 1.17.0-alpha.6

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 CHANGED
@@ -10,6 +10,45 @@ The [Embroider package spec](../../docs/spec.md) proposes fixing this by making
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
 
13
+ ## Setting Configuration: from a babel config
14
+
15
+ 1. Add `@embroider/macros` as `devDependency`.
16
+ 2. In your babel config, do:
17
+
18
+ ```js
19
+ const { buildMacros } = require('@embroider/macros/babel');
20
+
21
+ const macros = buildMacros({
22
+ // this is how you configure your own package
23
+ setOwnConfig: {
24
+ // your config goes here
25
+ },
26
+ // this is how you can optionally send configuration into your
27
+ // dependencies, if those dependencies choose to use
28
+ // @embroider/macros configs.
29
+ setConfig: {
30
+ 'some-dependency': {
31
+ // config for some-dependency
32
+ },
33
+ },
34
+ });
35
+
36
+ module.exports = {
37
+ plugins: [
38
+ // ...
39
+ [
40
+ "babel-plugin-ember-template-compilation",
41
+ {
42
+ compilerPath: "ember-source/dist/ember-template-compiler.js",
43
+ transforms: [...macros.templateMacros],
44
+ },
45
+ ],
46
+ ...macros.babelMacros,
47
+ ],
48
+ // ...
49
+ };
50
+ ```
51
+
13
52
  ## Setting Configuration: from an Ember app
14
53
 
15
54
  1. Add `@embroider/macros` as `devDependency`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@embroider/macros",
3
- "version": "1.17.0-alpha.4",
3
+ "version": "1.17.0-alpha.6",
4
4
  "private": false,
5
5
  "description": "Standardized build-time macros for ember apps.",
6
6
  "keywords": [
@@ -14,6 +14,11 @@
14
14
  "license": "MIT",
15
15
  "author": "Edward Faulkner",
16
16
  "main": "src/index.js",
17
+ "exports": {
18
+ ".": "./src/index.js",
19
+ "./babel": "./src/babel.js",
20
+ "./src/*": "./src/*.js"
21
+ },
17
22
  "files": [
18
23
  "src/**/*.js",
19
24
  "src/**/*.d.ts",
@@ -29,13 +34,13 @@
29
34
  },
30
35
  "dependencies": {
31
36
  "assert-never": "^1.2.1",
32
- "babel-import-util": "^2.0.0",
37
+ "babel-import-util": "^3.0.1",
33
38
  "ember-cli-babel": "^7.26.6",
34
39
  "find-up": "^5.0.0",
35
40
  "lodash": "^4.17.21",
36
41
  "resolve": "^1.20.0",
37
42
  "semver": "^7.3.2",
38
- "@embroider/shared-internals": "3.0.0-alpha.3"
43
+ "@embroider/shared-internals": "3.0.0-alpha.4"
39
44
  },
40
45
  "devDependencies": {
41
46
  "@babel/core": "^7.14.5",
@@ -54,7 +59,7 @@
54
59
  "code-equality-assertions": "^1.0.1",
55
60
  "scenario-tester": "^3.0.1",
56
61
  "typescript": "^5.4.5",
57
- "@embroider/core": "4.0.0-alpha.5",
62
+ "@embroider/core": "4.0.0-alpha.7",
58
63
  "@embroider/test-support": "0.36.0"
59
64
  },
60
65
  "peerDependencies": {
@@ -51,7 +51,7 @@ function main(context) {
51
51
  let visitor = {
52
52
  Program: {
53
53
  enter(path, state) {
54
- (0, state_1.initState)(t, path, state);
54
+ (0, state_1.initState)(context, path, state);
55
55
  },
56
56
  exit(_, state) {
57
57
  // @embroider/macros itself has no runtime behaviors and should always be removed
@@ -1 +1 @@
1
- {"version":3,"file":"macros-babel-plugin.js","sourceRoot":"","sources":["macros-babel-plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA,uBAiPC;AA9PD,mCAAoC;AAEpC,6CAAiE;AACjE,qEAAyE;AACzE,iCAAgD;AAEhD,oDAA4B;AAC5B,8DAAqC;AACrC,mDAA2D;AAE3D,2BAA6C;AAC7C,+BAA8C;AAE9C,SAAwB,IAAI,CAAC,OAAqB;IAChD,IAAI,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC;IACtB,IAAI,OAAO,GAAG;QACZ,OAAO,EAAE;YACP,KAAK,CAAC,IAAyB,EAAE,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,EAAE,KAAY;gBAC5D,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,cAAc,EAAE,CAAC;oBACvC,IAAI,MAAM,GAAG,IAAI,yBAAS,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;oBACrD,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;wBACrB,IAAI,CAAC,WAAW,CAAC,IAAA,6BAAa,EAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;oBACzD,CAAC;gBACH,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,CAAC;AACrB,CAAC","sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport type { types as t } from '@babel/core';\nimport type State from './state';\nimport { initState } from './state';\nimport type { Mode as GetConfigMode } from './get-config';\nimport { inlineRuntimeConfig, insertConfig } 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';\nimport { existsSync, readdirSync } from 'fs';\nimport { resolve, dirname, join } from 'path';\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 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 let specifier = path.node.arguments[0];\n if (specifier?.type !== 'StringLiteral') {\n let relativePath = '';\n let property;\n if (specifier.type === 'TemplateLiteral') {\n relativePath = specifier.quasis[0].value.cooked!;\n property = specifier.expressions[0] as t.Expression;\n }\n // babel might transform template form `../my-path/${id}` to '../my-path/'.concat(id)\n if (\n specifier.type === 'CallExpression' &&\n specifier.callee.type === 'MemberExpression' &&\n specifier.callee.property.type === 'Identifier' &&\n specifier.callee.property.name === 'concat' &&\n specifier.callee.object.type === 'StringLiteral'\n ) {\n relativePath = specifier.callee.object.value;\n property = specifier.arguments[0] as t.Expression;\n }\n if (property && relativePath && relativePath.startsWith('.')) {\n const resolvedPath = resolve(dirname((state as any).filename), relativePath);\n let entries: string[] = [];\n if (existsSync(resolvedPath)) {\n entries = readdirSync(resolvedPath).filter(e => !e.startsWith('.'));\n }\n const obj = t.objectExpression(\n entries.map(e => {\n let key = e.split('.')[0];\n const rest = e.split('.').slice(1, -1);\n if (rest.length) {\n key += `.${rest}`;\n }\n const id = t.callExpression(\n state.importUtil.import(path, state.pathToOurAddon('es-compat2'), 'default', 'esc'),\n [state.importUtil.import(path, join(relativePath, key).replace(/\\\\/g, '/'), '*')]\n );\n return t.objectProperty(t.stringLiteral(key), id);\n })\n );\n const memberExpr = t.memberExpression(obj, property, true);\n path.replaceWith(memberExpr);\n state.calledIdentifiers.add(callee.node);\n return;\n } else {\n throw new Error(\n `importSync eager mode only supports dynamic paths which are relative, must start with a '.', had ${specifier.type}`\n );\n }\n }\n path.replaceWith(\n t.callExpression(state.importUtil.import(path, state.pathToOurAddon('es-compat2'), 'default', 'esc'), [\n state.importUtil.import(path, specifier.value, '*'),\n ])\n );\n state.calledIdentifiers.add(callee.node);\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.hideRequires &&\n path.node.name === 'require' &&\n !path.scope.hasBinding('require') &&\n state.owningPackage().isEmberAddon()\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA,uBAiPC;AA9PD,mCAAoC;AAEpC,6CAAiE;AACjE,qEAAyE;AACzE,iCAAgD;AAEhD,oDAA4B;AAC5B,8DAAqC;AACrC,mDAA2D;AAE3D,2BAA6C;AAC7C,+BAA8C;AAE9C,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,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;YAClC,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,CAAC;oBAC/B,OAAO,EAAE,CAAC;gBACZ,CAAC;YACH,CAAC;SACF;QACD,mCAAmC,EAAE;YACnC,KAAK,CAAC,IAAuD,EAAE,KAAY;gBACzE,IAAI,IAAA,sCAAoB,EAAC,IAAI,CAAC,EAAE,CAAC;oBAC/B,KAAK,CAAC,iBAAiB,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC;oBACjE,IAAA,yBAAc,EAAC,IAAI,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CAAqC;gBACrC,OAAO;YACT,CAAC;YAED,IACE,KAAK,CAAC,IAAI,CAAC,YAAY;gBACvB,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS;gBAC5B,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC;gBACjC,KAAK,CAAC,aAAa,EAAE,CAAC,YAAY,EAAE,EACpC,CAAC;gBACD,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;YAC1E,CAAC;QACH,CAAC;KACF,CAAC;IAEF,IAAK,OAAe,CAAC,KAAK,CAAC,wBAAwB,EAAE,CAAC;QACpD,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,CAAC;oBACvC,IAAI,MAAM,GAAG,IAAI,yBAAS,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;oBACrD,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;wBACrB,IAAI,CAAC,WAAW,CAAC,IAAA,6BAAa,EAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;oBACzD,CAAC;gBACH,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,CAAC;AACrB,CAAC","sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport type { types as t } from '@babel/core';\nimport type State from './state';\nimport { initState } from './state';\nimport type { Mode as GetConfigMode } from './get-config';\nimport { inlineRuntimeConfig, insertConfig } 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';\nimport { existsSync, readdirSync } from 'fs';\nimport { resolve, dirname, join } from 'path';\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(context, path, state);\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 let specifier = path.node.arguments[0];\n if (specifier?.type !== 'StringLiteral') {\n let relativePath = '';\n let property;\n if (specifier.type === 'TemplateLiteral') {\n relativePath = specifier.quasis[0].value.cooked!;\n property = specifier.expressions[0] as t.Expression;\n }\n // babel might transform template form `../my-path/${id}` to '../my-path/'.concat(id)\n if (\n specifier.type === 'CallExpression' &&\n specifier.callee.type === 'MemberExpression' &&\n specifier.callee.property.type === 'Identifier' &&\n specifier.callee.property.name === 'concat' &&\n specifier.callee.object.type === 'StringLiteral'\n ) {\n relativePath = specifier.callee.object.value;\n property = specifier.arguments[0] as t.Expression;\n }\n if (property && relativePath && relativePath.startsWith('.')) {\n const resolvedPath = resolve(dirname((state as any).filename), relativePath);\n let entries: string[] = [];\n if (existsSync(resolvedPath)) {\n entries = readdirSync(resolvedPath).filter(e => !e.startsWith('.'));\n }\n const obj = t.objectExpression(\n entries.map(e => {\n let key = e.split('.')[0];\n const rest = e.split('.').slice(1, -1);\n if (rest.length) {\n key += `.${rest}`;\n }\n const id = t.callExpression(\n state.importUtil.import(path, state.pathToOurAddon('es-compat2'), 'default', 'esc'),\n [state.importUtil.import(path, join(relativePath, key).replace(/\\\\/g, '/'), '*')]\n );\n return t.objectProperty(t.stringLiteral(key), id);\n })\n );\n const memberExpr = t.memberExpression(obj, property, true);\n path.replaceWith(memberExpr);\n state.calledIdentifiers.add(callee.node);\n return;\n } else {\n throw new Error(\n `importSync eager mode only supports dynamic paths which are relative, must start with a '.', had ${specifier.type}`\n );\n }\n }\n path.replaceWith(\n t.callExpression(state.importUtil.import(path, state.pathToOurAddon('es-compat2'), 'default', 'esc'), [\n state.importUtil.import(path, specifier.value, '*'),\n ])\n );\n state.calledIdentifiers.add(callee.node);\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.hideRequires &&\n path.node.name === 'require' &&\n !path.scope.hasBinding('require') &&\n state.owningPackage().isEmberAddon()\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"]}
@@ -28,4 +28,4 @@ export default interface State {
28
28
  mode: 'compile-time' | 'run-time';
29
29
  };
30
30
  }
31
- export declare function initState(t: typeof Babel.types, path: NodePath<Babel.types.Program>, state: State): void;
31
+ export declare function initState(t: typeof Babel, path: NodePath<Babel.types.Program>, state: State): void;
@@ -1 +1 @@
1
- {"version":3,"file":"state.js","sourceRoot":"","sources":["state.ts"],"names":[],"mappings":";;AAiDA,8BAUC;AA1DD,+BAA8C;AAE9C,kEAAgG;AAChG,yDAA+C;AA6C/C,SAAgB,SAAS,CAAC,CAAqB,EAAE,IAAmC,EAAE,KAAY;IAChG,KAAK,CAAC,UAAU,GAAG,IAAI,8BAA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AAI,CAAC,UAAU,CAAC,CAAC;IACzD,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CAAC,uDAAuD,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC;IAC5F,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,SAAS,qBAAqB;IAC5B,IAAI,GAAG,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;IAC/B,OAAO,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AACzC,CAAC","sourcesContent":["import type { NodePath, Node } from '@babel/traverse';\nimport { join, dirname, resolve } from 'path';\nimport type { Package } from '@embroider/shared-internals';\nimport { cleanUrl, explicitRelative, RewrittenPackageCache } 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 removed: Set<Node>;\n calledIdentifiers: Set<Node>;\n jobs: (() => void)[];\n packageCache: RewrittenPackageCache;\n sourceFile: string;\n pathToOurAddon(moduleName: string): string;\n owningPackage(): Package;\n originalOwningPackage(): Package;\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 hideRequires: boolean;\n\n mode: 'compile-time' | 'run-time';\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.jobs = [];\n state.removed = new Set();\n state.calledIdentifiers = new Set();\n state.packageCache = RewrittenPackageCache.shared('embroider', state.opts.appPackageRoot);\n state.sourceFile = state.opts.owningPackageRoot || cleanUrl((path.hub as any).file.opts.filename);\n state.pathToOurAddon = pathToAddon;\n state.owningPackage = owningPackage;\n state.originalOwningPackage = originalOwningPackage;\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 originalOwningPackage(this: State): Package {\n let pkg = this.owningPackage();\n return this.packageCache.original(pkg);\n}\n"]}
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["state.ts"],"names":[],"mappings":";;AAiDA,8BAUC;AA1DD,+BAA8C;AAE9C,kEAAgG;AAChG,yDAA+C;AA6C/C,SAAgB,SAAS,CAAC,CAAe,EAAE,IAAmC,EAAE,KAAY;IAC1F,KAAK,CAAC,UAAU,GAAG,IAAI,8BAAU,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IAC3C,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,YAAY,GAAG,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GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;IAC/B,OAAO,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AACzC,CAAC","sourcesContent":["import type { NodePath, Node } from '@babel/traverse';\nimport { join, dirname, resolve } from 'path';\nimport type { Package } from '@embroider/shared-internals';\nimport { cleanUrl, explicitRelative, RewrittenPackageCache } 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 removed: Set<Node>;\n calledIdentifiers: Set<Node>;\n jobs: (() => void)[];\n packageCache: RewrittenPackageCache;\n sourceFile: string;\n pathToOurAddon(moduleName: string): string;\n owningPackage(): Package;\n originalOwningPackage(): Package;\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 hideRequires: boolean;\n\n mode: 'compile-time' | 'run-time';\n };\n}\n\nexport function initState(t: typeof Babel, path: NodePath<Babel.types.Program>, state: State) {\n state.importUtil = new ImportUtil(t, path);\n state.jobs = [];\n state.removed = new Set();\n state.calledIdentifiers = new Set();\n state.packageCache = RewrittenPackageCache.shared('embroider', state.opts.appPackageRoot);\n state.sourceFile = state.opts.owningPackageRoot || cleanUrl((path.hub as any).file.opts.filename);\n state.pathToOurAddon = pathToAddon;\n state.owningPackage = owningPackage;\n state.originalOwningPackage = originalOwningPackage;\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 originalOwningPackage(this: State): Package {\n let pkg = this.owningPackage();\n return this.packageCache.original(pkg);\n}\n"]}
package/src/babel.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ import MacrosConfig from './macros-config';
2
+ export interface Options {
3
+ /**
4
+ * How you configure your own package / app
5
+ */
6
+ setOwnConfig?: object;
7
+ /**
8
+ * This is how you can optionally send configuration into
9
+ * your dependencies, if those dependencies choose to use
10
+ * @embroider/macros configs.
11
+ *
12
+ * @example
13
+ * ```js
14
+ * setConfig: {
15
+ * 'some-dependency': {
16
+ * // config for some-dependency
17
+ * }
18
+ * }
19
+ * ```
20
+ */
21
+ setConfig?: Record<string, object>;
22
+ /**
23
+ * Callback for further manipulation of the macros' configuration instance.
24
+ *
25
+ * Useful for libraries to provide their own config with defaults shared between sub-dependencies of those libraries.
26
+ */
27
+ configure?: (macrosInstance: MacrosConfig) => void;
28
+ /**
29
+ * Override the default directory used for the MacrosConfig
30
+ *
31
+ * defaults to the CWD, via process.cwd()
32
+ */
33
+ dir?: string;
34
+ }
35
+ interface ConfiguredMacros {
36
+ /**
37
+ * Array of plugins to add to the babel config plugins array
38
+ */
39
+ babelMacros: ReturnType<MacrosConfig['babelPluginConfig']>;
40
+ /**
41
+ * Array of template transforms to pass to the transforms array of the babel-plugin-ember-template-compilation babel plugin
42
+ */
43
+ templateMacros: ReturnType<(typeof MacrosConfig)['transforms']>['plugins'];
44
+ }
45
+ export declare function buildMacros(options?: Options): ConfiguredMacros;
46
+ export {};
package/src/babel.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.buildMacros = buildMacros;
7
+ const macros_config_1 = __importDefault(require("./macros-config"));
8
+ function buildMacros(options = {}) {
9
+ let root = options.dir || process.cwd();
10
+ let macros = macros_config_1.default.for({}, root);
11
+ let transforms = macros_config_1.default.transforms();
12
+ transforms.setConfig(macros);
13
+ let { setOwnConfig, setConfig, configure } = options;
14
+ if (setOwnConfig) {
15
+ macros.setOwnConfig(root, setOwnConfig);
16
+ }
17
+ if (setConfig) {
18
+ for (let [packageName, config] of Object.entries(setConfig)) {
19
+ macros.setConfig(root, packageName, config);
20
+ }
21
+ }
22
+ configure === null || configure === void 0 ? void 0 : configure(macros);
23
+ if (process.env.NODE_ENV === 'development') {
24
+ macros.enablePackageDevelopment(process.cwd());
25
+ macros.enableRuntimeMode();
26
+ }
27
+ macros.finalize();
28
+ return {
29
+ babelMacros: macros.babelPluginConfig(),
30
+ templateMacros: transforms.plugins,
31
+ };
32
+ }
33
+ //# sourceMappingURL=babel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"babel.js","sourceRoot":"","sources":["babel.ts"],"names":[],"mappings":";;;;;AAiDA,kCAiCC;AAlFD,oEAA2C;AAiD3C,SAAgB,WAAW,CAAC,UAAmB,EAAE;IAC/C,IAAI,IAAI,GAAG,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;IACxC,IAAI,MAAM,GAAG,uBAAY,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IAExC,IAAI,UAAU,GAAG,uBAAY,CAAC,UAAU,EAAE,CAAC;IAE3C,UAAU,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAE7B,IAAI,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;IAErD,IAAI,YAAY,EAAE,CAAC;QACjB,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IAC1C,CAAC;IAED,IAAI,SAAS,EAAE,CAAC;QACd,KAAK,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;YAC5D,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,WAAW,EAAE,MAAgB,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;IAED,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAG,MAAM,CAAC,CAAC;IAEpB,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,EAAE,CAAC;QAC3C,MAAM,CAAC,wBAAwB,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;QAC/C,MAAM,CAAC,iBAAiB,EAAE,CAAC;IAC7B,CAAC;IAED,MAAM,CAAC,QAAQ,EAAE,CAAC;IAElB,OAAO;QACL,WAAW,EAAE,MAAM,CAAC,iBAAiB,EAAE;QACvC,cAAc,EAAE,UAAU,CAAC,OAAO;KACnC,CAAC;AACJ,CAAC","sourcesContent":["import MacrosConfig from './macros-config';\n\nexport interface Options {\n /**\n * How you configure your own package / app\n */\n setOwnConfig?: object;\n /**\n * This is how you can optionally send configuration into\n * your dependencies, if those dependencies choose to use\n * @embroider/macros configs.\n *\n * @example\n * ```js\n * setConfig: {\n * 'some-dependency': {\n * // config for some-dependency\n * }\n * }\n * ```\n */\n setConfig?: Record<string, object>;\n\n /**\n * Callback for further manipulation of the macros' configuration instance.\n *\n * Useful for libraries to provide their own config with defaults shared between sub-dependencies of those libraries.\n */\n configure?: (macrosInstance: MacrosConfig) => void;\n\n /**\n * Override the default directory used for the MacrosConfig\n *\n * defaults to the CWD, via process.cwd()\n */\n dir?: string;\n}\n\ninterface ConfiguredMacros {\n /**\n * Array of plugins to add to the babel config plugins array\n */\n babelMacros: ReturnType<MacrosConfig['babelPluginConfig']>;\n /**\n * Array of template transforms to pass to the transforms array of the babel-plugin-ember-template-compilation babel plugin\n */\n templateMacros: ReturnType<(typeof MacrosConfig)['transforms']>['plugins'];\n}\n\nexport function buildMacros(options: Options = {}): ConfiguredMacros {\n let root = options.dir || process.cwd();\n let macros = MacrosConfig.for({}, root);\n\n let transforms = MacrosConfig.transforms();\n\n transforms.setConfig(macros);\n\n let { setOwnConfig, setConfig, configure } = options;\n\n if (setOwnConfig) {\n macros.setOwnConfig(root, setOwnConfig);\n }\n\n if (setConfig) {\n for (let [packageName, config] of Object.entries(setConfig)) {\n macros.setConfig(root, packageName, config as object);\n }\n }\n\n configure?.(macros);\n\n if (process.env.NODE_ENV === 'development') {\n macros.enablePackageDevelopment(process.cwd());\n macros.enableRuntimeMode();\n }\n\n macros.finalize();\n\n return {\n babelMacros: macros.babelPluginConfig(),\n templateMacros: transforms.plugins,\n };\n}\n"]}