UNPKG

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