UNPKG

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