Skip to content

Commit

Permalink
Compile the gatsby-module-loader with babel
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Farrell committed Aug 23, 2017
1 parent d0b382c commit c414e02
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 99 deletions.
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

0 comments on commit c414e02

Please sign in to comment.