Skip to content

Commit

Permalink
fix: do not break code with shebang
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Feb 14, 2020
1 parent ba1cb8a commit fac58cb
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/index.js
Expand Up @@ -275,7 +275,8 @@ class TerserPlugin {
}

const callback = (taskResult) => {
const { error, map, code, warnings } = taskResult;
let { code } = taskResult;
const { error, map, warnings } = taskResult;
const { extractedComments } = taskResult;

let sourceMap = null;
Expand All @@ -299,7 +300,24 @@ class TerserPlugin {
return;
}

const hasExtractedComments =
commentsFilename && extractedComments && extractedComments.length > 0;
const hasBannerForExtractedComments =
hasExtractedComments && this.options.extractComments.banner !== false;

let outputSource;
let shebang;

if (
hasExtractedComments &&
hasBannerForExtractedComments &&
code.startsWith('#!')
) {
const firstNewlinePosition = code.indexOf('\n');

shebang = code.substring(0, firstNewlinePosition);
code = code.substring(firstNewlinePosition + 1);
}

if (map) {
outputSource = new SourceMapSource(
Expand All @@ -315,11 +333,7 @@ class TerserPlugin {
}

// Write extracted comments to commentsFilename
if (
commentsFilename &&
extractedComments &&
extractedComments.length > 0
) {
if (hasExtractedComments) {
if (!allExtractedComments[commentsFilename]) {
// eslint-disable-next-line no-param-reassign
allExtractedComments[commentsFilename] = [];
Expand All @@ -331,7 +345,7 @@ class TerserPlugin {
].concat(extractedComments);

// Add a banner to the original file
if (this.options.extractComments.banner !== false) {
if (hasBannerForExtractedComments) {
let banner =
this.options.extractComments.banner ||
`For license information please see ${path
Expand All @@ -344,6 +358,7 @@ class TerserPlugin {

if (banner) {
outputSource = new ConcatSource(
shebang ? `${shebang}\n` : '',
`/*! ${banner} */\n`,
outputSource
);
Expand Down
16 changes: 16 additions & 0 deletions test/__snapshots__/extractComments-option.test.js.snap.webpack4
Expand Up @@ -734,6 +734,22 @@ exports[`extractComments option should match snapshot and extract "some" comment

exports[`extractComments option should match snapshot and extract "some" comments: warnings 1`] = `Array []`;

exports[`extractComments option should match snapshot and keep shebang: assets 1`] = `
Object {
"shebang.js": "#!/usr/bin/env node
/*! For license information please see shebang.js.LICENSE.txt */
!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){console.log(\\"HERE\\")}]);",
"shebang.js.LICENSE.txt": "/*! Legal Comment */
",
"shebang1.js": "#!/usr/bin/env node
!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=1)}([,function(e,t){console.log(\\"HERE\\")}]);",
}
`;

exports[`extractComments option should match snapshot and keep shebang: errors 1`] = `Array []`;

exports[`extractComments option should match snapshot and keep shebang: warnings 1`] = `Array []`;

exports[`extractComments option should match snapshot and preserve "all" and do not extract comments: assets 1`] = `
Object {
"chunks/4.4.js": "(window.webpackJsonp=window.webpackJsonp||[]).push([[4],{
Expand Down
16 changes: 16 additions & 0 deletions test/__snapshots__/extractComments-option.test.js.snap.webpack5
Expand Up @@ -634,6 +634,22 @@ exports[`extractComments option should match snapshot and extract "some" comment

exports[`extractComments option should match snapshot and extract "some" comments: warnings 1`] = `Array []`;

exports[`extractComments option should match snapshot and keep shebang: assets 1`] = `
Object {
"shebang.js": "#!/usr/bin/env node
/*! For license information please see shebang.js.LICENSE.txt */
console.log(\\"HERE\\");",
"shebang.js.LICENSE.txt": "/*! Legal Comment */
",
"shebang1.js": "#!/usr/bin/env node
console.log(\\"HERE\\");",
}
`;

exports[`extractComments option should match snapshot and keep shebang: errors 1`] = `Array []`;

exports[`extractComments option should match snapshot and keep shebang: warnings 1`] = `Array []`;

exports[`extractComments option should match snapshot and preserve "all" and do not extract comments: assets 1`] = `
Object {
"chunks/627.627.js": "(window.webpackJsonp=window.webpackJsonp||[]).push([[627],{
Expand Down
23 changes: 23 additions & 0 deletions test/extractComments-option.test.js
@@ -1,3 +1,5 @@
import webpack from 'webpack';

import TerserPlugin from '../src/index';

import {
Expand Down Expand Up @@ -642,4 +644,25 @@ describe('extractComments option', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should match snapshot and keep shebang', async () => {
compiler = getCompiler({
entry: {
shebang: `${__dirname}/fixtures/shebang.js`,
shebang1: `${__dirname}/fixtures/shebang-1.js`,
},
target: 'node',
plugins: [
new webpack.BannerPlugin({ banner: '#!/usr/bin/env node', raw: true }),
],
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});
});
1 change: 1 addition & 0 deletions test/fixtures/shebang-1.js
@@ -0,0 +1 @@
console.log('HERE');
3 changes: 3 additions & 0 deletions test/fixtures/shebang.js
@@ -0,0 +1,3 @@
/* Comment */
/*! Legal Comment */
console.log('HERE');

0 comments on commit fac58cb

Please sign in to comment.