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

Handle single quote escaping in ids #3632

Merged
merged 7 commits into from Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/finalisers/amd.ts
Expand Up @@ -2,6 +2,7 @@ import { Bundle as MagicStringBundle } from 'magic-string';
import { NormalizedOutputOptions } from '../rollup/types';
import { INTEROP_NAMESPACE_VARIABLE } from '../utils/variableNames';
import { FinaliserOptions } from './index';
import { escapeId } from './shared/escapeId';
import { compactEsModuleExport, esModuleExport } from './shared/esModuleExport';
import getExportBlock from './shared/getExportBlock';
import getInteropBlock from './shared/getInteropBlock';
Expand Down Expand Up @@ -37,7 +38,7 @@ export default function amd(
) {
warnOnBuiltins(warn, dependencies);

const deps = dependencies.map(m => `'${removeExtensionFromRelativeAmdId(m.id)}'`);
const deps = dependencies.map(m => `'${removeExtensionFromRelativeAmdId(escapeId(m.id))}'`);
const args = dependencies.map(m => m.name);
const n = options.compact ? '' : '\n';
const _ = options.compact ? '' : ' ';
Expand Down
10 changes: 6 additions & 4 deletions src/finalisers/cjs.ts
Expand Up @@ -2,6 +2,7 @@ import { Bundle as MagicStringBundle } from 'magic-string';
import { NormalizedOutputOptions } from '../rollup/types';
import { INTEROP_DEFAULT_VARIABLE, INTEROP_NAMESPACE_VARIABLE } from '../utils/variableNames';
import { FinaliserOptions } from './index';
import { escapeId } from './shared/escapeId';
import { compactEsModuleExport, esModuleExport } from './shared/esModuleExport';
import getExportBlock from './shared/getExportBlock';
import { getInteropNamespace } from './shared/getInteropNamespace';
Expand Down Expand Up @@ -51,21 +52,22 @@ export default function cjs(
importBlock += !options.compact || definingVariable ? `;${n}` : ',';
}
definingVariable = false;
importBlock += `require('${id}')`;
importBlock += `require('${escapeId(id)}')`;
} else {
importBlock +=
options.compact && definingVariable ? ',' : `${importBlock ? `;${n}` : ''}${varOrConst} `;
definingVariable = true;

if (!options.interop || isChunk || !exportsDefault || !namedExportsMode) {
importBlock += `${name}${_}=${_}require('${id}')`;
importBlock += `${name}${_}=${_}require('${escapeId(id)}')`;
} else {
needsInterop = true;
if (exportsNames)
importBlock += `${name}${_}=${_}require('${id}')${
importBlock += `${name}${_}=${_}require('${escapeId(id)}')${
options.compact ? ',' : `;\n${varOrConst} `
}${name}__default${_}=${_}${INTEROP_DEFAULT_VARIABLE}(${name})`;
else importBlock += `${name}${_}=${_}${INTEROP_DEFAULT_VARIABLE}(require('${id}'))`;
else
importBlock += `${name}${_}=${_}${INTEROP_DEFAULT_VARIABLE}(require('${escapeId(id)}'))`;
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/finalisers/es.ts
Expand Up @@ -2,6 +2,7 @@ import { Bundle as MagicStringBundle } from 'magic-string';
import { ChunkDependencies, ChunkExports, ImportSpecifier, ReexportSpecifier } from '../Chunk';
import { NormalizedOutputOptions } from '../rollup/types';
import { FinaliserOptions } from './index';
import { escapeId } from './shared/escapeId';

export default function es(
magicString: MagicStringBundle,
Expand All @@ -26,7 +27,7 @@ function getImportBlock(dependencies: ChunkDependencies, _: string): string[] {
const importBlock: string[] = [];
for (const { id, reexports, imports, name } of dependencies) {
if (!reexports && !imports) {
importBlock.push(`import${_}'${id}';`);
importBlock.push(`import${_}'${escapeId(id)}';`);
continue;
}
if (imports) {
Expand All @@ -43,10 +44,10 @@ function getImportBlock(dependencies: ChunkDependencies, _: string): string[] {
}
}
if (starImport) {
importBlock.push(`import${_}*${_}as ${starImport.local} from${_}'${id}';`);
importBlock.push(`import${_}*${_}as ${starImport.local} from${_}'${escapeId(id)}';`);
}
if (defaultImport && importedNames.length === 0) {
importBlock.push(`import ${defaultImport.local} from${_}'${id}';`);
importBlock.push(`import ${defaultImport.local} from${_}'${escapeId(id)}';`);
} else if (importedNames.length > 0) {
importBlock.push(
`import ${defaultImport ? `${defaultImport.local},${_}` : ''}{${_}${importedNames
Expand All @@ -57,7 +58,7 @@ function getImportBlock(dependencies: ChunkDependencies, _: string): string[] {
return `${specifier.imported} as ${specifier.local}`;
}
})
.join(`,${_}`)}${_}}${_}from${_}'${id}';`
.join(`,${_}`)}${_}}${_}from${_}'${escapeId(id)}';`
);
}
}
Expand All @@ -75,14 +76,14 @@ function getImportBlock(dependencies: ChunkDependencies, _: string): string[] {
}
}
if (starExport) {
importBlock.push(`export${_}*${_}from${_}'${id}';`);
importBlock.push(`export${_}*${_}from${_}'${escapeId(id)}';`);
}
if (namespaceReexports.length > 0) {
if (
!imports ||
!imports.some(specifier => specifier.imported === '*' && specifier.local === name)
) {
importBlock.push(`import${_}*${_}as ${name} from${_}'${id}';`);
importBlock.push(`import${_}*${_}as ${name} from${_}'${escapeId(id)}';`);
}
for (const specifier of namespaceReexports) {
importBlock.push(
Expand All @@ -102,7 +103,7 @@ function getImportBlock(dependencies: ChunkDependencies, _: string): string[] {
return `${specifier.imported} as ${specifier.reexported}`;
}
})
.join(`,${_}`)}${_}}${_}from${_}'${id}';`
.join(`,${_}`)}${_}}${_}from${_}'${escapeId(id)}';`
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/finalisers/shared/escapeId.ts
@@ -0,0 +1,4 @@
const quoteRegEx = /'/g;
export function escapeId(id: string) {
return id.replace(quoteRegEx, "\\'");
}
3 changes: 2 additions & 1 deletion src/finalisers/system.ts
Expand Up @@ -3,6 +3,7 @@ import { ChunkExports, ModuleDeclarations } from '../Chunk';
import { NormalizedOutputOptions } from '../rollup/types';
import { MISSING_EXPORT_SHIM_VARIABLE } from '../utils/variableNames';
import { FinaliserOptions } from './index';
import { escapeId } from './shared/escapeId';

function getStarExcludes({ dependencies, exports }: ModuleDeclarations): Set<string> {
const starExcludes = new Set(exports.map(expt => expt.exported));
Expand Down Expand Up @@ -106,7 +107,7 @@ export default function system(
const n = options.compact ? '' : '\n';
const _ = options.compact ? '' : ' ';

const dependencyIds = dependencies.map(m => `'${m.id}'`);
const dependencyIds = dependencies.map(m => `'${escapeId(m.id)}'`);

const importBindings: string[] = [];
let starExcludes: Set<string> | undefined;
Expand Down
19 changes: 19 additions & 0 deletions test/form/samples/quote-id/_config.js
@@ -0,0 +1,19 @@
module.exports = {
description: 'supports quote characters in external ids',
options: {
output: {
name: 'Q',
globals: {
"quoted'external": 'quotedExternal'
}
},
plugins: [
{
resolveId(id, parent) {
if (!parent) return id;
return { id: "quoted'external", external: true };
}
}
]
}
};
16 changes: 16 additions & 0 deletions test/form/samples/quote-id/_expected/amd.js
@@ -0,0 +1,16 @@
define(['exports', 'quoted\'external'], function (exports, quoted_external) { 'use strict';



Object.keys(quoted_external).forEach(function (k) {
if (k !== 'default') Object.defineProperty(exports, k, {
enumerable: true,
get: function () {
return quoted_external[k];
}
});
});

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

});
16 changes: 16 additions & 0 deletions test/form/samples/quote-id/_expected/cjs.js
@@ -0,0 +1,16 @@
'use strict';

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

var quoted_external = require('quoted\'external');



Object.keys(quoted_external).forEach(function (k) {
if (k !== 'default') Object.defineProperty(exports, k, {
enumerable: true,
get: function () {
return quoted_external[k];
}
});
});
1 change: 1 addition & 0 deletions test/form/samples/quote-id/_expected/es.js
@@ -0,0 +1 @@
export * from 'quoted\'external';
17 changes: 17 additions & 0 deletions test/form/samples/quote-id/_expected/iife.js
@@ -0,0 +1,17 @@
var Q = (function (exports, quoted_external) {
'use strict';



Object.keys(quoted_external).forEach(function (k) {
if (k !== 'default') Object.defineProperty(exports, k, {
enumerable: true,
get: function () {
return quoted_external[k];
}
});
});

return exports;

}({}, quotedExternal));
18 changes: 18 additions & 0 deletions test/form/samples/quote-id/_expected/system.js
@@ -0,0 +1,18 @@
System.register('Q', ['quoted\'external'], function (exports) {
'use strict';
var _starExcludes = { default: 1 };
return {
setters: [function (module) {
var _setter = {};
for (var _$p in module) {
if (!_starExcludes[_$p]) _setter[_$p] = module[_$p];
}
exports(_setter);
}],
execute: function () {



}
};
});
18 changes: 18 additions & 0 deletions test/form/samples/quote-id/_expected/umd.js
@@ -0,0 +1,18 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('quoted'external')) :
typeof define === 'function' && define.amd ? define(['exports', 'quoted'external'], factory) :
(global = global || self, factory(global.Q = {}, global.quotedExternal));
}(this, (function (exports, quoted_external) { 'use strict';

Object.keys(quoted_external).forEach(function (k) {
if (k !== 'default') Object.defineProperty(exports, k, {
enumerable: true,
get: function () {
return quoted_external[k];
}
});
});

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

})));
1 change: 1 addition & 0 deletions test/form/samples/quote-id/main.js
@@ -0,0 +1 @@
export * from 'external';