Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unused tagged template literals produce invalid minified code #78

Closed
jondlm opened this issue Mar 7, 2019 · 2 comments
Closed

Unused tagged template literals produce invalid minified code #78

jondlm opened this issue Mar 7, 2019 · 2 comments

Comments

@jondlm
Copy link

jondlm commented Mar 7, 2019

  • Operating System: macOS 10.12.6
  • Node Version: v10.14.2
  • NPM Version: 6.8.0
  • webpack Version: 4.29.6 (bug first appeared in 4.26.0 with the switch to terser)
  • terser-webpack-plugin Version: 1.2.3

Expected Behavior

Webpack production mode emits valid javascript that can be executed by a browser. E.g.

(function(e){return e})`one`

Actual Behavior

Webpack production mode is emitting invalid javascript. Specifically it's emitting an unnamed function (i.e. function() {...}) that is not syntactically valid. E.g.

function(e){return e}`one`

Code

webpack.config.js

module.exports = {
  mode: "production",
  entry: `${__dirname}/src/index.js`,
  output: {
    path: `${__dirname}/build`,
    filename: "repro.min.js"
  }
};

src/index.js

function tpl(str) {
  return str;
}

const unused = tpl`one`;

// Uncomment the line below to work around the bug:
// const anotherUnused = tpl`two`;

build/repro.min.js (webpack default wrappers removed for clarity)

function(e){return e}`one`

You can see that a single unused tagged template literal is producing an unnamed function error. It can be worked around by referencing the template literal function multiple times but wasn't a bug before terser was introduced as the minification engine for webpack.

Interestingly enough, I was not able to reproduce the bug in terser (v3.16.1), which leads me to believe the bug exists in this plugin.

How Do We Reproduce?

I've created a really small repo which should help you reproduce the issue (instructions assume you're on a mac since it uses the open command to open the index file in a browser):

git clone [email protected]:jondlm/webpack-template-literal-bug.git
cd webpack-template-literal-bug
npm install
npm start
@alexander-akait
Copy link
Member

We just wrapper around terser (https://github.com/terser-js/terser), if you think it is bug please open issue in their repo, we can't do something on our side (we don't modify your code, just send this code to terser and return output), thanks!

@jondlm
Copy link
Author

jondlm commented Mar 11, 2019

Sounds good. I'll try a bit harder to reproduce the issue in terser. I didn't try taking the entire output from webpack and using that as a test case over there. Sorry for the noise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants