Can uglifyjs ignore undefined symbols - javascript

The first file that I am passing to uglifyjs declares some namespaces like
window.MyNamespace = {}
when uglifyjs sees this line it complains that window is not defined.
Is there a way to have uglifyjs ignore undefined symbols? I have tried using the --no-dead-code option

You can wrap your global code in a function:
(function(window) {
window.whatever = something;
// ...
})(this);
You can also do this:
(function(window) {
"use strict";
// ...
})(this);
which is probably a good idea anyway. You'll get warnings/errors from stray undefined variables even without uglify.

Related

Currently running javascript function name?

This works fine AFAIK:
(function f() {
console.log(f.name); //logs f
})();
But some of the answers posted here are a lot longer, which makes me think that I might be missing a gotcha (In other words it works in this case, BUT ...) with the above statement?
Here's a slightly different typescript variation:
function f1() {}
function f2(f:Function) {
console.log(f.name);
}
f2(f1);
The Function.name property is only available in ES6/ES2015-compliant engines. So for example if you try to access it without additional configuration in Typescript you will get the error:
[ts] Property 'name' does not exist on type 'Function'.
So for typescript include es2015 in your --lib value to get the property declaration.
{
"compilerOptions": {
...
"lib": ["es2015"], /* Specify library files to be included in the compilation. */
...
}

What happens if I don't put "use strict" in a function? [duplicate]

I include the statement:
"use strict";
at the beginning of most of my Javascript files.
JSLint has never before warned about this. But now it is, saying:
Use the function form of "use strict".
Does anyone know what the "function form" would be?
Include 'use strict'; as the first statement in a wrapping function, so it only affects that function. This prevents problems when concatenating scripts that aren't strict.
See Douglas Crockford's latest blog post Strict Mode Is Coming To Town.
Example from that post:
(function () {
'use strict';
// this function is strict...
}());
(function () {
// but this function is sloppy...
}());
Update:
In case you don't want to wrap in immediate function (e.g. it is a node module), then you can disable the warning.
For JSLint (per Zhami):
/*jslint node: true */
For JSHint:
/*jshint strict:false */
or (per Laith Shadeed)
/* jshint -W097 */
To disable any arbitrary warning from JSHint, check the map in JSHint source code (details in docs).
Update 2: JSHint supports node:boolean option. See .jshintrc at github.
/* jshint node: true */
If you're writing modules for NodeJS, they are already encapsulated. Tell JSLint that you've got node by including at the top of your file:
/*jslint node: true */
I'd suggest to use jshint instead.
It allows to suppress this warning via /*jshint globalstrict: true*/.
If you are writing a library, I would only suggest using global strict if your code is encapsulated into modules as is the case with nodejs.
Otherwise you'd force everyone who is using your library into strict mode.
I started creating a Node.js/browserify application following the Cross Platform JavaScript blog post. And I ran into this issue, because my brand new Gruntfile didn't pass jshint.
Luckily I found an answer in the Leanpub book on Grunt:
If we try it now, we will scan our Gruntfile… and get some errors:
$ grunt jshint
Running "jshint:all" (jshint) task
Linting Gruntfile.js...ERROR
[L1:C1] W097: Use the function form of "use strict".
'use strict';
Linting Gruntfile.js...ERROR
[L3:C1] W117: 'module' is not defined.
module.exports = function (grunt) {
Warning: Task "jshint:all" failed. Use --force to continue.
Both errors are because the Gruntfile is a Node program, and by default JSHint does not recognise or allow the use of module and the string version of use strict. We can set a JSHint rule that will accept our Node programs. Let’s edit our jshint task configuration and add an options key:
jshint: {
options: {
node: true
},
}
Adding node: true to the jshint options, to put jshint into "Node mode", removed both errors for me.
Add a file .jslintrc (or .jshintrc in the case of jshint) at the root of your project with the following content:
{
"node": true
}
There's nothing innately wrong with the string form.
Rather than avoid the "global" strict form for worry of concatenating non-strict javascript, it's probably better to just fix the damn non-strict javascript to be strict.
process.on('warning', function(e) {
'use strict';
console.warn(e.stack);
});
process.on('uncaughtException', function(e) {
'use strict';
console.warn(e.stack);
});
add this lines to at the starting point of your file
I think everyone missed the "suddenly" part of this question. Most likely, your .jshintrc has a syntax error, so it's not including the 'browser' line. Run it through a json validator to see where the error is.
This is how simple it is: If you want to be strict with all your code, add "use strict"; at the start of your JavaScript.
But if you only want to be strict with some of your code, use the function form. Anyhow, I would recomend you to use it at the beginning of your JavaScript because this will help you be a better coder.

How to set the scope for defined variables in another file for JSHint in WebStorm?

How can I set the scope of the defined variables for JSHint to my whole project in WebStorm?
If I have multiple files and imports like jquery or Backbone I don't need to see the error JSHint: 'Backbone' is not defined.(W117). This is not only form my imported libraries, but also for my own external files.
Some suggestions is that I should disable undefined errors, but this is the functionality that I want to use.
I.E.
In my main.js I have this:
function Main(){
// Some epic code
}
Main.prototype.theBestFunctionEver = function(awesome, stuff){
return awesome + stuff;
}
and in foo.js I have this:
function init(){
var main = new Main(); // Shows that Main is undefined
var wrongVar = 6 + unInited // This should always give me an error
// Rest of init
}
JSHint works on per-file basis and doesn't 'see' variables defined in other files unless they are added to 'global' list. This can be done by either adding the corresponding comments ('/* global MY_LIB*/ - see http://www.jshint.com/docs/) in code, or by adding variables/functions you'd like to use globally to the 'Predefined' list in Preferences -> Javascript -> Code Quality Tool -> JSHint -> Predefined (,separated).

Use the function form of "use strict" [duplicate]

I include the statement:
"use strict";
at the beginning of most of my Javascript files.
JSLint has never before warned about this. But now it is, saying:
Use the function form of "use strict".
Does anyone know what the "function form" would be?
Include 'use strict'; as the first statement in a wrapping function, so it only affects that function. This prevents problems when concatenating scripts that aren't strict.
See Douglas Crockford's latest blog post Strict Mode Is Coming To Town.
Example from that post:
(function () {
'use strict';
// this function is strict...
}());
(function () {
// but this function is sloppy...
}());
Update:
In case you don't want to wrap in immediate function (e.g. it is a node module), then you can disable the warning.
For JSLint (per Zhami):
/*jslint node: true */
For JSHint:
/*jshint strict:false */
or (per Laith Shadeed)
/* jshint -W097 */
To disable any arbitrary warning from JSHint, check the map in JSHint source code (details in docs).
Update 2: JSHint supports node:boolean option. See .jshintrc at github.
/* jshint node: true */
If you're writing modules for NodeJS, they are already encapsulated. Tell JSLint that you've got node by including at the top of your file:
/*jslint node: true */
I'd suggest to use jshint instead.
It allows to suppress this warning via /*jshint globalstrict: true*/.
If you are writing a library, I would only suggest using global strict if your code is encapsulated into modules as is the case with nodejs.
Otherwise you'd force everyone who is using your library into strict mode.
I started creating a Node.js/browserify application following the Cross Platform JavaScript blog post. And I ran into this issue, because my brand new Gruntfile didn't pass jshint.
Luckily I found an answer in the Leanpub book on Grunt:
If we try it now, we will scan our Gruntfile… and get some errors:
$ grunt jshint
Running "jshint:all" (jshint) task
Linting Gruntfile.js...ERROR
[L1:C1] W097: Use the function form of "use strict".
'use strict';
Linting Gruntfile.js...ERROR
[L3:C1] W117: 'module' is not defined.
module.exports = function (grunt) {
Warning: Task "jshint:all" failed. Use --force to continue.
Both errors are because the Gruntfile is a Node program, and by default JSHint does not recognise or allow the use of module and the string version of use strict. We can set a JSHint rule that will accept our Node programs. Let’s edit our jshint task configuration and add an options key:
jshint: {
options: {
node: true
},
}
Adding node: true to the jshint options, to put jshint into "Node mode", removed both errors for me.
Add a file .jslintrc (or .jshintrc in the case of jshint) at the root of your project with the following content:
{
"node": true
}
There's nothing innately wrong with the string form.
Rather than avoid the "global" strict form for worry of concatenating non-strict javascript, it's probably better to just fix the damn non-strict javascript to be strict.
process.on('warning', function(e) {
'use strict';
console.warn(e.stack);
});
process.on('uncaughtException', function(e) {
'use strict';
console.warn(e.stack);
});
add this lines to at the starting point of your file
I think everyone missed the "suddenly" part of this question. Most likely, your .jshintrc has a syntax error, so it's not including the 'browser' line. Run it through a json validator to see where the error is.
This is how simple it is: If you want to be strict with all your code, add "use strict"; at the start of your JavaScript.
But if you only want to be strict with some of your code, use the function form. Anyhow, I would recomend you to use it at the beginning of your JavaScript because this will help you be a better coder.

node-qunit doesn't include my source files in scope

I've installed node-qunit (stable) from npm, but can't seem to get any tests working. My source files don't seem to be included in scope.
./source/myscript.js:
var myObj = {
a : true
}
./test/tests.js:
test("that a is true", function () {
ok(myObj.a);
});
./test/runner.js:
var runner = require('qunit');
runner.run({
code : './source/myscript.js',
tests : './test/tests.js'
});
./Makefile:
test :
<tab>node ./test/testrunner.js
.PHONY: install test
If I run make test, I get a 'ReferenceError: myObj is not defined' error. The source file does run, because it can throw errors. It just doesn't seem to be included in the global scope as it should. It doesn't work if I do it from the command line, as per the instructions in the node-qunit readme. Anyone have any idea how to get this working?
You're not exporting anything. Behind the scenes, node-qunit is using require to load the specified modules. To expose variables when a module is required, you have to add them to the exports object (or assign your own object to the exports variable)
(There's also a syntax error - ; in the object literal)
This works for me:
./source/myscript.js:
exports.myObj = {
a: true
}
./test/tests.js:
QUnit.module('tests')
test("that a is true", function () {
ok(myObj.a)
})
./test/runner.js:
var runner = require('qunit')
runner.run({
code : './source/myscript.js'
, tests : './test/tests.js'
})

Categories