Skip to content

Commit 245223f

Browse files
authored
fix: resolve warnings when building with sourcemaps enabled (#344)
1 parent 0e4cbfb commit 245223f

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

src/plugins/pluginAddEntry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from 'fs';
22
import * as path from 'pathe';
33
import { Plugin } from 'vite';
4+
import { mapCodeToCodeWithSourcemap } from '../utils/mapCodeToCodeWithSourcemap';
45

56
interface AddEntryOptions {
67
entryName: string;
@@ -153,7 +154,7 @@ const addEntry = ({
153154
const injection = `
154155
import ${JSON.stringify(entryPath)};
155156
`;
156-
return injection + code;
157+
return mapCodeToCodeWithSourcemap(injection + code);
157158
}
158159
},
159160
},

src/plugins/pluginProxyRemoteEntry.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { createFilter } from '@rollup/pluginutils';
22
import { Plugin } from 'vite';
3+
import { mapCodeToCodeWithSourcemap } from '../utils/mapCodeToCodeWithSourcemap';
34
import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFederationOptions';
5+
import { resolvePublicPath } from '../utils/publicPath';
46
import {
57
generateExposes,
68
generateRemoteEntry,
@@ -9,7 +11,6 @@ import {
911
VIRTUAL_EXPOSES,
1012
} from '../virtualModules';
1113
import { parsePromise } from './pluginModuleParseEnd';
12-
import { resolvePublicPath } from '../utils/publicPath';
1314

1415
const filter: (id: string) => boolean = createFilter();
1516

@@ -46,25 +47,28 @@ export default function (): Plugin {
4647
return id;
4748
}
4849
},
49-
async transform(code: string, id: string) {
50-
if (!filter(id)) return;
51-
if (id.includes(REMOTE_ENTRY_ID)) {
52-
return parsePromise.then((_) => generateRemoteEntry(getNormalizeModuleFederationOptions()));
53-
}
54-
if (id === VIRTUAL_EXPOSES) {
55-
return generateExposes();
56-
}
57-
if (id.includes(getHostAutoInitPath())) {
58-
const options = getNormalizeModuleFederationOptions();
59-
if (_command === 'serve') {
60-
const host =
61-
typeof viteConfig.server?.host === 'string' && viteConfig.server.host !== '0.0.0.0'
62-
? viteConfig.server.host
63-
: 'localhost';
64-
const publicPath = JSON.stringify(
65-
resolvePublicPath(options, viteConfig.base) + options.filename
50+
transform(code: string, id: string) {
51+
const transformedCode = (() => {
52+
if (!filter(id)) return;
53+
if (id.includes(REMOTE_ENTRY_ID)) {
54+
return parsePromise.then((_) =>
55+
generateRemoteEntry(getNormalizeModuleFederationOptions())
6656
);
67-
return `
57+
}
58+
if (id === VIRTUAL_EXPOSES) {
59+
return generateExposes();
60+
}
61+
if (id.includes(getHostAutoInitPath())) {
62+
const options = getNormalizeModuleFederationOptions();
63+
if (_command === 'serve') {
64+
const host =
65+
typeof viteConfig.server?.host === 'string' && viteConfig.server.host !== '0.0.0.0'
66+
? viteConfig.server.host
67+
: 'localhost';
68+
const publicPath = JSON.stringify(
69+
resolvePublicPath(options, viteConfig.base) + options.filename
70+
);
71+
return `
6872
const origin = (window && ${!options.ignoreOrigin}) ? window.origin : "//${host}:${viteConfig.server?.port}"
6973
const remoteEntryPromise = await import(origin + ${publicPath})
7074
// __tla only serves as a hack for vite-plugin-top-level-await.
@@ -74,9 +78,12 @@ export default function (): Plugin {
7478
.then(remoteEntry.init).catch(remoteEntry.init)
7579
})
7680
`;
81+
}
82+
return code;
7783
}
78-
return code;
79-
}
84+
})();
85+
86+
return mapCodeToCodeWithSourcemap(transformedCode);
8087
},
8188
};
8289
}

src/plugins/pluginProxySharedModule_preBuild.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Plugin, UserConfig } from 'vite';
2+
import { mapCodeToCodeWithSourcemap } from '../utils/mapCodeToCodeWithSourcemap';
23
import { NormalizedShared } from '../utils/normalizeModuleFederationOptions';
34
import { PromiseStore } from '../utils/PromiseStore';
5+
import VirtualModule, { assertModuleFound } from '../utils/VirtualModule';
46
import {
57
addUsedShares,
68
generateLocalSharedImportMap,
@@ -12,7 +14,6 @@ import {
1214
writePreBuildLibPath,
1315
} from '../virtualModules';
1416
import { parsePromise } from './pluginModuleParseEnd';
15-
import VirtualModule, { assertModuleFound } from '../utils/VirtualModule';
1617

1718
export function proxySharedModule(options: {
1819
shared?: NormalizedShared;
@@ -30,9 +31,11 @@ export function proxySharedModule(options: {
3031
return parsePromise.then((_) => generateLocalSharedImportMap());
3132
}
3233
},
33-
transform(code, id) {
34+
transform(_, id) {
3435
if (id.includes(getLocalSharedImportMapPath())) {
35-
return parsePromise.then((_) => generateLocalSharedImportMap());
36+
return mapCodeToCodeWithSourcemap(
37+
parsePromise.then((_) => generateLocalSharedImportMap())
38+
);
3639
}
3740
},
3841
},
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import MagicString from 'magic-string';
2+
3+
export async function mapCodeToCodeWithSourcemap(code?: string | Promise<string>) {
4+
const resolvedCode = await code;
5+
6+
if (resolvedCode === undefined) {
7+
return;
8+
}
9+
10+
const s = new MagicString(resolvedCode);
11+
12+
return {
13+
code: s.toString(),
14+
map: s.generateMap({ hires: true }),
15+
};
16+
}

0 commit comments

Comments
 (0)