Skip to content

Commit

Permalink
Enable devMode with react-native init-windows old/* templates (#13362)
Browse files Browse the repository at this point in the history
## Description

The `init-windows` CLI command can be used to create apps with the old arch templates, but "devMode" (i.e. using your local, linked RNW repo instead of the package from NPM) didn't work.

This PR fixes that functionality so `init-windows` can become the new method.

### Type of Change
- Bug fix (non-breaking change which fixes an issue)

### Why
We want `init-windows` to be the default method for creating new RNW projects, but it still needs to support the old templates for now, and this was the last "missing" feature.

### What
Switched the old templates to using the same `metro.config.js` file that the new templates use (with devMode template variable) and also piping through the correct value for that variable.

## Screenshots
N/A

## Testing
Verified I could create a new (old) app locally with linked RNW and it recognized and implemented devMode correctly, so that the app could launch.

## Changelog
Should this change be included in the release notes: yes

Enabled detecting devMode with `react-native init-window`s and the old/* templates
  • Loading branch information
jonthysell committed Jun 18, 2024
1 parent 4621ce6 commit 813ba19
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Enable devMode with react-native init-windows old/* templates",
"packageName": "@react-native-windows/cli",
"email": "jthysell@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Enable devMode with react-native init-windows old/* templates",
"packageName": "react-native-windows",
"email": "jthysell@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ export async function copyProjectTemplateAndReplace(
packageGuid: packageGuid,
currentUser: currentUser,

devMode: options.useDevMode,

useExperimentalNuget: options.experimentalNuGetDependency,
nuGetTestFeed: options.nuGetTestFeed,
nuGetADOFeed: nugetVersion.startsWith('0.0.0-'),
Expand Down Expand Up @@ -230,12 +232,7 @@ export async function copyProjectTemplateAndReplace(
? [
// app common mappings
{
from: path.join(
srcRootPath,
options.useDevMode
? 'metro.devMode.config.js'
: 'metro.config.js',
),
from: path.join(srcRootPath, 'metro.config.js'),
to: 'metro.config.js',
},
{
Expand Down
15 changes: 13 additions & 2 deletions vnext/template/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const rnwPath = fs.realpathSync(
path.resolve(require.resolve('react-native-windows/package.json'), '..'),
);

//{{#devMode}} [devMode
const rnwRootNodeModules = path.resolve(rnwPath, '..', 'node_modules');
const rnwPackages = path.resolve(rnwPath, '..', 'packages');
// devMode]{{/devMode}}

/**
* Metro configuration
* https://facebook.github.io/metro/docs/configuration
Expand All @@ -16,6 +21,9 @@ const rnwPath = fs.realpathSync(
*/

const config = {
//{{#devMode}} [devMode
watchFolders: [rnwPath, rnwRootNodeModules, rnwPackages],
// devMode]{{/devMode}}
resolver: {
blockList: exclusionList([
// This stops "react-native run-windows" from causing the metro server to crash if its already running
Expand All @@ -27,6 +35,11 @@ const config = {
new RegExp(`${rnwPath}/target/.*`),
/.*\.ProjectImports\.zip/,
]),
//{{#devMode}} [devMode
extraNodeModules: {
'react-native-windows': rnwPath,
},
// devMode]{{/devMode}}
},
transformer: {
getTransformOptions: async () => ({
Expand All @@ -35,8 +48,6 @@ const config = {
inlineRequires: true,
},
}),
// This fixes the 'missing-asset-registry-path` error (see https://github.com/microsoft/react-native-windows/issues/11437)
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
},
};

Expand Down
56 changes: 0 additions & 56 deletions vnext/template/metro.devMode.config.js

This file was deleted.

5 changes: 4 additions & 1 deletion vnext/templates/old/generateWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

const generateWindows = require('../../generate');
const templateUtils = require('../templateUtils');

function makeGenerateWindowsWrapper(
language = 'cpp',
Expand All @@ -33,6 +34,8 @@ function makeGenerateWindowsWrapper(
const postInstall = async (config = {}, options = {}) => {
const experimentalFeatures = config?.project?.windows?.experimentalFeatures;

const rnwInfo = templateUtils.getRnwInfo(config, options);

const generateOptions = {
overwrite: !!options.overwrite,
language,
Expand All @@ -41,7 +44,7 @@ function makeGenerateWindowsWrapper(
experimentalFeatures?.UseExperimentalNuget ?? false,
useWinUI3: experimentalFeatures?.UseWinUI3 ?? false,
useHermes: experimentalFeatures?.UseHermes ?? true,
useDevMode: false,
useDevMode: rnwInfo.devMode,
verbose: !!options.logging,
telemetry: !!options.telemetry,
};
Expand Down

0 comments on commit 813ba19

Please sign in to comment.