@aws/nx-plugin 0.0.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.
- package/README.md +63 -0
- package/generators.json +52 -0
- package/package.json +27 -0
- package/src/cloudscape-website/app/README.md +253 -0
- package/src/cloudscape-website/app/__snapshots__/generator.spec.ts.snap +539 -0
- package/src/cloudscape-website/app/files/app/src/config.ts.template +4 -0
- package/src/cloudscape-website/app/files/app/src/layouts/App/index.tsx.template +132 -0
- package/src/cloudscape-website/app/files/app/src/layouts/App/navitems.ts.template +8 -0
- package/src/cloudscape-website/app/files/app/src/layouts/Routes/index.tsx.template +18 -0
- package/src/cloudscape-website/app/files/app/src/main.tsx.template +22 -0
- package/src/cloudscape-website/app/files/app/src/pages/Home/index.tsx.template +25 -0
- package/src/cloudscape-website/app/files/common/constructs/src/__websiteNameKebabCase__/cloudfront-web-acl.ts.template +317 -0
- package/src/cloudscape-website/app/files/common/constructs/src/__websiteNameKebabCase__/index.ts.template +4 -0
- package/src/cloudscape-website/app/files/common/constructs/src/__websiteNameKebabCase__/static-website.ts.template +237 -0
- package/src/cloudscape-website/app/files/common/constructs/src/__websiteNameKebabCase__/webacl_event_handler/index.ts.template +301 -0
- package/src/cloudscape-website/app/files/e2e/cypress/src/e2e/app.cy.ts.template +13 -0
- package/src/cloudscape-website/app/files/e2e/cypress/src/support/app.po.ts.template +1 -0
- package/src/cloudscape-website/app/files/e2e/playwright/src/example.spec.ts.template +6 -0
- package/src/cloudscape-website/app/generator.d.ts +4 -0
- package/src/cloudscape-website/app/generator.js +177 -0
- package/src/cloudscape-website/app/generator.js.map +1 -0
- package/src/cloudscape-website/app/schema.d.js +6 -0
- package/src/cloudscape-website/app/schema.d.js.map +1 -0
- package/src/cloudscape-website/app/schema.d.ts +35 -0
- package/src/cloudscape-website/app/schema.json +189 -0
- package/src/cloudscape-website/cognito-auth/README.md +172 -0
- package/src/cloudscape-website/cognito-auth/__snapshots__/generator.spec.ts.snap +238 -0
- package/src/cloudscape-website/cognito-auth/files/app/components/CognitoAuth/index.tsx.template +50 -0
- package/src/cloudscape-website/cognito-auth/files/common/constructs/src/identity/index.ts.template +4 -0
- package/src/cloudscape-website/cognito-auth/files/common/constructs/src/identity/user-identity.ts.template +69 -0
- package/src/cloudscape-website/cognito-auth/files/common/constructs/src/identity/userpool-with-mfa.ts.template +70 -0
- package/src/cloudscape-website/cognito-auth/generator.d.ts +4 -0
- package/src/cloudscape-website/cognito-auth/generator.js +100 -0
- package/src/cloudscape-website/cognito-auth/generator.js.map +1 -0
- package/src/cloudscape-website/cognito-auth/schema.d.js +6 -0
- package/src/cloudscape-website/cognito-auth/schema.d.js.map +1 -0
- package/src/cloudscape-website/cognito-auth/schema.d.ts +4 -0
- package/src/cloudscape-website/cognito-auth/schema.json +36 -0
- package/src/cloudscape-website/runtime-config/__snapshots__/generator.spec.ts.snap +112 -0
- package/src/cloudscape-website/runtime-config/files/app/components/RuntimeConfig/index.tsx.template +46 -0
- package/src/cloudscape-website/runtime-config/generator.d.ts +4 -0
- package/src/cloudscape-website/runtime-config/generator.js +74 -0
- package/src/cloudscape-website/runtime-config/generator.js.map +1 -0
- package/src/cloudscape-website/runtime-config/schema.d.js +6 -0
- package/src/cloudscape-website/runtime-config/schema.d.js.map +1 -0
- package/src/cloudscape-website/runtime-config/schema.d.ts +3 -0
- package/src/cloudscape-website/runtime-config/schema.json +19 -0
- package/src/gitlab/files/.gitlab-ci.yml.template +26 -0
- package/src/gitlab/generator.d.ts +4 -0
- package/src/gitlab/generator.js +26 -0
- package/src/gitlab/generator.js.map +1 -0
- package/src/gitlab/schema.d.js +6 -0
- package/src/gitlab/schema.d.js.map +1 -0
- package/src/gitlab/schema.d.ts +5 -0
- package/src/gitlab/schema.json +52 -0
- package/src/index.d.ts +0 -0
- package/src/index.js +3 -0
- package/src/index.js.map +1 -0
- package/src/infra/app/README.md +175 -0
- package/src/infra/app/__snapshots__/generator.spec.ts.snap +864 -0
- package/src/infra/app/files/cdk.json +67 -0
- package/src/infra/app/files/src/main.ts.template +37 -0
- package/src/infra/app/files/src/stacks/application-stack.ts.template +10 -0
- package/src/infra/app/generator.d.ts +4 -0
- package/src/infra/app/generator.js +75 -0
- package/src/infra/app/generator.js.map +1 -0
- package/src/infra/app/schema.d.js +6 -0
- package/src/infra/app/schema.d.js.map +1 -0
- package/src/infra/app/schema.d.ts +6 -0
- package/src/infra/app/schema.json +35 -0
- package/src/trpc/backend/README.md +549 -0
- package/src/trpc/backend/__snapshots__/generator.spec.ts.snap +110 -0
- package/src/trpc/backend/files/backend/src/index.ts.template +1 -0
- package/src/trpc/backend/files/backend/src/lambdas/index.ts.template +1 -0
- package/src/trpc/backend/files/backend/src/lambdas/middleware.ts.template +146 -0
- package/src/trpc/backend/files/backend/src/lambdas/router.ts.template +36 -0
- package/src/trpc/backend/files/common/constructs/src/__apiNameKebabCase__/index.ts.template +64 -0
- package/src/trpc/backend/files/schema/src/index.ts.template +7 -0
- package/src/trpc/backend/generator.d.ts +4 -0
- package/src/trpc/backend/generator.js +128 -0
- package/src/trpc/backend/generator.js.map +1 -0
- package/src/trpc/backend/schema.d.js +6 -0
- package/src/trpc/backend/schema.d.js.map +1 -0
- package/src/trpc/backend/schema.d.ts +8 -0
- package/src/trpc/backend/schema.json +44 -0
- package/src/trpc/react/README.md +320 -0
- package/src/trpc/react/__snapshots__/generator.spec.ts.snap +98 -0
- package/src/trpc/react/files/src/components/TRPCClientProvider/index.tsx.template +34 -0
- package/src/trpc/react/files/src/hooks/useTrpc.tsx.template +5 -0
- package/src/trpc/react/generator.d.ts +4 -0
- package/src/trpc/react/generator.js +81 -0
- package/src/trpc/react/generator.js.map +1 -0
- package/src/trpc/react/schema.d.js +6 -0
- package/src/trpc/react/schema.d.js.map +1 -0
- package/src/trpc/react/schema.d.ts +5 -0
- package/src/trpc/react/schema.json +32 -0
- package/src/ts/cjs-to-esm/generator.d.ts +8 -0
- package/src/ts/cjs-to-esm/generator.js +201 -0
- package/src/ts/cjs-to-esm/generator.js.map +1 -0
- package/src/ts/cjs-to-esm/schema.d.js +6 -0
- package/src/ts/cjs-to-esm/schema.d.js.map +1 -0
- package/src/ts/cjs-to-esm/schema.d.ts +5 -0
- package/src/ts/cjs-to-esm/schema.json +28 -0
- package/src/ts/lib/README.md +149 -0
- package/src/ts/lib/__snapshots__/generator.spec.ts.snap +260 -0
- package/src/ts/lib/eslint.d.ts +3 -0
- package/src/ts/lib/eslint.js +41 -0
- package/src/ts/lib/eslint.js.map +1 -0
- package/src/ts/lib/files/src/index.ts.template +3 -0
- package/src/ts/lib/generator.d.ts +21 -0
- package/src/ts/lib/generator.js +61 -0
- package/src/ts/lib/generator.js.map +1 -0
- package/src/ts/lib/schema.d.js +6 -0
- package/src/ts/lib/schema.d.js.map +1 -0
- package/src/ts/lib/schema.d.ts +13 -0
- package/src/ts/lib/schema.json +46 -0
- package/src/ts/lib/ts-project-utils.d.ts +6 -0
- package/src/ts/lib/ts-project-utils.js +107 -0
- package/src/ts/lib/ts-project-utils.js.map +1 -0
- package/src/ts/lib/types.d.ts +10 -0
- package/src/ts/lib/types.js +6 -0
- package/src/ts/lib/types.js.map +1 -0
- package/src/ts/lib/vitest.d.ts +3 -0
- package/src/ts/lib/vitest.js +67 -0
- package/src/ts/lib/vitest.js.map +1 -0
- package/src/utils/files/common/constructs/src/index.ts.template +1 -0
- package/src/utils/files/common/constructs/src/runtime-config/index.ts.template +1 -0
- package/src/utils/files/common/constructs/src/runtime-config/runtime-config.ts.template +33 -0
- package/src/utils/files/common/types/src/index.ts.template +1 -0
- package/src/utils/files/common/types/src/runtime-config.ts.template +13 -0
- package/src/utils/npm-scope.d.ts +7 -0
- package/src/utils/npm-scope.js +37 -0
- package/src/utils/npm-scope.js.map +1 -0
- package/src/utils/paths.d.ts +3 -0
- package/src/utils/paths.js +32 -0
- package/src/utils/paths.js.map +1 -0
- package/src/utils/shared-constructs.d.ts +7 -0
- package/src/utils/shared-constructs.js +72 -0
- package/src/utils/shared-constructs.js.map +1 -0
- package/src/utils/versions.d.ts +31 -0
- package/src/utils/versions.js +49 -0
- package/src/utils/versions.js.map +1 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
cjsToEsm: function() {
|
|
13
|
+
return cjsToEsm;
|
|
14
|
+
},
|
|
15
|
+
cjsToEsmGenerator: function() {
|
|
16
|
+
return cjsToEsmGenerator;
|
|
17
|
+
},
|
|
18
|
+
default: function() {
|
|
19
|
+
return _default;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
|
|
23
|
+
const _devkit = require("@nx/devkit");
|
|
24
|
+
const _tsquery = require("@phenomnomnominal/tsquery");
|
|
25
|
+
const _typescript = /*#__PURE__*/ _interop_require_wildcard._(require("typescript"));
|
|
26
|
+
const _minimatch = require("minimatch");
|
|
27
|
+
// Replaces "module.exports = value" with "export default value"
|
|
28
|
+
const replaceModuleExportsWithExportDefault = (sourceFile)=>{
|
|
29
|
+
const transformer = (context)=>(rootNode)=>{
|
|
30
|
+
function visit(node) {
|
|
31
|
+
if (_typescript.isExpressionStatement(node)) {
|
|
32
|
+
const expr = node.expression;
|
|
33
|
+
if (_typescript.isBinaryExpression(expr) && _typescript.isPropertyAccessExpression(expr.left) && _typescript.isIdentifier(expr.left.expression) && expr.left.expression.text === 'module' && _typescript.isIdentifier(expr.left.name) && expr.left.name.text === 'exports' && expr.operatorToken.kind === _typescript.SyntaxKind.EqualsToken) {
|
|
34
|
+
// Create an export default statement
|
|
35
|
+
return context.factory.createExportAssignment(undefined, undefined, expr.right);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return _typescript.visitEachChild(node, visit, context);
|
|
39
|
+
}
|
|
40
|
+
return _typescript.visitNode(rootNode, visit);
|
|
41
|
+
};
|
|
42
|
+
const result = _typescript.transform(sourceFile, [
|
|
43
|
+
transformer
|
|
44
|
+
]);
|
|
45
|
+
return result.transformed[0];
|
|
46
|
+
};
|
|
47
|
+
// Replaces "const x = require('y')" with "import x from 'y'"
|
|
48
|
+
const replaceRequires = (sourceFile)=>{
|
|
49
|
+
const transformer = (context)=>(rootNode)=>{
|
|
50
|
+
function visit(node) {
|
|
51
|
+
if (_typescript.isVariableStatement(node)) {
|
|
52
|
+
const declaration = node.declarationList.declarations[0];
|
|
53
|
+
if (declaration && _typescript.isVariableDeclaration(declaration)) {
|
|
54
|
+
const initializer = declaration.initializer;
|
|
55
|
+
if (initializer && _typescript.isCallExpression(initializer) && _typescript.isIdentifier(initializer.expression) && initializer.expression.text === 'require' && initializer.arguments.length === 1) {
|
|
56
|
+
const modulePath = initializer.arguments[0];
|
|
57
|
+
if (_typescript.isStringLiteral(modulePath)) {
|
|
58
|
+
// Handle destructuring pattern
|
|
59
|
+
if (_typescript.isObjectBindingPattern(declaration.name)) {
|
|
60
|
+
const importSpecifiers = declaration.name.elements.map((element)=>{
|
|
61
|
+
if (_typescript.isBindingElement(element) && _typescript.isIdentifier(element.name)) {
|
|
62
|
+
return context.factory.createImportSpecifier(false, undefined, element.name);
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}).filter((spec)=>spec !== undefined);
|
|
66
|
+
return context.factory.createImportDeclaration(undefined, context.factory.createImportClause(false, undefined, context.factory.createNamedImports(importSpecifiers)), modulePath, undefined);
|
|
67
|
+
}
|
|
68
|
+
// Handle simple require
|
|
69
|
+
if (_typescript.isIdentifier(declaration.name)) {
|
|
70
|
+
return context.factory.createImportDeclaration(undefined, context.factory.createImportClause(false, declaration.name, undefined), modulePath, undefined);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return _typescript.visitEachChild(node, visit, context);
|
|
77
|
+
}
|
|
78
|
+
return _typescript.visitNode(rootNode, visit);
|
|
79
|
+
};
|
|
80
|
+
const result = _typescript.transform(sourceFile, [
|
|
81
|
+
transformer
|
|
82
|
+
]);
|
|
83
|
+
return result.transformed[0];
|
|
84
|
+
};
|
|
85
|
+
// Replaces requires not directly assigned to variables eg
|
|
86
|
+
// "const x = require('y').foo()" with
|
|
87
|
+
// import _import0 from 'y'
|
|
88
|
+
// const x = _import0.foo()
|
|
89
|
+
const replaceInlineRequires = (sourceFile)=>{
|
|
90
|
+
let importCounter = 0;
|
|
91
|
+
const imports = [];
|
|
92
|
+
const importMap = new Map();
|
|
93
|
+
const transformer = (context)=>(rootNode)=>{
|
|
94
|
+
function visit(node) {
|
|
95
|
+
if (_typescript.isCallExpression(node) && _typescript.isIdentifier(node.expression) && node.expression.text === 'require' && node.arguments.length === 1 && _typescript.isStringLiteral(node.arguments[0])) {
|
|
96
|
+
const modulePath = node.arguments[0];
|
|
97
|
+
const modulePathText = modulePath.text;
|
|
98
|
+
// Check if we already have an import for this module
|
|
99
|
+
if (!importMap.has(modulePathText)) {
|
|
100
|
+
const importName = `_import${importCounter++}`;
|
|
101
|
+
importMap.set(modulePathText, importName);
|
|
102
|
+
// Create import declaration
|
|
103
|
+
imports.push(context.factory.createImportDeclaration(undefined, context.factory.createImportClause(false, context.factory.createIdentifier(importName), undefined), modulePath, undefined));
|
|
104
|
+
}
|
|
105
|
+
// Replace require() with the import reference
|
|
106
|
+
return context.factory.createIdentifier(importMap.get(modulePathText));
|
|
107
|
+
}
|
|
108
|
+
return _typescript.visitEachChild(node, visit, context);
|
|
109
|
+
}
|
|
110
|
+
// First pass to collect all requires and create imports
|
|
111
|
+
const visited = _typescript.visitNode(rootNode, visit);
|
|
112
|
+
// If we found any inline requires, prepend the imports to the source file
|
|
113
|
+
if (imports.length > 0 && _typescript.isSourceFile(visited)) {
|
|
114
|
+
return context.factory.updateSourceFile(visited, [
|
|
115
|
+
...imports,
|
|
116
|
+
...visited.statements
|
|
117
|
+
]);
|
|
118
|
+
}
|
|
119
|
+
return visited;
|
|
120
|
+
};
|
|
121
|
+
const result = _typescript.transform(sourceFile, [
|
|
122
|
+
transformer
|
|
123
|
+
]);
|
|
124
|
+
return result.transformed[0];
|
|
125
|
+
};
|
|
126
|
+
function cjsToEsm(tree, relativePathInTree, options) {
|
|
127
|
+
var _options_include;
|
|
128
|
+
const includePatterns = (_options_include = options == null ? void 0 : options.include) != null ? _options_include : [
|
|
129
|
+
'**/*.{js,ts}'
|
|
130
|
+
];
|
|
131
|
+
var _options_exclude;
|
|
132
|
+
const excludePatterns = (_options_exclude = options == null ? void 0 : options.exclude) != null ? _options_exclude : [
|
|
133
|
+
'node_modules',
|
|
134
|
+
'dist',
|
|
135
|
+
'build',
|
|
136
|
+
'tmp',
|
|
137
|
+
'.nx'
|
|
138
|
+
].flatMap((p)=>[
|
|
139
|
+
p,
|
|
140
|
+
`**/${p}/**/*`,
|
|
141
|
+
`${p}/**/*`
|
|
142
|
+
]);
|
|
143
|
+
// Process each TypeScript/JavaScript file in the project
|
|
144
|
+
const processFile = (filePath)=>{
|
|
145
|
+
const content = tree.read(filePath, 'utf-8');
|
|
146
|
+
if (!content) return;
|
|
147
|
+
// Parse the source file
|
|
148
|
+
const originalSourceFile = _tsquery.tsquery.ast(content);
|
|
149
|
+
let sourceFile = originalSourceFile;
|
|
150
|
+
sourceFile = replaceModuleExportsWithExportDefault(sourceFile);
|
|
151
|
+
sourceFile = replaceRequires(sourceFile);
|
|
152
|
+
sourceFile = replaceInlineRequires(sourceFile);
|
|
153
|
+
const printer = _typescript.createPrinter({
|
|
154
|
+
removeComments: false,
|
|
155
|
+
newLine: _typescript.NewLineKind.LineFeed
|
|
156
|
+
});
|
|
157
|
+
tree.write(filePath, printer.printNode(_typescript.EmitHint.Unspecified, sourceFile, originalSourceFile));
|
|
158
|
+
};
|
|
159
|
+
// Process all files in the given directory (recursively)
|
|
160
|
+
const processDirectory = (dirPath)=>{
|
|
161
|
+
const entries = tree.children(dirPath);
|
|
162
|
+
entries.forEach((entry)=>{
|
|
163
|
+
const fullPath = `${dirPath}/${entry}`;
|
|
164
|
+
const relativePath = fullPath.replace(`${relativePathInTree}/`, '');
|
|
165
|
+
// Return early for exclude to ensure we don't traverse node_modules etc!
|
|
166
|
+
const shouldExclude = excludePatterns.some((pattern)=>(0, _minimatch.minimatch)(relativePath, pattern));
|
|
167
|
+
if (shouldExclude) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (tree.isFile(fullPath)) {
|
|
171
|
+
const shouldInclude = includePatterns.some((pattern)=>(0, _minimatch.minimatch)(relativePath, pattern));
|
|
172
|
+
if (shouldInclude) {
|
|
173
|
+
processFile(fullPath);
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
processDirectory(fullPath);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
// Start processing from the given directories
|
|
181
|
+
processDirectory(relativePathInTree);
|
|
182
|
+
}
|
|
183
|
+
async function cjsToEsmGenerator(tree, options) {
|
|
184
|
+
// Get the project configuration
|
|
185
|
+
const projects = (0, _devkit.getProjects)(tree);
|
|
186
|
+
const project = projects.get(options.project);
|
|
187
|
+
if (!project) {
|
|
188
|
+
throw new Error(`Project ${options.project} not found`);
|
|
189
|
+
}
|
|
190
|
+
cjsToEsm(tree, project.root, {
|
|
191
|
+
include: options.include ? [
|
|
192
|
+
options.include
|
|
193
|
+
] : undefined,
|
|
194
|
+
exclude: options.exclude ? [
|
|
195
|
+
options.exclude
|
|
196
|
+
] : undefined
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
const _default = cjsToEsmGenerator;
|
|
200
|
+
|
|
201
|
+
//# sourceMappingURL=generator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../packages/nx-plugin/src/ts/cjs-to-esm/generator.ts"],"sourcesContent":["import {\n Tree,\n getProjects,\n} from '@nx/devkit';\nimport { CjsToEsmGeneratorSchema } from './schema';\nimport { tsquery } from '@phenomnomnominal/tsquery';\nimport * as ts from 'typescript';\nimport { minimatch } from 'minimatch';\n\n// Replaces \"module.exports = value\" with \"export default value\"\nconst replaceModuleExportsWithExportDefault = (sourceFile: ts.SourceFile): ts.SourceFile => {\n const transformer = <T extends ts.Node>(context: ts.TransformationContext) => (rootNode: T) => {\n function visit(node: ts.Node): ts.Node {\n if (ts.isExpressionStatement(node)) {\n const expr = node.expression;\n if (ts.isBinaryExpression(expr) &&\n ts.isPropertyAccessExpression(expr.left) &&\n ts.isIdentifier(expr.left.expression) &&\n expr.left.expression.text === 'module' &&\n ts.isIdentifier(expr.left.name) &&\n expr.left.name.text === 'exports' &&\n expr.operatorToken.kind === ts.SyntaxKind.EqualsToken) {\n\n // Create an export default statement\n return context.factory.createExportAssignment(\n undefined,\n undefined,\n expr.right\n );\n }\n }\n return ts.visitEachChild(node, visit, context);\n }\n return ts.visitNode(rootNode, visit);\n };\n\n const result = ts.transform(sourceFile, [transformer]);\n return result.transformed[0] as ts.SourceFile;\n};\n\n// Replaces \"const x = require('y')\" with \"import x from 'y'\"\nconst replaceRequires = (sourceFile: ts.SourceFile): ts.SourceFile => {\n const transformer = <T extends ts.Node>(context: ts.TransformationContext) => (rootNode: T) => {\n function visit(node: ts.Node): ts.Node {\n if (ts.isVariableStatement(node)) {\n const declaration = node.declarationList.declarations[0];\n if (declaration && ts.isVariableDeclaration(declaration)) {\n const initializer = declaration.initializer;\n if (initializer && ts.isCallExpression(initializer) &&\n ts.isIdentifier(initializer.expression) &&\n initializer.expression.text === 'require' &&\n initializer.arguments.length === 1) {\n\n const modulePath = initializer.arguments[0];\n if (ts.isStringLiteral(modulePath)) {\n // Handle destructuring pattern\n if (ts.isObjectBindingPattern(declaration.name)) {\n const importSpecifiers = declaration.name.elements.map(element => {\n if (ts.isBindingElement(element) && ts.isIdentifier(element.name)) {\n return context.factory.createImportSpecifier(\n false,\n undefined,\n element.name\n );\n }\n return undefined;\n }).filter((spec): spec is ts.ImportSpecifier => spec !== undefined);\n\n return context.factory.createImportDeclaration(\n undefined,\n context.factory.createImportClause(\n false,\n undefined,\n context.factory.createNamedImports(importSpecifiers)\n ),\n modulePath,\n undefined\n );\n }\n\n // Handle simple require\n if (ts.isIdentifier(declaration.name)) {\n return context.factory.createImportDeclaration(\n undefined,\n context.factory.createImportClause(\n false,\n declaration.name,\n undefined\n ),\n modulePath,\n undefined\n );\n }\n }\n }\n }\n }\n return ts.visitEachChild(node, visit, context);\n }\n return ts.visitNode(rootNode, visit);\n };\n\n const result = ts.transform(sourceFile, [transformer]);\n return result.transformed[0] as ts.SourceFile;\n};\n\n// Replaces requires not directly assigned to variables eg\n// \"const x = require('y').foo()\" with\n// import _import0 from 'y'\n// const x = _import0.foo()\nconst replaceInlineRequires = (sourceFile: ts.SourceFile): ts.SourceFile => {\n let importCounter = 0;\n const imports: ts.ImportDeclaration[] = [];\n const importMap = new Map<string, string>();\n\n const transformer = <T extends ts.Node>(context: ts.TransformationContext) => (rootNode: T) => {\n function visit(node: ts.Node): ts.Node {\n if (ts.isCallExpression(node) &&\n ts.isIdentifier(node.expression) &&\n node.expression.text === 'require' &&\n node.arguments.length === 1 &&\n ts.isStringLiteral(node.arguments[0])) {\n\n const modulePath = node.arguments[0];\n const modulePathText = modulePath.text;\n\n // Check if we already have an import for this module\n if (!importMap.has(modulePathText)) {\n const importName = `_import${importCounter++}`;\n importMap.set(modulePathText, importName);\n\n // Create import declaration\n imports.push(context.factory.createImportDeclaration(\n undefined,\n context.factory.createImportClause(\n false,\n context.factory.createIdentifier(importName),\n undefined\n ),\n modulePath,\n undefined\n ));\n }\n\n // Replace require() with the import reference\n return context.factory.createIdentifier(importMap.get(modulePathText)!);\n }\n\n return ts.visitEachChild(node, visit, context);\n }\n\n // First pass to collect all requires and create imports\n const visited = ts.visitNode(rootNode, visit);\n\n // If we found any inline requires, prepend the imports to the source file\n if (imports.length > 0 && ts.isSourceFile(visited)) {\n return context.factory.updateSourceFile(\n visited,\n [...imports, ...visited.statements]\n );\n }\n\n return visited;\n };\n\n const result = ts.transform(sourceFile, [transformer]);\n return result.transformed[0] as ts.SourceFile;\n};\n\nexport function cjsToEsm(tree: Tree, relativePathInTree: string, options?: { include?: string[]; exclude?: string[] }) {\n const includePatterns = options?.include ?? ['**/*.{js,ts}'];\n const excludePatterns = options?.exclude ?? ['node_modules', 'dist', 'build', 'tmp', '.nx'].flatMap(p => [p, `**/${p}/**/*`, `${p}/**/*`]);\n\n // Process each TypeScript/JavaScript file in the project\n const processFile = (filePath: string) => {\n const content = tree.read(filePath, 'utf-8');\n if (!content) return;\n\n // Parse the source file\n const originalSourceFile = tsquery.ast(content);\n let sourceFile = originalSourceFile;\n\n sourceFile = replaceModuleExportsWithExportDefault(sourceFile);\n sourceFile = replaceRequires(sourceFile);\n sourceFile = replaceInlineRequires(sourceFile);\n\n const printer = ts.createPrinter({ removeComments: false, newLine: ts.NewLineKind.LineFeed });\n tree.write(filePath, printer.printNode(ts.EmitHint.Unspecified, sourceFile, originalSourceFile));\n };\n\n // Process all files in the given directory (recursively)\n const processDirectory = (dirPath: string) => {\n const entries = tree.children(dirPath);\n entries.forEach(entry => {\n const fullPath = `${dirPath}/${entry}`;\n const relativePath = fullPath.replace(`${relativePathInTree}/`, '');\n\n // Return early for exclude to ensure we don't traverse node_modules etc!\n const shouldExclude = excludePatterns.some(pattern => minimatch(relativePath, pattern));\n if (shouldExclude) {\n return;\n }\n\n if (tree.isFile(fullPath)) {\n const shouldInclude = includePatterns.some(pattern => minimatch(relativePath, pattern));\n if (shouldInclude) {\n processFile(fullPath);\n }\n } else {\n processDirectory(fullPath);\n }\n });\n };\n\n // Start processing from the given directories\n processDirectory(relativePathInTree);\n}\n\nexport async function cjsToEsmGenerator(\n tree: Tree,\n options: CjsToEsmGeneratorSchema\n) {\n // Get the project configuration\n const projects = getProjects(tree);\n const project = projects.get(options.project);\n\n if (!project) {\n throw new Error(`Project ${options.project} not found`);\n }\n\n cjsToEsm(tree, project.root, {\n include: options.include ? [options.include] : undefined,\n exclude: options.exclude ? [options.exclude] : undefined,\n });\n}\n\nexport default cjsToEsmGenerator;\n"],"names":["cjsToEsm","cjsToEsmGenerator","replaceModuleExportsWithExportDefault","sourceFile","transformer","context","rootNode","visit","node","ts","isExpressionStatement","expr","expression","isBinaryExpression","isPropertyAccessExpression","left","isIdentifier","text","name","operatorToken","kind","SyntaxKind","EqualsToken","factory","createExportAssignment","undefined","right","visitEachChild","visitNode","result","transform","transformed","replaceRequires","isVariableStatement","declaration","declarationList","declarations","isVariableDeclaration","initializer","isCallExpression","arguments","length","modulePath","isStringLiteral","isObjectBindingPattern","importSpecifiers","elements","map","element","isBindingElement","createImportSpecifier","filter","spec","createImportDeclaration","createImportClause","createNamedImports","replaceInlineRequires","importCounter","imports","importMap","Map","modulePathText","has","importName","set","push","createIdentifier","get","visited","isSourceFile","updateSourceFile","statements","tree","relativePathInTree","options","includePatterns","include","excludePatterns","exclude","flatMap","p","processFile","filePath","content","read","originalSourceFile","tsquery","ast","printer","createPrinter","removeComments","newLine","NewLineKind","LineFeed","write","printNode","EmitHint","Unspecified","processDirectory","dirPath","entries","children","forEach","entry","fullPath","relativePath","replace","shouldExclude","some","pattern","minimatch","isFile","shouldInclude","projects","getProjects","project","Error","root"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;;;;;;;;IAyKgBA,QAAQ;eAARA;;IAiDMC,iBAAiB;eAAjBA;;IAkBtB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0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/schema",
|
|
3
|
+
"cli": "nx",
|
|
4
|
+
"$id": "ts-cjs-to-esm",
|
|
5
|
+
"title": "Convert CommonJS to ESM",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"project": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "The name of the project to convert",
|
|
11
|
+
"$default": {
|
|
12
|
+
"$source": "argv",
|
|
13
|
+
"index": 0
|
|
14
|
+
},
|
|
15
|
+
"x-dropdown": "projects"
|
|
16
|
+
},
|
|
17
|
+
"include": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"description": "Glob pattern for files to include in the conversion",
|
|
20
|
+
"default": "**/*.{js,ts}"
|
|
21
|
+
},
|
|
22
|
+
"exclude": {
|
|
23
|
+
"type": "string",
|
|
24
|
+
"description": "Glob pattern for files to exclude from the conversion. If not provided, patterns from .gitignore will be used"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"required": ["project"]
|
|
28
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# TypeScript Library Generator
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
This generator creates a new TypeScript library with modern configuration and best practices. It sets up a complete TypeScript project with ESM modules, proper build configuration, and optional linting and testing support. The generator is designed to create reusable TypeScript packages that can be shared across your organization's projects.
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
You can generate a new TypeScript library in two ways:
|
|
9
|
+
|
|
10
|
+
### 1. Using VSCode IDE
|
|
11
|
+
|
|
12
|
+
First, install the NX Console extension for VSCode:
|
|
13
|
+
1. Open VSCode
|
|
14
|
+
2. Go to Extensions (Ctrl+Shift+X / Cmd+Shift+X)
|
|
15
|
+
3. Search for "Nx Console"
|
|
16
|
+
4. Install [Nx Console](https://marketplace.visualstudio.com/items?itemName=nrwl.angular-console)
|
|
17
|
+
|
|
18
|
+
Then generate your library:
|
|
19
|
+
1. Open the NX Console in VSCode
|
|
20
|
+
2. Click on "Generate"
|
|
21
|
+
3. Search for "ts#lib"
|
|
22
|
+
4. Fill in the required parameters in the form
|
|
23
|
+
5. Click "Run"
|
|
24
|
+
|
|
25
|
+
### 2. Using CLI
|
|
26
|
+
|
|
27
|
+
Generate the library:
|
|
28
|
+
```bash
|
|
29
|
+
nx g @aws/nx-plugin:ts#lib my-lib --directory=packages
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
You can also perform a dry-run to see what files would be generated without actually creating them:
|
|
33
|
+
```bash
|
|
34
|
+
nx g @aws/nx-plugin:ts#lib my-lib --directory=packages --dry-run
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Both methods will create a new TypeScript library in the specified directory with all the necessary configuration.
|
|
38
|
+
|
|
39
|
+
## Input Parameters
|
|
40
|
+
|
|
41
|
+
| Parameter | Type | Default | Description |
|
|
42
|
+
|-----------|------|---------|-------------|
|
|
43
|
+
| name* | string | - | Library name (required). Used to generate package name and file paths. |
|
|
44
|
+
| directory | string | "packages" | Parent directory where the library is placed. |
|
|
45
|
+
| linter | string | "eslint" | The tool to use for running lint checks. Options: eslint, none |
|
|
46
|
+
| unitTestRunner | string | "none" | Test runner to use for unit tests. Options: jest, vitest, none |
|
|
47
|
+
| scope | string | - | Scope for your package (e.g., @my-company). If omitted, this will be inferred from your project configuration. Must be in format @scope or @scope/subscope. |
|
|
48
|
+
| subDirectory | string | library name | The sub directory the lib is placed in. By default, this is the library name. |
|
|
49
|
+
|
|
50
|
+
*Required parameter
|
|
51
|
+
|
|
52
|
+
## Expected Output
|
|
53
|
+
|
|
54
|
+
The generator creates a TypeScript library with the following structure:
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
<directory>/<sub-directory>/
|
|
58
|
+
├── src/
|
|
59
|
+
│ └── index.ts # Main entry point for your library
|
|
60
|
+
├── tsconfig.json # TypeScript configuration
|
|
61
|
+
├── tsconfig.lib.json # TypeScript build configuration
|
|
62
|
+
├── project.json # Project configuration and build targets
|
|
63
|
+
└── .eslintrc.json # ESLint configuration (if enabled)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Additionally, it:
|
|
67
|
+
1. Configures the project for ESM (ECMAScript Modules)
|
|
68
|
+
2. Sets up proper TypeScript configuration for library development
|
|
69
|
+
3. Configures build settings for production deployment
|
|
70
|
+
4. Sets up linting with ESLint (if enabled)
|
|
71
|
+
5. Configures test runner (if enabled)
|
|
72
|
+
6. Installs required dependencies
|
|
73
|
+
|
|
74
|
+
## Best Practices
|
|
75
|
+
|
|
76
|
+
### 1. Export Patterns
|
|
77
|
+
|
|
78
|
+
Use explicit exports in your index.ts:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// Good
|
|
82
|
+
export { MyClass } from './my-class';
|
|
83
|
+
export type { MyType } from './types';
|
|
84
|
+
|
|
85
|
+
// Avoid
|
|
86
|
+
export * from './everything';
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 2. TypeScript Configuration
|
|
90
|
+
|
|
91
|
+
The generator sets up optimal TypeScript configuration, but you can customize it.
|
|
92
|
+
|
|
93
|
+
### 3. Testing Setup
|
|
94
|
+
|
|
95
|
+
If you enable testing, follow these practices:
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// my-feature.test.ts
|
|
99
|
+
describe('MyFeature', () => {
|
|
100
|
+
it('should handle basic case', () => {
|
|
101
|
+
// Arrange
|
|
102
|
+
const input = 'test';
|
|
103
|
+
|
|
104
|
+
// Act
|
|
105
|
+
const result = processInput(input);
|
|
106
|
+
|
|
107
|
+
// Assert
|
|
108
|
+
expect(result).toBe('TEST');
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 4. Documentation
|
|
114
|
+
|
|
115
|
+
Add JSDoc comments to your public APIs:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
/**
|
|
119
|
+
* Processes the input string according to business rules.
|
|
120
|
+
*
|
|
121
|
+
* @param input - The string to process
|
|
122
|
+
* @returns The processed string
|
|
123
|
+
* @throws {ValidationError} If input is invalid
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* const result = processInput('test');
|
|
128
|
+
* console.log(result); // 'TEST'
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export function processInput(input: string): string {
|
|
132
|
+
// Implementation
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### 5. Build Process
|
|
137
|
+
|
|
138
|
+
The generator configures a build process that:
|
|
139
|
+
- Compiles TypeScript to JavaScript
|
|
140
|
+
- Generates type definitions
|
|
141
|
+
- Creates source maps
|
|
142
|
+
- Handles ESM modules properly
|
|
143
|
+
|
|
144
|
+
You can build your library using:
|
|
145
|
+
```bash
|
|
146
|
+
nx build my-lib
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
This will create a `dist` directory in the root of your monorepo with your compiled library ready for distribution.
|