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

Add sanitizeFileName option #4058

Merged
merged 26 commits into from Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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: 8 additions & 0 deletions docs/999-big-list-of-options.md
Expand Up @@ -1320,6 +1320,14 @@ Default: `false`

Generate `const` declarations for exports rather than `var` declarations.

#### output.sanitizeFileName
Type: `boolean | (string) => string`<br>
Default: `true`

Set to `false` to disable all chunk name sanitizations (removal of `\0`, `?` and `*` characters).

Alternatively set to a function to allow custom chunk name sanitization.

#### output.strict
Type: `boolean`<br>
CLI: `--strict`/`--no-strict`<br>
Expand Down
16 changes: 12 additions & 4 deletions src/Chunk.ts
Expand Up @@ -210,6 +210,7 @@ export default class Chunk {
} = Object.create(null);
private renderedModuleSources = new Map<Module, MagicString>();
private renderedSource: MagicStringBundle | null = null;
private sanitizeFileName: (id: string) => string;
private sortedExportNames: string[] | null = null;
private strictFacade = false;
private usedModules: Module[] = undefined as any;
Expand Down Expand Up @@ -254,6 +255,12 @@ export default class Chunk {
}
}
this.suggestedVariableName = makeLegal(this.generateVariableName());
if (this.outputOptions.sanitizeFileName === false)
this.sanitizeFileName = (id) => id;
else if (typeof this.outputOptions.sanitizeFileName === 'function')
this.sanitizeFileName = this.outputOptions.sanitizeFileName;
else
this.sanitizeFileName = sanitizeFileName;
guybedford marked this conversation as resolved.
Show resolved Hide resolved
}

canModuleBeFacade(module: Module, exposedVariables: Set<Variable>): boolean {
Expand Down Expand Up @@ -424,7 +431,8 @@ export default class Chunk {
? this.computeContentHashWithDependencies(addons, options, existingNames)
: '[hash]',
name: () => this.getChunkName()
}
},
this.sanitizeFileName
),
existingNames
);
Expand All @@ -437,7 +445,7 @@ export default class Chunk {
unsetOptions: Set<string>
): string {
const id = this.orderedModules[0].id;
const sanitizedId = sanitizeFileName(id);
const sanitizedId = this.sanitizeFileName(id);
let path: string;
if (isAbsolute(id)) {
const extension = extname(id);
Expand Down Expand Up @@ -501,7 +509,7 @@ export default class Chunk {
}

getChunkName(): string {
return this.name || (this.name = sanitizeFileName(this.getFallbackChunkName()));
return this.name || (this.name = this.sanitizeFileName(this.getFallbackChunkName()));
}

getExportNames(): string[] {
Expand Down Expand Up @@ -816,7 +824,7 @@ export default class Chunk {
if (fileName) {
this.fileName = fileName;
} else {
this.name = sanitizeFileName(name || getChunkNameFromModule(facadedModule));
this.name = this.sanitizeFileName(name || getChunkNameFromModule(facadedModule));
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/rollup/types.d.ts
Expand Up @@ -649,6 +649,7 @@ export interface OutputOptions {
preferConst?: boolean;
preserveModules?: boolean;
preserveModulesRoot?: string;
sanitizeFileName?: boolean | ((fileName: string) => string);
sourcemap?: boolean | 'inline' | 'hidden';
sourcemapExcludeSources?: boolean;
sourcemapFile?: string;
Expand Down Expand Up @@ -693,6 +694,7 @@ export interface NormalizedOutputOptions {
preferConst: boolean;
preserveModules: boolean;
preserveModulesRoot: string | undefined;
sanitizeFileName: boolean | ((fileName: string) => string);
sourcemap: boolean | 'inline' | 'hidden';
sourcemapExcludeSources: boolean;
sourcemapFile: string | undefined;
Expand Down
1 change: 1 addition & 0 deletions src/utils/options/mergeOptions.ts
Expand Up @@ -223,6 +223,7 @@ function mergeOutputOptions(
preferConst: getOption('preferConst'),
preserveModules: getOption('preserveModules'),
preserveModulesRoot: getOption('preserveModulesRoot'),
sanitizeFileName: getOption('sanitizeFileName'),
sourcemap: getOption('sourcemap'),
sourcemapExcludeSources: getOption('sourcemapExcludeSources'),
sourcemapFile: getOption('sourcemapFile'),
Expand Down
1 change: 1 addition & 0 deletions src/utils/options/normalizeOutputOptions.ts
Expand Up @@ -66,6 +66,7 @@ export function normalizeOutputOptions(
preferConst: (config.preferConst as boolean | undefined) || false,
preserveModules,
preserveModulesRoot: getPreserveModulesRoot(config),
sanitizeFileName: config.sanitizeFileName as NormalizedOutputOptions['sanitizeFileName'] ?? true,
sourcemap: (config.sourcemap as boolean | 'inline' | 'hidden' | undefined) || false,
sourcemapExcludeSources: (config.sourcemapExcludeSources as boolean | undefined) || false,
sourcemapFile: config.sourcemapFile as string | undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/relativeId.ts
@@ -1,5 +1,5 @@
import { basename, extname, isAbsolute, relative, resolve } from './path';
import { sanitizeFileName } from './sanitizeFileName';
import { sanitizeFileName as defaultSanitizeFileName } from './sanitizeFileName';

export function getAliasName(id: string) {
const base = basename(id);
Expand All @@ -11,7 +11,7 @@ export default function relativeId(id: string) {
return relative(resolve(), id);
}

export function isPlainPathFragment(name: string) {
export function isPlainPathFragment(name: string, sanitizeFileName = defaultSanitizeFileName) {
guybedford marked this conversation as resolved.
Show resolved Hide resolved
// not starting with "/", "./", "../"
return (
name[0] !== '/' &&
Expand Down
6 changes: 4 additions & 2 deletions src/utils/renderNamePattern.ts
@@ -1,13 +1,15 @@
import { errFailedValidation, error } from './error';
import { extname } from './path';
import { isPlainPathFragment } from './relativeId';
import { sanitizeFileName as defaultSanitizeFileName } from './sanitizeFileName';

export function renderNamePattern(
pattern: string,
patternName: string,
replacements: { [name: string]: () => string }
replacements: { [name: string]: () => string },
sanitizeFileName = defaultSanitizeFileName
guybedford marked this conversation as resolved.
Show resolved Hide resolved
) {
if (!isPlainPathFragment(pattern))
if (!isPlainPathFragment(pattern, sanitizeFileName))
return error(
errFailedValidation(
`Invalid pattern "${pattern}" for "${patternName}", patterns can be neither absolute nor relative paths and must not contain invalid characters.`
Expand Down
1 change: 1 addition & 0 deletions test/function/samples/output-options-hook/_config.js
Expand Up @@ -42,6 +42,7 @@ module.exports = {
plugins: [],
preferConst: false,
preserveModules: false,
sanitizeFileName: true,
sourcemap: false,
sourcemapExcludeSources: false,
strict: true,
Expand Down
21 changes: 21 additions & 0 deletions test/hooks/index.js
Expand Up @@ -1100,6 +1100,27 @@ describe('hooks', () => {
});
});

it('supports disabling sanitization for in-memory / in-browser / non-fs builds', () => {
return rollup
.rollup({
input: 'input.js',
plugins: [{
resolveId: id => id,
load: () => `export default 5`
}]
})
.then(bundle => {
return bundle.generate({
format: 'es',
sanitizeFileName: false,
entryFileNames: 'test:[name]'
});
})
.then(({ output }) => {
assert.strictEqual(output[0].fileName, 'test:input');
});
});

describe('deprecated', () => {
it('caches chunk emission in transform hook', () => {
let cache;
Expand Down
4 changes: 2 additions & 2 deletions test/misc/optionList.js

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