我正在尝试在我的项目中使用
invertase/firestore-ios-sdk-frameworks
,但这意味着直接修改ios podfile。
在 expo 管理的项目中有没有办法将特定行添加到 podfile 中?
如果您仍然需要这个,解决方案如下: 首先创建一个文件
projectroot/plugins/withPodfile(or whatever you want).js
然后在这个文件中:
const { withDangerousMod, withPlugins } = require( '@expo/config-plugins' )
const fs = require( 'fs' )
const path = require( 'path' )
async function readFile( path ) {
return fs.promises.readFile( path, 'utf8' )
}
async function saveFile( path, content ) {
return fs.promises.writeFile( path, content, 'utf8' )
}
module.exports = ( config ) => withPlugins( config, [ ( config ) => {
return withDangerousMod( config, [
'ios',
async ( config ) => {
const file = path.join( config.modRequest.platformProjectRoot, 'Podfile' )
/*
* You need to remove the line before adding it.
* If you don't do this and you run `expo prebuild` in a dirt project
* your file will have the same line added twice
*/
const contents = ( await readFile( file ) ).replace(
/Write here the content you want to add...\n\n/g,
''
)
/*
* Now re-adds the content
*/
await saveFile( file, `Write here the content you want to add...\n\n${ contents }` )
return config
}
] )
} ] )
现在,要使用创建的插件,您只需将其添加到
app.json
或 app.config.js
内的插件部分:
{
"expo": {
...
plugins: [
... all your other plugins,
"./plugins/withPodfile(your plugin file name)"
]
}
}
我有类似的问题,拯救我的是这里
中描述的解决方案我只添加了一些小的调整来添加打字稿支持(如果你想要原始的解决方案,你可以在这里找到它
附注要将插件添加到 app.json 中的插件数组中,请不要忘记:
// add-podfile-source-plugin.ts
import { withDangerousMod, type ConfigPlugin } from "expo/config-plugins";
import { mergeContents } from "@expo/config-plugins/build/utils/generateCode";
const fs = require("fs");
const path = require("path");
// source of the plugin:
// https://github.com/expo/expo/discussions/18456
/**
*
* 1. watch the .ts file and generate the .js file
* npx tsc <file-name>.ts --watch --skipLibCheck
* ---
* 2. add the <file-name>.js to the plugins array in the app.json
* ---
* 3. to generate the native folders, to check if the plugin is working or not
* npx expo prebuild -p android --clean
*
*/
async function readFileAsync(path: string) {
return fs.promises.readFile(path, "utf8");
}
async function saveFileAsync(path: string, content: string) {
return fs.promises.writeFile(path, content, "utf8");
}
const withAddPodSource: ConfigPlugin = c => {
return withDangerousMod(c, [
"ios",
async config => {
const file = path.join(config.modRequest.platformProjectRoot, "Podfile");
const contents = await readFileAsync(file);
await saveFileAsync(file, addPodSource(contents));
return config;
},
]);
};
const source_1 = "source 'https://cdn.cocoapods.org/'";
const source_2 = "source 'https://github.com/somelib/SomeLibSpecs.git'";
function addPodSource(src: string) {
return mergeContents({
tag: `rn-add-pod-source`,
src,
newSrc: `${source_1}\n${source_2}`,
anchor: /^/,
offset: 0,
comment: "#",
}).contents;
}
export default withAddPodSource;