Skip to content

Commit 1d1e6cd

Browse files
committed
support taro
1 parent f16aff5 commit 1d1e6cd

File tree

2 files changed

+192
-92
lines changed

2 files changed

+192
-92
lines changed

cli.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@
145145
},
146146
"sourcemap": {
147147
"default": false
148+
},
149+
"taro": {
150+
"default": false
151+
},
152+
"expo": {
153+
"default": false
154+
},
155+
"rncli": {
156+
"default": false
148157
}
149158
}
150159
},

src/bundle.ts

Lines changed: 183 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,29 @@ try {
2222
hdiff = require('node-hdiffpatch').diff;
2323
} catch (e) {}
2424

25-
26-
async function runReactNativeBundleCommand(
27-
bundleName: string,
28-
development: string,
29-
entryFile: string,
30-
outputFolder: string,
31-
platform: string,
32-
sourcemapOutput: string,
33-
config: string,
34-
) {
25+
async function runReactNativeBundleCommand({
26+
bundleName,
27+
dev,
28+
entryFile,
29+
outputFolder,
30+
platform,
31+
sourcemapOutput,
32+
config,
33+
cli,
34+
}: {
35+
bundleName: string;
36+
dev: string;
37+
entryFile: string;
38+
outputFolder: string;
39+
platform: string;
40+
sourcemapOutput: string;
41+
config?: string;
42+
cli: {
43+
taro?: boolean;
44+
expo?: boolean;
45+
rncli?: boolean;
46+
};
47+
}) {
3548
let gradleConfig: {
3649
crunchPngs?: boolean;
3750
enableHermes?: boolean;
@@ -58,26 +71,31 @@ async function runReactNativeBundleCommand(
5871

5972
fs.emptyDirSync(outputFolder);
6073

61-
let cliPath;
62-
74+
let cliPath: string | undefined;
6375
let usingExpo = false;
64-
try {
65-
cliPath = require.resolve('@expo/cli', {
66-
paths: [process.cwd()],
67-
});
68-
const expoCliVersion = JSON.parse(
69-
fs.readFileSync(
70-
require.resolve('@expo/cli/package.json', {
71-
paths: [process.cwd()],
72-
}),
73-
),
74-
).version;
75-
// expo cli 0.10.17 (expo 49) 开始支持 bundle:embed
76-
if (semverSatisfies(expoCliVersion, '>= 0.10.17')) {
77-
usingExpo = true;
78-
}
79-
} catch (e) {}
80-
if (!usingExpo) {
76+
77+
const getExpoCli = () => {
78+
try {
79+
cliPath = require.resolve('@expo/cli', {
80+
paths: [process.cwd()],
81+
});
82+
const expoCliVersion = JSON.parse(
83+
fs.readFileSync(
84+
require.resolve('@expo/cli/package.json', {
85+
paths: [process.cwd()],
86+
}),
87+
),
88+
).version;
89+
// expo cli 0.10.17 (expo 49) 开始支持 bundle:embed
90+
if (semverSatisfies(expoCliVersion, '>= 0.10.17')) {
91+
usingExpo = true;
92+
} else {
93+
cliPath = undefined;
94+
}
95+
} catch (e) {}
96+
};
97+
98+
const getRnCli = () => {
8199
try {
82100
// rn >= 0.75
83101
cliPath = require.resolve('@react-native-community/cli/build/bin.js', {
@@ -89,20 +107,49 @@ async function runReactNativeBundleCommand(
89107
paths: [process.cwd()],
90108
});
91109
}
110+
};
111+
112+
const getTaroCli = () => {
113+
try {
114+
cliPath = require.resolve('@tarojs/cli/bin/taro.js', {
115+
paths: [process.cwd()],
116+
});
117+
} catch (e) {}
118+
};
119+
120+
if (cli.expo) {
121+
getExpoCli();
122+
} else if (cli.taro) {
123+
getTaroCli();
124+
} else if (cli.rncli) {
125+
getRnCli();
126+
}
127+
128+
if (!cliPath) {
129+
getExpoCli();
130+
if (!usingExpo) {
131+
getRnCli();
132+
}
92133
}
134+
93135
const bundleParams = await checkPlugins();
94136
const isSentry = bundleParams.sentry;
95-
const bundleCommand = usingExpo
96-
? 'export:embed'
97-
: platform === 'harmony'
98-
? 'bundle-harmony'
99-
: 'bundle';
137+
138+
let bundleCommand = 'bundle';
139+
if (usingExpo) {
140+
bundleCommand = 'export:embed';
141+
} else if (platform === 'harmony') {
142+
bundleCommand = 'bundle-harmony';
143+
} else if (cli.taro) {
144+
bundleCommand = 'build';
145+
}
146+
100147
if (platform === 'harmony') {
101148
Array.prototype.push.apply(reactNativeBundleArgs, [
102149
cliPath,
103150
bundleCommand,
104151
'--dev',
105-
development,
152+
dev,
106153
'--entry-file',
107154
entryFile,
108155
]);
@@ -122,14 +169,24 @@ async function runReactNativeBundleCommand(
122169
outputFolder,
123170
'--bundle-output',
124171
path.join(outputFolder, bundleName),
125-
'--dev',
126-
development,
127-
'--entry-file',
128-
entryFile,
129172
'--platform',
130173
platform,
131174
'--reset-cache',
132175
]);
176+
177+
if (cli.taro) {
178+
reactNativeBundleArgs.push(...[
179+
'--type',
180+
'rn',
181+
])
182+
} else {
183+
reactNativeBundleArgs.push(...[
184+
'--dev',
185+
dev,
186+
'--entry-file',
187+
entryFile,
188+
])
189+
}
133190

134191
if (sourcemapOutput) {
135192
reactNativeBundleArgs.push('--sourcemap-output', sourcemapOutput);
@@ -165,7 +222,9 @@ async function runReactNativeBundleCommand(
165222
let hermesEnabled: boolean | undefined = false;
166223

167224
if (platform === 'android') {
168-
const gradlePropeties = await new Promise<{ hermesEnabled?: boolean }>((resolve) => {
225+
const gradlePropeties = await new Promise<{
226+
hermesEnabled?: boolean;
227+
}>((resolve) => {
169228
properties.parse(
170229
'./android/gradle.properties',
171230
{ path: true },
@@ -322,7 +381,11 @@ async function compileHermesByteCode(
322381
}
323382
}
324383

325-
async function copyDebugidForSentry(bundleName: string, outputFolder: string, sourcemapOutput: string) {
384+
async function copyDebugidForSentry(
385+
bundleName: string,
386+
outputFolder: string,
387+
sourcemapOutput: string,
388+
) {
326389
if (sourcemapOutput) {
327390
let copyDebugidPath;
328391
try {
@@ -423,7 +486,10 @@ async function pack(dir: string, output: string) {
423486
}
424487
const childs = fs.readdirSync(root);
425488
for (const name of childs) {
426-
if (ignorePackingFileNames.includes(name) || ignorePackingExtensions.some(ext => name.endsWith(`.${ext}`))) {
489+
if (
490+
ignorePackingFileNames.includes(name) ||
491+
ignorePackingExtensions.some((ext) => name.endsWith(`.${ext}`))
492+
) {
427493
continue;
428494
}
429495
const fullPath = path.join(root, name);
@@ -723,55 +789,66 @@ async function diffFromPackage(
723789
await writePromise;
724790
}
725791

726-
export async function enumZipEntries(zipFn: string, callback: (entry: any, zipFile: any) => void, nestedPath = '') {
792+
export async function enumZipEntries(
793+
zipFn: string,
794+
callback: (entry: any, zipFile: any) => void,
795+
nestedPath = '',
796+
) {
727797
return new Promise((resolve, reject) => {
728-
openZipFile(zipFn, { lazyEntries: true }, async (err: any, zipfile: ZipFile) => {
729-
if (err) {
730-
return reject(err);
731-
}
798+
openZipFile(
799+
zipFn,
800+
{ lazyEntries: true },
801+
async (err: any, zipfile: ZipFile) => {
802+
if (err) {
803+
return reject(err);
804+
}
732805

733-
zipfile.on('end', resolve);
734-
zipfile.on('error', reject);
735-
zipfile.on('entry', async (entry) => {
736-
const fullPath = nestedPath + entry.fileName;
737-
738-
try {
739-
if (
740-
!entry.fileName.endsWith('/') &&
741-
entry.fileName.toLowerCase().endsWith('.hap')
742-
) {
743-
const tempDir = path.join(os.tmpdir(), `nested_zip_${Date.now()}`);
744-
await fs.ensureDir(tempDir);
745-
const tempZipPath = path.join(tempDir, 'temp.zip');
746-
747-
await new Promise((res, rej) => {
748-
zipfile.openReadStream(entry, async (err, readStream) => {
749-
if (err) return rej(err);
750-
const writeStream = fs.createWriteStream(tempZipPath);
751-
readStream.pipe(writeStream);
752-
writeStream.on('finish', res);
753-
writeStream.on('error', rej);
806+
zipfile.on('end', resolve);
807+
zipfile.on('error', reject);
808+
zipfile.on('entry', async (entry) => {
809+
const fullPath = nestedPath + entry.fileName;
810+
811+
try {
812+
if (
813+
!entry.fileName.endsWith('/') &&
814+
entry.fileName.toLowerCase().endsWith('.hap')
815+
) {
816+
const tempDir = path.join(
817+
os.tmpdir(),
818+
`nested_zip_${Date.now()}`,
819+
);
820+
await fs.ensureDir(tempDir);
821+
const tempZipPath = path.join(tempDir, 'temp.zip');
822+
823+
await new Promise((res, rej) => {
824+
zipfile.openReadStream(entry, async (err, readStream) => {
825+
if (err) return rej(err);
826+
const writeStream = fs.createWriteStream(tempZipPath);
827+
readStream.pipe(writeStream);
828+
writeStream.on('finish', res);
829+
writeStream.on('error', rej);
830+
});
754831
});
755-
});
756832

757-
await enumZipEntries(tempZipPath, callback, `${fullPath}/`);
833+
await enumZipEntries(tempZipPath, callback, `${fullPath}/`);
758834

759-
await fs.remove(tempDir);
760-
}
835+
await fs.remove(tempDir);
836+
}
761837

762-
const result = callback(entry, zipfile, fullPath);
763-
if (result && typeof result.then === 'function') {
764-
await result;
838+
const result = callback(entry, zipfile, fullPath);
839+
if (result && typeof result.then === 'function') {
840+
await result;
841+
}
842+
} catch (error) {
843+
console.error('处理文件时出错:', error);
765844
}
766-
} catch (error) {
767-
console.error('处理文件时出错:', error);
768-
}
769845

770-
zipfile.readEntry();
771-
});
846+
zipfile.readEntry();
847+
});
772848

773-
zipfile.readEntry();
774-
});
849+
zipfile.readEntry();
850+
},
851+
);
775852
});
776853
}
777854

@@ -817,11 +894,20 @@ export const commands = {
817894
options.platform || (await question('平台(ios/android/harmony):')),
818895
);
819896

820-
const { bundleName, entryFile, intermediaDir, output, dev, sourcemap } =
821-
translateOptions({
822-
...options,
823-
platform,
824-
});
897+
const {
898+
bundleName,
899+
entryFile,
900+
intermediaDir,
901+
output,
902+
dev,
903+
sourcemap,
904+
taro,
905+
expo,
906+
rncli,
907+
} = translateOptions({
908+
...options,
909+
platform,
910+
});
825911

826912
const bundleParams = await checkPlugins();
827913
const sourcemapPlugin = bundleParams.sourcemap;
@@ -839,20 +925,25 @@ export const commands = {
839925

840926
console.log(`Bundling with react-native: ${version}`);
841927

842-
await runReactNativeBundleCommand(
928+
await runReactNativeBundleCommand({
843929
bundleName,
844930
dev,
845931
entryFile,
846-
intermediaDir,
932+
outputFolder: intermediaDir,
847933
platform,
848-
sourcemap || sourcemapPlugin ? sourcemapOutput : '',
849-
);
934+
sourcemapOutput: sourcemap || sourcemapPlugin ? sourcemapOutput : '',
935+
cli: {
936+
taro,
937+
expo,
938+
rncli,
939+
},
940+
});
850941

851942
await pack(path.resolve(intermediaDir), realOutput);
852943

853944
const v = await question('是否现在上传此热更包?(Y/N)');
854945
if (v.toLowerCase() === 'y') {
855-
const versionName = await this.publish({
946+
const versionName = await this.publish({
856947
args: [realOutput],
857948
options: {
858949
platform,

0 commit comments

Comments
 (0)