-
Notifications
You must be signed in to change notification settings - Fork 58
/
metro.config.js
93 lines (80 loc) · 2.7 KB
/
metro.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const path = require( 'path' );
const fs = require( 'fs' );
const metroResolver = require( 'metro-resolver' );
const nodeModulePaths = [
'../../node_modules',
'../../../jetpack/projects/plugins/jetpack/node_modules',
];
const gutenbergMetroConfig = require( './gutenberg/packages/react-native-editor/metro.config.js' );
const extraNodeModules = {};
const gutenbergMetroConfigCopy = {
...gutenbergMetroConfig,
projectRoot: path.resolve( __dirname ),
resolver: {
...gutenbergMetroConfig.resolver,
unstable_enableSymlinks: false,
sourceExts: [ 'js', 'cjs', 'jsx', 'json', 'scss', 'sass', 'ts', 'tsx' ],
extraNodeModules,
// Exclude `ios-xcframework` folder to avoid conflicts with packages contained in Pods.
blockList: [
/ios-xcframework\/.*/,
// Exclude all @wordpress packages in the "block-experiments" folder,
// this prevents issues with older versions of native files.
// We are importing Gutenberg directly so all packages are already available.
/block-experiments\/node_modules\/@wordpress\/.*/,
],
},
};
const possibleModulePaths = ( name ) =>
nodeModulePaths.map( ( dir ) => path.join( process.cwd(), dir, name ) );
gutenbergMetroConfigCopy.resolver.resolveRequest = (
context,
moduleName,
platform
) => {
// This handles part of the Jetpack Config setup typically handled by Webpack's externals.
if ( moduleName.startsWith( '@automattic/jetpack-config' ) ) {
return {
filePath: path.resolve( __dirname + '/src/jetpack-config.js' ),
type: 'sourceFile',
};
}
// Add the module to the extra node modules object if the module is not on a local path.
if ( ! ( moduleName.startsWith( '.' ) || moduleName.startsWith( '/' ) ) ) {
const [ namespace, module = '' ] = moduleName.split( '/' );
const name = path.join( namespace, module );
if ( ! extraNodeModules[ name ] ) {
let extraNodeModulePath;
const modulePath = possibleModulePaths( name ).find(
fs.existsSync
);
extraNodeModulePath = modulePath && fs.realpathSync( modulePath );
// If we haven't resolved the module yet, check if the module is managed by pnpm.
if (
! extraNodeModulePath &&
context.originModulePath.includes( '.pnpm' )
) {
const filePath = require.resolve( name, {
paths: [ path.dirname( context.originModulePath ) ],
} );
const innerNodeModules =
filePath.match( /.*node_modules/ )?.[ 0 ];
extraNodeModulePath =
innerNodeModules && path.join( innerNodeModules, name );
}
if ( extraNodeModulePath ) {
extraNodeModules[ name ] = extraNodeModulePath;
}
}
}
// Restore the original resolver
return metroResolver.resolve(
{
...context,
resolveRequest: null,
},
moduleName,
platform
);
};
module.exports = gutenbergMetroConfigCopy;