UNPKG

2.37 kBJavaScriptView Raw
1import { createOperation } from "@dmail/cancellation"
2
3const { rollup } = import.meta.require("rollup")
4
5export const generateBundleUsingRollup = async ({
6 cancellationToken,
7 rollupParseOptions,
8 rollupGenerateOptions,
9 writeOnFileSystem,
10}) => {
11 const rollupBundle = await createOperation({
12 cancellationToken,
13 start: () =>
14 rollup({
15 // about cache here, we should/could reuse previous rollup call
16 // to get the cache from the entryPointMap
17 // as shown here: https://rollupjs.org/guide/en#cache
18 // it could be passed in arguments to this function
19 // however parallelism and having different rollup options per
20 // call make it a bit complex
21 // cache: null
22 // https://rollupjs.org/guide/en#experimentaltoplevelawait
23 experimentalTopLevelAwait: true,
24 // if we want to ignore some warning
25 // please use https://rollupjs.org/guide/en#onwarn
26 // to be very clear about what we want to ignore
27 onwarn: (warning, warn) => {
28 if (warningIsThisIsUndefinedFromGlobalThis(warning)) return
29 warn(warning)
30 },
31 ...rollupParseOptions,
32 }),
33 })
34
35 const rollupOutputArray = await createOperation({
36 cancellationToken,
37 start: () => {
38 if (writeOnFileSystem) {
39 return rollupBundle.write({
40 // https://rollupjs.org/guide/en#experimentaltoplevelawait
41 experimentalTopLevelAwait: true,
42 // we could put prefConst to true by checking 'transform-block-scoping'
43 // presence in babelPluginMap
44 preferConst: false,
45 ...rollupGenerateOptions,
46 })
47 }
48 return rollupBundle.generate({
49 // https://rollupjs.org/guide/en#experimentaltoplevelawait
50 experimentalTopLevelAwait: true,
51 // we could put prefConst to true by checking 'transform-block-scoping'
52 // presence in babelPluginMap
53 preferConst: false,
54 ...rollupGenerateOptions,
55 })
56 },
57 })
58
59 return rollupOutputArray
60}
61
62const warningIsThisIsUndefinedFromGlobalThis = ({ code, loc }) => {
63 if (code !== "THIS_IS_UNDEFINED") return false
64 if (!loc) return false
65
66 const { file } = loc
67 if (file.endsWith("/.jsenv/helpers/global-this.js")) return true
68 if (file.endsWith("\\.jsenv\\helpers\\global-this.js")) return true
69 return false
70}