Skip to content

Commit

Permalink
Use entryFileNames when generating filenames for virtual modules (#4270)
Browse files Browse the repository at this point in the history
* Use entryFileNames for virtual files when preserveModules is set

* Fix existing tests

- virtual files now have a .js extension by default
- systemjs module references include this file extension
- amd module references omit this file extension. this seems to be
  consistent with amd output of other chunks

* Add new test for virtual file output when modifying entryFileNames

Use entryFileNames to change the extension of the filename to be mjs,
and see that the file created from the virtual file chunk has a .mjs
extension

Note that the amd definition contains the .mjs extension, as amd ids only
strip the .js file extension, which is slightly odd but probably ok as
this amd is not the targetted usecase for this behaviour

* Refactor - don't repeat pattern generation logic
  • Loading branch information
BPScott committed Nov 22, 2021
1 parent 8d98341 commit 9fc39d1
Show file tree
Hide file tree
Showing 69 changed files with 234 additions and 55 deletions.
39 changes: 23 additions & 16 deletions src/Chunk.ts
Expand Up @@ -434,23 +434,22 @@ export default class Chunk {
const id = this.orderedModules[0].id;
const sanitizedId = this.outputOptions.sanitizeFileName(id);
let path: string;

const patternOpt = unsetOptions.has('entryFileNames')
? '[name][assetExtname].js'
: options.entryFileNames;
const pattern = typeof patternOpt === 'function' ? patternOpt(this.getChunkInfo()) : patternOpt;

if (isAbsolute(id)) {
const extension = extname(id);
const pattern = unsetOptions.has('entryFileNames')
? '[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,
name: () => this.getChunkName()
}
);
const extension = extname(id);
const fileName = renderNamePattern(pattern, 'output.entryFileNames', {
assetExtname: () => (NON_ASSET_EXTENSIONS.includes(extension) ? '' : extension),
ext: () => extension.substr(1),
extname: () => extension,
format: () => options.format as string,
name: () => this.getChunkName()
});
const currentPath = `${currentDir}/${fileName}`;
const { preserveModulesRoot } = options;
if (preserveModulesRoot && currentPath.startsWith(preserveModulesRoot)) {
Expand All @@ -459,7 +458,15 @@ export default class Chunk {
path = relative(preserveModulesRelativeDir, currentPath);
}
} else {
path = `_virtual/${basename(sanitizedId)}`;
const extension = extname(sanitizedId);
const fileName = renderNamePattern(pattern, 'output.entryFileNames', {
assetExtname: () => (NON_ASSET_EXTENSIONS.includes(extension) ? '' : extension),
ext: () => extension.substr(1),
extname: () => extension,
format: () => options.format as string,
name: () => getAliasName(sanitizedId)
});
path = `_virtual/${fileName}`;
}
return makeUnique(normalize(path), existingNames);
}
Expand Down
Expand Up @@ -2,7 +2,7 @@

var require$$0 = require('external');
require('./other.js');
var other = require('./_virtual/other.js_commonjs-exports');
var other = require('./_virtual/other.js_commonjs-exports.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

Expand Down
@@ -1,6 +1,6 @@
'use strict';

var other = require('./_virtual/other.js_commonjs-exports');
var other = require('./_virtual/other.js_commonjs-exports.js');

other.__exports.value = 43;

Expand Down
@@ -1,6 +1,6 @@
import require$$0 from 'external';
import './other.js';
import { __exports as other } from './_virtual/other.js_commonjs-exports';
import { __exports as other } from './_virtual/other.js_commonjs-exports.js';

const external = require$$0;
const { value } = other;
Expand Down
@@ -1,4 +1,4 @@
import { __exports as other } from './_virtual/other.js_commonjs-exports';
export { __exports as default } from './_virtual/other.js_commonjs-exports';
import { __exports as other } from './_virtual/other.js_commonjs-exports.js';
export { __exports as default } from './_virtual/other.js_commonjs-exports.js';

other.value = 43;
@@ -1,4 +1,4 @@
System.register(['external', './other.js', './_virtual/other.js_commonjs-exports'], (function (exports) {
System.register(['external', './other.js', './_virtual/other.js_commonjs-exports.js'], (function (exports) {
'use strict';
var require$$0, other;
return {
Expand Down
@@ -1,4 +1,4 @@
System.register(['./_virtual/other.js_commonjs-exports'], (function (exports) {
System.register(['./_virtual/other.js_commonjs-exports.js'], (function (exports) {
'use strict';
var other;
return {
Expand Down
@@ -1,5 +1,5 @@
'use strict';

var _virtualModule = require('./_virtual/_virtualModule');
var _virtualModule = require('./_virtual/_virtualModule.js');

assert.equal(_virtualModule.virtual, 'Virtual!');
@@ -1,3 +1,3 @@
import { virtual } from './_virtual/_virtualModule';
import { virtual } from './_virtual/_virtualModule.js';

assert.equal(virtual, 'Virtual!');
@@ -1,4 +1,4 @@
System.register(['./_virtual/_virtualModule'], (function () {
System.register(['./_virtual/_virtualModule.js'], (function () {
'use strict';
var virtual;
return {
Expand Down
Expand Up @@ -2,7 +2,7 @@

var require$$0 = require('external');
require('./other.js');
var other = require('./_virtual/other.js_commonjs-exports');
var other = require('./_virtual/other.js_commonjs-exports.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

Expand Down
@@ -1,6 +1,6 @@
'use strict';

var other = require('./_virtual/other.js_commonjs-exports');
var other = require('./_virtual/other.js_commonjs-exports.js');

other.__exports.value = 43;

Expand Down
@@ -1,6 +1,6 @@
import require$$0 from 'external';
import './other.js';
import { __exports as other } from './_virtual/other.js_commonjs-exports';
import { __exports as other } from './_virtual/other.js_commonjs-exports.js';

const external = require$$0;
const { value } = other;
Expand Down
@@ -1,4 +1,4 @@
import { __exports as other } from './_virtual/other.js_commonjs-exports';
export { __exports as default } from './_virtual/other.js_commonjs-exports';
import { __exports as other } from './_virtual/other.js_commonjs-exports.js';
export { __exports as default } from './_virtual/other.js_commonjs-exports.js';

other.value = 43;
@@ -1,4 +1,4 @@
System.register(['external', './other.js', './_virtual/other.js_commonjs-exports'], (function (exports) {
System.register(['external', './other.js', './_virtual/other.js_commonjs-exports.js'], (function (exports) {
'use strict';
var require$$0, other;
return {
Expand Down
@@ -1,4 +1,4 @@
System.register(['./_virtual/other.js_commonjs-exports'], (function (exports) {
System.register(['./_virtual/other.js_commonjs-exports.js'], (function (exports) {
'use strict';
var other;
return {
Expand Down
@@ -1,7 +1,7 @@
'use strict';

require('../custom_modules/@my-scope/my-base-pkg/index.js');
var index = require('../_virtual/index.js_commonjs-exports');
var index = require('../_virtual/index.js_commonjs-exports.js');

const base2 = index.__exports;

Expand Down
@@ -1,6 +1,6 @@
'use strict';

var index = require('../../../_virtual/index.js_commonjs-exports');
var index = require('../../../_virtual/index.js_commonjs-exports.js');

Object.defineProperty(index.__exports, '__esModule', { value: true });

Expand Down
@@ -1,7 +1,7 @@
'use strict';

require('./custom_modules/@my-scope/my-base-pkg/index.js');
var index = require('./_virtual/index.js_commonjs-exports');
var index = require('./_virtual/index.js_commonjs-exports.js');

const base = index.__exports;

Expand Down
@@ -1,5 +1,5 @@
import '../custom_modules/@my-scope/my-base-pkg/index.js';
import { __exports as myBasePkg } from '../_virtual/index.js_commonjs-exports';
import { __exports as myBasePkg } from '../_virtual/index.js_commonjs-exports.js';

const base2 = myBasePkg;

Expand Down
@@ -1,5 +1,5 @@
import { __exports as myBasePkg } from '../../../_virtual/index.js_commonjs-exports';
export { __exports as default } from '../../../_virtual/index.js_commonjs-exports';
import { __exports as myBasePkg } from '../../../_virtual/index.js_commonjs-exports.js';
export { __exports as default } from '../../../_virtual/index.js_commonjs-exports.js';

Object.defineProperty(myBasePkg, '__esModule', { value: true });

Expand Down
@@ -1,5 +1,5 @@
import './custom_modules/@my-scope/my-base-pkg/index.js';
import { __exports as myBasePkg } from './_virtual/index.js_commonjs-exports';
import { __exports as myBasePkg } from './_virtual/index.js_commonjs-exports.js';

const base = myBasePkg;

Expand Down
@@ -1,4 +1,4 @@
System.register(['../custom_modules/@my-scope/my-base-pkg/index.js', '../_virtual/index.js_commonjs-exports'], (function (exports) {
System.register(['../custom_modules/@my-scope/my-base-pkg/index.js', '../_virtual/index.js_commonjs-exports.js'], (function (exports) {
'use strict';
var myBasePkg;
return {
Expand Down
@@ -1,4 +1,4 @@
System.register(['../../../_virtual/index.js_commonjs-exports'], (function (exports) {
System.register(['../../../_virtual/index.js_commonjs-exports.js'], (function (exports) {
'use strict';
var myBasePkg;
return {
Expand Down
@@ -1,4 +1,4 @@
System.register(['./custom_modules/@my-scope/my-base-pkg/index.js', './_virtual/index.js_commonjs-exports'], (function (exports) {
System.register(['./custom_modules/@my-scope/my-base-pkg/index.js', './_virtual/index.js_commonjs-exports.js'], (function (exports) {
'use strict';
var myBasePkg;
return {
Expand Down
@@ -0,0 +1,34 @@
module.exports = {
description:
'Generates actual files whose filename adheres to entryFileNames for virtual modules when preserving modules',
options: {
input: 'main.js',
output: {
preserveModules: true,
entryFileNames: 'entry-[name]-[format]-[ext][extname][assetExtname].mjs'
},
plugins: [
{
resolveId(id) {
if (id === '\0virtualModule') return id;
if (id === '\0virtualWithExt.js') return id;
if (id === '\0virtualWithAssetExt.str') return id;
},
load(id) {
if (id === '\0virtualModule') return 'export const virtual = "Virtual!";\n';
if (id === '\0virtualWithExt.js') return 'export const virtual2 = "Virtual2!";\n';
if (id === '\0virtualWithAssetExt.str') return 'export const virtual3 = "Virtual3!";\n';
},
transform(code, id) {
if (id === '\0virtualModule') return null;
if (id === '\0virtualWithExt.js') return null;
if (id === '\0virtualWithAssetExt.str') return null;
return (
'import {virtual} from "\0virtualModule";\nimport {virtual2} from "\0virtualWithExt.js";\nimport {virtual3} from "\0virtualWithAssetExt.str";' +
code
);
}
}
]
}
};
@@ -0,0 +1,9 @@
define(['exports'], (function (exports) { 'use strict';

const virtual3 = "Virtual3!";

exports.virtual3 = virtual3;

Object.defineProperty(exports, '__esModule', { value: true });

}));
@@ -0,0 +1,9 @@
define(['exports'], (function (exports) { 'use strict';

const virtual2 = "Virtual2!";

exports.virtual2 = virtual2;

Object.defineProperty(exports, '__esModule', { value: true });

}));
@@ -0,0 +1,7 @@
define(['./_virtual/entry-_virtualModule-amd-.mjs', './_virtual/entry-_virtualWithExt-amd-js.js.mjs', './_virtual/entry-_virtualWithAssetExt-amd-str.str.str.mjs'], (function (_virtualModule, _virtualWithExt, _virtualWithAssetExt) { 'use strict';

assert.equal(_virtualModule.virtual, 'Virtual!');
assert.equal(_virtualWithExt.virtual2, 'Virtual2!');
assert.equal(_virtualWithAssetExt.virtual3, 'Virtual3!');

}));
@@ -0,0 +1,7 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const virtual3 = "Virtual3!";

exports.virtual3 = virtual3;
@@ -0,0 +1,7 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const virtual2 = "Virtual2!";

exports.virtual2 = virtual2;
@@ -0,0 +1,9 @@
'use strict';

var _virtualModule = require('./_virtual/entry-_virtualModule-cjs-.mjs');
var _virtualWithExt = require('./_virtual/entry-_virtualWithExt-cjs-js.js.mjs');
var _virtualWithAssetExt = require('./_virtual/entry-_virtualWithAssetExt-cjs-str.str.str.mjs');

assert.equal(_virtualModule.virtual, 'Virtual!');
assert.equal(_virtualWithExt.virtual2, 'Virtual2!');
assert.equal(_virtualWithAssetExt.virtual3, 'Virtual3!');
@@ -0,0 +1,3 @@
const virtual3 = "Virtual3!";

export { virtual3 };
@@ -0,0 +1,3 @@
const virtual2 = "Virtual2!";

export { virtual2 };
@@ -0,0 +1,7 @@
import { virtual } from './_virtual/entry-_virtualModule-es-.mjs';
import { virtual2 } from './_virtual/entry-_virtualWithExt-es-js.js.mjs';
import { virtual3 } from './_virtual/entry-_virtualWithAssetExt-es-str.str.str.mjs';

assert.equal(virtual, 'Virtual!');
assert.equal(virtual2, 'Virtual2!');
assert.equal(virtual3, 'Virtual3!');
@@ -0,0 +1,10 @@
System.register([], (function (exports) {
'use strict';
return {
execute: (function () {

const virtual3 = exports('virtual3', "Virtual3!");

})
};
}));
@@ -0,0 +1,10 @@
System.register([], (function (exports) {
'use strict';
return {
execute: (function () {

const virtual2 = exports('virtual2', "Virtual2!");

})
};
}));
@@ -0,0 +1,20 @@
System.register(['./_virtual/entry-_virtualModule-system-.mjs', './_virtual/entry-_virtualWithExt-system-js.js.mjs', './_virtual/entry-_virtualWithAssetExt-system-str.str.str.mjs'], (function () {
'use strict';
var virtual, virtual2, virtual3;
return {
setters: [function (module) {
virtual = module.virtual;
}, function (module) {
virtual2 = module.virtual2;
}, function (module) {
virtual3 = module.virtual3;
}],
execute: (function () {

assert.equal(virtual, 'Virtual!');
assert.equal(virtual2, 'Virtual2!');
assert.equal(virtual3, 'Virtual3!');

})
};
}));
@@ -0,0 +1,3 @@
assert.equal(virtual, 'Virtual!');
assert.equal(virtual2, 'Virtual2!');
assert.equal(virtual3, 'Virtual3!');
@@ -0,0 +1,9 @@
define(['exports'], (function (exports) { 'use strict';

const virtual = "Virtual!";

exports.virtual = virtual;

Object.defineProperty(exports, '__esModule', { value: true });

}));
@@ -0,0 +1,7 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const virtual = "Virtual!";

exports.virtual = virtual;

0 comments on commit 9fc39d1

Please sign in to comment.