Skip to content

Commit 787f686

Browse files
author
Ivan Kuchaev
committed
fix: add an error for collisions
1 parent f9c839c commit 787f686

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/cli/moduleId.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const crypto = require('crypto')
66
* @param {string} path - The file path to hash.
77
* @returns {number} - The hash number not exceeding Number.MAX_SAFE_INTEGER.
88
*/
9-
function moduleId(path) {
9+
function moduleId(path, maxNumber = Number.MAX_SAFE_INTEGER) {
1010
const hash = crypto.createHash('sha256').update(path).digest('hex')
1111

12-
return parseInt(hash.substring(0, 15), 16) % Number.MAX_SAFE_INTEGER
12+
return parseInt(hash.substring(0, 15), 16) % maxNumber
1313
}
1414

1515
module.exports = {

src/metro-config/createModuleIdFactory.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@ const { moduleId } = require('../cli/moduleId')
44
const root = process.cwd()
55

66
// https://github.com/facebook/metro/blob/ed84760a8db1afc1a938c6ef005365bcf68df554/packages/metro/src/lib/createModuleIdFactory.js#L14
7-
function createModuleIdFactory() {
8-
const modules = {}
9-
return (pathToFile) => {
10-
if (modules[pathToFile]) {
11-
return modules[pathToFile]
7+
const createCreateModuleIdFactory =
8+
({ moduleIdMaxNumber } = {}) =>
9+
() => {
10+
const modules = {}
11+
const hashes = {}
12+
return (pathToFile) => {
13+
if (modules[pathToFile]) {
14+
return modules[pathToFile]
15+
}
16+
const id = moduleId(path.relative(root, pathToFile), moduleIdMaxNumber)
17+
if (hashes[id] && hashes[id] !== pathToFile) {
18+
throw new Error(`${pathToFile} collides with ${hashes[id]}`)
19+
}
20+
modules[pathToFile] = id
21+
hashes[id] = pathToFile
22+
return id
1223
}
13-
const id = moduleId(path.relative(root, pathToFile))
14-
modules[pathToFile] = id
15-
return id
1624
}
17-
}
1825

19-
module.exports = createModuleIdFactory
26+
module.exports = createCreateModuleIdFactory

src/metro-config/withChunksConfig.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require('path')
22
const { minimatch } = require('minimatch')
3-
const createModuleIdFactory = require('./createModuleIdFactory')
3+
const createCreateModuleIdFactory = require('./createModuleIdFactory')
44
const { getChunksConfig } = require('../cli/getChunksConfig')
55
const { getOutputDir } = require('../cli/utils')
66
const { MetroSerializer } = require('./MetroSerializer')
@@ -23,13 +23,13 @@ const bootstraps = [
2323
*
2424
* @param {import('metro-config').MetroConfig} config
2525
*/
26-
const serializeChunks = (config, chunksConfig, chunkId) => {
26+
const serializeChunks = (config, chunksConfig, chunkId, options) => {
2727
const customSerializer = MetroSerializer({ isChunkBundle })
2828
let result = {
2929
...config.serializer,
3030
getRunModuleStatement: customSerializer.getRunModuleStatement,
3131
customSerializer: customSerializer,
32-
createModuleIdFactory: createModuleIdFactory,
32+
createModuleIdFactory: createCreateModuleIdFactory(options),
3333
}
3434
if (!isChunkBundle) {
3535
return result
@@ -114,7 +114,7 @@ const serializeChunks = (config, chunksConfig, chunkId) => {
114114
*
115115
* @param {import('metro-config').MetroConfig} config
116116
*/
117-
function withChunksConfig(config) {
117+
function withChunksConfig(config, options) {
118118
const chunksConfig = isChunkBundle ? getChunksConfig({ root }) : null
119119

120120
return {
@@ -124,7 +124,7 @@ function withChunksConfig(config) {
124124
assetExts: [...config.resolver.assetExts, 'chunk'],
125125
resolveRequest: resolveRequest(config, chunksConfig, chunkId),
126126
},
127-
serializer: serializeChunks(config, chunksConfig, chunkId),
127+
serializer: serializeChunks(config, chunksConfig, chunkId, options),
128128
}
129129
}
130130

0 commit comments

Comments
 (0)