-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Implements `require.resolveWeak`, a low-level [Webpack API](https://webpack.js.org/api/module-methods/#requireresolveweak) that returns a module's ID without necessarily including it in the bundle. Reviewed By: GijsWeterings Differential Revision: D42621299 fbshipit-source-id: ad9b53c45aeafe8ae2db0d54f7ae81d7e77f1906
- Loading branch information
1 parent
c8da588
commit 354d6e4
Showing
13 changed files
with
321 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/metro/src/integration_tests/__tests__/require-resolveWeak-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Metro = require('../../..'); | ||
const execBundle = require('../execBundle'); | ||
|
||
jest.unmock('cosmiconfig'); | ||
|
||
jest.setTimeout(30 * 1000); | ||
|
||
test('resolveWeak() returns a different ID for each resolved module', async () => { | ||
await expect(execTest('require-resolveWeak/multiple.js')).resolves.toEqual({ | ||
counterModuleId1: 1, | ||
counterModuleId2: 1, | ||
throwingModuleId: 2, | ||
}); | ||
}); | ||
|
||
describe('resolveWeak() without calling require()', () => { | ||
test('runtime semantics', async () => { | ||
await expect( | ||
execTest('require-resolveWeak/never-required.js'), | ||
).resolves.toEqual({ | ||
moduleId: 1, | ||
}); | ||
}); | ||
|
||
test('the weak dependency is omitted', async () => { | ||
const {code} = await buildTest('require-resolveWeak/never-required.js'); | ||
expect(code).not.toContain('This module cannot be evaluated.'); | ||
}); | ||
}); | ||
|
||
test('calling both require() and resolveWeak() with the same module', async () => { | ||
await expect( | ||
execTest('require-resolveWeak/require-and-resolveWeak.js'), | ||
).resolves.toEqual({ | ||
moduleId: 1, | ||
timesIncremented: 2, | ||
}); | ||
}); | ||
|
||
test('calling both import() and resolveWeak() with the same module', async () => { | ||
await expect( | ||
execTest('require-resolveWeak/import-and-resolveWeak.js'), | ||
).resolves.toEqual({ | ||
moduleId: 1, | ||
timesIncremented: 2, | ||
}); | ||
}); | ||
|
||
async function buildTest(entry, {dev = true}: $ReadOnly<{dev: boolean}> = {}) { | ||
const config = await Metro.loadConfig({ | ||
config: require.resolve('../metro.config.js'), | ||
}); | ||
|
||
const result = await Metro.runBuild(config, { | ||
entry, | ||
dev, | ||
minify: !dev, | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
async function execTest(entry, {dev = true}: $ReadOnly<{dev: boolean}> = {}) { | ||
const result = await buildTest(entry, {dev}); | ||
return execBundle(result.code); | ||
} |
33 changes: 33 additions & 0 deletions
33
...es/metro/src/integration_tests/basic_bundle/require-resolveWeak/import-and-resolveWeak.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
|
||
import type {RequireWithResolveWeak} from './utils'; | ||
|
||
declare var require: RequireWithResolveWeak; | ||
|
||
async function main() { | ||
const moduleId = require.resolveWeak('./subdir/counter-module'); | ||
|
||
// Require the module statically via its path, spelled slightly differently | ||
(await import('./subdir/counter-module.js')).increment(); | ||
|
||
const dynamicRequire = require; | ||
|
||
// Require the module dynamically via its ID | ||
const timesIncremented = dynamicRequire(moduleId).increment(); | ||
|
||
return { | ||
moduleId, | ||
// Should be 2, proving there's just one module instance | ||
timesIncremented, | ||
}; | ||
} | ||
|
||
module.exports = (main(): mixed); |
23 changes: 23 additions & 0 deletions
23
packages/metro/src/integration_tests/basic_bundle/require-resolveWeak/multiple.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
|
||
import type {RequireWithResolveWeak} from './utils'; | ||
|
||
declare var require: RequireWithResolveWeak; | ||
|
||
async function main() { | ||
return { | ||
counterModuleId1: require.resolveWeak('./subdir/counter-module'), | ||
counterModuleId2: require.resolveWeak('./subdir/counter-module.js'), | ||
throwingModuleId: require.resolveWeak('./subdir/throwing-module.js'), | ||
}; | ||
} | ||
|
||
module.exports = (main(): mixed); |
21 changes: 21 additions & 0 deletions
21
packages/metro/src/integration_tests/basic_bundle/require-resolveWeak/never-required.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
|
||
import type {RequireWithResolveWeak} from './utils'; | ||
|
||
declare var require: RequireWithResolveWeak; | ||
|
||
function main() { | ||
return { | ||
moduleId: require.resolveWeak('./subdir/throwing-module'), | ||
}; | ||
} | ||
|
||
module.exports = (main(): mixed); |
33 changes: 33 additions & 0 deletions
33
...s/metro/src/integration_tests/basic_bundle/require-resolveWeak/require-and-resolveWeak.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
|
||
import type {RequireWithResolveWeak} from './utils'; | ||
|
||
declare var require: RequireWithResolveWeak; | ||
|
||
function main() { | ||
const moduleId = require.resolveWeak('./subdir/counter-module'); | ||
|
||
const dynamicRequire = require; | ||
|
||
// Require the module dynamically via its ID | ||
dynamicRequire(moduleId).increment(); | ||
|
||
// Require the module statically via its path, spelled slightly differently | ||
const timesIncremented = require('./subdir/counter-module.js').increment(); | ||
|
||
return { | ||
moduleId, | ||
// Should be 2, proving there's just one module instance | ||
timesIncremented, | ||
}; | ||
} | ||
|
||
module.exports = (main(): mixed); |
18 changes: 18 additions & 0 deletions
18
...ges/metro/src/integration_tests/basic_bundle/require-resolveWeak/subdir/counter-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
|
||
let count: number = 0; | ||
|
||
module.exports = { | ||
increment(): number { | ||
++count; | ||
return count; | ||
}, | ||
}; |
Oops, something went wrong.
354d6e4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you use
import()
in RN then? What's needed for splitting in RN?