doSlide
doSlide copied to clipboard
need webpack-garbage clean browser version
Not anyone uses webpack. Besides I load third-party modules via ajax (which needs assigning to window object as property)
So every time I come accross a nice lib I get frustrated to change the code, removing AMD UMD CJS and so forth wrappers.
Nightmare, and what is it in aid of? Trend?
So would be nice to have a clean source code, and then I would decide for myself, what environment will be the code applied in.
Thanks for your work,
Do you mean that you get the code via AJAX then use eval
to execute it? So why don't you try this:
function applyCodeInContext (code, context) {
return (function () { return eval(code) }).call(context)
}
applyCodeInContext('this.a', { a: 1 })
@MopTym I managed to pass jshint like so: https://github.com/englishextra/englishextra.github.io/blob/master/cdn/doSlide/1.1.4/js/do-slide.fixed.min.js as for eval and Function substitute, yes I do it in similar mannar as you suggested:
/*!
* Load and execute JS via AJAX
* gist.github.com/englishextra/8dc9fe7b6ff8bdf5f9b483bf772b9e1c
* IE 5.5+, Firefox, Opera, Chrome, Safari XHR object
* gist.github.com/Xeoncross/7663273
* modified callback(x.responseText,x); to callback(eval(x.responseText),x);
* stackoverflow.com/questions/3728798/running-javascript-downloaded-with-xmlhttprequest
* @param {String} u path string
* @param {Object} [cb] callback function
* @param {Object} [e] on error callback function
* ajaxLoadTriggerJS(u,cb,e)
*/
var ajaxLoadTriggerJS = function (u, cb, e) {
var w = window,
x = w.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
x.overrideMimeType("application/javascript;charset=utf-8");
x.open("GET", u, !0);
x.onreadystatechange = function () {
if (x.status == "404") {
if (e && "function" === typeof e) {
e();
}
console.log("Error XMLHttpRequest-ing file", x.status);
return !1;
} else if (x.readyState == 4 && x.status == 200 && x.responseText) {
try {
var Fn = Function,
new Fn(x.responseText.toString()).call(this);
} catch (m) {
throw new Error("Error evaluating file. " + m);
}
if (cb && "function" === typeof cb) {
cb(x.responseText);
}
}
};
x.send(null);
};
In the code:
x.onreadystatechange = function () {
...
new Fn(x.responseText.toString()).call(this);
...
}
this
is always equal to x
. I guess this is the problem. Try to change it to:
new Fn(x.responseText.toString()).call(<your context object>);
Then in the file: https://github.com/englishextra/englishextra.github.io/blob/master/cdn/doSlide/1.1.4/js/do-slide.fixed.min.js
;(function webpackUniversalModuleDefinition(root,factory){window.DoSlide=factory();})(window,function(){...})
should be:
;(function webpackUniversalModuleDefinition(root,factory){root.DoSlide=factory();})(this,function(){...})
just like the original code.
Oh, I guess I stated my problem wrong. To get the code gotten via ajax I need to change global to window - that's what I always do with masonry packery do-slide and others that have module wrappers. So to me its not a problem. But what worries me when I come accross the library with lots of module compatibility code which makes the code source grow about 10%. So There's no problem - there is a wish to have a clean source. See like the collegue does here:
a clean source with no module/webpack stuff: https://github.com/tristen/tablesort/blob/gh-pages/src/tablesort.js
This is what I'm asking about. And thanks for the eval suggestions.
UPD Oh yeah
should be:
.call("undefined"!==typeof window?window:this)
for instance, thanks
BTW I reuse JS code in Electron environment and js was acquired in regular mannar with script tag, and libs with module wrappers wouldnt work in Electron window. Yeah because masonry and packery and imagesloaded are intended to be required, which I dont want to. Babel also likes to change this to undefined when the first is an argument. So I put two and two together thinking why should I?
I suggest that you use do-slide.min.js instead of do-slide.js, there is no unnecessary webpack code in the compressed file. DoSlide's original source code is in the src
folder. I need webpack and Babel to convert ES6 to ES5, which is different from tablesort.
https://github.com/MopTym/doSlide/blob/dev/dist/do-slide.min.js wont pass jshint so it cant be embeded in a bundle that is going to be jshinted.
I need webpack and Babel to convert ES6 to ES5
I see.
Thanks anyways. Will manage.
To disable JSHint, add /* jshint ignore:start */
to the top of do-slide.min.js
: ) .
Yes, I know that, just chose unminified version which has less warnings to fix. No problem.