-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[0.71] Add saveAssetPlugin to fix long path assets (#12006)
* [Win32] Reduce usage of long paths in assets which can cause long path issues (#11839) * Add saveAssetPlugin to fix long path assets * Change files * comment * add back compat * fix * turn off win32 assetPlugin for now * fix * fix * fix * Change files * fix * fix
- Loading branch information
1 parent
a69f755
commit 8195305
Showing
8 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@office-iss-react-native-win32-43b80e6f-96e4-479c-b4d7-46c1fb056647.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Add saveAssetPlugin to fix long path assets", | ||
"packageName": "@office-iss/react-native-win32", | ||
"email": "30809111+acoates-ms@users.noreply.github.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/@office-iss/react-native-win32/metroShortPathAssetDataPlugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// @ts-check | ||
/** | ||
* @typedef {import("metro").AssetData} AssetData; | ||
**/ | ||
|
||
/** | ||
* @param {AssetData & {__useShortPath: boolean}} asset | ||
* @returns {Promise<AssetData>} | ||
*/ | ||
async function metroShortPathAssetDataPlugin(asset) { | ||
asset.__useShortPath = true; | ||
return Promise.resolve(asset); | ||
} | ||
|
||
module.exports = metroShortPathAssetDataPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/@office-iss/react-native-win32/saveAssetPlugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// @ts-check | ||
const path = require('path'); | ||
const ensureShortPath = require('./Libraries/Image/assetPaths'); | ||
|
||
/** | ||
* @typedef {import("metro").AssetData} AssetData; | ||
**/ | ||
|
||
/** | ||
* @param {AssetData} asset | ||
* @param {number} scale | ||
* @returns {string} | ||
*/ | ||
function getAssetDestPath(asset, scale) { | ||
const suffix = scale === 1 ? '' : `@${scale}x`; | ||
const fileName = `${asset.name + suffix}.${asset.type}`; | ||
return path.join( | ||
// Assets can have relative paths outside of the project root. | ||
// Replace `../` with `_` to make sure they don't end up outside of | ||
// the expected assets directory. | ||
ensureShortPath(asset.httpServerLocation.substr(1).replace(/\.\.\//g, '_')), | ||
fileName, | ||
); | ||
} | ||
|
||
/** | ||
* @param {ReadonlyArray<AssetData>} assets | ||
* @param {string} _platform | ||
* @param {string | undefined} _assetsDest | ||
* @param {string | undefined} _assetCatalogDest | ||
* @param {(asset: AssetData, allowedScales: number[] | undefined, getAssetDestPath: (asset: AssetData, scale: number) => string) => void} addAssetToCopy | ||
*/ | ||
function saveAssetsWin32( | ||
assets, | ||
_platform, | ||
_assetsDest, | ||
_assetCatalogDest, | ||
addAssetToCopy, | ||
) { | ||
assets.forEach((asset) => | ||
addAssetToCopy(asset, undefined, getAssetDestPath), | ||
); | ||
} | ||
|
||
module.exports = saveAssetsWin32; |
36 changes: 36 additions & 0 deletions
36
packages/@office-iss/react-native-win32/src/Libraries/Image/assetPaths.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT License. | ||
* | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// Some windows machines may not have long paths enabled | ||
// https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation | ||
// Assets in nested node_modules (common when using pnpm) - end up creating very long paths | ||
// Using this function we shorten longer paths to prevent paths from hitting the path limit | ||
function ensureShortPath(str: string): string { | ||
if (str.length < 40) return str; | ||
|
||
const assetsPrefix = 'assets/'; | ||
|
||
if (!str.startsWith(assetsPrefix)) { | ||
console.warn(`Unexpected asset uri - ${str} may not load correctly.`); | ||
} | ||
|
||
const postStr = str.slice(assetsPrefix.length); | ||
var hash = 0, | ||
i, | ||
chr; | ||
for (i = 0; i < postStr.length; i++) { | ||
chr = postStr.charCodeAt(i); | ||
hash = (hash << 5) - hash + chr; | ||
hash |= 0; // Convert to 32bit integer | ||
} | ||
return assetsPrefix + hash.toString(); | ||
} | ||
|
||
module.exports = ensureShortPath; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters