@embroider/macros 0.50.0 → 1.1.0

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.
@@ -1 +1 @@
1
- {"version":3,"file":"macros-babel-plugin.js","sourceRoot":"","sources":["macros-babel-plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAEA,kEAA2D;AAC3D,mCAA2D;AAC3D,6CAAwF;AACxF,qEAAyE;AACzE,iCAAgD;AAEhD,oDAA4B;AAC5B,8DAAqC;AACrC,mDAA2D;AAG3D,MAAM,YAAY,GAAG,+BAAY,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC;AAE7D,SAAwB,IAAI,CAAC,OAAqB;IAChD,IAAI,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC;IACtB,IAAI,OAAO,GAAG;QACZ,OAAO,EAAE;YACP,KAAK,CAAC,CAAsB,EAAE,KAAY;gBACxC,KAAK,CAAC,iBAAiB,GAAG,IAAI,GAAG,EAAE,CAAC;gBACpC,KAAK,CAAC,IAAI,GAAG,EAAE,CAAC;gBAChB,KAAK,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;gBAC1B,KAAK,CAAC,iBAAiB,GAAG,IAAI,GAAG,EAAE,CAAC;gBACpC,KAAK,CAAC,oBAAoB,GAAG,IAAI,GAAG,EAAE,CAAC;gBACvC,KAAK,CAAC,kBAAkB,GAAG,IAAI,GAAG,EAAE,CAAC;YACvC,CAAC;YACD,IAAI,CAAC,IAAyB,EAAE,KAAY;gBAC1C,iBAAiB,CAAC,IAAI,CAAC,CAAC;gBACxB,iBAAiB,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;gBACxC,eAAe,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;gBAChC,KAAK,IAAI,OAAO,IAAI,KAAK,CAAC,IAAI,EAAE;oBAC9B,OAAO,EAAE,CAAC;iBACX;YACH,CAAC;SACF;QACD,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UAAU,CAAC,SAAS,CAAC,CAAC,EAAE;QACpF,SAAS,GAAG,GAAG,IAAI,GAAG,OAAO,EAAE,EAAE,CAAC;KACnC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport type { types as t } from '@babel/core';\nimport { PackageCache } from '@embroider/shared-internals';\nimport State, { sourceFile, pathToRuntime } 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\nconst packageCache = PackageCache.shared('embroider-stage3');\n\nexport default function main(context: typeof Babel): unknown {\n let t = context.types;\n let visitor = {\n Program: {\n enter(_: NodePath<t.Program>, state: State) {\n state.generatedRequires = new Set();\n state.jobs = [];\n state.removed = new Set();\n state.calledIdentifiers = new Set();\n state.neededRuntimeImports = new Map();\n state.neededEagerImports = new Map();\n },\n exit(path: NodePath<t.Program>, state: State) {\n pruneMacroImports(path);\n addRuntimeImports(path, state, context);\n addEagerImports(path, state, t);\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') {\n let pkg = packageCache.ownerOfFile(sourceFile(path, state));\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 state.neededRuntimeImports.set(callee.node.name, '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 let replacePaths = state.neededEagerImports.get(specifier.value);\n if (!replacePaths) {\n replacePaths = [];\n state.neededEagerImports.set(specifier.value, replacePaths);\n }\n replacePaths.push(path);\n state.calledIdentifiers.add(callee.node);\n } else {\n let r = t.identifier('require');\n state.generatedRequires.add(r);\n callee.replaceWith(r);\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 ownedByEmberPackage(path, state)\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\n// This removes imports from \"@embroider/macros\" itself, because we have no\n// runtime behavior at all.\nfunction pruneMacroImports(path: NodePath) {\n if (!path.isProgram()) {\n return;\n }\n for (let topLevelPath of path.get('body')) {\n if (topLevelPath.isImportDeclaration() && topLevelPath.get('source').node.value === '@embroider/macros') {\n topLevelPath.remove();\n }\n }\n}\n\nfunction addRuntimeImports(path: NodePath<t.Program>, state: State, context: typeof Babel) {\n let t = context.types;\n if (state.neededRuntimeImports.size > 0) {\n path.node.body.push(\n t.importDeclaration(\n [...state.neededRuntimeImports].map(([local, imported]) =>\n t.importSpecifier(t.identifier(local), t.identifier(imported))\n ),\n t.stringLiteral(pathToRuntime(path, state))\n )\n );\n }\n}\n\nfunction addEagerImports(path: NodePath<t.Program>, state: State, t: typeof Babel['types']) {\n let createdNames = new Set<string>();\n for (let [specifier, replacePaths] of state.neededEagerImports.entries()) {\n let local = unusedNameLike('a', replacePaths, createdNames);\n createdNames.add(local);\n path.node.body.push(\n t.importDeclaration([t.importNamespaceSpecifier(t.identifier(local))], t.stringLiteral(specifier))\n );\n for (let nodePath of replacePaths) {\n nodePath.replaceWith(t.identifier(local));\n }\n }\n}\n\nfunction ownedByEmberPackage(path: NodePath, state: State) {\n let filename = sourceFile(path, state);\n let pkg = packageCache.ownerOfFile(filename);\n return pkg && pkg.isEmberPackage();\n}\n\nfunction unusedNameLike(name: string, paths: NodePath<unknown>[], banned: Set<string>) {\n let candidate = name;\n let counter = 0;\n while (banned.has(candidate) || paths.some(path => path.scope.getBinding(candidate))) {\n candidate = `${name}${counter++}`;\n }\n return candidate;\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,GAAG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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') {\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"]}
@@ -3,7 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const state_1 = require("./state");
7
6
  const error_1 = __importDefault(require("./error"));
8
7
  const evaluate_json_1 = require("./evaluate-json");
9
8
  const resolve_1 = __importDefault(require("resolve"));
@@ -16,9 +15,8 @@ function moduleExists(path, state) {
16
15
  if (moduleSpecifier.type !== 'StringLiteral') {
17
16
  throw (0, error_1.default)((0, evaluate_json_1.assertArray)(path.get('arguments'))[0], `the first argument to moduleExists must be a string literal`);
18
17
  }
19
- let sourceFileName = (0, state_1.sourceFile)(path, state);
20
18
  try {
21
- resolve_1.default.sync(moduleSpecifier.value, { basedir: (0, path_1.dirname)(sourceFileName) });
19
+ resolve_1.default.sync(moduleSpecifier.value, { basedir: (0, path_1.dirname)(state.sourceFile) });
22
20
  return true;
23
21
  }
24
22
  catch (err) {
@@ -1 +1 @@
1
- {"version":3,"file":"module-exists.js","sourceRoot":"","sources":["module-exists.ts"],"names":[],"mappings":";;;;;AAEA,mCAA4C;AAC5C,oDAA4B;AAC5B,mDAA8C;AAC9C,sDAA8B;AAC9B,+BAA+B;AAE/B,SAAwB,YAAY,CAAC,IAAgC,EAAE,KAAY;IACjF,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;QACpC,MAAM,IAAA,eAAK,EAAC,IAAI,EAAE,uDAAuD,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;KACxG;IACD,IAAI,CAAC,eAAe,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;IAC5C,IAAI,eAAe,CAAC,IAAI,KAAK,eAAe,EAAE;QAC5C,MAAM,IAAA,eAAK,EAAC,IAAA,2BAAW,EAAC,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,6DAA6D,CAAC,CAAC;KACnH;IACD,IAAI,cAAc,GAAG,IAAA,kBAAU,EAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC7C,IAAI;QACF,iBAAO,CAAC,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,IAAA,cAAO,EAAC,cAAc,CAAC,EAAE,CAAC,CAAC;QAC1E,OAAO,IAAI,CAAC;KACb;IAAC,OAAO,GAAG,EAAE;QACZ,IAAI,GAAG,CAAC,IAAI,KAAK,kBAAkB,EAAE;YACnC,MAAM,GAAG,CAAC;SACX;QACD,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAlBD,+BAkBC","sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport type { types as t } from '@babel/core';\nimport State, { sourceFile } from './state';\nimport error from './error';\nimport { assertArray } from './evaluate-json';\nimport resolve from 'resolve';\nimport { dirname } from 'path';\n\nexport default function moduleExists(path: NodePath<t.CallExpression>, state: State): boolean {\n if (path.node.arguments.length !== 1) {\n throw error(path, `moduleExists takes exactly one argument, you passed ${path.node.arguments.length}`);\n }\n let [moduleSpecifier] = path.node.arguments;\n if (moduleSpecifier.type !== 'StringLiteral') {\n throw error(assertArray(path.get('arguments'))[0], `the first argument to moduleExists must be a string literal`);\n }\n let sourceFileName = sourceFile(path, state);\n try {\n resolve.sync(moduleSpecifier.value, { basedir: dirname(sourceFileName) });\n return true;\n } catch (err) {\n if (err.code !== 'MODULE_NOT_FOUND') {\n throw err;\n }\n return false;\n }\n}\n"]}
1
+ {"version":3,"file":"module-exists.js","sourceRoot":"","sources":["module-exists.ts"],"names":[],"mappings":";;;;;AAGA,oDAA4B;AAC5B,mDAA8C;AAC9C,sDAA8B;AAC9B,+BAA+B;AAE/B,SAAwB,YAAY,CAAC,IAAgC,EAAE,KAAY;IACjF,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;QACpC,MAAM,IAAA,eAAK,EAAC,IAAI,EAAE,uDAAuD,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;KACxG;IACD,IAAI,CAAC,eAAe,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;IAC5C,IAAI,eAAe,CAAC,IAAI,KAAK,eAAe,EAAE;QAC5C,MAAM,IAAA,eAAK,EAAC,IAAA,2BAAW,EAAC,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,6DAA6D,CAAC,CAAC;KACnH;IACD,IAAI;QACF,iBAAO,CAAC,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,IAAA,cAAO,EAAC,KAAK,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;QAC5E,OAAO,IAAI,CAAC;KACb;IAAC,OAAO,GAAG,EAAE;QACZ,IAAI,GAAG,CAAC,IAAI,KAAK,kBAAkB,EAAE;YACnC,MAAM,GAAG,CAAC;SACX;QACD,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAjBD,+BAiBC","sourcesContent":["import type { NodePath } from '@babel/traverse';\nimport type { types as t } from '@babel/core';\nimport State from './state';\nimport error from './error';\nimport { assertArray } from './evaluate-json';\nimport resolve from 'resolve';\nimport { dirname } from 'path';\n\nexport default function moduleExists(path: NodePath<t.CallExpression>, state: State): boolean {\n if (path.node.arguments.length !== 1) {\n throw error(path, `moduleExists takes exactly one argument, you passed ${path.node.arguments.length}`);\n }\n let [moduleSpecifier] = path.node.arguments;\n if (moduleSpecifier.type !== 'StringLiteral') {\n throw error(assertArray(path.get('arguments'))[0], `the first argument to moduleExists must be a string literal`);\n }\n try {\n resolve.sync(moduleSpecifier.value, { basedir: dirname(state.sourceFile) });\n return true;\n } catch (err) {\n if (err.code !== 'MODULE_NOT_FOUND') {\n throw err;\n }\n return false;\n }\n}\n"]}
@@ -1,12 +1,18 @@
1
1
  import type { NodePath, Node } from '@babel/traverse';
2
- import { Package } from '@embroider/shared-internals';
2
+ import { Package, PackageCache } from '@embroider/shared-internals';
3
+ import { ImportUtil } from 'babel-import-util';
4
+ import type * as Babel from '@babel/core';
3
5
  export default interface State {
6
+ importUtil: ImportUtil;
4
7
  generatedRequires: Set<Node>;
5
8
  removed: Set<Node>;
6
9
  calledIdentifiers: Set<Node>;
7
10
  jobs: (() => void)[];
8
- neededRuntimeImports: Map<string, string>;
9
- neededEagerImports: Map<string, NodePath[]>;
11
+ packageCache: PackageCache;
12
+ sourceFile: string;
13
+ pathToOurAddon(moduleName: string): string;
14
+ owningPackage(): Package;
15
+ cloneDeep(node: Node): Node;
10
16
  opts: {
11
17
  userConfigs: {
12
18
  [pkgRoot: string]: unknown;
@@ -22,8 +28,4 @@ export default interface State {
22
28
  importSyncImplementation: 'cjs' | 'eager';
23
29
  };
24
30
  }
25
- export declare function pathToRuntime(path: NodePath, state: State): string;
26
- export declare function sourceFile(path: NodePath, state: State): string;
27
- export declare function owningPackage(path: NodePath, state: State): Package;
28
- export declare function cloneDeep(node: Node, state: State): Node;
29
- export declare function unusedNameLike(name: string, path: NodePath<unknown>): string;
31
+ export declare function initState(t: typeof Babel.types, path: NodePath<Babel.types.Program>, state: State): void;
@@ -3,17 +3,30 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.unusedNameLike = exports.cloneDeep = exports.owningPackage = exports.sourceFile = exports.pathToRuntime = void 0;
6
+ exports.initState = void 0;
7
7
  const cloneDeepWith_1 = __importDefault(require("lodash/cloneDeepWith"));
8
8
  const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
9
9
  const path_1 = require("path");
10
10
  const shared_internals_1 = require("@embroider/shared-internals");
11
- const runtimePath = (0, path_1.resolve)((0, path_1.join)(__dirname, '..', 'addon', 'runtime'));
12
- function pathToRuntime(path, state) {
13
- if (!state.opts.owningPackageRoot) {
11
+ const babel_import_util_1 = require("babel-import-util");
12
+ function initState(t, path, state) {
13
+ state.importUtil = new babel_import_util_1.ImportUtil(t, path);
14
+ state.generatedRequires = new Set();
15
+ state.jobs = [];
16
+ state.removed = new Set();
17
+ state.calledIdentifiers = new Set();
18
+ state.packageCache = shared_internals_1.PackageCache.shared('embroider-stage3', state.opts.appPackageRoot);
19
+ state.sourceFile = state.opts.owningPackageRoot || path.hub.file.opts.filename;
20
+ state.pathToOurAddon = pathToAddon;
21
+ state.owningPackage = owningPackage;
22
+ state.cloneDeep = cloneDeep;
23
+ }
24
+ exports.initState = initState;
25
+ const runtimeAddonPath = (0, path_1.resolve)((0, path_1.join)(__dirname, '..', 'addon'));
26
+ function pathToAddon(moduleName) {
27
+ if (!this.opts.owningPackageRoot) {
14
28
  // running inside embroider, so make a relative path to the module
15
- let source = sourceFile(path, state);
16
- return (0, shared_internals_1.explicitRelative)((0, path_1.dirname)(source), runtimePath);
29
+ return (0, shared_internals_1.explicitRelative)((0, path_1.dirname)(this.sourceFile), (0, path_1.join)(runtimeAddonPath, moduleName));
17
30
  }
18
31
  else {
19
32
  // running inside a classic build, so use a classic-compatible runtime
@@ -24,25 +37,18 @@ function pathToRuntime(path, state) {
24
37
  // introducing incompatible changes to its API, you need to change this name
25
38
  // (by tacking on a version number, etc) and rename the corresponding file
26
39
  // in ../addon.
27
- return '@embroider/macros/runtime';
40
+ return `@embroider/macros/${moduleName}`;
28
41
  }
29
42
  }
30
- exports.pathToRuntime = pathToRuntime;
31
- function sourceFile(path, state) {
32
- return state.opts.owningPackageRoot || path.hub.file.opts.filename;
33
- }
34
- exports.sourceFile = sourceFile;
35
- const packageCache = shared_internals_1.PackageCache.shared('embroider-stage3');
36
- function owningPackage(path, state) {
37
- let file = sourceFile(path, state);
38
- let pkg = packageCache.ownerOfFile(file);
43
+ function owningPackage() {
44
+ let pkg = this.packageCache.ownerOfFile(this.sourceFile);
39
45
  if (!pkg) {
40
- throw new Error(`unable to determine which npm package owns the file ${file}`);
46
+ throw new Error(`unable to determine which npm package owns the file ${this.sourceFile}`);
41
47
  }
42
48
  return pkg;
43
49
  }
44
- exports.owningPackage = owningPackage;
45
- function cloneDeep(node, state) {
50
+ function cloneDeep(node) {
51
+ let state = this;
46
52
  return (0, cloneDeepWith_1.default)(node, function (value) {
47
53
  if (state.generatedRequires.has(value)) {
48
54
  let cloned = (0, cloneDeep_1.default)(value);
@@ -51,14 +57,4 @@ function cloneDeep(node, state) {
51
57
  }
52
58
  });
53
59
  }
54
- exports.cloneDeep = cloneDeep;
55
- function unusedNameLike(name, path) {
56
- let candidate = name;
57
- let counter = 0;
58
- while (path.scope.getBinding(candidate)) {
59
- candidate = `${name}${counter++}`;
60
- }
61
- return candidate;
62
- }
63
- exports.unusedNameLike = unusedNameLike;
64
60
  //# sourceMappingURL=state.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"state.js","sourceRoot":"","sources":["state.ts"],"names":[],"mappings":";;;;;;AACA,yEAAiD;AACjD,iEAA+C;AAC/C,+BAA8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I,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;AARD,8BAQC;AAED,SAAgB,cAAc,CAAC,IAAY,EAAE,IAAuB;IAClE,IAAI,SAAS,GAAG,IAAI,CAAC;IACrB,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,OAAO,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE;QACvC,SAAS,GAAG,GAAG,IAAI,GAAG,OAAO,EAAE,EAAE,CAAC;KACnC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAPD,wCAOC","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';\n\nexport default interface State {\n generatedRequires: Set<Node>;\n removed: Set<Node>;\n calledIdentifiers: Set<Node>;\n jobs: (() => void)[];\n\n // map from local name to imported name from @embroider/macros own runtime\n // implementations.\n neededRuntimeImports: Map<string, string>;\n\n // when we're running with importSync's eager implementation, this maps from\n // module specifier to the set of nodes that should be replaced with the\n // module value.\n neededEagerImports: Map<string, NodePath[]>;\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, if the app is under active\n // development. Needed so that we can get consistent answers to\n // `isDevelopingApp` and `isDeveopingThisPackage`\n appPackageRoot: string;\n\n embroiderMacrosConfigMarker: true;\n\n mode: 'compile-time' | 'run-time';\n\n importSyncImplementation: 'cjs' | 'eager';\n };\n}\n\nconst runtimePath = resolve(join(__dirname, '..', 'addon', 'runtime'));\n\nexport function pathToRuntime(path: NodePath, state: State): string {\n if (!state.opts.owningPackageRoot) {\n // running inside embroider, so make a relative path to the module\n let source = sourceFile(path, state);\n return explicitRelative(dirname(source), runtimePath);\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/runtime';\n }\n}\n\nexport function sourceFile(path: NodePath, state: State): string {\n return state.opts.owningPackageRoot || path.hub.file.opts.filename;\n}\n\nconst packageCache = PackageCache.shared('embroider-stage3');\n\nexport function owningPackage(path: NodePath, state: State): Package {\n let file = sourceFile(path, state);\n let pkg = packageCache.ownerOfFile(file);\n if (!pkg) {\n throw new Error(`unable to determine which npm package owns the file ${file}`);\n }\n return pkg;\n}\n\nexport function cloneDeep(node: Node, state: State): Node {\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\nexport function unusedNameLike(name: string, path: NodePath<unknown>) {\n let candidate = name;\n let counter = 0;\n while (path.scope.getBinding(candidate)) {\n candidate = `${name}${counter++}`;\n }\n return candidate;\n}\n"]}
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,SA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AC;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.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"]}
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
2
  const path_1 = require("path");
3
3
  const node_1 = require("./node");
4
+ // this can differ from appInstance.project.root because Dummy apps are terrible
5
+ function getAppRoot(appInstance) {
6
+ return (0, path_1.join)(appInstance.project.configPath(), '..', '..');
7
+ }
8
+ function getMacrosConfig(appInstance) {
9
+ let appRoot = (0, path_1.join)(appInstance.project.configPath(), '..', '..');
10
+ return node_1.MacrosConfig.for(appInstance, appRoot);
11
+ }
4
12
  module.exports = {
5
13
  name: '@embroider/macros',
6
14
  included(parent) {
@@ -8,28 +16,28 @@ module.exports = {
8
16
  this.options.babel = { plugins: [] };
9
17
  let parentOptions = (parent.options = parent.options || {});
10
18
  let ownOptions = (parentOptions['@embroider/macros'] = parentOptions['@embroider/macros'] || {});
11
- const appInstance = this._findHost();
12
- this.setMacrosConfig(node_1.MacrosConfig.for(appInstance));
19
+ let appInstance = this._findHost();
20
+ let macrosConfig = getMacrosConfig(appInstance);
21
+ this.setMacrosConfig(macrosConfig);
13
22
  // if parent is an addon it has root. If it's an app it has project.root.
14
23
  let source = parent.root || parent.project.root;
15
24
  if (ownOptions.setOwnConfig) {
16
- node_1.MacrosConfig.for(appInstance).setOwnConfig(source, ownOptions.setOwnConfig);
25
+ macrosConfig.setOwnConfig(source, ownOptions.setOwnConfig);
17
26
  }
18
27
  if (ownOptions.setConfig) {
19
28
  for (let [packageName, config] of Object.entries(ownOptions.setConfig)) {
20
- node_1.MacrosConfig.for(appInstance).setConfig(source, packageName, config);
29
+ macrosConfig.setConfig(source, packageName, config);
21
30
  }
22
31
  }
23
32
  if (appInstance.env !== 'production') {
24
- let macros = node_1.MacrosConfig.for(appInstance);
25
- // tell the macros where our app is
26
- macros.enableAppDevelopment((0, path_1.join)(appInstance.project.configPath(), '..', '..'));
33
+ // tell the macros our app is under development
34
+ macrosConfig.enablePackageDevelopment(getAppRoot(appInstance));
27
35
  // also tell them our root project is under development. This can be
28
36
  // different, in the case where this is an addon and the app is the dummy
29
37
  // app.
30
- macros.enablePackageDevelopment(appInstance.project.root);
38
+ macrosConfig.enablePackageDevelopment(appInstance.project.root);
31
39
  // keep the macros in runtime mode for development & testing
32
- macros.enableRuntimeMode();
40
+ macrosConfig.enableRuntimeMode();
33
41
  }
34
42
  // add our babel plugin to our parent's babel
35
43
  this.installBabelPlugin(parent);
@@ -43,9 +51,9 @@ module.exports = {
43
51
  // forbidden and consuming it becomes allowed. There's no existing hook with
44
52
  // that timing.
45
53
  const originalToTree = appInstance.toTree;
46
- appInstance.toTree = function () {
47
- node_1.MacrosConfig.for(appInstance).finalize();
48
- return originalToTree.apply(appInstance, arguments);
54
+ appInstance.toTree = function (...args) {
55
+ macrosConfig.finalize();
56
+ return originalToTree.apply(appInstance, args);
49
57
  };
50
58
  },
51
59
  // Other addons are allowed to call this. It's needed if an addon needs to
@@ -59,7 +67,7 @@ module.exports = {
59
67
  let babelPlugins = (babelOptions.plugins = babelOptions.plugins || []);
60
68
  if (!babelPlugins.some(node_1.isEmbroiderMacrosPlugin)) {
61
69
  let appInstance = this._findHost();
62
- babelPlugins.unshift(...node_1.MacrosConfig.for(appInstance).babelPluginConfig(appOrAddonInstance));
70
+ babelPlugins.unshift(...getMacrosConfig(appInstance).babelPluginConfig(appOrAddonInstance));
63
71
  }
64
72
  },
65
73
  setupPreprocessorRegistry(type, registry) {
@@ -71,27 +79,24 @@ module.exports = {
71
79
  // MacrosConfig.astPlugins is static because in classic ember-cli, at this
72
80
  // point there's not yet an appInstance, so we defer getting it and
73
81
  // calling setConfig until our included hook.
74
- let { plugins, setConfig, getConfigForPlugin } = node_1.MacrosConfig.astPlugins(this.parent.root);
82
+ let { plugins, setConfig, lazyParams } = node_1.MacrosConfig.astPlugins(this.parent.root);
75
83
  this.setMacrosConfig = setConfig;
76
84
  plugins.forEach((plugin, index) => {
77
85
  let name = `@embroider/macros/${index}`;
78
86
  let baseDir = (0, path_1.join)(__dirname, '..');
79
- let projectRoot = this.parent.root;
87
+ let params = {
88
+ name,
89
+ firstTransformParams: lazyParams,
90
+ methodName: index === 0 ? 'makeSecondTransform' : 'makeFirstTransform',
91
+ baseDir,
92
+ };
80
93
  registry.add('htmlbars-ast-plugin', {
81
94
  name,
82
95
  plugin,
83
96
  parallelBabel: {
84
97
  requireFile: (0, path_1.join)(__dirname, 'glimmer', 'ast-transform.js'),
85
98
  buildUsing: 'buildPlugin',
86
- params: {
87
- name,
88
- get configs() {
89
- return getConfigForPlugin();
90
- },
91
- methodName: index === 0 ? 'makeSecondTransform' : 'makeFirstTransform',
92
- projectRoot: projectRoot,
93
- baseDir,
94
- },
99
+ params,
95
100
  },
96
101
  baseDir: () => baseDir,
97
102
  });
@@ -1 +1 @@
1
- {"version":3,"file":"ember-addon-main.js","sourceRoot":"","sources":["ember-addon-main.ts"],"names":[],"mappings":";AAAA,+BAA4B;AAC5B,iCAA+D;AAE/D,iBAAS;IACP,IAAI,EAAE,mBAAmB;IACzB,QAAQ,CAAY,MAAW;QAC7B,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAC5C,IAAI,CAAC,OAAO,CAAC,KAAK,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;QACrC,IAAI,aAAa,GAAG,CAAC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;QAC5D,IAAI,UAAU,GAAG,CAAC,aAAa,CAAC,mBAAmB,CAAC,GAAG,aAAa,CAAC,mBAAmB,CAAC,IAAI,EAAE,CAAC,CAAC;QAEjG,MAAM,WAAW,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;QACrC,IAAI,CAAC,eAAe,CAAC,mBAAY,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC;QACpD,yEAAyE;QACzE,IAAI,MAAM,GAAG,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC;QAEhD,IAAI,UAAU,CA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IAAA,WAAI,EAAC,SAAS,EAAE,IAAI,CAAC,CAAC;gBACpC,IAAI,WAAW,GAAI,IAAY,CAAC,MAAM,CAAC,IAAI,CAAC;gBAE5C,QAAQ,CAAC,GAAG,CAAC,qBAAqB,EAAE;oBAClC,IAAI;oBACJ,MAAM;oBACN,aAAa,EAAE;wBACb,WAAW,EAAE,IAAA,WAAI,EAAC,SAAS,EAAE,SAAS,EAAE,kBAAkB,CAAC;wBAC3D,UAAU,EAAE,aAAa;wBACzB,MAAM,EAAE;4BACN,IAAI;4BACJ,IAAI,OAAO;gCACT,OAAO,kBAAkB,EAAE,CAAC;4BAC9B,CAAC;4BACD,UAAU,EAAE,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,oBAAoB;4BACtE,WAAW,EAAE,WAAW;4BACxB,OAAO;yBACR;qBACF;oBACD,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO;iBACvB,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;IAED,OAAO,EAAE,EAAE;CACZ,CAAC","sourcesContent":["import { join } from 'path';\nimport { MacrosConfig, isEmbroiderMacrosPlugin } from './node';\n\nexport = {\n name: '@embroider/macros',\n included(this: any, parent: any) {\n this._super.included.apply(this, arguments);\n this.options.babel = { plugins: [] };\n let parentOptions = (parent.options = parent.options || {});\n let ownOptions = (parentOptions['@embroider/macros'] = parentOptions['@embroider/macros'] || {});\n\n const appInstance = this._findHost();\n this.setMacrosConfig(MacrosConfig.for(appInstance));\n // if parent is an addon it has root. If it's an app it has project.root.\n let source = parent.root || parent.project.root;\n\n if (ownOptions.setOwnConfig) {\n MacrosConfig.for(appInstance).setOwnConfig(source, ownOptions.setOwnConfig);\n }\n\n if (ownOptions.setConfig) {\n for (let [packageName, config] of Object.entries(ownOptions.setConfig)) {\n MacrosConfig.for(appInstance).setConfig(source, packageName, config as object);\n }\n }\n\n if (appInstance.env !== 'production') {\n let macros = MacrosConfig.for(appInstance);\n // tell the macros where our app is\n macros.enableAppDevelopment(join(appInstance.project.configPath(), '..', '..'));\n // also tell them our root project is under development. This can be\n // different, in the case where this is an addon and the app is the dummy\n // app.\n macros.enablePackageDevelopment(appInstance.project.root);\n // keep the macros in runtime mode for development & testing\n macros.enableRuntimeMode();\n }\n\n // add our babel plugin to our parent's babel\n this.installBabelPlugin(parent);\n\n // and to our own babel, because we may need to inline runtime config into\n // our source code\n this.installBabelPlugin(this);\n\n appInstance.import('vendor/embroider-macros-test-support.js', { type: 'test' });\n\n // When we're used inside the traditional ember-cli build pipeline without\n // Embroider, we unfortunately need to hook into here uncleanly because we\n // need to delineate the point in time after which writing macro config is\n // forbidden and consuming it becomes allowed. There's no existing hook with\n // that timing.\n const originalToTree = appInstance.toTree;\n appInstance.toTree = function () {\n MacrosConfig.for(appInstance).finalize();\n return originalToTree.apply(appInstance, arguments);\n };\n },\n\n // Other addons are allowed to call this. It's needed if an addon needs to\n // emit code containing macros into that addon's parent (via a babel plugin,\n // for exmple). This is only an issue in classic builds, under embroider all\n // babel plugins should be thought of as *language extensions* that are\n // available everywhere, we don't scope them so narrowly so this probably\n // doesn't come up.\n installBabelPlugin(this: any, appOrAddonInstance: any) {\n let babelOptions = (appOrAddonInstance.options.babel = appOrAddonInstance.options.babel || {});\n let babelPlugins = (babelOptions.plugins = babelOptions.plugins || []);\n if (!babelPlugins.some(isEmbroiderMacrosPlugin)) {\n let appInstance = this._findHost();\n babelPlugins.unshift(...MacrosConfig.for(appInstance).babelPluginConfig(appOrAddonInstance));\n }\n },\n\n setupPreprocessorRegistry(this: any, type: 'parent' | 'self', registry: any) {\n if (type === 'parent') {\n // the htmlbars-ast-plugins are split into two parts because order is\n // important. Weirdly, they appear to run in the reverse order that you\n // register them here.\n //\n // MacrosConfig.astPlugins is static because in classic ember-cli, at this\n // point there's not yet an appInstance, so we defer getting it and\n // calling setConfig until our included hook.\n let { plugins, setConfig, getConfigForPlugin } = MacrosConfig.astPlugins((this as any).parent.root);\n this.setMacrosConfig = setConfig;\n plugins.forEach((plugin, index) => {\n let name = `@embroider/macros/${index}`;\n let baseDir = join(__dirname, '..');\n let projectRoot = (this as any).parent.root;\n\n registry.add('htmlbars-ast-plugin', {\n name,\n plugin,\n parallelBabel: {\n requireFile: join(__dirname, 'glimmer', 'ast-transform.js'),\n buildUsing: 'buildPlugin',\n params: {\n name,\n get configs() {\n return getConfigForPlugin();\n },\n methodName: index === 0 ? 'makeSecondTransform' : 'makeFirstTransform',\n projectRoot: projectRoot,\n baseDir,\n },\n },\n baseDir: () => baseDir,\n });\n });\n }\n },\n\n options: {},\n};\n"]}
1
+ {"version":3,"file":"ember-addon-main.js","sourceRoot":"","sources":["ember-addon-main.ts"],"names":[],"mappings":";AACA,+BAA4B;AAE5B,iCAA+D;AAiH/D,gFAAgF;AAChF,SAAS,UAAU,CAAC,WAAwB;IAC1C,OAAO,IAAA,WAAI,EAAC,WAAW,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAC5D,CAAC;AAED,SAAS,eAAe,CAAC,WAAwB;IAC/C,IAAI,OAAO,GAAG,IAAA,WAAI,EAAC,WAAW,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IACjE,OAAO,mBAAY,CAAC,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;AAChD,CAAC;AAvHD,iBAAS;IACP,IAAI,EAAE,mBAAmB;IACzB,QAAQ,CAAY,MAAW;QAC7B,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAC5C,IAAI,CAAC,OAAO,CAAC,KAAK,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;QACrC,IAAI,aAAa,GAAG,CAAC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;QAC5D,IAAI,UAAU,GAAG,CAA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AAC;YACjC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;gBAChC,IAAI,IAAI,GAAG,qBAAqB,KAAK,EAAE,CAAC;gBACxC,IAAI,OAAO,GAAG,IAAA,WAAI,EAAC,SAAS,EAAE,IAAI,CAAC,CAAC;gBAEpC,IAAI,MAAM,GAAsB;oBAC9B,IAAI;oBACJ,oBAAoB,EAAE,UAAU;oBAChC,UAAU,EAAE,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,oBAAoB;oBACtE,OAAO;iBACR,CAAC;gBAEF,QAAQ,CAAC,GAAG,CAAC,qBAAqB,EAAE;oBAClC,IAAI;oBACJ,MAAM;oBACN,aAAa,EAAE;wBACb,WAAW,EAAE,IAAA,WAAI,EAAC,SAAS,EAAE,SAAS,EAAE,kBAAkB,CAAC;wBAC3D,UAAU,EAAE,aAAa;wBACzB,MAAM;qBACP;oBACD,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO;iBACvB,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;IAED,OAAO,EAAE,EAAE;CACZ,CAAC","sourcesContent":["import { AppInstance } from '@embroider/shared-internals';\nimport { join } from 'path';\nimport { BuildPluginParams } from './glimmer/ast-transform';\nimport { MacrosConfig, isEmbroiderMacrosPlugin } from './node';\n\nexport = {\n name: '@embroider/macros',\n included(this: any, parent: any) {\n this._super.included.apply(this, arguments);\n this.options.babel = { plugins: [] };\n let parentOptions = (parent.options = parent.options || {});\n let ownOptions = (parentOptions['@embroider/macros'] = parentOptions['@embroider/macros'] || {});\n\n let appInstance: AppInstance = this._findHost();\n let macrosConfig = getMacrosConfig(appInstance);\n this.setMacrosConfig(macrosConfig);\n\n // if parent is an addon it has root. If it's an app it has project.root.\n let source = parent.root || parent.project.root;\n\n if (ownOptions.setOwnConfig) {\n macrosConfig.setOwnConfig(source, ownOptions.setOwnConfig);\n }\n\n if (ownOptions.setConfig) {\n for (let [packageName, config] of Object.entries(ownOptions.setConfig)) {\n macrosConfig.setConfig(source, packageName, config as object);\n }\n }\n\n if (appInstance.env !== 'production') {\n // tell the macros our app is under development\n macrosConfig.enablePackageDevelopment(getAppRoot(appInstance));\n // also tell them our root project is under development. This can be\n // different, in the case where this is an addon and the app is the dummy\n // app.\n macrosConfig.enablePackageDevelopment(appInstance.project.root);\n // keep the macros in runtime mode for development & testing\n macrosConfig.enableRuntimeMode();\n }\n\n // add our babel plugin to our parent's babel\n this.installBabelPlugin(parent);\n\n // and to our own babel, because we may need to inline runtime config into\n // our source code\n this.installBabelPlugin(this);\n\n appInstance.import('vendor/embroider-macros-test-support.js', { type: 'test' });\n\n // When we're used inside the traditional ember-cli build pipeline without\n // Embroider, we unfortunately need to hook into here uncleanly because we\n // need to delineate the point in time after which writing macro config is\n // forbidden and consuming it becomes allowed. There's no existing hook with\n // that timing.\n const originalToTree = appInstance.toTree;\n appInstance.toTree = function (...args) {\n macrosConfig.finalize();\n return originalToTree.apply(appInstance, args);\n };\n },\n\n // Other addons are allowed to call this. It's needed if an addon needs to\n // emit code containing macros into that addon's parent (via a babel plugin,\n // for exmple). This is only an issue in classic builds, under embroider all\n // babel plugins should be thought of as *language extensions* that are\n // available everywhere, we don't scope them so narrowly so this probably\n // doesn't come up.\n installBabelPlugin(this: any, appOrAddonInstance: any) {\n let babelOptions = (appOrAddonInstance.options.babel = appOrAddonInstance.options.babel || {});\n let babelPlugins = (babelOptions.plugins = babelOptions.plugins || []);\n if (!babelPlugins.some(isEmbroiderMacrosPlugin)) {\n let appInstance = this._findHost();\n babelPlugins.unshift(...getMacrosConfig(appInstance).babelPluginConfig(appOrAddonInstance));\n }\n },\n\n setupPreprocessorRegistry(this: any, type: 'parent' | 'self', registry: any) {\n if (type === 'parent') {\n // the htmlbars-ast-plugins are split into two parts because order is\n // important. Weirdly, they appear to run in the reverse order that you\n // register them here.\n //\n // MacrosConfig.astPlugins is static because in classic ember-cli, at this\n // point there's not yet an appInstance, so we defer getting it and\n // calling setConfig until our included hook.\n let { plugins, setConfig, lazyParams } = MacrosConfig.astPlugins((this as any).parent.root);\n this.setMacrosConfig = setConfig;\n plugins.forEach((plugin, index) => {\n let name = `@embroider/macros/${index}`;\n let baseDir = join(__dirname, '..');\n\n let params: BuildPluginParams = {\n name,\n firstTransformParams: lazyParams,\n methodName: index === 0 ? 'makeSecondTransform' : 'makeFirstTransform',\n baseDir,\n };\n\n registry.add('htmlbars-ast-plugin', {\n name,\n plugin,\n parallelBabel: {\n requireFile: join(__dirname, 'glimmer', 'ast-transform.js'),\n buildUsing: 'buildPlugin',\n params,\n },\n baseDir: () => baseDir,\n });\n });\n }\n },\n\n options: {},\n};\n\n// this can differ from appInstance.project.root because Dummy apps are terrible\nfunction getAppRoot(appInstance: AppInstance): string {\n return join(appInstance.project.configPath(), '..', '..');\n}\n\nfunction getMacrosConfig(appInstance: AppInstance): MacrosConfig {\n let appRoot = join(appInstance.project.configPath(), '..', '..');\n return MacrosConfig.for(appInstance, appRoot);\n}\n"]}
@@ -1,10 +1,17 @@
1
- export declare function buildPlugin(params: {
1
+ export interface BuildPluginParams {
2
2
  name: string;
3
3
  baseDir: string;
4
- projectRoot: string;
5
4
  methodName: string;
6
- configs: any;
7
- }): {
5
+ firstTransformParams: FirstTransformParams;
6
+ }
7
+ export interface FirstTransformParams {
8
+ packageRoot: string | undefined;
9
+ appRoot: string;
10
+ configs: {
11
+ [packageRoot: string]: object;
12
+ };
13
+ }
14
+ export declare function buildPlugin(params: BuildPluginParams): {
8
15
  name: string;
9
16
  plugin: (env: {
10
17
  syntax: {
@@ -27,12 +34,7 @@ export declare function buildPlugin(params: {
27
34
  };
28
35
  baseDir: () => string;
29
36
  };
30
- export declare function makeFirstTransform(opts: {
31
- userConfigs: {
32
- [packageRoot: string]: unknown;
33
- };
34
- baseDir?: string;
35
- }): (env: {
37
+ export declare function makeFirstTransform(opts: FirstTransformParams): (env: {
36
38
  syntax: {
37
39
  builders: any;
38
40
  };
@@ -10,11 +10,12 @@ const dependency_satisfies_1 = __importDefault(require("./dependency-satisfies")
10
10
  const macro_maybe_attrs_1 = require("./macro-maybe-attrs");
11
11
  const macro_condition_1 = require("./macro-condition");
12
12
  const fail_build_1 = require("./fail-build");
13
+ const shared_internals_1 = require("@embroider/shared-internals");
13
14
  function buildPlugin(params) {
14
15
  return {
15
16
  name: params.name,
16
17
  plugin: params.methodName === 'makeFirstTransform'
17
- ? makeFirstTransform({ userConfigs: params.configs, baseDir: params.projectRoot })
18
+ ? makeFirstTransform(params.firstTransformParams)
18
19
  : makeSecondTransform(),
19
20
  baseDir: () => params.baseDir,
20
21
  };
@@ -22,13 +23,14 @@ function buildPlugin(params) {
22
23
  exports.buildPlugin = buildPlugin;
23
24
  function makeFirstTransform(opts) {
24
25
  function embroiderFirstMacrosTransform(env) {
25
- if (!opts.baseDir && !env.filename) {
26
- throw new Error(`bug in @embroider/macros. Running without baseDir but don't have filename.`);
26
+ if (!opts.packageRoot && !env.filename) {
27
+ throw new Error(`bug in @embroider/macros. Running without packageRoot but don't have filename.`);
27
28
  }
29
+ let packageCache = shared_internals_1.PackageCache.shared('embroider-stage3', opts.appRoot);
28
30
  let scopeStack = [];
29
- // baseDir is set when we run inside classic ember-cli. Otherwise we're in
31
+ // packageRoot is set when we run inside classic ember-cli. Otherwise we're in
30
32
  // Embroider, where we can use absolute filenames.
31
- const moduleName = opts.baseDir ? env.meta.moduleName : env.filename;
33
+ const moduleName = opts.packageRoot ? env.meta.moduleName : env.filename;
32
34
  return {
33
35
  name: '@embroider/macros/first',
34
36
  visitor: {
@@ -52,13 +54,13 @@ function makeFirstTransform(opts) {
52
54
  return;
53
55
  }
54
56
  if (node.path.original === 'macroGetOwnConfig') {
55
- return (0, literal_1.default)((0, get_config_1.default)(node, opts.userConfigs, opts.baseDir, moduleName, true), env.syntax.builders);
57
+ return (0, literal_1.default)((0, get_config_1.default)(node, opts.configs, opts.packageRoot, moduleName, true, packageCache), env.syntax.builders);
56
58
  }
57
59
  if (node.path.original === 'macroGetConfig') {
58
- return (0, literal_1.default)((0, get_config_1.default)(node, opts.userConfigs, opts.baseDir, moduleName, false), env.syntax.builders);
60
+ return (0, literal_1.default)((0, get_config_1.default)(node, opts.configs, opts.packageRoot, moduleName, false, packageCache), env.syntax.builders);
59
61
  }
60
62
  if (node.path.original === 'macroDependencySatisfies') {
61
- return (0, literal_1.default)((0, dependency_satisfies_1.default)(node, opts.baseDir, moduleName), env.syntax.builders);
63
+ return (0, literal_1.default)((0, dependency_satisfies_1.default)(node, opts.packageRoot, moduleName, packageCache), env.syntax.builders);
62
64
  }
63
65
  },
64
66
  MustacheStatement(node) {
@@ -69,13 +71,13 @@ function makeFirstTransform(opts) {
69
71
  return;
70
72
  }
71
73
  if (node.path.original === 'macroGetOwnConfig') {
72
- return env.syntax.builders.mustache((0, literal_1.default)((0, get_config_1.default)(node, opts.userConfigs, opts.baseDir, moduleName, true), env.syntax.builders));
74
+ return env.syntax.builders.mustache((0, literal_1.default)((0, get_config_1.default)(node, opts.configs, opts.packageRoot, moduleName, true, packageCache), env.syntax.builders));
73
75
  }
74
76
  if (node.path.original === 'macroGetConfig') {
75
- return env.syntax.builders.mustache((0, literal_1.default)((0, get_config_1.default)(node, opts.userConfigs, opts.baseDir, moduleName, false), env.syntax.builders));
77
+ return env.syntax.builders.mustache((0, literal_1.default)((0, get_config_1.default)(node, opts.configs, opts.packageRoot, moduleName, false, packageCache), env.syntax.builders));
76
78
  }
77
79
  if (node.path.original === 'macroDependencySatisfies') {
78
- return env.syntax.builders.mustache((0, literal_1.default)((0, dependency_satisfies_1.default)(node, opts.baseDir, moduleName), env.syntax.builders));
80
+ return env.syntax.builders.mustache((0, literal_1.default)((0, dependency_satisfies_1.default)(node, opts.packageRoot, moduleName, packageCache), env.syntax.builders));
79
81
  }
80
82
  },
81
83
  },
@@ -86,10 +88,7 @@ function makeFirstTransform(opts) {
86
88
  requireFile: __filename,
87
89
  buildUsing: 'makeFirstTransform',
88
90
  get params() {
89
- return {
90
- userConfigs: opts.userConfigs,
91
- baseDir: opts.baseDir,
92
- };
91
+ return opts;
93
92
  },
94
93
  };
95
94
  return embroiderFirstMacrosTransform;