Skip to content

Commit 1ca2ad1

Browse files
authored
Migrate build/commands to ESM (#34222)
Migrate `build/commands` code to ESM to allow `.js`/`.ts` imports to work correctly in any direction. This allows for gradual migration to `.ts` while keeping `.js` files around. Few notes on migration: 1. `import.meta.*` cannot be used reliably with Jest. I tried different versions and approaches, eventually came up with a `.cjs` module that still uses `__dirName`, but can be imported into ESM and CommonJS modules without issues. I also considered rewriting Jest tests into built-in Node test runner, but since I was able to make Jest work, this was abandoned. 2. `tsc` accepts both `file.js`/`file.ts` in imports and passes a compiler check without errors, but Node requires an import of a real file (type stripping is based on a file type). This means an incorrect import will pass `test:scripts`, but fail on real execution (see [related discussion](microsoft/TypeScript#61021)). An eslint rule was added to enforce that. 3. local `require()` calls replaced with async/global imports depending on context.
1 parent c9936e8 commit 1ca2ad1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+397
-359
lines changed

build/commands/lib/actionGuard.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
// You can obtain one at https://mozilla.org/MPL/2.0/.
55

6-
const assert = require('assert')
7-
const fs = require('fs-extra')
8-
const path = require('path')
6+
import assert from 'assert'
7+
import fs from 'fs-extra'
8+
import path from 'path'
99

1010
// This function is used to get the call stack of the guarded operation. It is
1111
// stored in the guard file.
@@ -21,7 +21,7 @@ function getGuardCallStack() {
2121

2222
// This class is used to ensure that a given action is successfully completed,
2323
// otherwise a rerun might be required.
24-
class ActionGuard {
24+
export default class ActionGuard {
2525
// Path to the guard file.
2626
#guardFilePath
2727
// Cleanup closure to perform a cleanup (optional) before running the action.
@@ -86,5 +86,3 @@ class ActionGuard {
8686
}
8787
}
8888
}
89-
90-
module.exports = ActionGuard

build/commands/lib/actionGuard.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
// You can obtain one at https://mozilla.org/MPL/2.0/.
55

6-
const fs = require('fs-extra')
7-
const path = require('path')
8-
const ActionGuard = require('./actionGuard')
6+
import fs from 'fs-extra'
7+
import path from 'path'
8+
import ActionGuard from './actionGuard.js'
99

1010
describe('ActionGuard', () => {
1111
const guardFilePath = '/path/to/guard/file'

build/commands/lib/affectedTests.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
// You can obtain one at https://mozilla.org/MPL/2.0/.
55

6-
const { promisify } = require('util')
7-
const { readFile, writeFile } = require('fs/promises')
8-
const exec = promisify(require('child_process').execFile)
9-
const path = require('path')
10-
const config = require('./config')
11-
const { unlink } = require('fs-extra')
12-
const { randomUUID } = require('crypto')
13-
const {
6+
import { promisify } from 'node:util'
7+
import { readFile, writeFile } from 'fs/promises'
8+
import child_process from 'node:child_process'
9+
import path from 'path'
10+
import config from './config.js'
11+
import fs from 'fs-extra'
12+
import { randomUUID } from 'crypto'
13+
import { tmpdir } from 'os'
14+
import {
1415
getApplicableFilters,
1516
getTestsToRun,
1617
gnTargetToExecutableName,
17-
} = require('./testUtils')
18-
const { tmpdir } = require('os')
18+
} from './testUtils.js'
19+
20+
const exec = promisify(child_process.execFile)
1921

2022
const getTestTargets = (outDir, filters = ['//*']) => {
2123
const { env, shell } = config.defaultOptions
@@ -113,7 +115,7 @@ async function analyzeAffectedTests(
113115

114116
const output = await readFile(analyzeOutJson, 'utf-8').then(JSON.parse)
115117

116-
await Promise.all([unlink(analyzeJson), unlink(analyzeOutJson)])
118+
await Promise.all([fs.unlink(analyzeJson), fs.unlink(analyzeOutJson)])
117119

118120
return {
119121
outDir,
@@ -167,7 +169,4 @@ async function getAffectedTests(args = {}) {
167169
return [...new Set([...affectedTests, ...testAffectedDueModifiedFilterFiles])]
168170
}
169171

170-
module.exports = {
171-
analyzeAffectedTests,
172-
getAffectedTests,
173-
}
172+
export { analyzeAffectedTests, getAffectedTests }

build/commands/lib/applyPatches.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
// You can obtain one at https://mozilla.org/MPL/2.0/.
55

6-
const config = require('../lib/config')
7-
const util = require('../lib/util')
6+
import config from './config.js'
7+
import util from './util.js'
88

99
const applyPatches = (
1010
buildConfig = config.defaultBuildConfig,
@@ -22,4 +22,4 @@ const applyPatches = (
2222
})
2323
}
2424

25-
module.exports = applyPatches
25+
export default applyPatches

build/commands/lib/branding.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
// You can obtain one at https://mozilla.org/MPL/2.0/.
55

6-
const path = require('path')
7-
const config = require('./config')
8-
const fs = require('fs-extra')
9-
const l10nUtil = require('./l10nUtil')
10-
const util = require('./util')
11-
const Log = require('./logging')
6+
import path from 'path'
7+
import config from './config.js'
8+
import fs from 'fs-extra'
9+
import l10nUtil from './l10nUtil.js'
10+
import util from './util.js'
11+
import Log from './logging.js'
1212

13-
exports.update = () => {
13+
const update = () => {
1414
Log.progressStart('update branding')
1515
const chromeComponentsDir = path.join(config.srcDir, 'components')
1616
const braveComponentsDir = path.join(config.braveCoreDir, 'components')
@@ -515,3 +515,5 @@ exports.update = () => {
515515
}
516516
Log.progressFinish('update branding')
517517
}
518+
519+
export default { update }

build/commands/lib/build.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
// You can obtain one at https://mozilla.org/MPL/2.0/.
55

6-
const config = require('../lib/config')
7-
const util = require('../lib/util')
8-
const path = require('path')
9-
const fs = require('fs-extra')
10-
const Log = require('../lib/logging')
11-
const branding = require('../lib/branding')
6+
import config from './config.js'
7+
import util from './util.js'
8+
import path from 'path'
9+
import fs from 'fs-extra'
10+
import Log from './logging.js'
11+
import branding from './branding.js'
1212

1313
/**
1414
* Checks to make sure the src/chrome/VERSION matches brave-core's package.json version
@@ -55,4 +55,4 @@ const build = async (buildConfig = config.defaultBuildConfig, options = {}) => {
5555
}
5656
}
5757

58-
module.exports = build
58+
export default build

build/commands/lib/buildChromiumRelease.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
// Designed to be used on CI, but should work locally too.
99
// The script includes syncing; there is no need to run npm run sync before.
1010

11-
const config = require('./config')
12-
const util = require('./util')
13-
const path = require('path')
14-
const fs = require('fs-extra')
15-
const depotTools = require('./depotTools')
16-
const syncUtil = require('./syncUtils')
17-
const Log = require('./logging')
11+
import config from './config.js'
12+
import util from './util.js'
13+
import path from 'path'
14+
import fs from 'fs-extra'
15+
import depotTools from './depotTools.js'
16+
import syncUtil from './syncUtils.js'
17+
import Log from './logging.js'
1818

1919
// Use the same filename as for Brave archive.
2020
const getOutputFilename = () => {
@@ -212,4 +212,4 @@ function buildChromiumRelease(buildOptions = {}) {
212212
})
213213
}
214214

215-
module.exports = buildChromiumRelease
215+
export default buildChromiumRelease

build/commands/lib/calculateFileChecksum.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
// you can obtain one at https://mozilla.org/MPL/2.0/.
55

6-
const crypto = require('crypto')
7-
const fs = require('fs')
6+
import crypto from 'crypto'
7+
import fs from 'fs'
88

9-
module.exports = function CalculateFileChecksum(
10-
filePath,
11-
algorithm = 'sha256',
12-
) {
9+
export default function calculateFileChecksum(filePath, algorithm = 'sha256') {
1310
return new Promise((resolve, reject) => {
1411
try {
1512
const checksumGenerator = crypto.createHash(algorithm)

build/commands/lib/calculateFileChecksum.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
// You can obtain one at https://mozilla.org/MPL/2.0/.
55

6-
const path = require('path')
7-
const fs = require('fs-extra')
8-
const os = require('os')
9-
const calculateFileChecksum = require('./calculateFileChecksum')
6+
import path from 'path'
7+
import fs from 'fs-extra'
8+
import os from 'os'
9+
import calculateFileChecksum from './calculateFileChecksum.js'
1010

1111
const dirPrefixTmp = 'brave-browser-test-calculate-file-checksum-'
1212
const testFile1Name = 'file1'

build/commands/lib/checkEnvironment.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
* You can obtain one at https://mozilla.org/MPL/2.0/. */
55

6-
const fs = require('fs')
7-
const path = require('path')
8-
const semver = require('semver')
9-
const Log = require('./logging')
6+
import fs from 'fs'
7+
import path from 'path'
8+
import semver from 'semver'
9+
import Log from './logging.js'
1010

1111
checkNodeVersion()
1212
checkNpmVersion()

0 commit comments

Comments
 (0)