Skip to content

Commit

Permalink
Add assetExtname replacement in entryFileNames (#4077)
Browse files Browse the repository at this point in the history
* Add assetExtname replacement in entryFileNames

This only has an effect when preserveModules is enaled.

This allows consumers who want to customise the entry file name to
easily implement the "only add an extension for non js/ts files" logic
that rollup does if you don't specify entryFileNames.

* Update test snapshots

* Fix linting

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
BPScott and lukastaegert committed May 15, 2021
1 parent b460516 commit 645cc5c
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/999-big-list-of-options.md
Expand Up @@ -446,6 +446,7 @@ This pattern will also be used when setting the [`output.preserveModules`](guide
* `[name]`: The file name (without extension) of the file.
* `[ext]`: The extension of the file.
* `[extname]`: The extension of the file, prefixed by `.` if it is not empty.
* `[assetExtname]`: The extension of the file, prefixed by `.` if it is not empty and it is not one of `js`, `jsx`, `ts` or `tsx`.

#### output.extend
Type: `boolean`<br>
Expand Down
5 changes: 2 additions & 3 deletions src/Chunk.ts
Expand Up @@ -441,15 +441,14 @@ export default class Chunk {
if (isAbsolute(id)) {
const extension = extname(id);
const pattern = unsetOptions.has('entryFileNames')
? NON_ASSET_EXTENSIONS.includes(extension)
? '[name].js'
: '[name][extname].js'
? '[name][assetExtname].js'
: options.entryFileNames;
const currentDir = dirname(sanitizedId);
const fileName = renderNamePattern(
typeof pattern === 'function' ? pattern(this.getChunkInfo()) : pattern,
'output.entryFileNames',
{
assetExtname: () => NON_ASSET_EXTENSIONS.includes(extension) ? '' : extension,
ext: () => extension.substr(1),
extname: () => extension,
format: () => options.format as string,
Expand Down
@@ -1,10 +1,25 @@
const path = require('path');

module.exports = {
description: 'entryFileNames pattern supported in combination with preserveModules',
options: {
input: 'src/main.ts',
output: {
entryFileNames: 'entry-[name]-[format]-[ext][extname].js',
entryFileNames: 'entry-[name]-[format]-[ext][extname][assetExtname].js',
preserveModules: true
}
},
plugins: [
{
name: 'str-plugin',
transform(code, id) {
switch (path.extname(id)) {
case '.str':
return { code: `export default "${code.trim()}"` };
default:
return null;
}
}
}
]
}
};
@@ -0,0 +1,7 @@
define(function () { 'use strict';

var lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

return lorem;

});
@@ -1,10 +1,11 @@
define(['exports', './entry-foo-amd-ts.ts', './nested/entry-bar-amd-ts.ts', './nested/entry-baz-amd-ts.ts', './entry-no-ext-amd-'], function (exports, foo, bar, baz, noExt) { 'use strict';
define(['exports', './entry-foo-amd-ts.ts', './nested/entry-bar-amd-ts.ts', './nested/entry-baz-amd-ts.ts', './entry-lorem-amd-str.str.str', './entry-no-ext-amd-'], function (exports, foo, bar, baz, lorem, noExt) { 'use strict';



exports.foo = foo;
exports.bar = bar;
exports.baz = baz;
exports.lorem = lorem;
exports.noExt = noExt;

Object.defineProperty(exports, '__esModule', { value: true });
Expand Down
@@ -0,0 +1,5 @@
'use strict';

var lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

module.exports = lorem;
Expand Up @@ -5,11 +5,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
var foo = require('./entry-foo-cjs-ts.ts.js');
var bar = require('./nested/entry-bar-cjs-ts.ts.js');
var baz = require('./nested/entry-baz-cjs-ts.ts.js');
var lorem = require('./entry-lorem-cjs-str.str.str.js');
var noExt = require('./entry-no-ext-cjs-.js');



exports.foo = foo;
exports.bar = bar;
exports.baz = baz;
exports.lorem = lorem;
exports.noExt = noExt;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -1,4 +1,5 @@
export { default as foo } from './entry-foo-es-ts.ts.js';
export { default as bar } from './nested/entry-bar-es-ts.ts.js';
export { default as baz } from './nested/entry-baz-es-ts.ts.js';
export { default as lorem } from './entry-lorem-es-str.str.str.js';
export { default as noExt } from './entry-no-ext-es-.js';
@@ -0,0 +1,10 @@
System.register([], function (exports) {
'use strict';
return {
execute: function () {

var lorem = exports('default', "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");

}
};
});
@@ -1,4 +1,4 @@
System.register(['./entry-foo-system-ts.ts.js', './nested/entry-bar-system-ts.ts.js', './nested/entry-baz-system-ts.ts.js', './entry-no-ext-system-.js'], function (exports) {
System.register(['./entry-foo-system-ts.ts.js', './nested/entry-bar-system-ts.ts.js', './nested/entry-baz-system-ts.ts.js', './entry-lorem-system-str.str.str.js', './entry-no-ext-system-.js'], function (exports) {
'use strict';
return {
setters: [function (module) {
Expand All @@ -7,6 +7,8 @@ System.register(['./entry-foo-system-ts.ts.js', './nested/entry-bar-system-ts.ts
exports('bar', module.default);
}, function (module) {
exports('baz', module.default);
}, function (module) {
exports('lorem', module.default);
}, function (module) {
exports('noExt', module.default);
}],
Expand Down
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Expand Up @@ -5,4 +5,6 @@ export { default as bar } from './nested/bar.ts';
// @ts-ignore
export { default as baz } from './nested/baz.ts';
// @ts-ignore
export { default as lorem } from './lorem.str';
// @ts-ignore
export { default as noExt } from './no-ext';

0 comments on commit 645cc5c

Please sign in to comment.