-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e24be30
commit 92a0b45
Showing
86 changed files
with
5,453 additions
and
21 deletions.
There are no files selected for viewing
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
77 changes: 77 additions & 0 deletions
77
packages/community-cli-plugin/dist/commands/bundle/assetCatalogIOS.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,77 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true, | ||
}); | ||
exports.cleanAssetCatalog = cleanAssetCatalog; | ||
exports.getImageSet = getImageSet; | ||
exports.isCatalogAsset = isCatalogAsset; | ||
exports.writeImageSet = writeImageSet; | ||
var _assetPathUtils = _interopRequireDefault(require("./assetPathUtils")); | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _path = _interopRequireDefault(require("path")); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { default: obj }; | ||
} | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
function cleanAssetCatalog(catalogDir) { | ||
const files = _fs.default | ||
.readdirSync(catalogDir) | ||
.filter((file) => file.endsWith(".imageset")); | ||
for (const file of files) { | ||
_fs.default.rmSync(_path.default.join(catalogDir, file), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
} | ||
} | ||
function getImageSet(catalogDir, asset, scales) { | ||
const fileName = _assetPathUtils.default.getResourceIdentifier(asset); | ||
return { | ||
basePath: _path.default.join(catalogDir, `${fileName}.imageset`), | ||
files: scales.map((scale, idx) => { | ||
const suffix = scale === 1 ? "" : `@${scale}x`; | ||
return { | ||
name: `${fileName + suffix}.${asset.type}`, | ||
scale, | ||
src: asset.files[idx], | ||
}; | ||
}), | ||
}; | ||
} | ||
function isCatalogAsset(asset) { | ||
return asset.type === "png" || asset.type === "jpg" || asset.type === "jpeg"; | ||
} | ||
function writeImageSet(imageSet) { | ||
_fs.default.mkdirSync(imageSet.basePath, { | ||
recursive: true, | ||
}); | ||
for (const file of imageSet.files) { | ||
const dest = _path.default.join(imageSet.basePath, file.name); | ||
_fs.default.copyFileSync(file.src, dest); | ||
} | ||
_fs.default.writeFileSync( | ||
_path.default.join(imageSet.basePath, "Contents.json"), | ||
JSON.stringify({ | ||
images: imageSet.files.map((file) => ({ | ||
filename: file.name, | ||
idiom: "universal", | ||
scale: `${file.scale}x`, | ||
})), | ||
info: { | ||
author: "xcode", | ||
version: 1, | ||
}, | ||
}) | ||
); | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/community-cli-plugin/dist/commands/bundle/assetCatalogIOS.js.flow
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,29 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
import type { AssetData } from "metro/src/Assets"; | ||
|
||
declare export function cleanAssetCatalog(catalogDir: string): void; | ||
|
||
type ImageSet = { | ||
basePath: string, | ||
files: { name: string, src: string, scale: number }[], | ||
}; | ||
|
||
declare export function getImageSet( | ||
catalogDir: string, | ||
asset: AssetData, | ||
scales: $ReadOnlyArray<number> | ||
): ImageSet; | ||
|
||
declare export function isCatalogAsset(asset: AssetData): boolean; | ||
|
||
declare export function writeImageSet(imageSet: ImageSet): void; |
79 changes: 79 additions & 0 deletions
79
packages/community-cli-plugin/dist/commands/bundle/assetPathUtils.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,79 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true, | ||
}); | ||
exports.default = void 0; | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
/** | ||
* FIXME: using number to represent discrete scale numbers is fragile in essence because of | ||
* floating point numbers imprecision. | ||
*/ | ||
function getAndroidAssetSuffix(scale) { | ||
switch (scale) { | ||
case 0.75: | ||
return "ldpi"; | ||
case 1: | ||
return "mdpi"; | ||
case 1.5: | ||
return "hdpi"; | ||
case 2: | ||
return "xhdpi"; | ||
case 3: | ||
return "xxhdpi"; | ||
case 4: | ||
return "xxxhdpi"; | ||
default: | ||
return ""; | ||
} | ||
} | ||
|
||
// See https://developer.android.com/guide/topics/resources/drawable-resource.html | ||
const drawableFileTypes = new Set(["gif", "jpeg", "jpg", "png", "webp", "xml"]); | ||
function getAndroidResourceFolderName(asset, scale) { | ||
if (!drawableFileTypes.has(asset.type)) { | ||
return "raw"; | ||
} | ||
const suffix = getAndroidAssetSuffix(scale); | ||
if (!suffix) { | ||
throw new Error( | ||
`Don't know which android drawable suffix to use for asset: ${JSON.stringify( | ||
asset | ||
)}` | ||
); | ||
} | ||
return `drawable-${suffix}`; | ||
} | ||
function getResourceIdentifier(asset) { | ||
const folderPath = getBasePath(asset); | ||
return `${folderPath}/${asset.name}` | ||
.toLowerCase() | ||
.replace(/\//g, "_") // Encode folder structure in file name | ||
.replace(/([^a-z0-9_])/g, "") // Remove illegal chars | ||
.replace(/^assets_/, ""); // Remove "assets_" prefix | ||
} | ||
|
||
function getBasePath(asset) { | ||
let basePath = asset.httpServerLocation; | ||
if (basePath[0] === "/") { | ||
basePath = basePath.substr(1); | ||
} | ||
return basePath; | ||
} | ||
var _default = { | ||
getAndroidAssetSuffix, | ||
getAndroidResourceFolderName, | ||
getResourceIdentifier, | ||
getBasePath, | ||
}; | ||
exports.default = _default; |
39 changes: 39 additions & 0 deletions
39
packages/community-cli-plugin/dist/commands/bundle/assetPathUtils.js.flow
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,39 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
export type PackagerAsset = $ReadOnly<{ | ||
httpServerLocation: string, | ||
name: string, | ||
type: string, | ||
... | ||
}>; | ||
|
||
/** | ||
* FIXME: using number to represent discrete scale numbers is fragile in essence because of | ||
* floating point numbers imprecision. | ||
*/ | ||
declare function getAndroidAssetSuffix(scale: number): string; | ||
|
||
declare function getAndroidResourceFolderName( | ||
asset: PackagerAsset, | ||
scale: number | ||
): string; | ||
|
||
declare function getResourceIdentifier(asset: PackagerAsset): string; | ||
|
||
declare function getBasePath(asset: PackagerAsset): string; | ||
|
||
declare export default { | ||
getAndroidAssetSuffix: getAndroidAssetSuffix, | ||
getAndroidResourceFolderName: getAndroidResourceFolderName, | ||
getResourceIdentifier: getResourceIdentifier, | ||
getBasePath: getBasePath, | ||
}; |
Oops, something went wrong.