UNPKG

8.65 kBTypeScriptView Raw
1import { FilterPattern } from '@rollup/pluginutils';
2import { Plugin } from 'rollup';
3
4type RequireReturnsDefaultOption = boolean | 'auto' | 'preferred' | 'namespace';
5
6interface RollupCommonJSOptions {
7 /**
8 * A minimatch pattern, or array of patterns, which specifies the files in
9 * the build the plugin should operate on. By default, all files with
10 * extension `".cjs"` or those in `extensions` are included, but you can narrow
11 * this list by only including specific files. These files will be analyzed
12 * and transpiled if either the analysis does not find ES module specific
13 * statements or `transformMixedEsModules` is `true`.
14 * @default undefined
15 */
16 include?: FilterPattern;
17 /**
18 * A minimatch pattern, or array of patterns, which specifies the files in
19 * the build the plugin should _ignore_. By default, all files with
20 * extensions other than those in `extensions` or `".cjs"` are ignored, but you
21 * can exclude additional files. See also the `include` option.
22 * @default undefined
23 */
24 exclude?: FilterPattern;
25 /**
26 * For extensionless imports, search for extensions other than .js in the
27 * order specified. Note that you need to make sure that non-JavaScript files
28 * are transpiled by another plugin first.
29 * @default [ '.js' ]
30 */
31 extensions?: ReadonlyArray<string>;
32 /**
33 * If true then uses of `global` won't be dealt with by this plugin
34 * @default false
35 */
36 ignoreGlobal?: boolean;
37 /**
38 * If false, skips source map generation for CommonJS modules. This will
39 * improve performance.
40 * @default true
41 */
42 sourceMap?: boolean;
43 /**
44 * Some `require` calls cannot be resolved statically to be translated to
45 * imports.
46 * When this option is set to `false`, the generated code will either
47 * directly throw an error when such a call is encountered or, when
48 * `dynamicRequireTargets` is used, when such a call cannot be resolved with a
49 * configured dynamic require target.
50 * Setting this option to `true` will instead leave the `require` call in the
51 * code or use it as a fallback for `dynamicRequireTargets`.
52 * @default false
53 */
54 ignoreDynamicRequires?: boolean;
55 /**
56 * Instructs the plugin whether to enable mixed module transformations. This
57 * is useful in scenarios with modules that contain a mix of ES `import`
58 * statements and CommonJS `require` expressions. Set to `true` if `require`
59 * calls should be transformed to imports in mixed modules, or `false` if the
60 * `require` expressions should survive the transformation. The latter can be
61 * important if the code contains environment detection, or you are coding
62 * for an environment with special treatment for `require` calls such as
63 * ElectronJS. See also the `ignore` option.
64 * @default false
65 */
66 transformMixedEsModules?: boolean;
67 /**
68 * Sometimes you have to leave require statements unconverted. Pass an array
69 * containing the IDs or a `id => boolean` function.
70 * @default []
71 */
72 ignore?: ReadonlyArray<string> | ((id: string) => boolean);
73 /**
74 * In most cases, where `require` calls are inside a `try-catch` clause,
75 * they should be left unconverted as it requires an optional dependency
76 * that may or may not be installed beside the rolled up package.
77 * Due to the conversion of `require` to a static `import` - the call is hoisted
78 * to the top of the file, outside of the `try-catch` clause.
79 *
80 * - `true`: All `require` calls inside a `try` will be left unconverted.
81 * - `false`: All `require` calls inside a `try` will be converted as if the `try-catch` clause is not there.
82 * - `remove`: Remove all `require` calls from inside any `try` block.
83 * - `string[]`: Pass an array containing the IDs to left unconverted.
84 * - `((id: string) => boolean|'remove')`: Pass a function that control individual IDs.
85 *
86 * @default false
87 */
88 ignoreTryCatch?:
89 | boolean
90 | 'remove'
91 | ReadonlyArray<string>
92 | ((id: string) => boolean | 'remove');
93 /**
94 * Controls how to render imports from external dependencies. By default,
95 * this plugin assumes that all external dependencies are CommonJS. This
96 * means they are rendered as default imports to be compatible with e.g.
97 * NodeJS where ES modules can only import a default export from a CommonJS
98 * dependency.
99 *
100 * If you set `esmExternals` to `true`, this plugins assumes that all
101 * external dependencies are ES modules and respect the
102 * `requireReturnsDefault` option. If that option is not set, they will be
103 * rendered as namespace imports.
104 *
105 * You can also supply an array of ids to be treated as ES modules, or a
106 * function that will be passed each external id to determine if it is an ES
107 * module.
108 * @default false
109 */
110 esmExternals?: boolean | ReadonlyArray<string> | ((id: string) => boolean);
111 /**
112 * Controls what is returned when requiring an ES module from a CommonJS file.
113 * When using the `esmExternals` option, this will also apply to external
114 * modules. By default, this plugin will render those imports as namespace
115 * imports i.e.
116 *
117 * ```js
118 * // input
119 * const foo = require('foo');
120 *
121 * // output
122 * import * as foo from 'foo';
123 * ```
124 *
125 * However there are some situations where this may not be desired.
126 * For these situations, you can change Rollup's behaviour either globally or
127 * per module. To change it globally, set the `requireReturnsDefault` option
128 * to one of the following values:
129 *
130 * - `false`: This is the default, requiring an ES module returns its
131 * namespace. This is the only option that will also add a marker
132 * `__esModule: true` to the namespace to support interop patterns in
133 * CommonJS modules that are transpiled ES modules.
134 * - `"namespace"`: Like `false`, requiring an ES module returns its
135 * namespace, but the plugin does not add the `__esModule` marker and thus
136 * creates more efficient code. For external dependencies when using
137 * `esmExternals: true`, no additional interop code is generated.
138 * - `"auto"`: This is complementary to how `output.exports: "auto"` works in
139 * Rollup: If a module has a default export and no named exports, requiring
140 * that module returns the default export. In all other cases, the namespace
141 * is returned. For external dependencies when using `esmExternals: true`, a
142 * corresponding interop helper is added.
143 * - `"preferred"`: If a module has a default export, requiring that module
144 * always returns the default export, no matter whether additional named
145 * exports exist. This is similar to how previous versions of this plugin
146 * worked. Again for external dependencies when using `esmExternals: true`,
147 * an interop helper is added.
148 * - `true`: This will always try to return the default export on require
149 * without checking if it actually exists. This can throw at build time if
150 * there is no default export. This is how external dependencies are handled
151 * when `esmExternals` is not used. The advantage over the other options is
152 * that, like `false`, this does not add an interop helper for external
153 * dependencies, keeping the code lean.
154 *
155 * To change this for individual modules, you can supply a function for
156 * `requireReturnsDefault` instead. This function will then be called once for
157 * each required ES module or external dependency with the corresponding id
158 * and allows you to return different values for different modules.
159 * @default false
160 */
161 requireReturnsDefault?:
162 | RequireReturnsDefaultOption
163 | ((id: string) => RequireReturnsDefaultOption);
164 /**
165 * Some modules contain dynamic `require` calls, or require modules that
166 * contain circular dependencies, which are not handled well by static
167 * imports. Including those modules as `dynamicRequireTargets` will simulate a
168 * CommonJS (NodeJS-like) environment for them with support for dynamic and
169 * circular dependencies.
170 *
171 * Note: In extreme cases, this feature may result in some paths being
172 * rendered as absolute in the final bundle. The plugin tries to avoid
173 * exposing paths from the local machine, but if you are `dynamicRequirePaths`
174 * with paths that are far away from your project's folder, that may require
175 * replacing strings like `"/Users/John/Desktop/foo-project/"` -> `"/"`.
176 */
177 dynamicRequireTargets?: string | ReadonlyArray<string>;
178}
179
180/**
181 * Convert CommonJS modules to ES6, so they can be included in a Rollup bundle
182 */
183export default function commonjs(options?: RollupCommonJSOptions): Plugin;
184
\No newline at end of file