diff --git a/packages/gatsby/src/loaders/gatsby-module-loader/index.js b/packages/gatsby/src/loaders/gatsby-module-loader/index.js index 8822aad1490f7..9b7fd9ef221a7 100644 --- a/packages/gatsby/src/loaders/gatsby-module-loader/index.js +++ b/packages/gatsby/src/loaders/gatsby-module-loader/index.js @@ -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; +}; \ No newline at end of file diff --git a/packages/gatsby/src/loaders/gatsby-module-loader/package.json b/packages/gatsby/src/loaders/gatsby-module-loader/package.json index 31c0e1ff848b0..8cc92112ddd3e 100644 --- a/packages/gatsby/src/loaders/gatsby-module-loader/package.json +++ b/packages/gatsby/src/loaders/gatsby-module-loader/package.json @@ -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": "", diff --git a/packages/gatsby/src/loaders/gatsby-module-loader/patch.js b/packages/gatsby/src/loaders/gatsby-module-loader/patch.js index d4383d2e52ab2..c3c98d212b401 100644 --- a/packages/gatsby/src/loaders/gatsby-module-loader/patch.js +++ b/packages/gatsby/src/loaders/gatsby-module-loader/patch.js @@ -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); + }; } -} +} \ No newline at end of file diff --git a/packages/gatsby/src/loaders/gatsby-module-loader/plugin.js b/packages/gatsby/src/loaders/gatsby-module-loader/plugin.js index c0b36a37aedfb..ac85f03481563 100644 --- a/packages/gatsby/src/loaders/gatsby-module-loader/plugin.js +++ b/packages/gatsby/src/loaders/gatsby-module-loader/plugin.js @@ -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; + }); + }); +}; \ No newline at end of file diff --git a/packages/gatsby/src/loaders/gatsby-module-loader/src/index.js b/packages/gatsby/src/loaders/gatsby-module-loader/src/index.js new file mode 100644 index 0000000000000..8822aad1490f7 --- /dev/null +++ b/packages/gatsby/src/loaders/gatsby-module-loader/src/index.js @@ -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 +} diff --git a/packages/gatsby/src/loaders/gatsby-module-loader/src/patch.js b/packages/gatsby/src/loaders/gatsby-module-loader/src/patch.js new file mode 100644 index 0000000000000..d4383d2e52ab2 --- /dev/null +++ b/packages/gatsby/src/loaders/gatsby-module-loader/src/patch.js @@ -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) + } + } +} diff --git a/packages/gatsby/src/loaders/gatsby-module-loader/src/plugin.js b/packages/gatsby/src/loaders/gatsby-module-loader/src/plugin.js new file mode 100644 index 0000000000000..c0b36a37aedfb --- /dev/null +++ b/packages/gatsby/src/loaders/gatsby-module-loader/src/plugin.js @@ -0,0 +1,23 @@ +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 = [] + + buf.push(``) + buf.push(``) + buf.push(`// expose the chunks object`) + buf.push(this.requireFn + `.s = installedChunks;`) + + return source + this.asString(buf) + } + + return source + }) + }) +}