@ms-cloudpack/package-utilities 5.6.3 → 5.7.1
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":"detectModuleType.d.ts","sourceRoot":"","sources":["../src/detectModuleType.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"detectModuleType.d.ts","sourceRoot":"","sources":["../src/detectModuleType.ts"],"names":[],"mappings":"AAOA;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,KAAK,GAAG,SAAS,CAAC;AAEnD,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAoGrE;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAe5E"}
|
package/lib/detectModuleType.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import fsPromises from 'fs/promises';
|
|
2
2
|
import { parse } from 'acorn';
|
|
3
3
|
import { simple } from 'acorn-walk';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { typescriptExtensions } from '@ms-cloudpack/path-utilities';
|
|
4
6
|
export function detectModuleTypeFromSource(source) {
|
|
5
7
|
let ast;
|
|
6
8
|
try {
|
|
@@ -100,6 +102,10 @@ export function detectModuleTypeFromSource(source) {
|
|
|
100
102
|
*/
|
|
101
103
|
export async function detectModuleType(filePath) {
|
|
102
104
|
let source;
|
|
105
|
+
// If it's a typescript file, return esm immediately.
|
|
106
|
+
if (typescriptExtensions.includes(path.extname(filePath))) {
|
|
107
|
+
return 'esm';
|
|
108
|
+
}
|
|
103
109
|
try {
|
|
104
110
|
source = (await fsPromises.readFile(filePath, 'utf8')).toString();
|
|
105
111
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"detectModuleType.js","sourceRoot":"","sources":["../src/detectModuleType.ts"],"names":[],"m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BAAkB,CAAC;AAC1C,CAAC;AAED;;GAEG;AACH,SAAS,YAAY,CAAC,IAAU;IAC9B,OAAO,IAAI,CAAC,IAAI,KAAK,YAAY,CAAC;AACpC,CAAC;AAED;;GAEG;AACH,SAAS,iBAAiB,CAAC,IAAU;IACnC,OAAO,IAAI,CAAC,IAAI,KAAK,iBAAiB,CAAC;AACzC,CAAC","sourcesContent":["import fsPromises from 'fs/promises';\nimport type { AssignmentExpression, CallExpression, Identifier, MemberExpression, Node, UnaryExpression } from 'acorn';\nimport { parse } from 'acorn';\nimport { simple } from 'acorn-walk';\nimport path from 'path';\nimport { typescriptExtensions } from '@ms-cloudpack/path-utilities';\n\n/**\n * Module type of a source file. Undefined implies that we can not determine the module type.\n */\nexport type ModuleType = 'esm' | 'cjs' | undefined;\n\nexport function detectModuleTypeFromSource(source: string): ModuleType {\n let ast: Node;\n\n try {\n ast = parse(source, {\n ecmaVersion: 'latest',\n sourceType: 'module', // Try to parse as a module first.\n });\n } catch (err) {\n // If the parse fails, try again as a script.\n try {\n ast = parse(source, {\n ecmaVersion: 'latest',\n sourceType: 'script', // Parse as a script for CommonJS.\n });\n } catch (e) {\n // If parsing fails again, the code is not valid JavaScript or too complex for a simple parse.\n return undefined;\n }\n }\n\n let isCJS = false;\n let isESM = false;\n\n const returnESM = () => {\n isESM = true;\n throw new Error('Found moduleType.');\n };\n\n const returnCJS = () => {\n isCJS = true;\n // Note, we do not throw for CJS and instead parse the full file. Sometimes ESM output can still have checks\n // for CommonJS, and short circuiting too soon might miss ESM syntax.\n };\n\n try {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-call\n simple(ast, {\n // ESM checks\n ImportDeclaration: returnESM,\n ExportNamedDeclaration: returnESM,\n ExportAllDeclaration: returnESM,\n ExportDefaultDeclaration: returnESM,\n\n // CommonJS checks\n CallExpression(node: Node) {\n if (isCallExpression(node) && node.callee.type === 'Identifier' && node.callee.name === 'require') {\n returnCJS();\n }\n },\n Identifier(node: Node) {\n if (isIdentifier(node) && node.name === 'exports') {\n returnCJS();\n }\n },\n UnaryExpression(node: Node) {\n if (isUnaryExpression(node) && node.operator === 'typeof') {\n const argument = node.argument;\n\n // Handle expressions like `typeof exports`\n if (isIdentifier(argument) && argument.name === 'exports') {\n returnCJS();\n }\n }\n },\n AssignmentExpression(node: Node) {\n if (isAssignmentExpression(node)) {\n const left = node.left;\n // Handle expressions like `module.exports = ...`\n if (isMemberExpression(left)) {\n if (\n 'object' in left &&\n left.object.type === 'Identifier' &&\n left.object.name === 'module' &&\n isIdentifier(left.property) &&\n left.property.name === 'exports'\n ) {\n returnCJS();\n }\n // Handle expressions like `exports.someProperty = ...`\n else if ('object' in left && left.object.type === 'Identifier' && left.object.name === 'exports') {\n returnCJS();\n }\n }\n }\n },\n });\n } catch {\n /* no-op */\n }\n\n if (isESM) {\n // Note: While it's possible for a file to include both styles, we'll prefer ESM if we find it.\n return 'esm';\n } else if (isCJS) {\n return 'cjs';\n }\n\n // We didn't find either.\n return undefined;\n}\n\n/**\n * Determines if the given file is commonjs or esm.\n */\nexport async function detectModuleType(filePath: string): Promise<ModuleType> {\n let source: string;\n\n // If it's a typescript file, return esm immediately.\n if (typescriptExtensions.includes(path.extname(filePath))) {\n return 'esm';\n }\n\n try {\n source = (await fsPromises.readFile(filePath, 'utf8')).toString();\n } catch (e) {\n return undefined;\n }\n\n return detectModuleTypeFromSource(source);\n}\n\n/**\n * Type guard for CallExpression.\n */\nfunction isCallExpression(node: Node): node is CallExpression {\n return node.type === 'CallExpression';\n}\n\n/**\n * Type guard for AssignmentExpression.\n */\nfunction isAssignmentExpression(node: Node): node is AssignmentExpression {\n return node.type === 'AssignmentExpression';\n}\n\n/**\n * Type guard for MemberExpression.\n */\nfunction isMemberExpression(node: Node): node is MemberExpression {\n return node.type === 'MemberExpression';\n}\n\n/**\n * Type guard for Identifier.\n */\nfunction isIdentifier(node: Node): node is Identifier {\n return node.type === 'Identifier';\n}\n\n/**\n * Type guard for UnaryExpression.\n */\nfunction isUnaryExpression(node: Node): node is UnaryExpression {\n return node.type === 'UnaryExpression';\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/package-utilities",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.7.1",
|
|
4
4
|
"description": "Utilities for resolving/parsing packages and their imports.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@ms-cloudpack/bundler-types": "^0.23.
|
|
18
|
-
"@ms-cloudpack/config-types": "^0.
|
|
17
|
+
"@ms-cloudpack/bundler-types": "^0.23.5",
|
|
18
|
+
"@ms-cloudpack/config-types": "^0.3.0",
|
|
19
19
|
"@ms-cloudpack/json-utilities": "^0.1.2",
|
|
20
|
-
"@ms-cloudpack/package-overrides": "^0.6.
|
|
21
|
-
"@ms-cloudpack/path-utilities": "^2.
|
|
20
|
+
"@ms-cloudpack/package-overrides": "^0.6.7",
|
|
21
|
+
"@ms-cloudpack/path-utilities": "^2.4.0",
|
|
22
22
|
"@ms-cloudpack/path-string-parsing": "^1.1.2",
|
|
23
23
|
"acorn": "^8.11.2",
|
|
24
24
|
"acorn-walk": "^8.2.1",
|