Skip to content

Commit 7d991c0

Browse files
committed
Polishment
1 parent 113a9b4 commit 7d991c0

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

packages/app/src/sandbox/eval/transpilers/babel/__snapshots__/convert-esmodule.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@ exports[`convert-esmodule can handle as imports 1`] = `
6666
"var $csb__b = require('./b');
6767
var b = $csb__b.a;"
6868
`;
69+
70+
exports[`convert-esmodule can handle inline comments 1`] = `
71+
"var $csb__b = require('./b');
72+
var b = $csb__b.a;
73+
var c = $csb__b.c;"
74+
`;
75+
76+
exports[`convert-esmodule ignores comments 1`] = `""`;

packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,18 @@ describe('convert-esmodule', () => {
6565
`;
6666
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
6767
});
68+
69+
it('ignores comments', () => {
70+
const code = `
71+
// import { a as b } from './b';
72+
`;
73+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
74+
});
75+
76+
it('can handle inline comments', () => {
77+
const code = `
78+
import { a as b, /* HELLO WORLD */ c } from './b';
79+
`;
80+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
81+
});
6882
});

packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// This code is written to be performant, that's why we opted to ignore these linting issues
2+
/* eslint-disable no-loop-func, no-continue */
13
import * as esprima from 'esprima';
24
import escodegen from 'escodegen';
35
import { basename } from 'path';
@@ -427,6 +429,7 @@ export function convertEsModule(path: string, code: string) {
427429
}
428430
const varName = getVarName(`$csb__${basename(source.value, '.js')}`);
429431

432+
// Create require statement instead of the import
430433
program.body[i] = generateRequireStatement(varName, source.value);
431434
i++;
432435

@@ -435,13 +438,18 @@ export function convertEsModule(path: string, code: string) {
435438
let importName: string;
436439

437440
if (specifier.type === n.ImportDefaultSpecifier) {
441+
// const _test = require('test');
442+
// var default = _test.default;
438443
localName = 'default';
439444
importName = 'default';
440445
} else if (specifier.type === n.ImportSpecifier) {
446+
// const _test = require('test');
447+
// var $localName = _test.$importName;
441448
localName = specifier.local.name;
442449
importName = specifier.imported.name;
443450
} else if (specifier.type === n.ImportNamespaceSpecifier) {
444-
// Wildcard, TODO
451+
// const _test = require('test');
452+
// var $localName = _test;
445453
localName = specifier.local.name;
446454
importName = null;
447455
}

packages/app/src/sandbox/utils/metrics.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ const runningMeasurements = new Map<string, number>();
88
const measurements: { [measurement: string]: number } = {};
99

1010
export function measure(key: MeasurementKey) {
11-
performance.mark(`${key}_start`);
12-
runningMeasurements.set(key, performance.now());
11+
if (process.env.NODE_ENV !== 'production') {
12+
return;
13+
}
14+
15+
try {
16+
performance.mark(`${key}_start`);
17+
runningMeasurements.set(key, performance.now());
18+
} catch (e) {
19+
console.warn(`Something went wrong while adding measure: ${e.message}`);
20+
}
1321
}
1422

1523
export function endMeasure(
@@ -19,26 +27,34 @@ export function endMeasure(
1927
lastTime?: number;
2028
} = {}
2129
) {
22-
const { lastTime } = options;
23-
performance.mark(`${key}_end`);
24-
25-
const lastMeasurement =
26-
typeof lastTime === 'undefined' ? runningMeasurements.get(key) : lastTime;
27-
28-
if (typeof lastMeasurement === 'undefined') {
29-
console.warn(
30-
`Measurement for '${key}' was requested, but never was started`
31-
);
30+
if (process.env.NODE_ENV !== 'production') {
3231
return;
3332
}
3433

35-
const nowMeasurement = performance.now();
34+
try {
35+
const { lastTime } = options;
36+
performance.mark(`${key}_end`);
37+
38+
const lastMeasurement =
39+
typeof lastTime === 'undefined' ? runningMeasurements.get(key) : lastTime;
40+
41+
if (typeof lastMeasurement === 'undefined') {
42+
console.warn(
43+
`Measurement for '${key}' was requested, but never was started`
44+
);
45+
return;
46+
}
3647

37-
measurements[key] = nowMeasurement - lastMeasurement;
38-
debug(`${name || key} Time: ${measurements[key].toFixed(2)}ms`);
39-
const hadKey = runningMeasurements.delete(key);
48+
const nowMeasurement = performance.now();
4049

41-
performance.measure(key, hadKey ? `${key}_start` : undefined, `${key}_end`);
50+
measurements[key] = nowMeasurement - lastMeasurement;
51+
debug(`${name || key} Time: ${measurements[key].toFixed(2)}ms`);
52+
const hadKey = runningMeasurements.delete(key);
53+
54+
performance.measure(key, hadKey ? `${key}_start` : undefined, `${key}_end`);
55+
} catch (e) {
56+
console.warn(`Something went wrong while adding measure: ${e.message}`);
57+
}
4258
}
4359

4460
const MEASUREMENT_API = `https://30vlq6h5qc.execute-api.eu-west-1.amazonaws.com/prod/metrics`;

0 commit comments

Comments
 (0)