Skip to content

Commit

Permalink
feat(cli): Option to inline JS source maps during sync (#5843)
Browse files Browse the repository at this point in the history
  • Loading branch information
theproducer committed Aug 12, 2022
1 parent b9d5954 commit 7ce6dd4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
9 changes: 7 additions & 2 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,17 @@ export function runProgram(config: Config): void {
'--deployment',
"Optional: if provided, Podfile.lock won't be deleted and pod install will use --deployment option",
)
.option(
'--inline',
'Optional: if true, all source maps will be inlined for easier debugging on mobile devices',
false,
)
.action(
wrapAction(
telemetryAction(config, async (platform, { deployment }) => {
telemetryAction(config, async (platform, { deployment, inline }) => {
checkExternalConfig(config.app);
const { syncCommand } = await import('./tasks/sync');
await syncCommand(config, platform, deployment);
await syncCommand(config, platform, deployment, inline);
}),
),
);
Expand Down
2 changes: 1 addition & 1 deletion cli/src/tasks/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export async function addCommand(
await editPlatforms(config, platformName);

if (await pathExists(config.app.webDirAbs)) {
await sync(config, platformName, false);
await sync(config, platformName, false, false);
if (platformName === config.android.name) {
await runTask('Syncing Gradle', async () => {
return createLocalProperties(config.android.platformDirAbs);
Expand Down
2 changes: 1 addition & 1 deletion cli/src/tasks/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function runCommand(

try {
if (options.sync) {
await sync(config, platformName, false);
await sync(config, platformName, false, true);
}

await run(config, platformName, options);
Expand Down
55 changes: 55 additions & 0 deletions cli/src/tasks/sourcemaps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
readdirSync,
existsSync,
readFileSync,
writeFileSync,
unlinkSync,
lstatSync,
} from '@ionic/utils-fs';
import { join, extname } from 'path';

import type { Config } from '../definitions';
import { logger } from '../log';

function walkDirectory(dirPath: string) {
const files = readdirSync(dirPath);
files.forEach(file => {
const targetFile = join(dirPath, file);
if (existsSync(targetFile) && lstatSync(targetFile).isDirectory()) {
walkDirectory(targetFile);
} else {
const mapFile = join(dirPath, `${file}.map`);
if (extname(file) === '.js' && existsSync(mapFile)) {
const bufMap = readFileSync(mapFile).toString('base64');
const bufFile = readFileSync(targetFile, 'utf8');
const result = bufFile.replace(
`sourceMappingURL=${file}.map`,
'sourceMappingURL=data:application/json;charset=utf-8;base64,' +
bufMap,
);
writeFileSync(targetFile, result);
unlinkSync(mapFile);
}
}
});
}

export async function inlineSourceMaps(
config: Config,
platformName: string,
): Promise<void> {
let buildDir = '';

if (platformName == config.ios.name) {
buildDir = await config.ios.webDirAbs;
}

if (platformName == config.android.name) {
buildDir = await config.android.webDirAbs;
}

if (buildDir) {
logger.info('Inlining sourcemaps');
walkDirectory(buildDir);
}
}
8 changes: 7 additions & 1 deletion cli/src/tasks/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { logger } from '../log';
import { allSerial } from '../util/promise';

import { copy, copyCommand } from './copy';
import { inlineSourceMaps } from './sourcemaps';
import { update, updateChecks, updateCommand } from './update';

/**
Expand All @@ -21,6 +22,7 @@ export async function syncCommand(
config: Config,
selectedPlatformName: string,
deployment: boolean,
inline: boolean,
): Promise<void> {
if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) {
try {
Expand All @@ -40,7 +42,7 @@ export async function syncCommand(
]);
await allSerial(
platforms.map(
platformName => () => sync(config, platformName, deployment),
platformName => () => sync(config, platformName, deployment, inline),
),
);
const now = +new Date();
Expand All @@ -60,6 +62,7 @@ export async function sync(
config: Config,
platformName: string,
deployment: boolean,
inline: boolean,
): Promise<void> {
await runPlatformHook(
config,
Expand All @@ -70,6 +73,9 @@ export async function sync(

try {
await copy(config, platformName);
if (inline) {
await inlineSourceMaps(config, platformName);
}
} catch (e) {
logger.error(e.stack ?? e);
}
Expand Down

0 comments on commit 7ce6dd4

Please sign in to comment.