Skip to content

Commit a6ff3de

Browse files
committed
Update custom resolver to consider package exports
1 parent bc7967b commit a6ff3de

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ module.exports = {
137137
jsdoc: {
138138
mode: 'typescript',
139139
},
140+
'import/resolver': require.resolve( './tools/eslint/import-resolver' ),
140141
},
141142
rules: {
142143
'jest/expect-expect': 'off',

packages/react-native-editor/.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module.exports = {
1414
version: 'detect',
1515
flowVersion: '0.92.0',
1616
},
17+
'import/resolver': require.resolve(
18+
'../../tools/eslint/import-resolver'
19+
),
1720
},
1821
rules: {
1922
'no-restricted-syntax': [

platform-docs/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
version: 'detect',
99
flowVersion: '0.92.0',
1010
},
11+
'import/resolver': require.resolve( '../tools/eslint/import-resolver' ),
1112
},
1213
overrides: [
1314
{

tools/eslint/import-resolver.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* External dependencies
3+
*/
4+
const resolverNode = require( 'eslint-import-resolver-typescript' );
5+
const path = require( 'path' );
6+
const fs = require( 'fs' );
7+
const PACKAGES_DIR = path.resolve( __dirname, '../../packages' );
8+
9+
exports.interfaceVersion = 2;
10+
11+
exports.resolve = function ( source, file, config ) {
12+
const resolve = ( sourcePath ) =>
13+
resolverNode.resolve( sourcePath, file, {
14+
...config,
15+
extensions: [ '.tsx', '.ts', '.mjs', '.js', '.json', '.node' ],
16+
} );
17+
18+
if ( source.startsWith( '@wordpress/' ) ) {
19+
const [ , packageName, ...pathParts ] = source.split( '/' );
20+
const subpath = path.join( '.', pathParts.join( '/' ) );
21+
22+
// Consider whether the package is local to the project. If it's not,
23+
// use the default resolution behavior.
24+
const packagePath = path.join( PACKAGES_DIR, packageName );
25+
if ( ! fs.existsSync( packagePath ) ) {
26+
return resolve( source );
27+
}
28+
29+
// For all local packages, ensure that we can resolve the requested
30+
// source file using its declared exports.
31+
try {
32+
const manifestPath = path.join( packagePath, 'package.json' );
33+
const manifest = require( manifestPath );
34+
const exportPath = manifest.exports?.[ subpath ]?.import;
35+
const sourcePath = exportPath.replace( 'build-module', 'src' );
36+
37+
return resolve( path.join( packagePath, sourcePath ) );
38+
} catch {
39+
return { found: false };
40+
}
41+
}
42+
43+
return resolve( source );
44+
};

0 commit comments

Comments
 (0)