Skip to content

Commit 325bc1e

Browse files
committed
fix: add utility functions to get module type
This PR will add more complete handling with regards to the CommonJS and ESM standards. This PR should solve #3203 and #2661. We now look at the extension first, if it's .cjs or .mjs, great, that's easy to resolve. Otherwise, we look for the first package.json we come across, check it's module type, and then determine the project's standards. The esbuild has been updated to accomodate this change as well.
1 parent 6af4f1f commit 325bc1e

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

packages/artillery/lib/util/prepare-test-execution-plan.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const csv = require('csv-parse');
22
const fs = require('node:fs');
33
const path = require('node:path');
44
const p = require('util').promisify;
5+
const { isPackageESM } = require('@artilleryio/int-core').util;
56

67
const {
78
readScript,
@@ -118,6 +119,8 @@ function replaceProcessorIfTypescript(script, scriptPath) {
118119
return script;
119120
}
120121

122+
const packageType = isPackageESM(scriptPath) ? 'esm' : 'cjs';
123+
121124
const actualProcessorPath = path.resolve(
122125
path.dirname(scriptPath),
123126
relativeProcessorPath
@@ -140,7 +143,7 @@ function replaceProcessorIfTypescript(script, scriptPath) {
140143
outfile: newProcessorPath,
141144
bundle: true,
142145
platform: 'node',
143-
format: 'cjs',
146+
format: packageType,
144147
sourcemap: 'inline',
145148
external: ['@playwright/test', ...userExternalPackages]
146149
});

packages/core/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,6 @@ module.exports = {
9494
engine_http: require('./lib/engine_http'),
9595
ssms: require('./lib/ssms'),
9696
isIdlePhase: require('./lib/is-idle-phase'),
97-
updateGlobalObject
97+
updateGlobalObject,
98+
util: require('./lib/util')
9899
};

packages/core/lib/runner.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const uuidv4 = require('uuid').v4;
1313
const { SSMS } = require('./ssms');
1414
const createPhaser = require('./phases');
1515
const createReader = require('./readers');
16+
const { isPackageESM } = require('./util');
1617
const engineUtil = require('@artilleryio/int-commons').engine_util;
1718
const wl = require('./weighted-pick');
1819
const { pathToFileURL } = require('url');
@@ -83,7 +84,7 @@ async function loadProcessor(script, options) {
8384
script.config.processor
8485
);
8586

86-
if (processorPath.endsWith('.mjs')) {
87+
if (isPackageESM(processorPath)) {
8788
const fileUrl = pathToFileURL(processorPath);
8889
const exports = await import(fileUrl.href);
8990
script.config.processor = Object.assign(

packages/core/lib/util.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
const path = require('path');
6+
const fs = require('fs');
7+
8+
function determineModuleTypeByPackageJson(filePath) {
9+
// If it's .js, we need to check the package.json
10+
try {
11+
// Start from script directory and move up until we find package.json
12+
let dir = path.dirname(filePath);
13+
while (dir !== path.parse(dir).root) {
14+
try {
15+
const pkgPath = path.join(dir, 'package.json');
16+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
17+
return pkg.type === 'module' ? 'esm' : 'commonjs';
18+
} catch (err) {
19+
dir = path.dirname(dir);
20+
}
21+
}
22+
23+
// No package.json found, default to commonjs
24+
return 'commonjs';
25+
} catch (err) {
26+
return 'commonjs';
27+
}
28+
}
29+
30+
function determineModuleType(filePath) {
31+
if (filePath.endsWith('.mjs')) return 'esm';
32+
if (filePath.endsWith('.cjs')) return 'commonjs';
33+
34+
return determineModuleTypeByPackageJson(filePath);
35+
}
36+
37+
/**
38+
* Determine if a package is ESM or not.
39+
* @param {string | undefined} filePath Path to the package.json file. If not provided, it will default to the current working directory.
40+
* @returns A boolean indicating if the package is ESM or not.
41+
*/
42+
function isPackageESM(filePath) {
43+
if (!filePath) filePath = process.cwd();
44+
45+
return determineModuleType(filePath) === 'esm';
46+
}
47+
48+
/**
49+
* Determine if a package is CommonJS or not.
50+
* @param {string | undefined} filePath Path to the package.json file. If not provided, it will default to the current working directory.
51+
* @returns A boolean indicating if the package is CommonJS or not.
52+
*/
53+
function isPackageCommonJS(filePath) {
54+
if (!filePath) filePath = process.cwd();
55+
56+
return determineModuleType(filePath) === 'commonjs';
57+
}
58+
59+
module.exports = {
60+
determineModuleType,
61+
isPackageESM,
62+
isPackageCommonJS
63+
};

0 commit comments

Comments
 (0)