ember-cli / ember-cli-terser

JavaScript minification for Ember-CLI
MIT License
23 stars 27 forks source link

Not working with source mapping #4

Closed mcm-ham closed 3 years ago

mcm-ham commented 9 years ago

With ember-cli 0.2.0, node 0.12 and ember-cli-uglify 1.0.1 I'm getting this error:

Error: Invalid mapping: {"generated":{"line":75794,"column":-50},"source":"bower_components/select2/select2.js","original":{"line":1,"column":0},"name":null}

If I remove select2 I get the same error with the next vendor package I have included so don't think it's specific to select2.

Cryrivers commented 8 years ago

@nikz

{
  "dependencies": {
    "ember": "2.3.0",
    "ember-cli-shims": "0.1.0",
    "ember-cli-test-loader": "0.2.2",
    "ember-load-initializers": "0.1.7",
    "ember-qunit-notifications": "0.1.0",
    "jquery": "~2.2.0",
    "normalize.css": "~3.0.3",
    "Sortable": "1.4.2",
    "phoneformat": "phoneformat.js#~1.0.2",
    "rangyinputs": "~1.2.0",
    "Caret.js": "caret.js#~0.2.2",
    "animation-frame": "~0.2.4",
    "cropper": "~1.0.0",
    "js-cookie": "^2.0",
    "loader.js": "^3.5.0",
    "socket.io-client": "~1.3.7",
    "json-bigint": "~0.0.0",
    "qunit": "~1.20.0",
    "sanitize.js": "~1.0.0"
  },
  "resolutions": {
    "jquery": "~2.2.0"
  }
}
johnnyshields commented 8 years ago

EDIT: Some info in this comment was later determined to be incorrect, see discussion here: ef4/broccoli-uglify-sourcemap#23

Here's an updated repo which reproduces the issue: https://github.com/johnnyshields/ember-sourcemap-issue

I believe I found the problem: ember-cli-uglify requires 3rd-party bower libs to have their magic sourcemap at the BEGINNING of their source, so that Ember CLI can concatenate them like this:

//# sourceMappingURL=lib1.map
lib1.js code

lib2.js code   // this lib doesn't have a sourcemap

//# sourceMappingURL=lib3.map
lib3.js code

If a lib declares the magic comment at the END of its file, the following happens:

lib1.js code

//# sourceMappingURL=lib1.map      ember gets con
lib2.js code

Because Ember CLI evaluates the maps on the CONCATENATED file, here it apparently gets confused and thinks the source map is for lib2 when it's actually for lib1.

So, infact in the original post of this thread, select2 is not the issue--whichever JS comes BEFORE select2 is the issue, but error thrown looks like it's select2.

My sample repo uses sprintf.js as an offender which has magic comment at the end. If you manually edit the sprintf.js to have the magic comment at the beginning, everything works.

stefanpenner commented 8 years ago

Lets transplant this conversation over to: https://github.com/ef4/broccoli-uglify-sourcemap/issues/23

johnnyshields commented 8 years ago

@stefanpenner @ef4 Update: I've made a test here: https://github.com/ember-cli/broccoli-concat/pull/50 for what I believe to be the root cause--broccoli-concat is generating an invalid vendor.js/vendor.map which broccoli-uglify-sourcemap chokes on later in the build process.

sandstrom commented 3 years ago

I solved this (or a similar issue) by adding this to my ember-cli-build.js config:

    // …
    'ember-cli-terser': {
      terser: {
        compress: {
          sequences: false,
        },
        output: {
          beautify: true,
          semicolons: true,
          comments: false,
          indent_level: 2,
        },
        sourceMap: false, // ADDED
      },
      hiddenSourceMap: true, // ADDED
    },

Perhaps it helps someone else.

rwjblue commented 3 years ago

Since that info is mostly in the README (hiddenSourceMap is mentioned there) I'm going to go ahead and close this. Happy to review/help with PRs to make it clearer though...