-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gatsby-module-loader to replace bundle-loader to add ability to d…
…elay executing scripts after loading
- Loading branch information
1 parent
0c8abd7
commit 69f60ae
Showing
11 changed files
with
212 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# `gatsby-module-loader` for webpack | ||
|
||
_Based on https://github.com/webpack/bundle-loader and https://github.com/NekR/async-module-loader_ | ||
|
||
Is lazy and chunk execution is always deferred + there's good error handling. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
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`) | ||
console.log("in gatsby-module-loader") | ||
|
||
module.exports = function() {} | ||
module.exports.pitch = function(remainingRequest) { | ||
this.cacheable && this.cacheable() | ||
|
||
console.log("this.query", this.query) | ||
const query = loaderUtils.parseQuery(this.query) | ||
console.log("query", query) | ||
let chunkName = `` | ||
|
||
if (query.name) { | ||
chunkName = loaderUtils.interpolateName(this, query.name, { | ||
context: query.context, | ||
regExp: query.regExp, | ||
}) | ||
chunkName = `, ${JSON.stringify(chunkName)}` | ||
} | ||
|
||
console.log(`chunkName`, chunkName) | ||
|
||
const request = loaderUtils.stringifyRequest(this, `!!` + remainingRequest) | ||
|
||
const callback = "callback(function() { return require(" + request + ") })" | ||
|
||
const executor = ` | ||
return function(callback, errback) { | ||
require.ensure([], function(_, error) { | ||
if (error) { | ||
errback() | ||
} else { | ||
${callback} | ||
} | ||
}${chunkName}); | ||
}` | ||
|
||
const result = ` | ||
require( | ||
${loaderUtils.stringifyRequest( | ||
this, | ||
`!${path.join(__dirname, `patch.js`)}` | ||
)} | ||
); | ||
module.exports = function() { ${executor} } | ||
` | ||
|
||
console.log(`result`, result) | ||
|
||
return result | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/gatsby/src/loaders/gatsby-module-loader/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "gatsby-module-loader", | ||
"version": "1.0.0", | ||
"description": "_Based on https://github.com/webpack/bundle-loader and https://github.com/NekR/async-module-loader_", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}; | ||
}; |
23 changes: 23 additions & 0 deletions
23
packages/gatsby/src/loaders/gatsby-module-loader/plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters