-
-
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.
- Loading branch information
Showing
25 changed files
with
857 additions
and
9 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
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,20 @@ | ||
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,29 @@ | ||
/* global __resourceQuery */ | ||
|
||
"use strict"; | ||
|
||
if (!module.hot) { | ||
throw new Error( | ||
"Environment doesn't support lazy compilation (requires Hot Module Replacement enabled)" | ||
); | ||
} | ||
|
||
var urlBase = decodeURIComponent(__resourceQuery.slice(1)); | ||
exports.keepAlive = function (key) { | ||
var response; | ||
require("http") | ||
.request( | ||
urlBase + key, | ||
{ | ||
agent: false, | ||
headers: { accept: "text/event-stream" } | ||
}, | ||
function (res) { | ||
response = res; | ||
} | ||
) | ||
.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,40 @@ | ||
/* global __resourceQuery */ | ||
|
||
"use strict"; | ||
|
||
if (typeof EventSource !== "function" || !module.hot) { | ||
throw new Error( | ||
"Environment doesn't support lazy compilation (requires EventSource and Hot Module Replacement enabled)" | ||
); | ||
} | ||
|
||
var urlBase = decodeURIComponent(__resourceQuery.slice(1)); | ||
var activeEventSource; | ||
var activeKeys = new Map(); | ||
|
||
var updateEventSource = function updateEventSource() { | ||
if (activeEventSource) activeEventSource.close(); | ||
activeEventSource = new EventSource( | ||
urlBase + Array.from(activeKeys.keys()).join("@") | ||
); | ||
}; | ||
|
||
exports.keepAlive = function (key) { | ||
var value = activeKeys.get(key) || 0; | ||
activeKeys.set(key, value + 1); | ||
if (value === 0) { | ||
updateEventSource(); | ||
} | ||
|
||
return function () { | ||
setTimeout(function () { | ||
var value = activeKeys.get(key); | ||
if (value === 1) { | ||
activeKeys.delete(key); | ||
updateEventSource(); | ||
} else { | ||
activeKeys.set(key, 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
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.