Skip to content

Commit 3c4648b

Browse files
committed
0.1.7 - Use .mjs extension for Azure DevOps ES module support (v0.1.7)
- Replace CommonJS conversion with native ES module approach using .mjs extension - Simplify build process by directly copying ncc output as index.mjs - Remove complex regex-based ES-to-CommonJS conversion script - Update Azure DevOps task execution target from index.js to index.mjs - Leverage Node20_1 native ES module support instead of compatibility workarounds This eliminates the need for post-processing ES modules to CommonJS, resulting in cleaner code and a more maintainable build pipeline.
1 parent 0f1fb24 commit 3c4648b

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

component-detection-github-submission-task/index.js renamed to component-detection-github-submission-task/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require('./sourcemap-register.cjs');const { createRequire: __WEBPACK_EXTERNAL_createRequire } = require("module");
1+
import './sourcemap-register.cjs';import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "module";
22
/******/ var __webpack_modules__ = ({
33

44
/***/ 4914:

component-detection-github-submission-task/package.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

component-detection-github-submission-task/task.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": {
1010
"Major": 0,
1111
"Minor": 1,
12-
"Patch": 5
12+
"Patch": 7
1313
},
1414
"instanceNameFormat": "Component Detection $(filePath)",
1515
"groups": [
@@ -159,7 +159,7 @@
159159
],
160160
"execution": {
161161
"Node20_1": {
162-
"target": "index.js",
162+
"target": "index.mjs",
163163
"argumentFormat": ""
164164
}
165165
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"prepare": "ncc build index.ts -o dist --source-map --license licenses.txt",
1010
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
1111
"all": "npm run lint && npm run prepare && npm run test",
12-
"ado:build": "ncc build ado-index.ts -o ado-dist --source-map --license licenses.txt --target es2020 && node scripts/convert-to-cjs.cjs",
12+
"ado:build": "ncc build ado-index.ts -o ado-dist --source-map --license licenses.txt --target es2020 && node -e \"require('fs').copyFileSync('ado-dist/index.js', 'component-detection-github-submission-task/index.mjs')\"",
1313
"ado:package": "tfx extension create --manifest-globs vss-extension.json",
1414
"ado:all": "npm run ado:build && npm run ado:package"
1515
},

scripts/convert-to-cjs.cjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* ES Module to CommonJS Converter for Azure DevOps Extension
3+
*
4+
* This script is necessary because:
5+
* 1. ncc (Node.js Compiler Collection) always outputs ES modules with import/export syntax
6+
* 2. Azure DevOps task runtime expects CommonJS modules with require() syntax
7+
* 3. Even with "type": "commonjs" in package.json, the bundled code contains ES module syntax
8+
* 4. Azure DevOps cannot execute ES modules, resulting in "Cannot use import statement outside a module" errors
9+
*
10+
* The script converts:
11+
* - import statements → require() calls
12+
* - import.meta.url → __filename (CommonJS equivalent)
13+
* - ES module destructuring → CommonJS destructuring
14+
*
15+
* This allows the same TypeScript codebase to work in both GitHub Actions (ES modules)
16+
* and Azure DevOps (CommonJS) environments.
17+
*/
18+
119
const fs = require('fs');
220
const path = require('path');
321

@@ -32,6 +50,18 @@ content = content.replace(
3250
'const { createRequire: __WEBPACK_EXTERNAL_createRequire } = require("module");'
3351
);
3452

53+
// Replace createRequire usage with import.meta.url to use __filename
54+
content = content.replace(
55+
/__WEBPACK_EXTERNAL_createRequire\(import\.meta\.url\)/g,
56+
'__WEBPACK_EXTERNAL_createRequire(__filename)'
57+
);
58+
59+
// Replace any remaining import.meta.url with __filename (CommonJS equivalent)
60+
content = content.replace(
61+
/import\.meta\.url/g,
62+
'__filename'
63+
);
64+
3565
// Replace any remaining import statements
3666
content = content.replace(
3767
/^import\s+(.*?)\s*;/gm,

vss-extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifestVersion": 1,
33
"id": "component-detection-github-submission-task",
44
"name": "Component Detection for GitHub Dependency Submission",
5-
"version": "0.1.5",
5+
"version": "0.1.7",
66
"publisher": "AdvancedSecurityOSS",
77
"description": "Upload information about your dependencies to the GitHub dependency graph using Microsoft's component detection library",
88
"public": false,

0 commit comments

Comments
 (0)