Skip to content

Commit

Permalink
Replace hard default of 'json' in sourceExts with package.json Watche…
Browse files Browse the repository at this point in the history
…rImpl glob

Summary:
This follows up D9942449 (f9539ef) by replacing this internal and undocumented config merge with a more specific `**/package.json` glob pattern for Watchman within `metro-file-map`.

**Who this affects**: Only users who have overridden the `resolver.sourceExts` option and have removed `'json'` from this list.

**What this fixes**:
- **Changed**: Configuring `resolver.sourceExts` with a list that excludes `'json'` will now cause implicit `require`/`import`s of a `.json` file to fail (e.g. the resolver will **not** translate `import someData from './someData';` to `./someData.json` in an app's source code).
- **No change**: `package.json` files will always be internally recognised to be used by `metro-resolver`.

Changelog:

**[Breaking]** `.json` files will no longer be implicitly resolved if removed from `resolver.sourceExts`

Reviewed By: robhogan

Differential Revision: D37998169

fbshipit-source-id: e1d51c8fb28cd55577088e03ac689a030c7711bb
  • Loading branch information
huntie authored and facebook-github-bot committed Jul 27, 2022
1 parent 1b47931 commit a3dc30a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
8 changes: 0 additions & 8 deletions packages/metro-config/src/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,6 @@ async function loadConfig(

const overriddenConfig: {[string]: mixed} = {};

// The resolver breaks if "json" is missing from `resolver.sourceExts`
const sourceExts = configWithArgs.resolver.sourceExts;
if (!configWithArgs.resolver.sourceExts.includes('json')) {
overriddenConfig.resolver = {
sourceExts: [...sourceExts, 'json'],
};
}

overriddenConfig.watchFolders = [
configWithArgs.projectRoot,
...configWithArgs.watchFolders,
Expand Down
9 changes: 7 additions & 2 deletions packages/metro-file-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export type {
// This should be bumped whenever a code change to `metro-file-map` itself
// would cause a change to the cache data structure and/or content (for a given
// filesystem state and build parameters).
const CACHE_BREAKER = '1';
const CACHE_BREAKER = '2';

const CHANGE_INTERVAL = 30;
const MAX_WAIT_TIME = 240000;
Expand Down Expand Up @@ -867,7 +867,12 @@ export default class HasteMap extends EventEmitter {
const createWatcher = (root: Path): Promise<Watcher> => {
const watcher = new WatcherImpl(root, {
dot: true,
glob: extensions.map(extension => '**/*.' + extension),
glob: [
// Ensure we always include package.json files, which are crucial for
/// module resolution.
'**/package.json',
...extensions.map(extension => '**/*.' + extension),
],
ignored: ignorePattern,
watchmanDeferStates: this._options.watchmanDeferStates,
});
Expand Down

0 comments on commit a3dc30a

Please sign in to comment.