Skip to content

Commit af6a347

Browse files
authored
feat: introduce the virtualDirname option to customize the virtual directory name (#5602)
1 parent eb12f34 commit af6a347

File tree

21 files changed

+138
-7
lines changed

21 files changed

+138
-7
lines changed

docs/configuration-options/index.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ define(['https://d3js.org/d3.v4.min'], function (d3) {
14721472
| CLI: | `--preserveModules`/`--no-preserveModules` |
14731473
| Default: | `false` |
14741474

1475-
Instead of creating as few chunks as possible, this mode will create separate chunks for all modules using the original module names as file names. Requires the [`output.dir`](#output-dir) option. Tree-shaking will still be applied, suppressing files that are not used by the provided entry points or do not have side effects when executed and removing unused exports of files that are not entry points. On the other hand, if plugins (like `@rollup/plugin-commonjs`) emit additional "virtual" files to achieve certain results, those files will be emitted as actual files using a pattern `_virtual/fileName.js`.
1475+
Instead of creating as few chunks as possible, this mode will create separate chunks for all modules using the original module names as file names. Requires the [`output.dir`](#output-dir) option. Tree-shaking will still be applied, suppressing files that are not used by the provided entry points or do not have side effects when executed and removing unused exports of files that are not entry points. On the other hand, if plugins (like `@rollup/plugin-commonjs`) emit additional "virtual" files to achieve certain results, those files will be emitted as actual files using a pattern [`${output.virtualDirname}/fileName.js`](#output-virtualdirname).
14761476

14771477
It is therefore not recommended to blindly use this option to transform an entire file structure to another format if you directly want to import from those files as expected exports may be missing. In that case, you should rather designate all files explicitly as entry points by adding them to the [`input` option object](#input), see the example there for how to do that.
14781478

@@ -1682,6 +1682,16 @@ Re-parses each generated chunk to detect if the generated code is valid JavaScri
16821682

16831683
If the code is invalid, a warning will be issued. Note that no error is thrown so that you can still inspect the generated output. To promote this warning to an error, you can watch for it in an [`onwarn`](#onwarn) handler.
16841684

1685+
### output.virtualDirname
1686+
1687+
| | |
1688+
| -------: | :--------------------------- |
1689+
| Type: | `string` |
1690+
| CLI: | `--virtualDirname <dirname>` |
1691+
| Default: | `_virtual` |
1692+
1693+
This option specifies the directory name for "virtual" files that might be emitted by plugins (like `@rollup/plugin-commonjs`). It is only validated when [`output.preserveModules`](#output-preservemodules) is enabled.
1694+
16851695
### preserveEntrySignatures
16861696

16871697
| | |

src/Chunk.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,9 @@ export default class Chunk {
10751075
? idWithoutExtension.slice(preserveModulesRoot.length).replace(/^[/\\]/, '')
10761076
: relative(this.inputBase, idWithoutExtension);
10771077
} else {
1078-
return `_virtual/${basename(idWithoutExtension)}`;
1078+
return (
1079+
this.outputOptions.virtualDirname.replace(/\/$/, '') + '/' + basename(idWithoutExtension)
1080+
);
10791081
}
10801082
}
10811083

src/rollup/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ export interface OutputOptions {
756756
strict?: boolean;
757757
systemNullSetters?: boolean;
758758
validate?: boolean;
759+
virtualDirname?: string;
759760
}
760761

761762
export interface NormalizedOutputOptions {
@@ -809,6 +810,7 @@ export interface NormalizedOutputOptions {
809810
strict: boolean;
810811
systemNullSetters: boolean;
811812
validate: boolean;
813+
virtualDirname: string;
812814
}
813815

814816
export type WarningHandlerWithDefault = (

src/utils/options/mergeOptions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ async function mergeOutputOptions(
279279
sourcemapPathTransform: getOption('sourcemapPathTransform'),
280280
strict: getOption('strict'),
281281
systemNullSetters: getOption('systemNullSetters'),
282-
validate: getOption('validate')
282+
validate: getOption('validate'),
283+
virtualDirname: getOption('virtualDirname')
283284
};
284285

285286
warnUnknownOptions(config, Object.keys(outputOptions), 'output options', log);

src/utils/options/normalizeOutputOptions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ export async function normalizeOutputOptions(
108108
| undefined,
109109
strict: config.strict ?? true,
110110
systemNullSetters: config.systemNullSetters ?? true,
111-
validate: config.validate || false
111+
validate: config.validate || false,
112+
virtualDirname: config.virtualDirname || '_virtual'
112113
};
113114

114115
warnUnknownOptions(config, Object.keys(outputOptions), 'output options', inputOptions.onLog);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const commonjs = require('@rollup/plugin-commonjs');
2+
3+
module.exports = defineTest({
4+
description: 'expect the directory name for "virtual" files to be rollup_virtual',
5+
options: {
6+
plugins: [commonjs()],
7+
output: { preserveModules: true, virtualDirname: 'rollup_virtual' }
8+
}
9+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
define(['./main2'], (function (main) { 'use strict';
2+
3+
4+
5+
return main;
6+
7+
}));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define(['./rollup_virtual/_commonjsHelpers'], (function (_commonjsHelpers) { 'use strict';
2+
3+
var main = true;
4+
5+
var main$1 = /*@__PURE__*/_commonjsHelpers.getDefaultExportFromCjs(main);
6+
7+
return main$1;
8+
9+
}));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define(['exports'], (function (exports) { 'use strict';
2+
3+
function getDefaultExportFromCjs (x) {
4+
return x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
5+
}
6+
7+
exports.getDefaultExportFromCjs = getDefaultExportFromCjs;
8+
9+
}));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var main = require('./main2.js');
4+
5+
6+
7+
module.exports = main;

0 commit comments

Comments
 (0)