@@ -13,71 +13,89 @@ On a `bundle` object, you can call `bundle.generate` multiple times with differe
1313Once you're finished with the ` bundle ` object, you should call ` bundle.close() ` , which will let plugins clean up their external processes or services via the [ ` closeBundle ` ] ( guide/en/#closebundle ) hook.
1414
1515``` javascript
16- const rollup = require ( ' rollup' ) ;
16+ import { rollup } from ' rollup' ;
1717
18- // see below for details on the options
18+ // see below for details on these options
1919const inputOptions = {... };
20- const outputOptions = {... };
2120
22- async function build () {
23- // create a bundle
24- const bundle = await rollup .rollup (inputOptions);
25-
26- console .log (bundle .watchFiles ); // an array of file names this bundle depends on
27-
28- // generate output specific code in-memory
29- // you can call this function multiple times on the same bundle object
30- const { output } = await bundle .generate (outputOptions);
31-
32- for (const chunkOrAsset of output) {
33- if (chunkOrAsset .type === ' asset' ) {
34- // For assets, this contains
35- // {
36- // fileName: string, // the asset file name
37- // source: string | Uint8Array // the asset source
38- // type: 'asset' // signifies that this is an asset
39- // }
40- console .log (' Asset' , chunkOrAsset);
41- } else {
42- // For chunks, this contains
43- // {
44- // code: string, // the generated JS code
45- // dynamicImports: string[], // external modules imported dynamically by the chunk
46- // exports: string[], // exported variable names
47- // facadeModuleId: string | null, // the id of a module that this chunk corresponds to
48- // fileName: string, // the chunk file name
49- // implicitlyLoadedBefore: string[]; // entries that should only be loaded after this chunk
50- // imports: string[], // external modules imported statically by the chunk
51- // importedBindings: {[imported: string]: string[]} // imported bindings per dependency
52- // isDynamicEntry: boolean, // is this chunk a dynamic entry point
53- // isEntry: boolean, // is this chunk a static entry point
54- // isImplicitEntry: boolean, // should this chunk only be loaded after other chunks
55- // map: string | null, // sourcemaps if present
56- // modules: { // information about the modules in this chunk
57- // [id: string]: {
58- // renderedExports: string[]; // exported variable names that were included
59- // removedExports: string[]; // exported variable names that were removed
60- // renderedLength: number; // the length of the remaining code in this module
61- // originalLength: number; // the original length of the code in this module
62- // code: string | null; // remaining code in this module
63- // };
64- // },
65- // name: string // the name of this chunk as used in naming patterns
66- // referencedFiles: string[] // files referenced via import.meta.ROLLUP_FILE_URL_<id>
67- // type: 'chunk', // signifies that this is a chunk
68- // }
69- console .log (' Chunk' , chunkOrAsset .modules );
70- }
71- }
21+ // you can create multiple outputs from the same input to generate e.g.
22+ // different formats like CommonJS and ESM
23+ const outputOptionsList = [{... }, {... }];
7224
73- // or write the bundle to disk
74- await bundle .write (outputOptions);
25+ build ();
7526
76- // closes the bundle
77- await bundle .close ();
27+ async function build () {
28+ let bundle;
29+ let buildFailed = false ;
30+ try {
31+ // create a bundle
32+ const bundle = await rollup (inputOptions);
33+
34+ // an array of file names this bundle depends on
35+ console .log (bundle .watchFiles );
36+
37+ await generateOutputs (bundle);
38+ } catch (error) {
39+ buildFailed = true ;
40+ // do some error reporting
41+ console .error (error);
42+ }
43+ if (bundle) {
44+ // closes the bundle
45+ await bundle .close ();
46+ }
47+ process .exit (buildFailed ? 1 : 0 );
7848}
7949
80- build ();
50+ async function generateOutputs (bundle ) {
51+ for (const outputOptions of outputOptionsList) {
52+ // generate output specific code in-memory
53+ // you can call this function multiple times on the same bundle object
54+ // replace bundle.generate with bundle.write to directly write to disk
55+ const { output } = await bundle .generate (outputOptions);
56+
57+ for (const chunkOrAsset of output) {
58+ if (chunkOrAsset .type === ' asset' ) {
59+ // For assets, this contains
60+ // {
61+ // fileName: string, // the asset file name
62+ // source: string | Uint8Array // the asset source
63+ // type: 'asset' // signifies that this is an asset
64+ // }
65+ console .log (' Asset' , chunkOrAsset);
66+ } else {
67+ // For chunks, this contains
68+ // {
69+ // code: string, // the generated JS code
70+ // dynamicImports: string[], // external modules imported dynamically by the chunk
71+ // exports: string[], // exported variable names
72+ // facadeModuleId: string | null, // the id of a module that this chunk corresponds to
73+ // fileName: string, // the chunk file name
74+ // implicitlyLoadedBefore: string[]; // entries that should only be loaded after this chunk
75+ // imports: string[], // external modules imported statically by the chunk
76+ // importedBindings: {[imported: string]: string[]} // imported bindings per dependency
77+ // isDynamicEntry: boolean, // is this chunk a dynamic entry point
78+ // isEntry: boolean, // is this chunk a static entry point
79+ // isImplicitEntry: boolean, // should this chunk only be loaded after other chunks
80+ // map: string | null, // sourcemaps if present
81+ // modules: { // information about the modules in this chunk
82+ // [id: string]: {
83+ // renderedExports: string[]; // exported variable names that were included
84+ // removedExports: string[]; // exported variable names that were removed
85+ // renderedLength: number; // the length of the remaining code in this module
86+ // originalLength: number; // the original length of the code in this module
87+ // code: string | null; // remaining code in this module
88+ // };
89+ // },
90+ // name: string // the name of this chunk as used in naming patterns
91+ // referencedFiles: string[] // files referenced via import.meta.ROLLUP_FILE_URL_<id>
92+ // type: 'chunk', // signifies that this is a chunk
93+ // }
94+ console .log (' Chunk' , chunkOrAsset .modules );
95+ }
96+ }
97+ }
98+ }
8199```
82100
83101#### inputOptions object
0 commit comments