Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore queries and hashes in file names when preserving modules #4374

Merged
merged 2 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Chunk.ts
Expand Up @@ -433,17 +433,17 @@ export default class Chunk {
unsetOptions: ReadonlySet<string>
): string {
const [{ id }] = this.orderedModules;
const sanitizedId = this.outputOptions.sanitizeFileName(id);
const sanitizedId = this.outputOptions.sanitizeFileName(id.split(QUERY_HASH_REGEX, 1)[0]);
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)) {
if (isAbsolute(sanitizedId)) {
const currentDir = dirname(sanitizedId);
const extension = extname(id);
const extension = extname(sanitizedId);
const fileName = renderNamePattern(pattern, 'output.entryFileNames', {
assetExtname: () => (NON_ASSET_EXTENSIONS.includes(extension) ? '' : extension),
ext: () => extension.substr(1),
Expand Down Expand Up @@ -1400,3 +1400,5 @@ export default class Chunk {
function getChunkNameFromModule(module: Module): string {
return module.chunkName || getAliasName(module.id);
}

const QUERY_HASH_REGEX = /[?#]/;
7 changes: 4 additions & 3 deletions src/utils/sanitizeFileName.ts
@@ -1,12 +1,13 @@
// https://datatracker.ietf.org/doc/html/rfc2396
// eslint-disable-next-line no-control-regex
const INVALID_CHAR_RE = /[\x00-\x1F\x7F<>*#"{}|^[\]`;?:&=+$,]/g;
const INVALID_CHAR_REGEX = /[\x00-\x1F\x7F<>*#"{}|^[\]`;?:&=+$,]/g;
const DRIVE_LETTER_REGEX = /^[a-z]:/i;

export function sanitizeFileName(name: string): string {
const match = /^[a-z]:/i.exec(name);
const match = DRIVE_LETTER_REGEX.exec(name);
const driveLetter = match ? match[0] : '';

// A `:` is only allowed as part of a windows drive letter (ex: C:\foo)
// Otherwise, avoid them because they can refer to NTFS alternate data streams.
return driveLetter + name.substr(driveLetter.length).replace(INVALID_CHAR_RE, '_');
return driveLetter + name.substr(driveLetter.length).replace(INVALID_CHAR_REGEX, '_');
}
@@ -1,4 +1,4 @@
define(['external', './other', './_virtual/other.js_commonjs-exports'], (function (require$$0, other$1, other) { 'use strict';
define(['external', './other', './_virtual/other'], (function (require$$0, other$1, other) { 'use strict';

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

Expand Down
@@ -1,4 +1,4 @@
define(['./_virtual/other.js_commonjs-exports'], (function (other) { 'use strict';
define(['./_virtual/other'], (function (other) { 'use strict';

other.__exports.value = 43;

Expand Down
Expand Up @@ -2,7 +2,7 @@

var require$$0 = require('external');
require('./other.js');
var other = require('./_virtual/other.js_commonjs-exports.js');
var other = require('./_virtual/other.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.js');
var other = require('./_virtual/other.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.js';
import { __exports as other } from './_virtual/other.js';

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

other.value = 43;
@@ -1,4 +1,4 @@
System.register(['external', './other.js', './_virtual/other.js_commonjs-exports.js'], (function (exports) {
System.register(['external', './other.js', './_virtual/other.js'], (function (exports) {
'use strict';
var require$$0, other;
return {
Expand Down
@@ -1,4 +1,4 @@
System.register(['./_virtual/other.js_commonjs-exports.js'], (function (exports) {
System.register(['./_virtual/other.js'], (function (exports) {
'use strict';
var other;
return {
Expand Down
15 changes: 15 additions & 0 deletions test/chunking-form/samples/ignore-chunk-name-query/_config.js
@@ -0,0 +1,15 @@
const { loader } = require('../../../utils.js');

module.exports = {
description: 'ignores queries and hashes for chunk names when preserving modules',
options: {
input: ['a.js?query', 'b.js#hash'],
output: { preserveModules: true },
plugins: [
loader({
'a.js?query': "console.log('a');",
'b.js#hash': "console.log('b');"
})
]
}
};
@@ -0,0 +1,5 @@
define((function () { 'use strict';

console.log('a');

}));
@@ -0,0 +1,5 @@
define((function () { 'use strict';

console.log('b');

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

console.log('a');
@@ -0,0 +1,3 @@
'use strict';

console.log('b');
@@ -0,0 +1 @@
console.log('a');
@@ -0,0 +1 @@
console.log('b');
@@ -0,0 +1,10 @@
System.register([], (function () {
'use strict';
return {
execute: (function () {

console.log('a');

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

console.log('b');

})
};
}));
@@ -1,4 +1,4 @@
define(['external', './other', './_virtual/other.js_commonjs-exports'], (function (require$$0, other$1, other) { 'use strict';
define(['external', './other', './_virtual/other'], (function (require$$0, other$1, other) { 'use strict';

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

Expand Down
@@ -1,4 +1,4 @@
define(['./_virtual/other.js_commonjs-exports'], (function (other) { 'use strict';
define(['./_virtual/other'], (function (other) { 'use strict';

other.__exports.value = 43;

Expand Down
Expand Up @@ -2,7 +2,7 @@

var require$$0 = require('external');
require('./other.js');
var other = require('./_virtual/other.js_commonjs-exports.js');
var other = require('./_virtual/other.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.js');
var other = require('./_virtual/other.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.js';
import { __exports as other } from './_virtual/other.js';

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

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

const base2 = index.__exports;

Expand Down
@@ -1,4 +1,4 @@
define(['../../../_virtual/index.js_commonjs-exports'], (function (index) { 'use strict';
define(['../../../_virtual/index'], (function (index) { 'use strict';

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

Expand Down
@@ -1,4 +1,4 @@
define(['./custom_modules/@my-scope/my-base-pkg/index', './_virtual/index.js_commonjs-exports'], (function (index$1, index) { 'use strict';
define(['./custom_modules/@my-scope/my-base-pkg/index', './_virtual/index'], (function (index$1, index) { 'use strict';

const base = index.__exports;

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.js');
var index = require('../_virtual/index.js');

const base2 = index.__exports;

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

var index = require('../../../_virtual/index.js_commonjs-exports.js');
var index = require('../../../_virtual/index.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.js');
var index = require('./_virtual/index.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.js';
import { __exports as myBasePkg } from '../_virtual/index.js';

const base2 = myBasePkg;

Expand Down
@@ -1,5 +1,5 @@
import { __exports as myBasePkg } from '../../../_virtual/index.js_commonjs-exports.js';
export { __exports as default } from '../../../_virtual/index.js_commonjs-exports.js';
import { __exports as myBasePkg } from '../../../_virtual/index.js';
export { __exports as default } from '../../../_virtual/index.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.js';
import { __exports as myBasePkg } from './_virtual/index.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.js'], (function (exports) {
System.register(['../custom_modules/@my-scope/my-base-pkg/index.js', '../_virtual/index.js'], (function (exports) {
'use strict';
var myBasePkg;
return {
Expand Down
@@ -1,4 +1,4 @@
System.register(['../../../_virtual/index.js_commonjs-exports.js'], (function (exports) {
System.register(['../../../_virtual/index.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.js'], (function (exports) {
System.register(['./custom_modules/@my-scope/my-base-pkg/index.js', './_virtual/index.js'], (function (exports) {
'use strict';
var myBasePkg;
return {
Expand Down