@embroider/macros 1.21.1 → 1.21.2

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.
Files changed (80) hide show
  1. package/README.md +24 -0
  2. package/package.json +2 -2
  3. package/src/babel/app-ember-satisfies.d.ts +0 -5
  4. package/src/babel/app-ember-satisfies.js +0 -38
  5. package/src/babel/app-ember-satisfies.js.map +0 -1
  6. package/src/babel/dependency-satisfies.d.ts +0 -5
  7. package/src/babel/dependency-satisfies.js +0 -38
  8. package/src/babel/dependency-satisfies.js.map +0 -1
  9. package/src/babel/each.d.ts +0 -14
  10. package/src/babel/each.js +0 -55
  11. package/src/babel/each.js.map +0 -1
  12. package/src/babel/error.d.ts +0 -3
  13. package/src/babel/error.js +0 -21
  14. package/src/babel/error.js.map +0 -1
  15. package/src/babel/evaluate-json.d.ts +0 -36
  16. package/src/babel/evaluate-json.js +0 -429
  17. package/src/babel/evaluate-json.js.map +0 -1
  18. package/src/babel/fail-build.d.ts +0 -5
  19. package/src/babel/fail-build.js +0 -36
  20. package/src/babel/fail-build.js.map +0 -1
  21. package/src/babel/get-config.d.ts +0 -9
  22. package/src/babel/get-config.js +0 -107
  23. package/src/babel/get-config.js.map +0 -1
  24. package/src/babel/import-sync-pattern.d.ts +0 -22
  25. package/src/babel/import-sync-pattern.js +0 -151
  26. package/src/babel/import-sync-pattern.js.map +0 -1
  27. package/src/babel/macro-condition.d.ts +0 -12
  28. package/src/babel/macro-condition.js +0 -76
  29. package/src/babel/macro-condition.js.map +0 -1
  30. package/src/babel/macros-babel-plugin.d.ts +0 -3
  31. package/src/babel/macros-babel-plugin.js +0 -312
  32. package/src/babel/macros-babel-plugin.js.map +0 -1
  33. package/src/babel/module-exists.d.ts +0 -5
  34. package/src/babel/module-exists.js +0 -30
  35. package/src/babel/module-exists.js.map +0 -1
  36. package/src/babel/state.d.ts +0 -34
  37. package/src/babel/state.js +0 -64
  38. package/src/babel/state.js.map +0 -1
  39. package/src/babel.d.ts +0 -47
  40. package/src/babel.js +0 -36
  41. package/src/babel.js.map +0 -1
  42. package/src/ember-addon-main.d.ts +0 -9
  43. package/src/ember-addon-main.js +0 -112
  44. package/src/ember-addon-main.js.map +0 -1
  45. package/src/glimmer/app-ember-satisfies.d.ts +0 -3
  46. package/src/glimmer/app-ember-satisfies.js +0 -32
  47. package/src/glimmer/app-ember-satisfies.js.map +0 -1
  48. package/src/glimmer/ast-transform.d.ts +0 -133
  49. package/src/glimmer/ast-transform.js +0 -240
  50. package/src/glimmer/ast-transform.js.map +0 -1
  51. package/src/glimmer/dependency-satisfies.d.ts +0 -3
  52. package/src/glimmer/dependency-satisfies.js +0 -37
  53. package/src/glimmer/dependency-satisfies.js.map +0 -1
  54. package/src/glimmer/evaluate.d.ts +0 -7
  55. package/src/glimmer/evaluate.js +0 -16
  56. package/src/glimmer/evaluate.js.map +0 -1
  57. package/src/glimmer/fail-build.d.ts +0 -2
  58. package/src/glimmer/fail-build.js +0 -22
  59. package/src/glimmer/fail-build.js.map +0 -1
  60. package/src/glimmer/get-config.d.ts +0 -5
  61. package/src/glimmer/get-config.js +0 -38
  62. package/src/glimmer/get-config.js.map +0 -1
  63. package/src/glimmer/literal.d.ts +0 -2
  64. package/src/glimmer/literal.js +0 -28
  65. package/src/glimmer/literal.js.map +0 -1
  66. package/src/glimmer/macro-condition.d.ts +0 -7
  67. package/src/glimmer/macro-condition.js +0 -119
  68. package/src/glimmer/macro-condition.js.map +0 -1
  69. package/src/glimmer/macro-maybe-attrs.d.ts +0 -2
  70. package/src/glimmer/macro-maybe-attrs.js +0 -31
  71. package/src/glimmer/macro-maybe-attrs.js.map +0 -1
  72. package/src/index.d.ts +0 -59
  73. package/src/index.js +0 -80
  74. package/src/index.js.map +0 -1
  75. package/src/macros-config.d.ts +0 -52
  76. package/src/macros-config.js +0 -394
  77. package/src/macros-config.js.map +0 -1
  78. package/src/node.d.ts +0 -5
  79. package/src/node.js +0 -21
  80. package/src/node.js.map +0 -1
@@ -1,31 +0,0 @@
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.maybeAttrs = maybeAttrs;
7
- const evaluate_1 = __importDefault(require("./evaluate"));
8
- function maybeAttrs(elementNode, node, builders) {
9
- let [predicate, ...bareAttrs] = node.params;
10
- if (!predicate) {
11
- throw new Error(`macroMaybeAttrs requires at least one argument`);
12
- }
13
- let result = (0, evaluate_1.default)(predicate);
14
- if (!result.confident) {
15
- throw new Error(`first argument to macroMaybeAttrs must be statically analyzable`);
16
- }
17
- for (let bareAttr of bareAttrs) {
18
- if (bareAttr.type !== 'PathExpression') {
19
- throw new Error(`macroMaybeAttrs found a ${bareAttr.type} where it expected a PathExpression`);
20
- }
21
- }
22
- if (result.value) {
23
- for (let bareAttr of bareAttrs) {
24
- elementNode.attributes.push(builders.attr(bareAttr.original, builders.text(''), bareAttr.loc));
25
- }
26
- for (let attr of node.hash.pairs) {
27
- elementNode.attributes.push(builders.attr(attr.key, builders.mustache(attr.value), attr.loc));
28
- }
29
- }
30
- }
31
- //# sourceMappingURL=macro-maybe-attrs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"macro-maybe-attrs.js","sourceRoot":"","sources":["macro-maybe-attrs.ts"],"names":[],"mappings":";;;;;AAEA,gCA2BC;AA7BD,0DAAkC;AAElC,SAAgB,UAAU,CAAC,WAAgB,EAAE,IAAS,EAAE,QAAa;IACnE,IAAI,CAAC,SAAS,EAAE,GAAG,SAAS,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;IAE5C,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IACpE,CAAC;IAED,IAAI,MAAM,GAAG,IAAA,kBAAQ,EAAC,SAAS,CAAC,CAAC;IACjC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;IACrF,CAAC;IAED,KAAK,IAAI,QAAQ,IAAI,SAAS,EAAE,CAAC;QAC/B,IAAI,QAAQ,CAAC,IAAI,KAAK,gBAAgB,EAAE,CAAC;YACvC,MAAM,IAAI,KAAK,CAAC,2BAA2B,QAAQ,CAAC,IAAI,qCAAqC,CAAC,CAAC;QACjG,CAAC;IACH,CAAC;IAED,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;QACjB,KAAK,IAAI,QAAQ,IAAI,SAAS,EAAE,CAAC;YAC/B,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;QACjG,CAAC;QAED,KAAK,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;YACjC,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QAChG,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import evaluate from './evaluate';\n\nexport function maybeAttrs(elementNode: any, node: any, builders: any) {\n let [predicate, ...bareAttrs] = node.params;\n\n if (!predicate) {\n throw new Error(`macroMaybeAttrs requires at least one argument`);\n }\n\n let result = evaluate(predicate);\n if (!result.confident) {\n throw new Error(`first argument to macroMaybeAttrs must be statically analyzable`);\n }\n\n for (let bareAttr of bareAttrs) {\n if (bareAttr.type !== 'PathExpression') {\n throw new Error(`macroMaybeAttrs found a ${bareAttr.type} where it expected a PathExpression`);\n }\n }\n\n if (result.value) {\n for (let bareAttr of bareAttrs) {\n elementNode.attributes.push(builders.attr(bareAttr.original, builders.text(''), bareAttr.loc));\n }\n\n for (let attr of node.hash.pairs) {\n elementNode.attributes.push(builders.attr(attr.key, builders.mustache(attr.value), attr.loc));\n }\n }\n}\n"]}
package/src/index.d.ts DELETED
@@ -1,59 +0,0 @@
1
- export declare function dependencySatisfies(packageName: string, semverRange: string): boolean;
2
- export declare function appEmberSatisfies(semverRange: string): boolean;
3
- export declare function macroCondition(predicate: boolean): boolean;
4
- export declare function each<T>(array: T[]): T[];
5
- export declare function importSync(specifier: string): unknown;
6
- export declare function getConfig<T>(packageName: string): T;
7
- export declare function getOwnConfig<T>(): T;
8
- export declare function getGlobalConfig<T>(): T;
9
- export declare function isDevelopingApp(): boolean;
10
- export declare function isTesting(): boolean;
11
- export declare function setTesting(value: boolean): boolean;
12
- export declare function failBuild(message: string): void;
13
- export declare function moduleExists(packageName: string): boolean;
14
- import type { HelperLike } from '@glint/template';
15
- export interface EmbroiderMacrosRegistry {
16
- macroGetOwnConfig: HelperLike<{
17
- Args: {
18
- Positional: [...keys: string[]];
19
- };
20
- Return: ReturnType<typeof getOwnConfig>;
21
- }>;
22
- macroGetConfig: HelperLike<{
23
- Args: {
24
- Positional: [packageName: string, ...keys: string[]];
25
- };
26
- Return: ReturnType<typeof getConfig>;
27
- }>;
28
- macroCondition: HelperLike<{
29
- Args: {
30
- Positional: [predicate: boolean];
31
- };
32
- Return: boolean;
33
- }>;
34
- macroDependencySatisfies: HelperLike<{
35
- Args: {
36
- Positional: Parameters<typeof dependencySatisfies>;
37
- };
38
- Return: ReturnType<typeof dependencySatisfies>;
39
- }>;
40
- macroAppEmberSatisfies: HelperLike<{
41
- Args: {
42
- Positional: Parameters<typeof appEmberSatisfies>;
43
- };
44
- Return: ReturnType<typeof appEmberSatisfies>;
45
- }>;
46
- macroMaybeAttrs: HelperLike<{
47
- Args: {
48
- Positional: [predicate: boolean, ...bareAttrs: unknown[]];
49
- };
50
- Return: void;
51
- }>;
52
- macroFailBuild: HelperLike<{
53
- Args: {
54
- Positional: Parameters<typeof failBuild>;
55
- };
56
- Return: ReturnType<typeof failBuild>;
57
- }>;
58
- }
59
- //# sourceMappingURL=index.d.ts.map
package/src/index.js DELETED
@@ -1,80 +0,0 @@
1
- "use strict";
2
- /* Macro Type Signatures */
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.dependencySatisfies = dependencySatisfies;
5
- exports.appEmberSatisfies = appEmberSatisfies;
6
- exports.macroCondition = macroCondition;
7
- exports.each = each;
8
- exports.importSync = importSync;
9
- exports.getConfig = getConfig;
10
- exports.getOwnConfig = getOwnConfig;
11
- exports.getGlobalConfig = getGlobalConfig;
12
- exports.isDevelopingApp = isDevelopingApp;
13
- exports.isTesting = isTesting;
14
- exports.setTesting = setTesting;
15
- exports.failBuild = failBuild;
16
- exports.moduleExists = moduleExists;
17
- /*
18
- CAUTION: this code is not necessarily what you are actually running. In
19
- general, the macros are implemented at build time using babel, and so calls to
20
- these functions get compiled away before they ever run. However, this code is
21
- here because it provides types to typescript users of the macros.
22
-
23
- Some macros also have runtime implementations that are useful in development
24
- mode, in addition to their build-time implementations in babel. You can find
25
- the runtime implementations in runtime.ts.
26
-
27
- Having a runtime mode lets us do things like produce a single build in
28
- development that works for both fastboot and browser, using the macros to
29
- switch between modes. For production, you would switch to the build-time macro
30
- implementation to get two optimized builds instead.
31
- */
32
- function dependencySatisfies(packageName, semverRange) {
33
- throw new Oops(packageName, semverRange);
34
- }
35
- function appEmberSatisfies(semverRange) {
36
- throw new Oops(semverRange);
37
- }
38
- function macroCondition(predicate) {
39
- throw new Oops(predicate);
40
- }
41
- function each(array) {
42
- throw new Oops(array);
43
- }
44
- // We would prefer to write:
45
- // export function importSync<T extends string>(specifier: T): typeof import(T) {
46
- // but TS doesn't seem to support that at present.
47
- function importSync(specifier) {
48
- throw new Oops(specifier);
49
- }
50
- function getConfig(packageName) {
51
- throw new Oops(packageName);
52
- }
53
- function getOwnConfig() {
54
- throw new Oops();
55
- }
56
- function getGlobalConfig() {
57
- throw new Oops();
58
- }
59
- function isDevelopingApp() {
60
- throw new Oops();
61
- }
62
- function isTesting() {
63
- throw new Oops();
64
- }
65
- function setTesting(value) {
66
- throw new Oops(value);
67
- }
68
- function failBuild(message) {
69
- throw new Oops(message);
70
- }
71
- function moduleExists(packageName) {
72
- throw new Oops(packageName);
73
- }
74
- class Oops extends Error {
75
- constructor(...params) {
76
- super(`this method is really implemented at compile time via a babel plugin. If you're seeing this exception, something went wrong`);
77
- this.params = params;
78
- }
79
- }
80
- //# sourceMappingURL=index.js.map
package/src/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA,2BAA2B;;AAkB3B,kDAEC;AAED,8CAEC;AAED,wCAEC;AAED,oBAEC;AAKD,gCAEC;AAED,8BAEC;AAED,oCAEC;AAED,0CAEC;AAED,0CAEC;AAED,8BAEC;AAED,gCAEC;AAED,8BAEC;AAED,oCAEC;AArED;;;;;;;;;;;;;;EAcE;AAEF,SAAgB,mBAAmB,CAAC,WAAmB,EAAE,WAAmB;IAC1E,MAAM,IAAI,IAAI,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AAC3C,CAAC;AAED,SAAgB,iBAAiB,CAAC,WAAmB;IACnD,MAAM,IAAI,IAAI,CAAC,WAAW,CAAC,CAAC;AAC9B,CAAC;AAED,SAAgB,cAAc,CAAC,SAAkB;IAC/C,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC;AAC5B,CAAC;AAED,SAAgB,IAAI,CAAI,KAAU;IAChC,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;AACxB,CAAC;AAED,4BAA4B;AAC5B,mFAAmF;AACnF,kDAAkD;AAClD,SAAgB,UAAU,CAAC,SAAiB;IAC1C,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC;AAC5B,CAAC;AAED,SAAgB,SAAS,CAAI,WAAmB;IAC9C,MAAM,IAAI,IAAI,CAAC,WAAW,CAAC,CAAC;AAC9B,CAAC;AAED,SAAgB,YAAY;IAC1B,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAED,SAAgB,eAAe;IAC7B,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAED,SAAgB,eAAe;IAC7B,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAED,SAAgB,SAAS;IACvB,MAAM,IAAI,IAAI,EAAE,CAAC;AACnB,CAAC;AAED,SAAgB,UAAU,CAAC,KAAc;IACvC,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;AACxB,CAAC;AAED,SAAgB,SAAS,CAAC,OAAe;IACvC,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC;AAC1B,CAAC;AAED,SAAgB,YAAY,CAAC,WAAmB;IAC9C,MAAM,IAAI,IAAI,CAAC,WAAW,CAAC,CAAC;AAC9B,CAAC;AAED,MAAM,IAAK,SAAQ,KAAK;IAEtB,YAAY,GAAG,MAAa;QAC1B,KAAK,CACH,6HAA6H,CAC9H,CAAC;QACF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF","sourcesContent":["/* Macro Type Signatures */\n\n/*\n CAUTION: this code is not necessarily what you are actually running. In\n general, the macros are implemented at build time using babel, and so calls to\n these functions get compiled away before they ever run. However, this code is\n here because it provides types to typescript users of the macros.\n\n Some macros also have runtime implementations that are useful in development\n mode, in addition to their build-time implementations in babel. You can find\n the runtime implementations in runtime.ts.\n\n Having a runtime mode lets us do things like produce a single build in\n development that works for both fastboot and browser, using the macros to\n switch between modes. For production, you would switch to the build-time macro\n implementation to get two optimized builds instead.\n*/\n\nexport function dependencySatisfies(packageName: string, semverRange: string): boolean {\n throw new Oops(packageName, semverRange);\n}\n\nexport function appEmberSatisfies(semverRange: string): boolean {\n throw new Oops(semverRange);\n}\n\nexport function macroCondition(predicate: boolean): boolean {\n throw new Oops(predicate);\n}\n\nexport function each<T>(array: T[]): T[] {\n throw new Oops(array);\n}\n\n// We would prefer to write:\n// export function importSync<T extends string>(specifier: T): typeof import(T) {\n// but TS doesn't seem to support that at present.\nexport function importSync(specifier: string): unknown {\n throw new Oops(specifier);\n}\n\nexport function getConfig<T>(packageName: string): T {\n throw new Oops(packageName);\n}\n\nexport function getOwnConfig<T>(): T {\n throw new Oops();\n}\n\nexport function getGlobalConfig<T>(): T {\n throw new Oops();\n}\n\nexport function isDevelopingApp(): boolean {\n throw new Oops();\n}\n\nexport function isTesting(): boolean {\n throw new Oops();\n}\n\nexport function setTesting(value: boolean): boolean {\n throw new Oops(value);\n}\n\nexport function failBuild(message: string): void {\n throw new Oops(message);\n}\n\nexport function moduleExists(packageName: string): boolean {\n throw new Oops(packageName);\n}\n\nclass Oops extends Error {\n params: any[];\n constructor(...params: any[]) {\n super(\n `this method is really implemented at compile time via a babel plugin. If you're seeing this exception, something went wrong`\n );\n this.params = params;\n }\n}\n\nimport type { HelperLike } from '@glint/template';\n\nexport interface EmbroiderMacrosRegistry {\n macroGetOwnConfig: HelperLike<{\n Args: { Positional: [...keys: string[]] };\n Return: ReturnType<typeof getOwnConfig>;\n }>;\n macroGetConfig: HelperLike<{\n Args: { Positional: [packageName: string, ...keys: string[]] };\n Return: ReturnType<typeof getConfig>;\n }>;\n macroCondition: HelperLike<{\n Args: { Positional: [predicate: boolean] };\n Return: boolean;\n }>;\n macroDependencySatisfies: HelperLike<{\n Args: { Positional: Parameters<typeof dependencySatisfies> };\n Return: ReturnType<typeof dependencySatisfies>;\n }>;\n macroAppEmberSatisfies: HelperLike<{\n Args: { Positional: Parameters<typeof appEmberSatisfies> };\n Return: ReturnType<typeof appEmberSatisfies>;\n }>;\n macroMaybeAttrs: HelperLike<{\n Args: { Positional: [predicate: boolean, ...bareAttrs: unknown[]] };\n Return: void;\n }>;\n macroFailBuild: HelperLike<{\n Args: { Positional: Parameters<typeof failBuild> };\n Return: ReturnType<typeof failBuild>;\n }>;\n}\n"]}
@@ -1,52 +0,0 @@
1
- import type { PluginItem } from '@babel/core';
2
- import type { FirstTransformParams } from './glimmer/ast-transform';
3
- export type SourceOfConfig = (config: object) => {
4
- readonly name: string;
5
- readonly root: string;
6
- readonly version: string;
7
- };
8
- export type Merger = (configs: object[], params: {
9
- sourceOfConfig: SourceOfConfig;
10
- }) => object;
11
- export default class MacrosConfig {
12
- private origAppRoot;
13
- static for(key: any, appRoot: string): MacrosConfig;
14
- private mode;
15
- private globalConfig;
16
- private isDevelopingPackageRoots;
17
- enableRuntimeMode(): void;
18
- enablePackageDevelopment(packageRoot: string): void;
19
- private _importSyncImplementation;
20
- get importSyncImplementation(): "cjs" | "eager";
21
- set importSyncImplementation(value: 'cjs' | 'eager');
22
- private constructor();
23
- private get packageCache();
24
- private get appRoot();
25
- private _configWritable;
26
- private configs;
27
- private configSources;
28
- private mergers;
29
- setConfig(fromPath: string, packageName: string, config: object): void;
30
- setOwnConfig(fromPath: string, config: object): void;
31
- setGlobalConfig(fromPath: string, key: string, value: object): void;
32
- private internalSetConfig;
33
- useMerger(fromPath: string, merger: Merger): void;
34
- private cachedUserConfigs;
35
- private get userConfigs();
36
- private makeConfigSourcer;
37
- babelPluginConfig(appOrAddonInstance?: any): PluginItem[];
38
- static astPlugins(owningPackageRoot?: string): {
39
- plugins: Function[];
40
- setConfig: (config: MacrosConfig) => void;
41
- lazyParams: FirstTransformParams;
42
- };
43
- static transforms(owningPackageRoot?: string): {
44
- plugins: Function[];
45
- setConfig: (config: MacrosConfig) => void;
46
- lazyParams: FirstTransformParams;
47
- };
48
- private mergerFor;
49
- private resolvePackage;
50
- finalize(): void;
51
- }
52
- //# sourceMappingURL=macros-config.d.ts.map
@@ -1,394 +0,0 @@
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
- const fs_1 = __importDefault(require("fs"));
7
- const path_1 = require("path");
8
- const crypto_1 = __importDefault(require("crypto"));
9
- const find_up_1 = __importDefault(require("find-up"));
10
- const shared_internals_1 = require("@embroider/shared-internals");
11
- const ast_transform_1 = require("./glimmer/ast-transform");
12
- const partition_1 = __importDefault(require("lodash/partition"));
13
- // this is a module-scoped cache. If multiple callers ask _this copy_ of
14
- // @embroider/macros for a shared MacrosConfig, they'll all get the same one.
15
- // And if somebody asks a *different* copy of @embroider/macros for the shared
16
- // MacrosConfig, it will have its own instance with its own code, but will still
17
- // share the GlobalSharedState beneath.
18
- let localSharedState = new WeakMap();
19
- function gatherAddonCacheKeyWorker(item, memo) {
20
- item.addons.forEach((addon) => {
21
- let key = `${addon.pkg.name}@${addon.pkg.version}`;
22
- memo.add(key);
23
- gatherAddonCacheKeyWorker(addon, memo);
24
- });
25
- }
26
- let addonCacheKey = new WeakMap();
27
- // creates a string representing all addons and their versions
28
- // (foo@1.0.0|bar@2.0.0) to use as a cachekey
29
- function gatherAddonCacheKey(project) {
30
- let cacheKey = addonCacheKey.get(project);
31
- if (cacheKey) {
32
- return cacheKey;
33
- }
34
- let memo = new Set();
35
- project.addons.forEach((addon) => {
36
- let key = `${addon.pkg.name}@${addon.pkg.version}`;
37
- memo.add(key);
38
- gatherAddonCacheKeyWorker(addon, memo);
39
- });
40
- cacheKey = Array.from(memo).join('|');
41
- addonCacheKey.set(project, cacheKey);
42
- return cacheKey;
43
- }
44
- const babelCacheBustingPluginPath = require.resolve('@embroider/shared-internals/src/babel-plugin-cache-busting');
45
- class MacrosConfig {
46
- static for(key, appRoot) {
47
- let found = localSharedState.get(key);
48
- if (found) {
49
- return found;
50
- }
51
- let g = global;
52
- if (!g.__embroider_macros_global__) {
53
- g.__embroider_macros_global__ = new WeakMap();
54
- }
55
- let shared = g.__embroider_macros_global__.get(key);
56
- if (shared) {
57
- // if an earlier version of @embroider/macros created the shared state, it
58
- // would have configSources.
59
- if (!shared.configSources) {
60
- shared.configSources = new WeakMap();
61
- }
62
- // earlier versions did not include this -- we may need to upgrade the
63
- // format here
64
- if (!shared.globalConfigs) {
65
- shared.globalConfigs = {};
66
- }
67
- }
68
- else {
69
- shared = {
70
- configs: new Map(),
71
- globalConfigs: {},
72
- configSources: new WeakMap(),
73
- mergers: new Map(),
74
- };
75
- g.__embroider_macros_global__.set(key, shared);
76
- }
77
- let config = new MacrosConfig(appRoot, shared);
78
- localSharedState.set(key, config);
79
- return config;
80
- }
81
- enableRuntimeMode() {
82
- if (this.mode !== 'run-time') {
83
- if (!this._configWritable) {
84
- throw new Error(`[Embroider:MacrosConfig] attempted to enableRuntimeMode after configs have been finalized`);
85
- }
86
- this.mode = 'run-time';
87
- }
88
- }
89
- enablePackageDevelopment(packageRoot) {
90
- if (!this.isDevelopingPackageRoots.has(packageRoot)) {
91
- if (!this._configWritable) {
92
- throw new Error(`[Embroider:MacrosConfig] attempted to enablePackageDevelopment after configs have been finalized`);
93
- }
94
- this.isDevelopingPackageRoots.add(packageRoot);
95
- }
96
- }
97
- get importSyncImplementation() {
98
- return this._importSyncImplementation;
99
- }
100
- set importSyncImplementation(value) {
101
- if (!this._configWritable) {
102
- throw new Error(`[Embroider:MacrosConfig] attempted to set importSyncImplementation after configs have been finalized`);
103
- }
104
- this._importSyncImplementation = value;
105
- }
106
- constructor(origAppRoot, shared) {
107
- this.origAppRoot = origAppRoot;
108
- this.mode = 'compile-time';
109
- this.isDevelopingPackageRoots = new Set();
110
- this._importSyncImplementation = 'cjs';
111
- this._configWritable = true;
112
- this.configs = shared.configs;
113
- this.globalConfig = shared.globalConfigs;
114
- this.configSources = shared.configSources;
115
- this.mergers = shared.mergers;
116
- // this uses globalConfig because these things truly are global. Even if a
117
- // package doesn't have a dep or peerDep on @embroider/macros, it's legit
118
- // for them to want to know the answer to these questions, and there is only
119
- // one answer throughout the whole dependency graph.
120
- this.globalConfig['@embroider/macros'] = {
121
- // this powers the `isTesting` macro. It always starts out false here,
122
- // because:
123
- // - if this is a production build, we will evaluate all macros at build
124
- // time and isTesting will stay false, so test-only code will not be
125
- // included.
126
- // - if this is a dev build, we evaluate macros at runtime, which allows
127
- // both "I'm running my app in development" and "I'm running my test
128
- // suite" to coexist within a single build. When you run the test
129
- // suite, early in the runtime boot process we can flip isTesting to
130
- // true to distinguish the two.
131
- isTesting: false,
132
- };
133
- }
134
- get packageCache() {
135
- return shared_internals_1.RewrittenPackageCache.shared('embroider', this.origAppRoot);
136
- }
137
- get appRoot() {
138
- return this.origAppRoot;
139
- }
140
- // Registers a new source of configuration to be given to the named package.
141
- // Your config type must be json-serializable. You must always set fromPath to
142
- // `__filename`.
143
- setConfig(fromPath, packageName, config) {
144
- return this.internalSetConfig(fromPath, packageName, config);
145
- }
146
- // Registers a new source of configuration to be given to your own package.
147
- // Your config type must be json-serializable. You must always set fromPath to
148
- // `__filename`.
149
- setOwnConfig(fromPath, config) {
150
- return this.internalSetConfig(fromPath, undefined, config);
151
- }
152
- // Registers a new source of configuration to be shared globally within the
153
- // app. USE GLOBALS SPARINGLY! Prefer setConfig or setOwnConfig instead,
154
- // unless your state is truly, necessarily global.
155
- //
156
- // Include a relevant package name in your key to help avoid collisions.
157
- //
158
- // Your value must be json-serializable. You must always set fromPath to
159
- // `__filename`.
160
- setGlobalConfig(fromPath, key, value) {
161
- if (!this._configWritable) {
162
- throw new Error(`[Embroider:MacrosConfig] attempted to set global config after configs have been finalized from: '${fromPath}'`);
163
- }
164
- this.globalConfig[key] = value;
165
- }
166
- internalSetConfig(fromPath, packageName, config) {
167
- if (!this._configWritable) {
168
- throw new Error(`[Embroider:MacrosConfig] attempted to set config after configs have been finalized from: '${fromPath}'`);
169
- }
170
- if (!isSerializable(config)) {
171
- throw new Error(`[Embroider:MacrosConfig] the given config from '${fromPath}' for packageName '${packageName}' is not JSON serializable.`);
172
- }
173
- let targetPackage = this.resolvePackage(fromPath, packageName);
174
- let peers = (0, shared_internals_1.getOrCreate)(this.configs, targetPackage.root, () => []);
175
- peers.push(config);
176
- this.configSources.set(config, fromPath);
177
- }
178
- // Allows you to set the merging strategy used for your package's config. The
179
- // merging strategy applies when multiple other packages all try to send
180
- // configuration to you.
181
- useMerger(fromPath, merger) {
182
- if (this._configWritable) {
183
- throw new Error(`[Embroider:MacrosConfig] attempted to call useMerger after configs have been finalized`);
184
- }
185
- let targetPackage = this.resolvePackage(fromPath, undefined);
186
- let other = this.mergers.get(targetPackage.root);
187
- if (other) {
188
- throw new Error(`[Embroider:MacrosConfig] conflicting mergers registered for package ${targetPackage.name} at ${targetPackage.root}. See ${other.fromPath} and ${fromPath}.`);
189
- }
190
- this.mergers.set(targetPackage.root, { merger, fromPath });
191
- }
192
- get userConfigs() {
193
- if (this._configWritable) {
194
- throw new Error('[Embroider:MacrosConfig] cannot read userConfigs until MacrosConfig has been finalized.');
195
- }
196
- if (!this.cachedUserConfigs) {
197
- let userConfigs = {};
198
- let sourceOfConfig = this.makeConfigSourcer(this.configSources);
199
- for (let [pkgRoot, configs] of this.configs) {
200
- let combined;
201
- if (configs.length > 1) {
202
- combined = this.mergerFor(pkgRoot)(configs, { sourceOfConfig });
203
- }
204
- else {
205
- combined = configs[0];
206
- }
207
- userConfigs[pkgRoot] = combined;
208
- }
209
- this.cachedUserConfigs = userConfigs;
210
- }
211
- return this.cachedUserConfigs;
212
- }
213
- makeConfigSourcer(configSources) {
214
- return config => {
215
- let fromPath = configSources.get(config);
216
- if (!fromPath) {
217
- throw new Error(`unknown object passed to sourceOfConfig(). You can only pass back the configs you were given.`);
218
- }
219
- let maybePkg = this.packageCache.ownerOfFile(fromPath);
220
- if (!maybePkg) {
221
- throw new Error(`bug: unexpected error, we always check that fromPath is owned during internalSetConfig so this should never happen`);
222
- }
223
- // our configs all deal in the original locations of packages, even if
224
- // embroider is rewriting some of them
225
- let pkg = this.packageCache.original(maybePkg);
226
- return {
227
- get name() {
228
- return pkg.name;
229
- },
230
- get version() {
231
- return pkg.version;
232
- },
233
- get root() {
234
- return pkg.root;
235
- },
236
- };
237
- };
238
- }
239
- // to be called from within your build system. Returns the thing you should
240
- // push into your babel plugins list.
241
- //
242
- // owningPackageRoot is needed when the files you will process (1) all belongs
243
- // to one package, (2) will not be located in globally correct paths such that
244
- // normal node_modules resolution can find their dependencies. In other words,
245
- // owningPackageRoot is needed when you use this inside classic ember-cli, and
246
- // it's not appropriate inside embroider.
247
- babelPluginConfig(appOrAddonInstance) {
248
- let self = this;
249
- let owningPackageRoot = appOrAddonInstance ? appOrAddonInstance.root || appOrAddonInstance.project.root : null;
250
- let opts = {
251
- // this is deliberately lazy because we want to allow everyone to finish
252
- // setting config before we generate the userConfigs
253
- get userConfigs() {
254
- return self.userConfigs;
255
- },
256
- get globalConfig() {
257
- return self.globalConfig;
258
- },
259
- owningPackageRoot,
260
- isDevelopingPackageRoots: [...this.isDevelopingPackageRoots],
261
- get appPackageRoot() {
262
- return self.appRoot;
263
- },
264
- // This is used as a signature so we can detect ourself among the plugins
265
- // emitted from v1 addons.
266
- embroiderMacrosConfigMarker: true,
267
- get mode() {
268
- return self.mode;
269
- },
270
- importSyncImplementation: this.importSyncImplementation,
271
- };
272
- let lockFilePath = find_up_1.default.sync(['yarn.lock', 'package-lock.json', 'pnpm-lock.yaml'], { cwd: self.appRoot });
273
- if (!lockFilePath) {
274
- lockFilePath = find_up_1.default.sync('package.json', { cwd: opts.appPackageRoot });
275
- }
276
- let lockFileBuffer = lockFilePath ? fs_1.default.readFileSync(lockFilePath) : 'no-cache-key';
277
- // @embroider/macros provides a macro called dependencySatisfies which checks if a given
278
- // package name satisfies a given semver version range. Due to the way babel caches this can
279
- // cause a problem where the macro plugin does not run (because it has been cached) but the version
280
- // of the dependency being checked for changes (due to installing a different version). This will lead to
281
- // the old evaluated state being used which might be invalid. This cache busting plugin keeps track of a
282
- // hash representing the lock file of the app and if it ever changes forces babel to rerun its plugins.
283
- // more information in issue #906
284
- let hash = crypto_1.default.createHash('sha256');
285
- hash = hash.update(lockFileBuffer);
286
- if (appOrAddonInstance) {
287
- // ensure that the actual running addon names and versions are accounted
288
- // for in the cache key; this ensures that we still invalidate the cache
289
- // when linking another project (e.g. ember-source) which would normally
290
- // not cause the lockfile to change;
291
- hash = hash.update(gatherAddonCacheKey(appOrAddonInstance.project));
292
- }
293
- let cacheKey = hash.digest('hex');
294
- return [
295
- [(0, path_1.join)(__dirname, 'babel', 'macros-babel-plugin.js'), opts],
296
- [babelCacheBustingPluginPath, { version: cacheKey }, `@embroider/macros cache buster: ${owningPackageRoot}`],
297
- ];
298
- }
299
- // provides the ast plugins that implement the macro system, in reverse order
300
- // for compatibility with the classic build, which historically always ran ast
301
- // plugins in backwards order.
302
- static astPlugins(owningPackageRoot) {
303
- let result = this.transforms(owningPackageRoot);
304
- result.plugins.reverse();
305
- return result;
306
- }
307
- // todo: type adjuments here
308
- // provides the ast plugins that implement the macro system
309
- static transforms(owningPackageRoot) {
310
- let configs;
311
- let lazyParams = {
312
- // this is deliberately lazy because we want to allow everyone to finish
313
- // setting config before we generate the userConfigs
314
- get configs() {
315
- if (!configs) {
316
- throw new Error(`Bug: @embroider/macros ast-transforms were not plugged into a MacrosConfig`);
317
- }
318
- return configs.userConfigs;
319
- },
320
- packageRoot: owningPackageRoot,
321
- get appRoot() {
322
- if (!configs) {
323
- throw new Error(`Bug: @embroider/macros ast-transforms were not plugged into a MacrosConfig`);
324
- }
325
- return configs.appRoot;
326
- },
327
- };
328
- let plugins = [(0, ast_transform_1.makeFirstTransform)(lazyParams), (0, ast_transform_1.makeSecondTransform)()];
329
- function setConfig(c) {
330
- configs = c;
331
- }
332
- return { plugins, setConfig, lazyParams };
333
- }
334
- mergerFor(pkgRoot) {
335
- let entry = this.mergers.get(pkgRoot);
336
- if (entry) {
337
- return entry.merger;
338
- }
339
- return defaultMergerFor(pkgRoot);
340
- }
341
- resolvePackage(fromPath, packageName) {
342
- let us = this.packageCache.ownerOfFile(fromPath);
343
- if (!us) {
344
- throw new Error(`[Embroider:MacrosConfig] unable to determine which npm package owns the file ${fromPath}`);
345
- }
346
- if (packageName) {
347
- let target = this.packageCache.resolve(packageName, us);
348
- return this.packageCache.original(target);
349
- }
350
- else {
351
- return this.packageCache.original(us);
352
- }
353
- }
354
- finalize() {
355
- this._configWritable = false;
356
- }
357
- }
358
- exports.default = MacrosConfig;
359
- function defaultMergerFor(pkgRoot) {
360
- return function defaultMerger(configs, { sourceOfConfig }) {
361
- let [ownConfigs, otherConfigs] = (0, partition_1.default)(configs, c => sourceOfConfig(c).root === pkgRoot);
362
- return Object.assign({}, ...ownConfigs, ...otherConfigs);
363
- };
364
- }
365
- function isSerializable(obj) {
366
- if (isScalar(obj)) {
367
- return true;
368
- }
369
- if (Array.isArray(obj)) {
370
- return !obj.some((arrayItem) => !isSerializable(arrayItem));
371
- }
372
- if (isPlainObject(obj)) {
373
- for (let property in obj) {
374
- let value = obj[property];
375
- if (!isSerializable(value)) {
376
- return false;
377
- }
378
- }
379
- return true;
380
- }
381
- console.error('non serializable item found in config:', obj);
382
- return false;
383
- }
384
- function isScalar(val) {
385
- return (typeof val === 'undefined' ||
386
- typeof val === 'string' ||
387
- typeof val === 'boolean' ||
388
- typeof val === 'number' ||
389
- val === null);
390
- }
391
- function isPlainObject(obj) {
392
- return typeof obj === 'object' && obj.constructor === Object && obj.toString() === '[object Object]';
393
- }
394
- //# sourceMappingURL=macros-config.js.map