Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 68d4720

Browse files
committed
fix: emit transpilation warnings after cache-hit
1 parent 1acd7fd commit 68d4720

File tree

2 files changed

+54
-43
lines changed

2 files changed

+54
-43
lines changed

src/transform/apply-transformers.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type IntersectionArray<T extends unknown[]> = (
1515
type TransformerResult = {
1616
code: string;
1717
map: SourceMap;
18+
warnings?: any[];
1819
} | undefined;
1920

2021
type Transformer<
@@ -39,6 +40,7 @@ type AddSourceMap<T> = Omit<T, 'map'> & { map: RawSourceMap };
3940
export type Transformed = {
4041
code: string;
4142
map: RawSourceMap;
43+
warnings: any[];
4244
};
4345

4446
export function applyTransformersSync<
@@ -49,6 +51,7 @@ export function applyTransformersSync<
4951
transformers: T,
5052
) {
5153
const maps: SourceMap[] = [];
54+
const warnings = [];
5255
const result = { code };
5356

5457
for (const transformer of transformers) {
@@ -57,12 +60,17 @@ export function applyTransformersSync<
5760
if (transformed) {
5861
Object.assign(result, transformed);
5962
maps.unshift(transformed.map);
63+
64+
if (transformed.warnings) {
65+
warnings.push(...transformed.warnings);
66+
}
6067
}
6168
}
6269

6370
return {
6471
...result,
6572
map: remapping(maps as SourceMapInput[], () => null),
73+
warnings,
6674
} as unknown as AddSourceMap<IntersectionArray<Results<[...T]>>>;
6775
}
6876

@@ -74,6 +82,7 @@ export async function applyTransformers<
7482
transformers: T,
7583
) {
7684
const maps: SourceMap[] = [];
85+
const warnings = [];
7786
const result = { code };
7887

7988
for (const transformer of transformers) {
@@ -82,11 +91,16 @@ export async function applyTransformers<
8291
if (transformed) {
8392
Object.assign(result, transformed);
8493
maps.unshift(transformed.map);
94+
95+
if (transformed.warnings) {
96+
warnings.push(...transformed.warnings);
97+
}
8598
}
8699
}
87100

88101
return {
89102
...result,
90103
map: remapping(maps as SourceMapInput[], () => null),
104+
warnings,
91105
} as unknown as AddSourceMap<IntersectionArray<Results<[...T]>>>;
92106
}

src/transform/index.ts

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,28 @@ export function transformSync(
3737
});
3838

3939
const hash = sha1(code + JSON.stringify(esbuildOptions) + esbuildVersion);
40-
const cacheHit = cache.get(hash);
40+
let transformed = cache.get(hash);
4141

42-
if (cacheHit) {
43-
return cacheHit;
44-
}
45-
46-
const transformed = applyTransformersSync(
47-
filePath,
48-
code,
49-
[
50-
// eslint-disable-next-line @typescript-eslint/no-shadow
51-
(filePath, code) => {
42+
if (!transformed) {
43+
transformed = applyTransformersSync(
44+
filePath,
45+
code,
46+
[
5247
// eslint-disable-next-line @typescript-eslint/no-shadow
53-
const transformed = esbuildTransformSync(code, esbuildOptions);
54-
if (esbuildOptions.sourcefile !== filePath) {
55-
transformed.map = transformed.map.replace(`"${esbuildOptions.sourcefile}"`, `"${filePath}"`);
56-
}
57-
return transformed;
58-
},
59-
transformDynamicImport,
60-
] as const,
61-
);
48+
(filePath, code) => {
49+
// eslint-disable-next-line @typescript-eslint/no-shadow
50+
const transformed = esbuildTransformSync(code, esbuildOptions);
51+
if (esbuildOptions.sourcefile !== filePath) {
52+
transformed.map = transformed.map.replace(`"${esbuildOptions.sourcefile}"`, `"${filePath}"`);
53+
}
54+
return transformed;
55+
},
56+
transformDynamicImport,
57+
] as const,
58+
);
59+
60+
cache.set(hash, transformed);
61+
}
6262

6363
if (transformed.warnings.length > 0) {
6464
const { warnings } = transformed;
@@ -67,8 +67,6 @@ export function transformSync(
6767
}
6868
}
6969

70-
cache.set(hash, transformed);
71-
7270
return transformed;
7371
}
7472

@@ -85,27 +83,28 @@ export async function transform(
8583
});
8684

8785
const hash = sha1(code + JSON.stringify(esbuildOptions) + esbuildVersion);
88-
const cacheHit = cache.get(hash);
89-
if (cacheHit) {
90-
return cacheHit;
91-
}
86+
let transformed = cache.get(hash);
9287

93-
const transformed = await applyTransformers(
94-
filePath,
95-
code,
96-
[
97-
// eslint-disable-next-line @typescript-eslint/no-shadow
98-
async (filePath, code) => {
88+
if (!transformed) {
89+
transformed = await applyTransformers(
90+
filePath,
91+
code,
92+
[
9993
// eslint-disable-next-line @typescript-eslint/no-shadow
100-
const transformed = await esbuildTransform(code, esbuildOptions);
101-
if (esbuildOptions.sourcefile !== filePath) {
102-
transformed.map = transformed.map.replace(`"${esbuildOptions.sourcefile}"`, `"${filePath}"`);
103-
}
104-
return transformed;
105-
},
106-
transformDynamicImport,
107-
] as const,
108-
);
94+
async (filePath, code) => {
95+
// eslint-disable-next-line @typescript-eslint/no-shadow
96+
const transformed = await esbuildTransform(code, esbuildOptions);
97+
if (esbuildOptions.sourcefile !== filePath) {
98+
transformed.map = transformed.map.replace(`"${esbuildOptions.sourcefile}"`, `"${filePath}"`);
99+
}
100+
return transformed;
101+
},
102+
transformDynamicImport,
103+
] as const,
104+
);
105+
106+
cache.set(hash, transformed);
107+
}
109108

110109
if (transformed.warnings.length > 0) {
111110
const { warnings } = transformed;
@@ -114,7 +113,5 @@ export async function transform(
114113
}
115114
}
116115

117-
cache.set(hash, transformed);
118-
119116
return transformed;
120117
}

0 commit comments

Comments
 (0)