Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the changes in our custom metro config #4939

Merged
3 commits merged into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "prerelease",
"comment": "Reduce need for custom metro configs",
"packageName": "@office-iss/react-native-win32",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-05-18T16:46:03.284Z"
}
8 changes: 8 additions & 0 deletions change/react-native-windows-2020-05-18-09-46-04-metrofix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "prerelease",
"comment": "Reduce need for custom metro configs",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-05-18T16:46:04.876Z"
}
13 changes: 0 additions & 13 deletions packages/E2ETest/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
*/
const path = require('path');
const blacklist = require('metro-config/src/defaults/blacklist');
const {
getModulesRunBeforeMainModule,
reactNativePlatformResolver,
} = require('react-native-windows/metro-react-native-platform');

const rnwPath = path.resolve(__dirname, '../../vnext');

Expand All @@ -23,9 +19,6 @@ module.exports = {
],

resolver: {
resolveRequest: reactNativePlatformResolver({
windows: 'react-native-windows',
}),
extraNodeModules: {
// Redirect metro to rnwPath instead of node_modules/react-native-windows, since metro doesn't like symlinks
'react-native-windows': rnwPath,
Expand All @@ -38,13 +31,7 @@ module.exports = {
),
]),
},
serializer: {
getModulesRunBeforeMainModule,
},
transformer: {
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
// Hopefully we can fix the default in the future
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
Expand Down
14 changes: 0 additions & 14 deletions packages/microsoft-reactnative-sampleapps/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
*/
const path = require('path');
const blacklist = require('metro-config/src/defaults/blacklist');
const {
getModulesRunBeforeMainModule,
reactNativePlatformResolver,
} = require('react-native-windows/metro-react-native-platform');

const rnwPath = path.resolve(__dirname, '../../vnext');

module.exports = {
Expand All @@ -23,9 +18,6 @@ module.exports = {
],

resolver: {
resolveRequest: reactNativePlatformResolver({
windows: 'react-native-windows',
}),
extraNodeModules: {
// Redirect metro to rnwPath instead of node_modules/react-native-windows, since metro doesn't like symlinks
'react-native-windows': rnwPath,
Expand All @@ -40,13 +32,7 @@ module.exports = {
),
]),
},
serializer: {
getModulesRunBeforeMainModule,
},
transformer: {
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
// Hopefully we can fix the default in the future
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
Expand Down
13 changes: 0 additions & 13 deletions packages/playground/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
const fs = require('fs');
const path = require('path');
const blacklist = require('metro-config/src/defaults/blacklist');
const {
getModulesRunBeforeMainModule,
reactNativePlatformResolver,
} = require('react-native-windows/metro-react-native-platform');

const rnwPath = fs.realpathSync(
path.resolve(require.resolve('react-native-windows/package.json'), '..'),
Expand All @@ -26,9 +22,6 @@ module.exports = {
],

resolver: {
resolveRequest: reactNativePlatformResolver({
windows: 'react-native-windows',
}),
extraNodeModules: {
// Redirect react-native-windows to avoid symlink (metro doesn't like symlinks)
'react-native-windows': rnwPath,
Expand All @@ -40,13 +33,7 @@ module.exports = {
),
]),
},
serializer: {
getModulesRunBeforeMainModule,
},
transformer: {
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
// Hopefully we can fix the default in the future
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
Expand Down
15 changes: 0 additions & 15 deletions packages/react-native-win32/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
*/
const fs = require('fs');
const path = require('path');
const {
getModulesRunBeforeMainModule,
reactNativePlatformResolver,
} = require('./metro-react-native-platform');

module.exports = {
// WatchFolders is only needed due to the yarn workspace layout of node_modules, we need to watch the symlinked locations separately
Expand All @@ -15,18 +11,7 @@ module.exports = {
path.resolve(__dirname, '../../node_modules'),
],

resolver: {
resolveRequest: reactNativePlatformResolver({
win32: '@office-iss/react-native-win32',
}),
},
serializer: {
getModulesRunBeforeMainModule,
},
transformer: {
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
// Hopefully we can fix the default in the future
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-win32/react-native.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
linkConfig: () => null,
projectConfig: (projectRoot, projectParams) => null,
dependencyConfig: (projectRoot, dependencyParams) => null,
npmPackageName: '@office-iss/react-native-win32',
},
},
};
14 changes: 0 additions & 14 deletions vnext/local-cli/generator-windows/templates/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@
const path = require('path');
const blacklist = require('metro-config/src/defaults/blacklist');

const {
getModulesRunBeforeMainModule,
reactNativePlatformResolver,
} = require('react-native-windows/metro-react-native-platform');

module.exports = {
resolver: {
resolveRequest: reactNativePlatformResolver({
windows: 'react-native-windows',
}),
blacklistRE: blacklist([
// This stops "react-native run-windows" from causing the metro server to crash if its already running
new RegExp(
Expand All @@ -30,13 +22,7 @@ module.exports = {
),
]),
},
serializer: {
getModulesRunBeforeMainModule,
},
transformer: {
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
// Hopefully we can fix the default in the future
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
Expand Down
77 changes: 0 additions & 77 deletions vnext/metro-react-native-platform.js

This file was deleted.

39 changes: 27 additions & 12 deletions vnext/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,34 @@
*/
const fs = require('fs');
const path = require('path');
const rnwPath = __dirname;
const {resolve} = require('metro-resolver');

const {
getModulesRunBeforeMainModule,
reactNativePlatformResolver,
} = require('react-native-windows/metro-react-native-platform');
function reactNativePlatformResolver(platformImplementations) {
return (context, _realModuleName, platform, moduleName) => {
let backupResolveRequest = context.resolveRequest;
delete context.resolveRequest;

const rnwPath = __dirname;
try {
let modifiedModuleName = moduleName;
if (platformImplementations[platform]) {
if (moduleName === 'react-native') {
modifiedModuleName = platformImplementations[platform];
} else if (moduleName.startsWith('react-native/')) {
modifiedModuleName = `${
platformImplementations[platform]
}/${modifiedModuleName.slice('react-native/'.length)}`;
}
}
let result = resolve(context, modifiedModuleName, platform);
return result;
} catch (e) {
throw e;
} finally {
context.resolveRequest = backupResolveRequest;
}
};
}

module.exports = {
// WatchFolders is only needed due to the yarn workspace layout of node_modules, we need to watch the symlinked locations separately
Expand All @@ -19,6 +40,7 @@ module.exports = {
],

resolver: {
// We need a custom resolveRequest right now since our integration tests use a "windesktop" platform thats specific to integration tests.
resolveRequest: reactNativePlatformResolver({
windesktop: 'react-native-windows',
windows: 'react-native-windows',
Expand All @@ -29,14 +51,7 @@ module.exports = {
},
},

serializer: {
getModulesRunBeforeMainModule,
},

transformer: {
// The cli defaults this to a full path to react-native, which bypasses the reactNativePlatformResolver above
// Hopefully we can fix the default in the future
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
Expand Down
1 change: 1 addition & 0 deletions vnext/react-native.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
linkConfig: () => null,
projectConfig: projectConfig.projectConfigWindows,
dependencyConfig: dependencyConfig.dependencyConfigWindows,
npmPackageName: 'react-native-windows',
},
},
};
Loading