Skip to content

Commit

Permalink
Change removal of sourcemap comment
Browse files Browse the repository at this point in the history
Do the removal without relying on the module parsing the code. This way even if
a plugin uses the `parse` method in the PluginContext the comments are removed.
  • Loading branch information
Yannay Livneh committed Mar 7, 2021
1 parent a12ec5b commit aebeefa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
32 changes: 25 additions & 7 deletions src/Module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as acorn from 'acorn';
import { findNodeAround } from 'acorn-walk';
import { locate } from 'locate-character';
import MagicString from 'magic-string';
import extractAssignedNames from 'rollup-pluginutils/src/extractAssignedNames';
Expand All @@ -15,7 +16,7 @@ import Literal from './ast/nodes/Literal';
import MetaProperty from './ast/nodes/MetaProperty';
import * as NodeType from './ast/nodes/NodeType';
import Program from './ast/nodes/Program';
import { ExpressionNode, NodeBase } from './ast/nodes/shared/Node';
import { ExpressionNode, GenericEsTreeNode, NodeBase } from './ast/nodes/shared/Node';
import TemplateLiteral from './ast/nodes/TemplateLiteral';
import VariableDeclaration from './ast/nodes/VariableDeclaration';
import ModuleScope from './ast/scopes/ModuleScope';
Expand Down Expand Up @@ -61,7 +62,7 @@ import { basename, extname } from './utils/path';
import { markPureCallExpressions } from './utils/pureComments';
import relativeId from './utils/relativeId';
import { RenderOptions } from './utils/renderHelpers';
import { SOURCEMAPPING_URL_RE } from './utils/sourceMappingURL';
import { SOURCEMAPPING_URL_COMMENT_RE } from './utils/sourceMappingURL';
import { timeEnd, timeStart } from './utils/timers';
import { markModuleAndImpureDependenciesAsExecuted } from './utils/traverseStaticDependencies';
import { MISSING_EXPORT_SHIM_VARIABLE } from './utils/variableNames';
Expand Down Expand Up @@ -720,14 +721,31 @@ export default class Module {
this.alwaysRemovedCode = alwaysRemovedCode || [];
if (!ast) {
ast = tryParse(this, this.graph.acornParser, this.options.acorn as acorn.Options);
for (const comment of this.comments) {
if (!comment.block && SOURCEMAPPING_URL_RE.test(comment.text)) {
this.alwaysRemovedCode.push([comment.start, comment.end]);
}
}
markPureCallExpressions(this.comments, ast);
}

let sourcemappingUrlMatch;
debugger;

while (sourcemappingUrlMatch = SOURCEMAPPING_URL_COMMENT_RE.exec(this.info.code)) {
const found = findNodeAround(ast, sourcemappingUrlMatch.index);

// if no node contains this string - it's surely a comment
if (!found || !found.node) {
debugger;
continue;
}

// if this regex is part of a literal or expression
if (!(found?.node as GenericEsTreeNode).body) {
debugger;
continue;
}

debugger;
this.alwaysRemovedCode.push([sourcemappingUrlMatch.index, SOURCEMAPPING_URL_COMMENT_RE.lastIndex]);
}

timeEnd('generate ast', 3);

this.resolvedIds = resolvedIds || Object.create(null);
Expand Down
8 changes: 6 additions & 2 deletions src/utils/sourceMappingURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// this for an actual sourceMappingURL
let SOURCEMAPPING_URL = 'sourceMa';
SOURCEMAPPING_URL += 'ppingURL';
const whiteSpaceNoNewline = '[ \\f\\r\\t\\v\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff]';

const SOURCEMAPPING_URL_RE = new RegExp(`^#\\s+${SOURCEMAPPING_URL}=.+\\n?`);
const SOURCEMAPPING_URL_LINE_COMMENT_RE = `//#${whiteSpaceNoNewline}+${SOURCEMAPPING_URL}=.+`;
const SOURCEMAPPING_URL_BLOCK_COMMENT_RE = `/\\*#${whiteSpaceNoNewline}+${SOURCEMAPPING_URL}=.+\\*/`;
const SOURCEMAPPING_URL_COMMENT_RE = new RegExp(
`(${SOURCEMAPPING_URL_LINE_COMMENT_RE})|(${SOURCEMAPPING_URL_BLOCK_COMMENT_RE})`, 'g');

export { SOURCEMAPPING_URL, SOURCEMAPPING_URL_RE };
export { SOURCEMAPPING_URL, SOURCEMAPPING_URL_COMMENT_RE};

0 comments on commit aebeefa

Please sign in to comment.