1 | import { pathnameToOperatingSystemPath } from "@jsenv/operating-system-path"
|
2 | import { createLogger } from "./logger.js"
|
3 | import { createImportFromGlobalRollupPlugin } from "./import-from-global-rollup-plugin.js"
|
4 | import { createJsenvRollupPlugin } from "./jsenv-rollup-plugin.js"
|
5 | import { generateBundleUsingRollup } from "./generate-bundle-using-rollup.js"
|
6 | import { formatToRollupFormat } from "./format-to-rollup-format.js"
|
7 |
|
8 | export const bundleWithBalancing = async ({
|
9 | cancellationToken,
|
10 | projectPathname,
|
11 | bundleIntoRelativePath,
|
12 | importDefaultExtension,
|
13 | importMapRelativePath,
|
14 | importMapForBundle,
|
15 | specifierMap,
|
16 | specifierDynamicMap,
|
17 | nativeModulePredicate,
|
18 | entryPointMap,
|
19 | babelPluginMap,
|
20 | convertMap,
|
21 | minify,
|
22 | logLevel,
|
23 | format,
|
24 | formatOutputOptions,
|
25 | groupMap,
|
26 | compileId,
|
27 | writeOnFileSystem,
|
28 | }) => {
|
29 | const logger = createLogger({ logLevel })
|
30 |
|
31 | const dir = pathnameToOperatingSystemPath(
|
32 | `${projectPathname}${bundleIntoRelativePath}/${compileId}`,
|
33 | )
|
34 |
|
35 | const { jsenvRollupPlugin } = await createJsenvRollupPlugin({
|
36 | cancellationToken,
|
37 | projectPathname,
|
38 | dir,
|
39 | entryPointMap,
|
40 | importDefaultExtension,
|
41 | importMapRelativePath,
|
42 | importMapForBundle,
|
43 | specifierMap,
|
44 | specifierDynamicMap,
|
45 | babelPluginMap,
|
46 | convertMap,
|
47 | featureNameArray: groupMap[compileId].incompatibleNameArray,
|
48 | minify,
|
49 | format,
|
50 | logger,
|
51 | })
|
52 |
|
53 | const importFromGlobalRollupPlugin = createImportFromGlobalRollupPlugin({
|
54 | platformGlobalName: "globalThis",
|
55 | })
|
56 |
|
57 | logger.logTrace(`
|
58 | bundle entry points with balancing.
|
59 | format: ${format}
|
60 | compileId: ${compileId}
|
61 | entryPointArray: ${Object.keys(entryPointMap)}
|
62 | dir: ${dir}
|
63 | minify: ${minify}
|
64 | `)
|
65 |
|
66 | const rollupBundle = await generateBundleUsingRollup({
|
67 | cancellationToken,
|
68 | writeOnFileSystem,
|
69 | rollupParseOptions: {
|
70 | input: entryPointMap,
|
71 | plugins: [importFromGlobalRollupPlugin, jsenvRollupPlugin],
|
72 | external: (id) => nativeModulePredicate(id),
|
73 | },
|
74 | rollupGenerateOptions: {
|
75 | dir,
|
76 | format: formatToRollupFormat(format),
|
77 | sourcemap: true,
|
78 | sourceMapExcludeSources: true,
|
79 | ...formatOutputOptions,
|
80 | },
|
81 | })
|
82 |
|
83 | return { rollupBundle }
|
84 | }
|