From 962be764b313e1367b639f76d7e6c8596ea7b53e Mon Sep 17 00:00:00 2001 From: Rohit Mohan Date: Fri, 27 Sep 2019 20:37:17 +0530 Subject: [PATCH] Generate sourcemaps but omit the comment (#3120) * Generate sourcemaps without comment using `hidden` option (#2743) Signed-off-by: Rohit Mohan * Added tests for hidden sourcemaps Signed-off-by: Rohit Mohan * Added cli tests for hidden sourcemaps Signed-off-by: Rohit Mohan * Update documentation and improve test --- docs/999-big-list-of-options.md | 4 ++-- src/rollup/index.ts | 4 +++- src/rollup/types.d.ts | 2 +- test/cli/samples/sourcemap-hidden/_config.js | 17 +++++++++++++++ test/cli/samples/sourcemap-hidden/main.js | 1 + .../form/samples/sourcemaps-hidden/_config.js | 7 +++++++ .../sourcemaps-hidden/_expected/amd.js | 16 ++++++++++++++ .../sourcemaps-hidden/_expected/amd.js.map | 1 + .../sourcemaps-hidden/_expected/cjs.js | 14 +++++++++++++ .../sourcemaps-hidden/_expected/cjs.js.map | 1 + .../samples/sourcemaps-hidden/_expected/es.js | 12 +++++++++++ .../sourcemaps-hidden/_expected/es.js.map | 1 + .../sourcemaps-hidden/_expected/iife.js | 17 +++++++++++++++ .../sourcemaps-hidden/_expected/iife.js.map | 1 + .../sourcemaps-hidden/_expected/system.js | 21 +++++++++++++++++++ .../sourcemaps-hidden/_expected/system.js.map | 1 + .../sourcemaps-hidden/_expected/umd.js | 19 +++++++++++++++++ .../sourcemaps-hidden/_expected/umd.js.map | 1 + test/form/samples/sourcemaps-hidden/bar.js | 3 +++ test/form/samples/sourcemaps-hidden/foo.js | 3 +++ test/form/samples/sourcemaps-hidden/main.js | 7 +++++++ 21 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 test/cli/samples/sourcemap-hidden/_config.js create mode 100644 test/cli/samples/sourcemap-hidden/main.js create mode 100644 test/form/samples/sourcemaps-hidden/_config.js create mode 100644 test/form/samples/sourcemaps-hidden/_expected/amd.js create mode 100644 test/form/samples/sourcemaps-hidden/_expected/amd.js.map create mode 100644 test/form/samples/sourcemaps-hidden/_expected/cjs.js create mode 100644 test/form/samples/sourcemaps-hidden/_expected/cjs.js.map create mode 100644 test/form/samples/sourcemaps-hidden/_expected/es.js create mode 100644 test/form/samples/sourcemaps-hidden/_expected/es.js.map create mode 100644 test/form/samples/sourcemaps-hidden/_expected/iife.js create mode 100644 test/form/samples/sourcemaps-hidden/_expected/iife.js.map create mode 100644 test/form/samples/sourcemaps-hidden/_expected/system.js create mode 100644 test/form/samples/sourcemaps-hidden/_expected/system.js.map create mode 100644 test/form/samples/sourcemaps-hidden/_expected/umd.js create mode 100644 test/form/samples/sourcemaps-hidden/_expected/umd.js.map create mode 100644 test/form/samples/sourcemaps-hidden/bar.js create mode 100644 test/form/samples/sourcemaps-hidden/foo.js create mode 100644 test/form/samples/sourcemaps-hidden/main.js diff --git a/docs/999-big-list-of-options.md b/docs/999-big-list-of-options.md index 786902dfd3b..4f093070905 100755 --- a/docs/999-big-list-of-options.md +++ b/docs/999-big-list-of-options.md @@ -486,11 +486,11 @@ define(['https://d3js.org/d3.v4.min'], function (d3) { ``` #### output.sourcemap -Type: `boolean | 'inline'`
+Type: `boolean | 'inline' | 'hidden'`
CLI: `-m`/`--sourcemap`/`--no-sourcemap`
Default: `false` -If `true`, a separate sourcemap file will be created. If `inline`, the sourcemap will be appended to the resulting `output` file as a data URI. +If `true`, a separate sourcemap file will be created. If `"inline"`, the sourcemap will be appended to the resulting `output` file as a data URI. `"hidden"` works like `true` except that the corresponding sourcemap comments in the bundled files are suppressed. #### output.sourcemapExcludeSources Type: `boolean`
diff --git a/src/rollup/index.ts b/src/rollup/index.ts index 0aeffbb597f..8f3eb874586 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -402,7 +402,9 @@ function writeOutputFile( url = `${basename(outputFile.fileName)}.map`; writeSourceMapPromise = writeFile(`${fileName}.map`, outputFile.map.toString()); } - source += `//# ${SOURCEMAPPING_URL}=${url}\n`; + if (outputOptions.sourcemap !== 'hidden') { + source += `//# ${SOURCEMAPPING_URL}=${url}\n`; + } } } diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 67daf46526a..dde8c504e0f 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -475,7 +475,7 @@ export interface OutputOptions { outro?: string | (() => string | Promise); paths?: OptionsPaths; preferConst?: boolean; - sourcemap?: boolean | 'inline'; + sourcemap?: boolean | 'inline' | 'hidden'; sourcemapExcludeSources?: boolean; sourcemapFile?: string; sourcemapPathTransform?: (sourcePath: string) => string; diff --git a/test/cli/samples/sourcemap-hidden/_config.js b/test/cli/samples/sourcemap-hidden/_config.js new file mode 100644 index 00000000000..811c7d18948 --- /dev/null +++ b/test/cli/samples/sourcemap-hidden/_config.js @@ -0,0 +1,17 @@ +const fs = require('fs'); +const assert = require('assert'); + +module.exports = { + description: 'omits sourcemap comments', + command: 'rollup -i main.js -f es -m hidden -o output.js', + test() { + assert.equal(fs.readFileSync('output.js', 'utf-8').trim(), 'console.log( 42 );'); + fs.unlinkSync('output.js'); + assert.equal( + fs.readFileSync('output.js.map', 'utf-8').trim(), + '{"version":3,"file":"output.js","sources":["main.js"],"sourcesContent":' + + '["console.log( 42 );\\n"],"names":[],"mappings":"AAAA,OAAO,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC"}' + ); + fs.unlinkSync('output.js.map'); + } +}; diff --git a/test/cli/samples/sourcemap-hidden/main.js b/test/cli/samples/sourcemap-hidden/main.js new file mode 100644 index 00000000000..5c72ff35124 --- /dev/null +++ b/test/cli/samples/sourcemap-hidden/main.js @@ -0,0 +1 @@ +console.log( 42 ); diff --git a/test/form/samples/sourcemaps-hidden/_config.js b/test/form/samples/sourcemaps-hidden/_config.js new file mode 100644 index 00000000000..9aa83cd79dc --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_config.js @@ -0,0 +1,7 @@ +module.exports = { + description: 'correct sourcemaps are written (separate file) without comment', + skipIfWindows: true, + options: { + output: { sourcemap: 'hidden' } + } +}; diff --git a/test/form/samples/sourcemaps-hidden/_expected/amd.js b/test/form/samples/sourcemaps-hidden/_expected/amd.js new file mode 100644 index 00000000000..da7dafc6803 --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/amd.js @@ -0,0 +1,16 @@ +define(function () { 'use strict'; + + function foo () { + console.log( 'hello from foo.js' ); + } + + function bar () { + console.log( 'hello from bar.js' ); + } + + console.log( 'hello from main.js' ); + + foo(); + bar(); + +}); diff --git a/test/form/samples/sourcemaps-hidden/_expected/amd.js.map b/test/form/samples/sourcemaps-hidden/_expected/amd.js.map new file mode 100644 index 00000000000..d945d52376c --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/amd.js.map @@ -0,0 +1 @@ +{"version":3,"file":"amd.js","sources":["../foo.js","../bar.js","../main.js"],"sourcesContent":["export default function foo () {\n\tconsole.log( 'hello from foo.js' );\n}\n","export default function bar () {\n\tconsole.log( 'hello from bar.js' );\n}\n","import foo from './foo';\nimport bar from './bar';\n\nconsole.log( 'hello from main.js' );\n\nfoo();\nbar();\n"],"names":[],"mappings":";;CAAe,SAAS,GAAG,IAAI;CAC/B,CAAC,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;CACpC,CAAC;;CCFc,SAAS,GAAG,IAAI;CAC/B,CAAC,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;CACpC,CAAC;;CCCD,OAAO,CAAC,GAAG,EAAE,oBAAoB,EAAE,CAAC;;CAEpC,GAAG,EAAE,CAAC;CACN,GAAG,EAAE,CAAC;;;;"} \ No newline at end of file diff --git a/test/form/samples/sourcemaps-hidden/_expected/cjs.js b/test/form/samples/sourcemaps-hidden/_expected/cjs.js new file mode 100644 index 00000000000..be410ec0c4e --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/cjs.js @@ -0,0 +1,14 @@ +'use strict'; + +function foo () { + console.log( 'hello from foo.js' ); +} + +function bar () { + console.log( 'hello from bar.js' ); +} + +console.log( 'hello from main.js' ); + +foo(); +bar(); diff --git a/test/form/samples/sourcemaps-hidden/_expected/cjs.js.map b/test/form/samples/sourcemaps-hidden/_expected/cjs.js.map new file mode 100644 index 00000000000..c47010ac654 --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/cjs.js.map @@ -0,0 +1 @@ +{"version":3,"file":"cjs.js","sources":["../foo.js","../bar.js","../main.js"],"sourcesContent":["export default function foo () {\n\tconsole.log( 'hello from foo.js' );\n}\n","export default function bar () {\n\tconsole.log( 'hello from bar.js' );\n}\n","import foo from './foo';\nimport bar from './bar';\n\nconsole.log( 'hello from main.js' );\n\nfoo();\nbar();\n"],"names":[],"mappings":";;AAAe,SAAS,GAAG,IAAI;CAC9B,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;CACnC;;ACFc,SAAS,GAAG,IAAI;CAC9B,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;CACnC;;ACCD,OAAO,CAAC,GAAG,EAAE,oBAAoB,EAAE,CAAC;;AAEpC,GAAG,EAAE,CAAC;AACN,GAAG,EAAE,CAAC"} \ No newline at end of file diff --git a/test/form/samples/sourcemaps-hidden/_expected/es.js b/test/form/samples/sourcemaps-hidden/_expected/es.js new file mode 100644 index 00000000000..a5385e2ecbe --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/es.js @@ -0,0 +1,12 @@ +function foo () { + console.log( 'hello from foo.js' ); +} + +function bar () { + console.log( 'hello from bar.js' ); +} + +console.log( 'hello from main.js' ); + +foo(); +bar(); diff --git a/test/form/samples/sourcemaps-hidden/_expected/es.js.map b/test/form/samples/sourcemaps-hidden/_expected/es.js.map new file mode 100644 index 00000000000..be026f2972a --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/es.js.map @@ -0,0 +1 @@ +{"version":3,"file":"es.js","sources":["../foo.js","../bar.js","../main.js"],"sourcesContent":["export default function foo () {\n\tconsole.log( 'hello from foo.js' );\n}\n","export default function bar () {\n\tconsole.log( 'hello from bar.js' );\n}\n","import foo from './foo';\nimport bar from './bar';\n\nconsole.log( 'hello from main.js' );\n\nfoo();\nbar();\n"],"names":[],"mappings":"AAAe,SAAS,GAAG,IAAI;CAC9B,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;CACnC;;ACFc,SAAS,GAAG,IAAI;CAC9B,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;CACnC;;ACCD,OAAO,CAAC,GAAG,EAAE,oBAAoB,EAAE,CAAC;;AAEpC,GAAG,EAAE,CAAC;AACN,GAAG,EAAE,CAAC"} \ No newline at end of file diff --git a/test/form/samples/sourcemaps-hidden/_expected/iife.js b/test/form/samples/sourcemaps-hidden/_expected/iife.js new file mode 100644 index 00000000000..eceb9d3de37 --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/iife.js @@ -0,0 +1,17 @@ +(function () { + 'use strict'; + + function foo () { + console.log( 'hello from foo.js' ); + } + + function bar () { + console.log( 'hello from bar.js' ); + } + + console.log( 'hello from main.js' ); + + foo(); + bar(); + +}()); diff --git a/test/form/samples/sourcemaps-hidden/_expected/iife.js.map b/test/form/samples/sourcemaps-hidden/_expected/iife.js.map new file mode 100644 index 00000000000..fbad1a686e6 --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/iife.js.map @@ -0,0 +1 @@ +{"version":3,"file":"iife.js","sources":["../foo.js","../bar.js","../main.js"],"sourcesContent":["export default function foo () {\n\tconsole.log( 'hello from foo.js' );\n}\n","export default function bar () {\n\tconsole.log( 'hello from bar.js' );\n}\n","import foo from './foo';\nimport bar from './bar';\n\nconsole.log( 'hello from main.js' );\n\nfoo();\nbar();\n"],"names":[],"mappings":";;;CAAe,SAAS,GAAG,IAAI;CAC/B,CAAC,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;CACpC,CAAC;;CCFc,SAAS,GAAG,IAAI;CAC/B,CAAC,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;CACpC,CAAC;;CCCD,OAAO,CAAC,GAAG,EAAE,oBAAoB,EAAE,CAAC;;CAEpC,GAAG,EAAE,CAAC;CACN,GAAG,EAAE,CAAC;;;;"} \ No newline at end of file diff --git a/test/form/samples/sourcemaps-hidden/_expected/system.js b/test/form/samples/sourcemaps-hidden/_expected/system.js new file mode 100644 index 00000000000..698e34e6f11 --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/system.js @@ -0,0 +1,21 @@ +System.register([], function () { + 'use strict'; + return { + execute: function () { + + function foo () { + console.log( 'hello from foo.js' ); + } + + function bar () { + console.log( 'hello from bar.js' ); + } + + console.log( 'hello from main.js' ); + + foo(); + bar(); + + } + }; +}); diff --git a/test/form/samples/sourcemaps-hidden/_expected/system.js.map b/test/form/samples/sourcemaps-hidden/_expected/system.js.map new file mode 100644 index 00000000000..365a44b3def --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/system.js.map @@ -0,0 +1 @@ +{"version":3,"file":"system.js","sources":["../foo.js","../bar.js","../main.js"],"sourcesContent":["export default function foo () {\n\tconsole.log( 'hello from foo.js' );\n}\n","export default function bar () {\n\tconsole.log( 'hello from bar.js' );\n}\n","import foo from './foo';\nimport bar from './bar';\n\nconsole.log( 'hello from main.js' );\n\nfoo();\nbar();\n"],"names":[],"mappings":";;;;;GAAe,SAAS,GAAG,IAAI;GAC/B,CAAC,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;GACpC,CAAC;;GCFc,SAAS,GAAG,IAAI;GAC/B,CAAC,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;GACpC,CAAC;;GCCD,OAAO,CAAC,GAAG,EAAE,oBAAoB,EAAE,CAAC;;GAEpC,GAAG,EAAE,CAAC;GACN,GAAG,EAAE,CAAC;;;;;;"} \ No newline at end of file diff --git a/test/form/samples/sourcemaps-hidden/_expected/umd.js b/test/form/samples/sourcemaps-hidden/_expected/umd.js new file mode 100644 index 00000000000..b3ccf75dcb3 --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/umd.js @@ -0,0 +1,19 @@ +(function (factory) { + typeof define === 'function' && define.amd ? define(factory) : + factory(); +}(function () { 'use strict'; + + function foo () { + console.log( 'hello from foo.js' ); + } + + function bar () { + console.log( 'hello from bar.js' ); + } + + console.log( 'hello from main.js' ); + + foo(); + bar(); + +})); diff --git a/test/form/samples/sourcemaps-hidden/_expected/umd.js.map b/test/form/samples/sourcemaps-hidden/_expected/umd.js.map new file mode 100644 index 00000000000..a9238714988 --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/_expected/umd.js.map @@ -0,0 +1 @@ +{"version":3,"file":"umd.js","sources":["../foo.js","../bar.js","../main.js"],"sourcesContent":["export default function foo () {\n\tconsole.log( 'hello from foo.js' );\n}\n","export default function bar () {\n\tconsole.log( 'hello from bar.js' );\n}\n","import foo from './foo';\nimport bar from './bar';\n\nconsole.log( 'hello from main.js' );\n\nfoo();\nbar();\n"],"names":[],"mappings":";;;;;CAAe,SAAS,GAAG,IAAI;CAC/B,CAAC,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;CACpC,CAAC;;CCFc,SAAS,GAAG,IAAI;CAC/B,CAAC,OAAO,CAAC,GAAG,EAAE,mBAAmB,EAAE,CAAC;CACpC,CAAC;;CCCD,OAAO,CAAC,GAAG,EAAE,oBAAoB,EAAE,CAAC;;CAEpC,GAAG,EAAE,CAAC;CACN,GAAG,EAAE,CAAC;;;;"} \ No newline at end of file diff --git a/test/form/samples/sourcemaps-hidden/bar.js b/test/form/samples/sourcemaps-hidden/bar.js new file mode 100644 index 00000000000..dcb017c734e --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/bar.js @@ -0,0 +1,3 @@ +export default function bar () { + console.log( 'hello from bar.js' ); +} diff --git a/test/form/samples/sourcemaps-hidden/foo.js b/test/form/samples/sourcemaps-hidden/foo.js new file mode 100644 index 00000000000..a80d173b0cf --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/foo.js @@ -0,0 +1,3 @@ +export default function foo () { + console.log( 'hello from foo.js' ); +} diff --git a/test/form/samples/sourcemaps-hidden/main.js b/test/form/samples/sourcemaps-hidden/main.js new file mode 100644 index 00000000000..4cb88bab0cf --- /dev/null +++ b/test/form/samples/sourcemaps-hidden/main.js @@ -0,0 +1,7 @@ +import foo from './foo'; +import bar from './bar'; + +console.log( 'hello from main.js' ); + +foo(); +bar();