-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12469 from webpack/feature/async-compilation
- Loading branch information
Showing
26 changed files
with
965 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
To run this example you need to install `webpack-dev-server` and run `webpack serve`. | ||
|
||
# example.js | ||
|
||
```javascript | ||
const libraries = { | ||
react: () => import("react"), | ||
acorn: () => import("acorn"), | ||
"core-js": () => import("core-js"), | ||
lodash: () => import("lodash"), | ||
xxhashjs: () => import("xxhashjs"), | ||
"all of them": () => import("./all") | ||
}; | ||
|
||
window.onload = () => { | ||
document.body.style = "font-size: 16pt;"; | ||
const pre = document.createElement("pre"); | ||
pre.style = "height: 200px; overflow-y: auto"; | ||
pre.innerText = | ||
"Click on a button to load the library with import(). The first click triggers a lazy compilation of the module."; | ||
for (const key of Object.keys(libraries)) { | ||
const button = document.createElement("button"); | ||
const loadFn = libraries[key]; | ||
button.innerText = key; | ||
button.onclick = async () => { | ||
pre.innerText = "Loading " + key + "..."; | ||
const result = await loadFn(); | ||
pre.innerText = `${key} = {\n ${Object.keys(result).join(",\n ")}\n}`; | ||
}; | ||
document.body.appendChild(button); | ||
} | ||
const button = document.createElement("button"); | ||
button.innerText = "Load more..."; | ||
button.onclick = async () => { | ||
pre.innerText = "Loading more..."; | ||
await import("./more"); | ||
pre.innerText = "More libraries available."; | ||
}; | ||
document.body.appendChild(button); | ||
document.body.appendChild(pre); | ||
}; | ||
``` | ||
|
||
# webpack.config.js | ||
|
||
```javascript | ||
const { HotModuleReplacementPlugin } = require("../../"); | ||
|
||
module.exports = { | ||
mode: "development", | ||
entry: { | ||
main: "./example.js" | ||
}, | ||
cache: { | ||
type: "filesystem", | ||
idleTimeout: 5000 | ||
}, | ||
experiments: { | ||
lazyCompilation: true | ||
}, | ||
devServer: { | ||
hot: true, | ||
publicPath: "/dist/" | ||
}, | ||
plugins: [new HotModuleReplacementPlugin()] | ||
}; | ||
``` |
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,8 @@ | ||
export * from "react"; | ||
export * from "react-dom"; | ||
export * from "acorn"; | ||
export * from "core-js"; | ||
export * from "date-fns"; | ||
export * from "lodash"; | ||
export * from "lodash-es"; | ||
export * from "xxhashjs"; |
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 @@ | ||
require("../build-common"); |
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,36 @@ | ||
const libraries = { | ||
react: () => import("react"), | ||
acorn: () => import("acorn"), | ||
"core-js": () => import("core-js"), | ||
lodash: () => import("lodash"), | ||
xxhashjs: () => import("xxhashjs"), | ||
"all of them": () => import("./all") | ||
}; | ||
|
||
window.onload = () => { | ||
document.body.style = "font-size: 16pt;"; | ||
const pre = document.createElement("pre"); | ||
pre.style = "height: 200px; overflow-y: auto"; | ||
pre.innerText = | ||
"Click on a button to load the library with import(). The first click triggers a lazy compilation of the module."; | ||
for (const key of Object.keys(libraries)) { | ||
const button = document.createElement("button"); | ||
const loadFn = libraries[key]; | ||
button.innerText = key; | ||
button.onclick = async () => { | ||
pre.innerText = "Loading " + key + "..."; | ||
const result = await loadFn(); | ||
pre.innerText = `${key} = {\n ${Object.keys(result).join(",\n ")}\n}`; | ||
}; | ||
document.body.appendChild(button); | ||
} | ||
const button = document.createElement("button"); | ||
button.innerText = "Load more..."; | ||
button.onclick = async () => { | ||
pre.innerText = "Loading more..."; | ||
await import("./more"); | ||
pre.innerText = "More libraries available."; | ||
}; | ||
document.body.appendChild(button); | ||
document.body.appendChild(pre); | ||
}; |
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,6 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="dist/main.js"></script> | ||
</head> | ||
</html> |
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,21 @@ | ||
const libraries = { | ||
"react-dom": () => import("react-dom"), | ||
"date-fns": () => import("date-fns"), | ||
xxhashjs: () => import("xxhashjs"), | ||
"lodash-es": () => import("lodash-es") | ||
}; | ||
|
||
const pre = document.querySelector("pre"); | ||
for (const key of Object.keys(libraries)) { | ||
const button = document.createElement("button"); | ||
const loadFn = libraries[key]; | ||
button.innerText = key; | ||
button.onclick = async () => { | ||
pre.innerText = "Loading " + key + "..."; | ||
const result = await loadFn(); | ||
pre.innerText = `${key} = {\n ${Object.keys(result).join(",\n ")}\n}`; | ||
}; | ||
document.body.appendChild(button); | ||
} | ||
|
||
export {}; |
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,13 @@ | ||
To run this example you need to install `webpack-dev-server` and run `webpack serve`. | ||
|
||
# example.js | ||
|
||
```javascript | ||
_{{example.js}}_ | ||
``` | ||
|
||
# webpack.config.js | ||
|
||
```javascript | ||
_{{webpack.config.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,22 @@ | ||
const { HotModuleReplacementPlugin } = require("../../"); | ||
|
||
module.exports = { | ||
mode: "development", | ||
entry: { | ||
main: "./example.js" | ||
}, | ||
cache: { | ||
type: "filesystem", | ||
idleTimeout: 5000 | ||
}, | ||
experiments: { | ||
lazyCompilation: { | ||
entries: false | ||
} | ||
}, | ||
devServer: { | ||
hot: true, | ||
publicPath: "/dist/" | ||
}, | ||
plugins: [new HotModuleReplacementPlugin()] | ||
}; |
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,38 @@ | ||
/* global __resourceQuery */ | ||
|
||
"use strict"; | ||
|
||
var urlBase = decodeURIComponent(__resourceQuery.slice(1)); | ||
exports.keepAlive = function (options) { | ||
var data = options.data; | ||
var onError = options.onError; | ||
var active = options.active; | ||
var module = options.module; | ||
var response; | ||
var request = require("http").request( | ||
urlBase + data, | ||
{ | ||
agent: false, | ||
headers: { accept: "text/event-stream" } | ||
}, | ||
function (res) { | ||
response = res; | ||
response.on("error", errorHandler); | ||
if (!active && !module.hot) { | ||
console.log( | ||
"Hot Module Replacement is not enabled. Waiting for process restart..." | ||
); | ||
} | ||
} | ||
); | ||
function errorHandler(err) { | ||
err.message = | ||
"Problem communicating active modules to the server: " + err.message; | ||
onError(err); | ||
} | ||
request.on("error", errorHandler); | ||
request.end(); | ||
return function () { | ||
response.destroy(); | ||
}; | ||
}; |
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,74 @@ | ||
/* global __resourceQuery */ | ||
|
||
"use strict"; | ||
|
||
if (typeof EventSource !== "function") { | ||
throw new Error( | ||
"Environment doesn't support lazy compilation (requires EventSource)" | ||
); | ||
} | ||
|
||
var urlBase = decodeURIComponent(__resourceQuery.slice(1)); | ||
var activeEventSource; | ||
var activeKeys = new Map(); | ||
var errorHandlers = new Set(); | ||
|
||
var updateEventSource = function updateEventSource() { | ||
if (activeEventSource) activeEventSource.close(); | ||
if (activeKeys.size) { | ||
activeEventSource = new EventSource( | ||
urlBase + Array.from(activeKeys.keys()).join("@") | ||
); | ||
activeEventSource.onerror = function (event) { | ||
errorHandlers.forEach(function (onError) { | ||
onError( | ||
new Error( | ||
"Problem communicating active modules to the server: " + | ||
event.message + | ||
" " + | ||
event.filename + | ||
":" + | ||
event.lineno + | ||
":" + | ||
event.colno + | ||
" " + | ||
event.error | ||
) | ||
); | ||
}); | ||
}; | ||
} else { | ||
activeEventSource = undefined; | ||
} | ||
}; | ||
|
||
exports.keepAlive = function (options) { | ||
var data = options.data; | ||
var onError = options.onError; | ||
var active = options.active; | ||
var module = options.module; | ||
errorHandlers.add(onError); | ||
var value = activeKeys.get(data) || 0; | ||
activeKeys.set(data, value + 1); | ||
if (value === 0) { | ||
updateEventSource(); | ||
} | ||
if (!active && !module.hot) { | ||
console.log( | ||
"Hot Module Replacement is not enabled. Waiting for process restart..." | ||
); | ||
} | ||
|
||
return function () { | ||
errorHandlers.delete(onError); | ||
setTimeout(function () { | ||
var value = activeKeys.get(data); | ||
if (value === 1) { | ||
activeKeys.delete(data); | ||
updateEventSource(); | ||
} else { | ||
activeKeys.set(data, value - 1); | ||
} | ||
}, 1000); | ||
}; | ||
}; |
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
Oops, something went wrong.