Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Option to inline JS source maps during sync #5843

Merged
merged 8 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
64 changes: 64 additions & 0 deletions cli/src/tasks/sourcemaps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
readdirSync,
existsSync,
readFileSync,
writeFileSync,
unlinkSync,
} from '@ionic/utils-fs';
import { join, extname } from 'path';

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

function findJSAssetsDir(buildDir: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS files could be any folder. Im not sure you would want to hard code this for React and Vue.

const reactJSDir = '/static/js';
const vueJSDir = '/js';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path.join is required for folder separators which will change based on platform (eg on windows its \ rather than /)


if (existsSync(buildDir + reactJSDir)) {
return reactJSDir;
}

if (existsSync(buildDir + vueJSDir)) {
return vueJSDir;
}

return '/';
}

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');
const jsAssetsDir = findJSAssetsDir(buildDir);
buildDir += jsAssetsDir;

const files = readdirSync(buildDir);
files.forEach(file => {
const mapFile = join(buildDir, `${file}.map`);
if (extname(file) === '.js' && existsSync(mapFile)) {
const targetFile = join(buildDir, file);
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);
}
});
}
}
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