Skip to content

Commit

Permalink
Resolve #3398 Add flag to disable sourcemap url annotation (#3517)
Browse files Browse the repository at this point in the history
* Add flag to disable sourcemap url annotation in compiled css.

* Add test for --source-map-no-annotation option.

Co-authored-by: Jeff Fennell <jfennell@techempower.com>
  • Loading branch information
hirosato and jfennell-techempower committed Jun 17, 2020
1 parent e018ba8 commit a3641e4
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
9 changes: 9 additions & 0 deletions bin/lessc
Expand Up @@ -10949,6 +10949,12 @@ function render() {
// make the sourcemap filename point to the sourcemap relative to the css file output directory
sourceMapOptions.sourceMapFilename = path.join(path.relative(outputDir, mapDir), path.basename(sourceMapOptions.sourceMapFullFilename));
}
if (sourceMapOptions.sourceMapURL && sourceMapOptions.disableSourcemapAnnotation) {
console.error('You cannot provide flag --source-map-url with --source-map-no-annotation.')
console.error('Please remove one of those flags.')
process.exitcode = 1;
return;
}
}
if (sourceMapOptions.sourceMapBasepath === undefined) {
sourceMapOptions.sourceMapBasepath = input ? path.dirname(input) : process.cwd();
Expand Down Expand Up @@ -11272,6 +11278,9 @@ function processPluginQueue() {
sourceMapOptions.sourceMapURL = match[2];
}
break;
case 'source-map-no-annotation':
sourceMapOptions.disableSourcemapAnnotation = true;
break;
case 'rp':
case 'rootpath':
if (checkArgFunc(arg, match[2])) {
Expand Down
1 change: 1 addition & 0 deletions lib/less-node/lessc-helper.js
Expand Up @@ -44,6 +44,7 @@ const lessc_helper = {
console.log(' --source-map-inline Puts the map (and any less files) as a base64 data uri into the output css file.');
console.log(' --source-map-url=URL Sets a custom URL to map file, for sourceMappingURL comment');
console.log(' in generated CSS file.');
console.log(' --source-map-no-annotation Excludes the sourceMappingURL comment from the output css file.');
console.log(' -rp, --rootpath=URL Sets rootpath for url rewriting in relative imports and urls');
console.log(' Works with or without the relative-urls option.');
console.log(' -ru=, --rewrite-urls= Rewrites URLs to make them relative to the base less file.');
Expand Down
7 changes: 6 additions & 1 deletion lib/less/source-map-builder.js
Expand Up @@ -17,7 +17,8 @@ export default (SourceMapOutput, environment) => {
sourceMapRootpath: this.options.sourceMapRootpath,
outputSourceFiles: this.options.outputSourceFiles,
sourceMapGenerator: this.options.sourceMapGenerator,
sourceMapFileInline: this.options.sourceMapFileInline
sourceMapFileInline: this.options.sourceMapFileInline,
disableSourcemapAnnotation: this.options.disableSourcemapAnnotation
});

const css = sourceMapOutput.toCSS(options);
Expand All @@ -42,6 +43,10 @@ export default (SourceMapOutput, environment) => {
sourceMapURL = `data:application/json;base64,${environment.encodeBase64(this.sourceMap)}`;
}

if (this.options.disableSourcemapAnnotation) {
return '';
}

if (sourceMapURL) {
return `/*# sourceMappingURL=${sourceMapURL} */`;
}
Expand Down
2 changes: 2 additions & 0 deletions test/index.js
Expand Up @@ -56,6 +56,8 @@ var testMap = [
}],
[{math: 'strict', strictUnits: true, sourceMap: {sourceMapFileInline: true}},
'sourcemaps-empty/', lessTester.testEmptySourcemap],
[{math: 'strict', strictUnits: true, sourceMap: {disableSourcemapAnnotation: true}},
'sourcemaps-disable-annotation/', lessTester.testSourcemapWithoutUrlAnnotation],
[{globalVars: true, banner: '/**\n * Test\n */\n'}, 'globalVars/',
null, null, null, function(name, type, baseFolder) { return path.join(baseFolder, name) + '.json'; }],
[{modifyVars: true}, 'modifyVars/',
Expand Down
33 changes: 32 additions & 1 deletion test/less-test.js
Expand Up @@ -123,6 +123,35 @@ module.exports = function() {
});
}

function testSourcemapWithoutUrlAnnotation(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
if (err) {
fail('ERROR: ' + (err && err.message));
return;
}
// This matches with strings that end($) with source mapping url annotation.
var sourceMapRegExp = /\/\*# sourceMappingURL=.+\.css\.map \*\/$/;
if (sourceMapRegExp.test(compiledLess)) {
fail('ERROR: sourceMappingURL found in ' + baseFolder + '/' + name + '.css.');
return;
}

// Even if annotation is not necessary, the map file should be there.
fs.readFile(path.join('test/', name) + '.json', 'utf8', function (e, expectedSourcemap) {
process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
if (sourcemap === expectedSourcemap) {
ok('OK');
} else if (err) {
fail('ERROR: ' + (err && err.message));
if (isVerbose) {
process.stdout.write('\n');
process.stdout.write(err.stack + '\n');
}
} else {
difference('FAIL', expectedSourcemap, sourcemap);
}
});
}

function testEmptySourcemap(name, err, compiledLess, doReplacements, sourcemap, baseFolder) {
process.stdout.write('- ' + path.join(baseFolder, name) + ': ');
if (err) {
Expand Down Expand Up @@ -309,7 +338,8 @@ module.exports = function() {
options.sourceMap = {
sourceMapOutputFilename: name + '.css',
sourceMapBasepath: path.join(process.cwd(), baseFolder),
sourceMapRootpath: 'testweb/'
sourceMapRootpath: 'testweb/',
disableSourcemapAnnotation: options.sourceMap.disableSourcemapAnnotation
};
// This options is normally set by the bin/lessc script. Setting it causes the sourceMappingURL comment to be appended to the CSS
// output. The value is designed to allow the sourceMapBasepath option to be tested, as it should be removed by less before
Expand Down Expand Up @@ -493,6 +523,7 @@ module.exports = function() {
testSyncronous: testSyncronous,
testErrors: testErrors,
testSourcemap: testSourcemap,
testSourcemapWithoutUrlAnnotation: testSourcemapWithoutUrlAnnotation,
testImports: testImports,
testEmptySourcemap: testEmptySourcemap,
testNoOptions: testNoOptions,
Expand Down
4 changes: 4 additions & 0 deletions test/less/sourcemaps-disable-annotation/basic.less
@@ -0,0 +1,4 @@
body {
/*# sourceMappingURL=this-should-be-ok.css.map */
color: white;
}
1 change: 1 addition & 0 deletions test/sourcemaps-disable-annotation/basic.json
@@ -0,0 +1 @@
{"version":3,"sources":["testweb/sourcemaps-disable-annotation/basic.less"],"names":[],"mappings":"AAAA;;EAEE,YAAA","file":"sourcemaps-disable-annotation/basic.css"}

0 comments on commit a3641e4

Please sign in to comment.