UNPKG

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