From a502f3036558e2f4e363eaeee231cf627fa28712 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Thu, 22 Dec 2016 09:34:57 -0500 Subject: [PATCH] add back @btd fix for #1104 --- src/Bundle.js | 4 +--- src/Module.js | 15 +++++++++------ src/rollup.js | 2 +- src/utils/sourceMappingURL.js | 4 +++- .../_expected/amd.js | 4 ++-- .../_expected/iife.js | 4 ++-- .../_expected/umd.js | 4 ++-- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Bundle.js b/src/Bundle.js index a2d11ee..692ab08 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -15,7 +15,6 @@ import { mapSequence } from './utils/promise.js'; import transform from './utils/transform.js'; import transformBundle from './utils/transformBundle.js'; import collapseSourcemaps from './utils/collapseSourcemaps.js'; -import SOURCEMAPPING_URL from './utils/sourceMappingURL.js'; import callIfFunction from './utils/callIfFunction.js'; import { dirname, isRelative, isAbsolute, normalize, relative, resolve } from './utils/path.js'; import BundleScope from './ast/scopes/BundleScope.js'; @@ -428,8 +427,7 @@ export default class Bundle { let map = null; const bundleSourcemapChain = []; - code = transformBundle( code, this.plugins, bundleSourcemapChain, options ) - .replace( new RegExp( `^\\/\\/# +${SOURCEMAPPING_URL}=.+\\n?`, 'gm' ), '' ); + code = transformBundle( code, this.plugins, bundleSourcemapChain, options ); if ( options.sourceMap ) { timeStart( 'sourceMap' ); diff --git a/src/Module.js b/src/Module.js index 20de137..bae304f 100644 --- a/src/Module.js +++ b/src/Module.js @@ -5,7 +5,7 @@ import { assign, blank, deepClone, keys } from './utils/object.js'; import { basename, extname } from './utils/path.js'; import getLocation from './utils/getLocation.js'; import makeLegalIdentifier from './utils/makeLegalIdentifier.js'; -import SOURCEMAPPING_URL from './utils/sourceMappingURL.js'; +import { SOURCEMAPPING_URL_RE } from './utils/sourceMappingURL.js'; import error from './utils/error.js'; import relativeId from './utils/relativeId.js'; import { SyntheticNamespaceDeclaration } from './Declaration.js'; @@ -72,11 +72,14 @@ export default class Module { }); // remove existing sourceMappingURL comments - const pattern = new RegExp( `^\\/\\/# +${SOURCEMAPPING_URL}=.+\\n?`, 'gm' ); - let match; - while ( match = pattern.exec( code ) ) { - this.magicString.remove( match.index, match.index + match[0].length ); - } + this.comments = this.comments.filter(comment => { + //only one line comment can contain source maps + const isSourceMapComment = !comment.block && SOURCEMAPPING_URL_RE.test(comment.text); + if (isSourceMapComment) { + this.magicString.remove(comment.start, comment.end ); + } + return !isSourceMapComment; + }); this.declarations = blank(); this.type = 'Module'; // TODO only necessary so that Scope knows this should be treated as a function scope... messy diff --git a/src/rollup.js b/src/rollup.js index cf79500..dfb537d 100644 --- a/src/rollup.js +++ b/src/rollup.js @@ -4,7 +4,7 @@ import { writeFile } from './utils/fs.js'; import { assign, keys } from './utils/object.js'; import { mapSequence } from './utils/promise.js'; import validateKeys from './utils/validateKeys.js'; -import SOURCEMAPPING_URL from './utils/sourceMappingURL.js'; +import { SOURCEMAPPING_URL } from './utils/sourceMappingURL.js'; import Bundle from './Bundle.js'; export const VERSION = '<@VERSION@>'; diff --git a/src/utils/sourceMappingURL.js b/src/utils/sourceMappingURL.js index 616c24d..c9adf60 100644 --- a/src/utils/sourceMappingURL.js +++ b/src/utils/sourceMappingURL.js @@ -3,4 +3,6 @@ let SOURCEMAPPING_URL = 'sourceMa'; SOURCEMAPPING_URL += 'ppingURL'; -export default SOURCEMAPPING_URL; +const SOURCEMAPPING_URL_RE = new RegExp( `^#\\s+${SOURCEMAPPING_URL}=.+\\n?` ); + +export { SOURCEMAPPING_URL, SOURCEMAPPING_URL_RE }; diff --git a/test/form/removes-existing-sourcemap-comments/_expected/amd.js b/test/form/removes-existing-sourcemap-comments/_expected/amd.js index f2854e1..a344337 100644 --- a/test/form/removes-existing-sourcemap-comments/_expected/amd.js +++ b/test/form/removes-existing-sourcemap-comments/_expected/amd.js @@ -5,8 +5,8 @@ define(function () { 'use strict'; }; var str = ` - //# sourceMappingURL=main.js.map - `; +//# sourceMappingURL=main.js.map +`; console.log( foo(str) ); diff --git a/test/form/removes-existing-sourcemap-comments/_expected/iife.js b/test/form/removes-existing-sourcemap-comments/_expected/iife.js index f898c02..01665ec 100644 --- a/test/form/removes-existing-sourcemap-comments/_expected/iife.js +++ b/test/form/removes-existing-sourcemap-comments/_expected/iife.js @@ -6,8 +6,8 @@ }; var str = ` - //# sourceMappingURL=main.js.map - `; +//# sourceMappingURL=main.js.map +`; console.log( foo(str) ); diff --git a/test/form/removes-existing-sourcemap-comments/_expected/umd.js b/test/form/removes-existing-sourcemap-comments/_expected/umd.js index cb202ab..d465a05 100644 --- a/test/form/removes-existing-sourcemap-comments/_expected/umd.js +++ b/test/form/removes-existing-sourcemap-comments/_expected/umd.js @@ -9,8 +9,8 @@ }; var str = ` - //# sourceMappingURL=main.js.map - `; +//# sourceMappingURL=main.js.map +`; console.log( foo(str) );