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

How generate minified file with source map? #1905

Closed
stasbamiza opened this issue May 10, 2017 · 9 comments · Fixed by #1909
Closed

How generate minified file with source map? #1905

stasbamiza opened this issue May 10, 2017 · 9 comments · Fixed by #1909

Comments

@stasbamiza
Copy link

stasbamiza commented May 10, 2017

Bug report or feature request?
Bug

ES5 or ES6+ input?
ES5

Uglify version (uglifyjs -V)
[email protected]

Hi!

I use this command for generate minified file with source map:
uglifyjs main.js -o main.min.js --source-map base="F:\UArush\web\angular2-test\test\test"
In main.min.js.map source variable have "..\\main.js" value but need have main.js and in main.min.js didn't add //# sourceMappingURL=/main.min.js.map

Thanks!

@kzc
Copy link
Contributor

kzc commented May 10, 2017

didn't add //# sourceMappingURL=/main.min.js.map

Yeah, that's a bug in [email protected].

$ cat main.js
function hello() {
    var hello = "Hello";
    var world = "world";
    console.log(hello + " " + world + "!");
}
hello();
$ uglifyjs -V
3.0.3
$ uglifyjs main.js -c -m --source-map -o main.min.js
$ cat main.min.js.map && echo
{"version":3,"sources":["main.js"],"names":["hello","console","log"],"mappings":"AAAA,QAASA,SAGLC,QAAQC,IAAIF,gBAEhBA"}
$ cat main.min.js && echo
function hello(){console.log("Hello world!")}hello();

It works in [email protected] (which has different command line options):

$ uglifyjs -V
uglify-js 2.7.5
$ uglifyjs main.js -c -m --source-map main.min.js.map -o main.min.js
$ cat main.min.js.map && echo
{"version":3,"sources":["main.js"],"names":["hello","world","console","log"],"mappings":"AAAA,QAASA,SACL,GAAIA,GAAQ,QACRC,EAAQ,OACZC,SAAQC,IAAIH,EAAQ,IAAMC,EAAQ,KAEtCD","file":"main.min.js"}
$ cat main.min.js && echo
function hello(){var l="Hello",o="world";console.log(l+" "+o+"!")}hello();
//# sourceMappingURL=main.min.js.map

@alexlamsl
Copy link
Collaborator

sourceMappingURL aren't added by default in 3.x due to one of the feature requests not to - some users are putting them within HTTP response headers instead.

So the command line for that would be:

$ uglifyjs main.js -cmo main.min.js --source-map url=main.min.js.map

@kzc
Copy link
Contributor

kzc commented May 10, 2017

@alexlamsl That's not very intuitive using url for the map filename. Can it be documented?

@kzc
Copy link
Contributor

kzc commented May 10, 2017

@alexlamsl The more I think about it, it's still a 3.x bug:

This [email protected] command produces both main.min.js and main.min.js.map:

$ uglifyjs main.js -c -m --source-map -o main.min.js

Yet it does not add the source map comment to the end of main.min.js - so it's useless. If uglify is smart enough to know to generate the map filename it can be used in the source map comment.

@mes-studio
Copy link

@alexlamsl Maybe you save CLI options from 2.x version for more compatibility

@alexlamsl
Copy link
Collaborator

@kzc because if someone needs to serve main.min.js.map but wishes to use X-SourceMap instead, they don't want //# sourceMappingURL= within main.min.js

I think the relevant issue is #1036

@kzc
Copy link
Contributor

kzc commented May 10, 2017

@alexlamsl It's your call. But it could be better documented in any case.

@alexlamsl
Copy link
Collaborator

@kzc I haven't made up my mind yet - just making sure we are on the same page about X-SourceMap

The rationale behind restructuring the whole sourceMap is to enable previously impossible use cases, mainly caused by all the path-related magic back then, but hardcoding output of sourceMappingURL was also one of those.

There is also options.sourceMap.filename which also defaults to undefined at the moment, which is responsible for the file attribute within .js.map output, which I guess might also caught some people off guard.

While there is no way to even guess a source map file name within minify() because we don't know any output file paths, bin/uglifyjs does have information about that and in theory we can short-circuit --source-map url, i.e. options.sourceMap.url=true to signal CLI to substitute its output .js.map into the URL. We can do the same for --source-map filename.

@kzc
Copy link
Contributor

kzc commented May 10, 2017

just making sure we are on the same page about X-SourceMap

Okay, I did not know the backstory about the source map configuration travails. A few lines of documentation with examples for typical usage scenarios is probably sufficient.

alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue May 11, 2017
- clarify options on `--source-map`
- fix `minify()` examples

fixes mishoo#1905
alexlamsl added a commit that referenced this issue May 11, 2017
- clarify options on `--source-map`
- fix `minify()` examples

fixes #1905
vlukashov pushed a commit to vaadin/router that referenced this issue Jun 19, 2018
 - instruct UglifyJS to add a `//# sourceMappingURL=vaadin-router.umd.min.js.map` comment to the end of the minified UMD bundle (see mishoo/UglifyJS#1905 for details)
 - specify the input source map as described in https://github.com/mishoo/UglifyJS2#composed-source-map
web-padawan pushed a commit to vaadin/router that referenced this issue Jun 19, 2018
* fix the missing source maps for the minified UMD bundle

 - instruct UglifyJS to add a `//# sourceMappingURL=vaadin-router.umd.min.js.map` comment to the end of the minified UMD bundle (see mishoo/UglifyJS#1905 for details)
 - specify the input source map as described in https://github.com/mishoo/UglifyJS2#composed-source-map

* Merge branch 'master' into fix/minified-umd-bundle-source-map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants