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

Compile the gatsby-module-loader with babel to fix IE #1891

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions packages/gatsby/src/loaders/gatsby-module-loader/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,35 @@
"use strict";

/*
Based on Tobias Koppers @sokra bundle-loader
https://github.com/webpack/bundle-loader

and Arthur Stolyar's async-module-loader
*/
const loaderUtils = require(`loader-utils`)
const path = require(`path`)
var loaderUtils = require("loader-utils");
var path = require("path");

module.exports = function() {}
module.exports.pitch = function(remainingRequest) {
this.cacheable && this.cacheable()
module.exports = function () {};
module.exports.pitch = function (remainingRequest) {
this.cacheable && this.cacheable();

const query = loaderUtils.parseQuery(this.query)
let chunkName = ``
var query = loaderUtils.parseQuery(this.query);
var chunkName = "";

if (query.name) {
chunkName = loaderUtils.interpolateName(this, query.name, {
context: query.context,
regExp: query.regExp,
})
chunkName = `, ${JSON.stringify(chunkName)}`
regExp: query.regExp });
chunkName = ", " + JSON.stringify(chunkName);
}

const request = loaderUtils.stringifyRequest(this, `!!` + remainingRequest)

const callback = `function() { return require(` + request + `) }`

const executor = `
return require.ensure([], function(_, error) {
if (error) {
console.log('bundle loading error', error)
cb(true)
} else {
cb(null, ${callback})
}
}${chunkName});
`

const result = `
require(
${loaderUtils.stringifyRequest(
this,
`!${path.join(__dirname, `patch.js`)}`
)}
);
module.exports = function(cb) { ${executor} }
`

return result
}
var request = loaderUtils.stringifyRequest(this, "!!" + remainingRequest);

var callback = "function() { return require(" + request + ") }";

var executor = "\n return require.ensure([], function(_, error) {\n if (error) {\n console.log('bundle loading error', error)\n cb(true)\n } else {\n cb(null, " + callback + ")\n }\n }" + chunkName + ");\n ";

var result = "\n require(\n " + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "patch.js")) + "\n );\n module.exports = function(cb) { " + executor + " }\n ";

return result;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "_Based on https://github.com/webpack/bundle-loader and https://github.com/NekR/async-module-loader_",
"main": "index.js",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
Expand Down
90 changes: 46 additions & 44 deletions packages/gatsby/src/loaders/gatsby-module-loader/patch.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,82 @@
patch()
"use strict";

patch();

function patch() {
var head = document.querySelector(`head`)
var ensure = __webpack_require__.e
var chunks = __webpack_require__.s
var failures
var head = document.querySelector("head");
var ensure = __webpack_require__.e;
var chunks = __webpack_require__.s;
var failures;

__webpack_require__.e = function(chunkId, callback) {
var loaded = false
var immediate = true
__webpack_require__.e = function (chunkId, callback) {
var loaded = false;
var immediate = true;

var handler = function(error) {
if (!callback) return
var handler = function (error) {
if (!callback) return;

callback(__webpack_require__, error)
callback = null
}
callback(__webpack_require__, error);
callback = null;
};

if (!chunks && failures && failures[chunkId]) {
handler(true)
return
handler(true);
return;
}

ensure(chunkId, function() {
if (loaded) return
loaded = true
ensure(chunkId, function () {
if (loaded) return;
loaded = true;

if (immediate) {
// webpack fires callback immediately if chunk was already loaded
// IE also fires callback immediately if script was already
// in a cache (AppCache counts too)
setTimeout(function() {
handler()
})
setTimeout(function () {
handler();
});
} else {
handler()
handler();
}
})
});

// This is |true| if chunk is already loaded and does not need onError call.
// This happens because in such case ensure() is performed in sync way
if (loaded) {
return
return;
}

immediate = false
immediate = false;

onError(function() {
if (loaded) return
loaded = true
onError(function () {
if (loaded) return;
loaded = true;

if (chunks) {
chunks[chunkId] = void 0
chunks[chunkId] = void 0;
} else {
failures || (failures = {})
failures[chunkId] = true
failures || (failures = {});
failures[chunkId] = true;
}

handler(true)
})
}
handler(true);
});
};

function onError(callback) {
var script = head.lastChild
var script = head.lastChild;

if (script.tagName !== `SCRIPT`) {
if (typeof console !== `undefined` && console.warn) {
console.warn(`Script is not a script`, script)
if (script.tagName !== "SCRIPT") {
if (typeof console !== "undefined" && console.warn) {
console.warn("Script is not a script", script);
}

return
return;
}

script.onload = script.onerror = function() {
script.onload = script.onerror = null
setTimeout(callback, 0)
}
script.onload = script.onerror = function () {
script.onload = script.onerror = null;
setTimeout(callback, 0);
};
}
}
}
34 changes: 16 additions & 18 deletions packages/gatsby/src/loaders/gatsby-module-loader/plugin.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
module.exports = function() {}
module.exports.prototype.apply = function(compiler) {
compiler.plugin(`compilation`, function(compilation) {
compilation.mainTemplate.plugin(`require-extensions`, function(
source,
chunk,
hash
) {
"use strict";

module.exports = function () {};
module.exports.prototype.apply = function (compiler) {
compiler.plugin("compilation", function (compilation) {
compilation.mainTemplate.plugin("require-extensions", function (source, chunk, hash) {
if (chunk.chunks.length > 0) {
var buf = []
var buf = [];

buf.push(``)
buf.push(``)
buf.push(`// expose the chunks object`)
buf.push(this.requireFn + `.s = installedChunks;`)
buf.push("");
buf.push("");
buf.push("// expose the chunks object");
buf.push(this.requireFn + ".s = installedChunks;");

return source + this.asString(buf)
return source + this.asString(buf);
}

return source
})
})
}
return source;
});
});
};
51 changes: 51 additions & 0 deletions packages/gatsby/src/loaders/gatsby-module-loader/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Based on Tobias Koppers @sokra bundle-loader
https://github.com/webpack/bundle-loader

and Arthur Stolyar's async-module-loader
*/
const loaderUtils = require(`loader-utils`)
const path = require(`path`)

module.exports = function() {}
module.exports.pitch = function(remainingRequest) {
this.cacheable && this.cacheable()

const query = loaderUtils.parseQuery(this.query)
let chunkName = ``

if (query.name) {
chunkName = loaderUtils.interpolateName(this, query.name, {
context: query.context,
regExp: query.regExp,
})
chunkName = `, ${JSON.stringify(chunkName)}`
}

const request = loaderUtils.stringifyRequest(this, `!!` + remainingRequest)

const callback = `function() { return require(` + request + `) }`

const executor = `
return require.ensure([], function(_, error) {
if (error) {
console.log('bundle loading error', error)
cb(true)
} else {
cb(null, ${callback})
}
}${chunkName});
`

const result = `
require(
${loaderUtils.stringifyRequest(
this,
`!${path.join(__dirname, `patch.js`)}`
)}
);
module.exports = function(cb) { ${executor} }
`

return result
}
80 changes: 80 additions & 0 deletions packages/gatsby/src/loaders/gatsby-module-loader/src/patch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
patch()

function patch() {
var head = document.querySelector(`head`)
var ensure = __webpack_require__.e
var chunks = __webpack_require__.s
var failures

__webpack_require__.e = function(chunkId, callback) {
var loaded = false
var immediate = true

var handler = function(error) {
if (!callback) return

callback(__webpack_require__, error)
callback = null
}

if (!chunks && failures && failures[chunkId]) {
handler(true)
return
}

ensure(chunkId, function() {
if (loaded) return
loaded = true

if (immediate) {
// webpack fires callback immediately if chunk was already loaded
// IE also fires callback immediately if script was already
// in a cache (AppCache counts too)
setTimeout(function() {
handler()
})
} else {
handler()
}
})

// This is |true| if chunk is already loaded and does not need onError call.
// This happens because in such case ensure() is performed in sync way
if (loaded) {
return
}

immediate = false

onError(function() {
if (loaded) return
loaded = true

if (chunks) {
chunks[chunkId] = void 0
} else {
failures || (failures = {})
failures[chunkId] = true
}

handler(true)
})
}

function onError(callback) {
var script = head.lastChild

if (script.tagName !== `SCRIPT`) {
if (typeof console !== `undefined` && console.warn) {
console.warn(`Script is not a script`, script)
}

return
}

script.onload = script.onerror = function() {
script.onload = script.onerror = null
setTimeout(callback, 0)
}
}
}
Loading