@nx/js 21.2.0-beta.4 → 21.2.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/js",
3
- "version": "21.2.0-beta.4",
3
+ "version": "21.2.0-beta.5",
4
4
  "private": false,
5
5
  "description": "The JS plugin for Nx contains executors and generators that provide the best experience for developing JavaScript and TypeScript projects. ",
6
6
  "repository": {
@@ -39,8 +39,8 @@
39
39
  "@babel/preset-env": "^7.23.2",
40
40
  "@babel/preset-typescript": "^7.22.5",
41
41
  "@babel/runtime": "^7.22.6",
42
- "@nx/devkit": "21.2.0-beta.4",
43
- "@nx/workspace": "21.2.0-beta.4",
42
+ "@nx/devkit": "21.2.0-beta.5",
43
+ "@nx/workspace": "21.2.0-beta.5",
44
44
  "@zkochan/js-yaml": "0.0.7",
45
45
  "babel-plugin-const-enum": "^1.0.1",
46
46
  "babel-plugin-macros": "^3.1.0",
@@ -0,0 +1 @@
1
+ export declare const afterDeclarations: (options: any, program: any) => () => void;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.afterDeclarations = void 0;
4
+ const afterDeclarations = (options, program) => {
5
+ return () => { }; // Mock transformer factory
6
+ };
7
+ exports.afterDeclarations = afterDeclarations;
@@ -0,0 +1 @@
1
+ export declare const after: (options: any, program: any) => () => void;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.after = void 0;
4
+ const after = (options, program) => {
5
+ return () => { }; // Mock transformer factory
6
+ };
7
+ exports.after = after;
@@ -0,0 +1,2 @@
1
+ declare const _default: (options: any, program: any) => () => void;
2
+ export default _default;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (options, program) => {
4
+ return () => { };
5
+ };
@@ -0,0 +1,3 @@
1
+ export declare const before: (options: any, program: any) => () => void;
2
+ export declare const after: (options: any, program: any) => () => void;
3
+ export declare const afterDeclarations: (options: any, program: any) => () => void;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.afterDeclarations = exports.after = exports.before = void 0;
4
+ const before = (options, program) => {
5
+ return () => { }; // Mock transformer factory
6
+ };
7
+ exports.before = before;
8
+ const after = (options, program) => {
9
+ return () => { }; // Mock transformer factory
10
+ };
11
+ exports.after = after;
12
+ const afterDeclarations = (options, program) => {
13
+ return () => { }; // Mock transformer factory
14
+ };
15
+ exports.afterDeclarations = afterDeclarations;
@@ -1 +1 @@
1
- export declare const before: () => void;
1
+ export declare const before: (options: any, program: any) => () => void;
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.before = void 0;
4
- const before = () => { };
4
+ const before = (options, program) => {
5
+ return () => { }; // Mock transformer factory
6
+ };
5
7
  exports.before = before;
@@ -1 +1 @@
1
- export declare const after: () => void;
1
+ export declare const after: (options: any, program: any) => () => void;
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.after = void 0;
4
- const after = () => { };
4
+ const after = (options, program) => {
5
+ return () => { }; // Mock transformer factory
6
+ };
5
7
  exports.after = after;
@@ -3,6 +3,54 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.loadTsTransformers = loadTsTransformers;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  const path_1 = require("path");
6
+ var TransformerFormat;
7
+ (function (TransformerFormat) {
8
+ TransformerFormat[TransformerFormat["STANDARD"] = 0] = "STANDARD";
9
+ TransformerFormat[TransformerFormat["FUNCTION_EXPORT"] = 1] = "FUNCTION_EXPORT";
10
+ TransformerFormat[TransformerFormat["UNKNOWN"] = 2] = "UNKNOWN";
11
+ })(TransformerFormat || (TransformerFormat = {}));
12
+ function detectTransformerFormat(plugin) {
13
+ // Check if it's a standard Nx/TypeScript transformer plugin
14
+ if (plugin && (plugin.before || plugin.after || plugin.afterDeclarations)) {
15
+ return TransformerFormat.STANDARD;
16
+ }
17
+ // Check if it's a function-based transformer (exports a function directly)
18
+ if (typeof plugin === 'function') {
19
+ return TransformerFormat.FUNCTION_EXPORT;
20
+ }
21
+ // Check if it has a function export (function-based plugin pattern)
22
+ if (plugin &&
23
+ (typeof plugin.before === 'function' ||
24
+ typeof plugin.after === 'function' ||
25
+ typeof plugin.afterDeclarations === 'function')) {
26
+ return TransformerFormat.FUNCTION_EXPORT;
27
+ }
28
+ return TransformerFormat.UNKNOWN;
29
+ }
30
+ function adaptFunctionBasedTransformer(plugin, pluginOptions) {
31
+ // Handle direct function export
32
+ if (typeof plugin === 'function') {
33
+ return {
34
+ before: (options, program) => plugin(options, program),
35
+ };
36
+ }
37
+ // Handle object with function exports - adapt all available hooks
38
+ if (plugin && typeof plugin === 'object') {
39
+ const adapted = {};
40
+ if (typeof plugin.before === 'function') {
41
+ adapted.before = (options, program) => plugin.before(options, program);
42
+ }
43
+ if (typeof plugin.after === 'function') {
44
+ adapted.after = (options, program) => plugin.after(options, program);
45
+ }
46
+ if (typeof plugin.afterDeclarations === 'function') {
47
+ adapted.afterDeclarations = (options, program) => plugin.afterDeclarations(options, program);
48
+ }
49
+ // Return adapted hooks if any were found, otherwise return original plugin
50
+ return Object.keys(adapted).length > 0 ? adapted : plugin;
51
+ }
52
+ return plugin;
53
+ }
6
54
  function loadTsTransformers(plugins, moduleResolver = require.resolve) {
7
55
  const beforeHooks = [];
8
56
  const afterHooks = [];
@@ -26,7 +74,16 @@ function loadTsTransformers(plugins, moduleResolver = require.resolve) {
26
74
  const binaryPath = moduleResolver(name, {
27
75
  paths: nodeModulePaths,
28
76
  });
29
- return require(binaryPath);
77
+ const loadedPlugin = require(binaryPath);
78
+ // Check if main export already has transformer hooks
79
+ if (loadedPlugin &&
80
+ (loadedPlugin.before ||
81
+ loadedPlugin.after ||
82
+ loadedPlugin.afterDeclarations)) {
83
+ return loadedPlugin;
84
+ }
85
+ // Only fall back to .default if main export lacks transformer hooks
86
+ return loadedPlugin?.default ?? loadedPlugin;
30
87
  }
31
88
  catch (e) {
32
89
  devkit_1.logger.warn(`"${name}" plugin could not be found!`);
@@ -35,19 +92,53 @@ function loadTsTransformers(plugins, moduleResolver = require.resolve) {
35
92
  });
36
93
  for (let i = 0; i < pluginRefs.length; i++) {
37
94
  const { name: pluginName, options: pluginOptions } = normalizedPlugins[i];
38
- const { before, after, afterDeclarations } = pluginRefs[i];
95
+ let plugin = pluginRefs[i];
96
+ // Skip empty plugins (failed to load)
97
+ if (!plugin ||
98
+ (typeof plugin !== 'function' && Object.keys(plugin).length === 0)) {
99
+ continue;
100
+ }
101
+ const format = detectTransformerFormat(plugin);
102
+ // Adapt function-based transformers to standard format
103
+ if (format === TransformerFormat.FUNCTION_EXPORT) {
104
+ devkit_1.logger.debug(`Adapting function-based transformer: ${pluginName}`);
105
+ plugin = adaptFunctionBasedTransformer(plugin, pluginOptions);
106
+ }
107
+ else if (format === TransformerFormat.UNKNOWN) {
108
+ devkit_1.logger.warn(`${pluginName} is not a recognized Transformer Plugin format. It should export ` +
109
+ `{ before?, after?, afterDeclarations? } functions or be a function-based transformer.`);
110
+ continue;
111
+ }
112
+ const { before, after, afterDeclarations } = plugin;
113
+ // Validate that at least one hook is available
39
114
  if (!before && !after && !afterDeclarations) {
40
- devkit_1.logger.warn(`${pluginName} is not a Transformer Plugin. It does not provide neither before(), after(), nor afterDeclarations()`);
115
+ devkit_1.logger.warn(`${pluginName} does not provide any transformer hooks (before, after, or afterDeclarations).`);
41
116
  continue;
42
117
  }
118
+ // Add hooks with proper error handling
43
119
  if (before) {
44
- beforeHooks.push(before.bind(before, pluginOptions));
120
+ try {
121
+ beforeHooks.push((program) => before(pluginOptions, program));
122
+ }
123
+ catch (error) {
124
+ devkit_1.logger.error(`Failed to register 'before' transformer for ${pluginName}: ${error.message}`);
125
+ }
45
126
  }
46
127
  if (after) {
47
- afterHooks.push(after.bind(after, pluginOptions));
128
+ try {
129
+ afterHooks.push((program) => after(pluginOptions, program));
130
+ }
131
+ catch (error) {
132
+ devkit_1.logger.error(`Failed to register 'after' transformer for ${pluginName}: ${error.message}`);
133
+ }
48
134
  }
49
135
  if (afterDeclarations) {
50
- afterDeclarationsHooks.push(afterDeclarations.bind(afterDeclarations, pluginOptions));
136
+ try {
137
+ afterDeclarationsHooks.push((program) => afterDeclarations(pluginOptions, program));
138
+ }
139
+ catch (error) {
140
+ devkit_1.logger.error(`Failed to register 'afterDeclarations' transformer for ${pluginName}: ${error.message}`);
141
+ }
51
142
  }
52
143
  }
53
144
  return {
@@ -10,6 +10,17 @@ export interface CompilerPlugin {
10
10
  after?: (options?: Record<string, unknown>, program?: Program) => TransformerFactory;
11
11
  afterDeclarations?: (options?: Record<string, unknown>, program?: Program) => TransformerFactory;
12
12
  }
13
+ /**
14
+ * Extended plugin interface to support different transformer API formats
15
+ * including function-based transformers and direct function exports
16
+ */
17
+ export type AnyCompilerPlugin = CompilerPlugin | ((options?: Record<string, unknown>, program?: Program) => TransformerFactory) | {
18
+ before: (options?: Record<string, unknown>, program?: Program) => TransformerFactory;
19
+ } | {
20
+ after: (options?: Record<string, unknown>, program?: Program) => TransformerFactory;
21
+ } | {
22
+ afterDeclarations: (options?: Record<string, unknown>, program?: Program) => TransformerFactory;
23
+ };
13
24
  export interface CompilerPluginHooks {
14
25
  beforeHooks: Array<(program?: Program) => TransformerFactory>;
15
26
  afterHooks: Array<(program?: Program) => TransformerFactory>;