mirror of https://github.com/webpack/webpack.git
Compare commits
3 Commits
a15432a60b
...
8f29ba80eb
| Author | SHA1 | Date |
|---|---|---|
|
|
8f29ba80eb | |
|
|
e1afcd4cc2 | |
|
|
fad1bc1f32 |
|
|
@ -376,7 +376,9 @@ class LazyCompilationPlugin {
|
||||||
apply(compiler) {
|
apply(compiler) {
|
||||||
/** @type {BackendApi} */
|
/** @type {BackendApi} */
|
||||||
let backend;
|
let backend;
|
||||||
compiler.hooks.beforeCompile.tapAsync(PLUGIN_NAME, (params, callback) => {
|
compiler.hooks.beforeCompile.tapAsync(
|
||||||
|
PLUGIN_NAME,
|
||||||
|
(/** @type {any} */ params, /** @type {(err?: Error | null) => void} */ callback) => {
|
||||||
if (backend !== undefined) return callback();
|
if (backend !== undefined) return callback();
|
||||||
const promise = this.backend(compiler, (err, result) => {
|
const promise = this.backend(compiler, (err, result) => {
|
||||||
if (err) return callback(err);
|
if (err) return callback(err);
|
||||||
|
|
@ -389,15 +391,25 @@ class LazyCompilationPlugin {
|
||||||
callback();
|
callback();
|
||||||
}, callback);
|
}, callback);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
);
|
||||||
compiler.hooks.thisCompilation.tap(
|
compiler.hooks.thisCompilation.tap(
|
||||||
PLUGIN_NAME,
|
PLUGIN_NAME,
|
||||||
|
/**
|
||||||
|
* @param {import("../Compilation")} compilation
|
||||||
|
* @param {{ normalModuleFactory: import("../NormalModuleFactory") }} param1
|
||||||
|
*/
|
||||||
(compilation, { normalModuleFactory }) => {
|
(compilation, { normalModuleFactory }) => {
|
||||||
normalModuleFactory.hooks.module.tap(
|
normalModuleFactory.hooks.module.tap(
|
||||||
PLUGIN_NAME,
|
PLUGIN_NAME,
|
||||||
|
/**
|
||||||
|
* @param {Module} module
|
||||||
|
* @param {*} createData
|
||||||
|
* @param {*} resolveData
|
||||||
|
*/
|
||||||
(module, createData, resolveData) => {
|
(module, createData, resolveData) => {
|
||||||
if (
|
if (
|
||||||
resolveData.dependencies.every((dep) =>
|
resolveData.dependencies.every((dep: any) =>
|
||||||
HMR_DEPENDENCY_TYPES.has(dep.type)
|
HMR_DEPENDENCY_TYPES.has(dep.type)
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
|
|
@ -457,7 +469,7 @@ class LazyCompilationPlugin {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
compiler.hooks.shutdown.tapAsync(PLUGIN_NAME, (callback) => {
|
compiler.hooks.shutdown.tapAsync(PLUGIN_NAME, (callback: (...args: any[]) => void) => {
|
||||||
backend.dispose(callback);
|
backend.dispose(callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import * as style from "./style.css";
|
||||||
import * as text1 from "./text-with-bom.txt";
|
import * as text1 from "./text-with-bom.txt";
|
||||||
import * as text2 from "./test-without-bom.text";
|
import * as text2 from "./test-without-bom.text";
|
||||||
|
|
||||||
it("should remove BOM", function() {
|
it("should remove BOM", async function() {
|
||||||
const url = new URL("./resource-with-bom.ext", import.meta.url);
|
const url = new URL("./resource-with-bom.ext", import.meta.url);
|
||||||
|
|
||||||
expect(mod).toBeDefined();
|
expect(mod).toBeDefined();
|
||||||
|
|
@ -13,7 +13,7 @@ it("should remove BOM", function() {
|
||||||
expect(url).toBeDefined();
|
expect(url).toBeDefined();
|
||||||
|
|
||||||
const module = "module.js"
|
const module = "module.js"
|
||||||
const modules = import("./dir/" + module);
|
const modules = await import("./dir/" + module);
|
||||||
|
|
||||||
expect(modules).toBeDefined();
|
expect(modules).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@ const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
findBundle() {
|
||||||
|
return ["dir_module_js.bundle0.js", "bundle0.js"];
|
||||||
|
},
|
||||||
afterExecute(options) {
|
afterExecute(options) {
|
||||||
const outputPath = options.output.path;
|
const outputPath = options.output.path;
|
||||||
const files = fs.readdirSync(outputPath);
|
const files = fs.readdirSync(outputPath);
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@ module.exports = {
|
||||||
output: {
|
output: {
|
||||||
assetModuleFilename: "[name][ext]"
|
assetModuleFilename: "[name][ext]"
|
||||||
},
|
},
|
||||||
|
optimization: {
|
||||||
|
chunkIds: "named"
|
||||||
|
},
|
||||||
module: {
|
module: {
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,39 @@
|
||||||
it("should set fetchPriority", () => {
|
function abortable(fn) {
|
||||||
import(/* webpackFetchPriority: "high" */ "./a");
|
return new Promise((resolve) => {
|
||||||
|
const timeoutId = setTimeout(() => {
|
||||||
|
fn = undefined;
|
||||||
|
resolve('Promise resolved after delay');
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
return fn();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
it("should set fetchPriority", async () => {
|
||||||
|
abortable(() => import(/* webpackFetchPriority: "high" */ "./a"));
|
||||||
expect(document.head._children).toHaveLength(4);
|
expect(document.head._children).toHaveLength(4);
|
||||||
const script1 = document.head._children[2];
|
const script1 = document.head._children[2];
|
||||||
expect(script1._attributes.fetchpriority).toBe("high");
|
expect(script1._attributes.fetchpriority).toBe("high");
|
||||||
|
|
||||||
import(/* webpackFetchPriority: "low" */ "./b");
|
abortable(() => import(/* webpackFetchPriority: "low" */ "./b"));
|
||||||
expect(document.head._children).toHaveLength(5);
|
expect(document.head._children).toHaveLength(5);
|
||||||
const script2 = document.head._children[4];
|
const script2 = document.head._children[4];
|
||||||
expect(script2._attributes.fetchpriority).toBe("low");
|
expect(script2._attributes.fetchpriority).toBe("low");
|
||||||
|
|
||||||
import(/* webpackFetchPriority: "low" */ "./c");
|
abortable(() => import(/* webpackFetchPriority: "low" */ "./c"));
|
||||||
expect(document.head._children).toHaveLength(6);
|
expect(document.head._children).toHaveLength(6);
|
||||||
const script3 = document.head._children[5];
|
const script3 = document.head._children[5];
|
||||||
expect(script3._attributes.fetchpriority).toBe("low");
|
expect(script3._attributes.fetchpriority).toBe("low");
|
||||||
|
|
||||||
import(/* webpackPrefetch: 20, webpackFetchPriority: "auto" */ "./c");
|
abortable(() => import(/* webpackPrefetch: 20, webpackFetchPriority: "auto" */ "./c"));
|
||||||
|
|
||||||
import("./d")
|
abortable(() => import("./d"))
|
||||||
expect(document.head._children).toHaveLength(7);
|
expect(document.head._children).toHaveLength(7);
|
||||||
const script4 = document.head._children[6];
|
const script4 = document.head._children[6];
|
||||||
expect(script4._attributes.fetchpriority).toBeUndefined();
|
expect(script4._attributes.fetchpriority).toBeUndefined();
|
||||||
|
|
||||||
import(/* webpackPrefetch: -20 */ "./d3");
|
abortable(() => import(/* webpackPrefetch: -20 */ "./d3"));
|
||||||
expect(document.head._children).toHaveLength(8);
|
expect(document.head._children).toHaveLength(8);
|
||||||
const script5 = document.head._children[7];
|
const script5 = document.head._children[7];
|
||||||
expect(script5._attributes.fetchpriority).toBeUndefined();
|
expect(script5._attributes.fetchpriority).toBeUndefined();
|
||||||
|
|
@ -29,12 +41,12 @@ it("should set fetchPriority", () => {
|
||||||
const condition = true;
|
const condition = true;
|
||||||
|
|
||||||
if (!condition) {
|
if (!condition) {
|
||||||
import(/* webpackFetchPriority: "high", webpackChunkName: "one" */ "./e");
|
abortable( () => import(/* webpackFetchPriority: "high", webpackChunkName: "one" */ "./e"));
|
||||||
expect(document.head._children).toHaveLength(9);
|
expect(document.head._children).toHaveLength(9);
|
||||||
const script6 = document.head._children[8];
|
const script6 = document.head._children[8];
|
||||||
expect(script6._attributes.fetchpriority).toBe("high");
|
expect(script6._attributes.fetchpriority).toBe("high");
|
||||||
} else {
|
} else {
|
||||||
import(/* webpackFetchPriority: "low", webpackChunkName: "two" */ "./e");
|
abortable(() => import(/* webpackFetchPriority: "low", webpackChunkName: "two" */ "./e"));
|
||||||
expect(document.head._children).toHaveLength(9);
|
expect(document.head._children).toHaveLength(9);
|
||||||
const script6 = document.head._children[8];
|
const script6 = document.head._children[8];
|
||||||
expect(script6._attributes.fetchpriority).toBe("low");
|
expect(script6._attributes.fetchpriority).toBe("low");
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,25 @@
|
||||||
|
function abortable(fn) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const timeoutId = setTimeout(() => {
|
||||||
|
fn = undefined;
|
||||||
|
resolve('Promise resolved after delay');
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
return fn();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
it("should set fetchPriority", () => {
|
it("should set fetchPriority", () => {
|
||||||
// Single Chunk
|
// Single Chunk
|
||||||
import(/* webpackFetchPriority: "high" */ "./a");
|
abortable(() => import(/* webpackFetchPriority: "high" */ "./a"));
|
||||||
expect(document.head._children).toHaveLength(1);
|
expect(document.head._children).toHaveLength(1);
|
||||||
const script1 = document.head._children[0];
|
const script1 = document.head._children[0];
|
||||||
expect(script1._attributes.fetchpriority).toBe("high");
|
expect(script1._attributes.fetchpriority).toBe("high");
|
||||||
|
|
||||||
// Multiple Chunks
|
// Multiple Chunks
|
||||||
import(/* webpackFetchPriority: "high" */ "./b");
|
abortable(() => import(/* webpackFetchPriority: "high" */ "./b"));
|
||||||
import(/* webpackFetchPriority: "high" */ "./b2");
|
abortable(() => import(/* webpackFetchPriority: "high" */ "./b2"));
|
||||||
expect(document.head._children).toHaveLength(4);
|
expect(document.head._children).toHaveLength(4);
|
||||||
const script2 = document.head._children[1];
|
const script2 = document.head._children[1];
|
||||||
const script3 = document.head._children[2];
|
const script3 = document.head._children[2];
|
||||||
|
|
@ -17,19 +29,19 @@ it("should set fetchPriority", () => {
|
||||||
expect(script4._attributes.fetchpriority).toBe("high");
|
expect(script4._attributes.fetchpriority).toBe("high");
|
||||||
|
|
||||||
// Single Chunk, low
|
// Single Chunk, low
|
||||||
import(/* webpackFetchPriority: "low" */ "./c");
|
abortable(() => import(/* webpackFetchPriority: "low" */ "./c"));
|
||||||
expect(document.head._children).toHaveLength(5);
|
expect(document.head._children).toHaveLength(5);
|
||||||
const script5 = document.head._children[4];
|
const script5 = document.head._children[4];
|
||||||
expect(script5._attributes.fetchpriority).toBe("low");
|
expect(script5._attributes.fetchpriority).toBe("low");
|
||||||
|
|
||||||
// Single Chunk, auto
|
// Single Chunk, auto
|
||||||
import(/* webpackFetchPriority: "auto" */ "./d");
|
abortable(() => import(/* webpackFetchPriority: "auto" */ "./d"));
|
||||||
expect(document.head._children).toHaveLength(6);
|
expect(document.head._children).toHaveLength(6);
|
||||||
const script6 = document.head._children[5];
|
const script6 = document.head._children[5];
|
||||||
expect(script6._attributes.fetchpriority).toBe("auto");
|
expect(script6._attributes.fetchpriority).toBe("auto");
|
||||||
|
|
||||||
// No fetch priority
|
// No fetch priority
|
||||||
import("./e");
|
abortable(() => import("./e"));
|
||||||
expect(document.head._children).toHaveLength(7);
|
expect(document.head._children).toHaveLength(7);
|
||||||
const script7 = document.head._children[6];
|
const script7 = document.head._children[6];
|
||||||
expect(script7._attributes.fetchpriority).toBeUndefined();
|
expect(script7._attributes.fetchpriority).toBeUndefined();
|
||||||
|
|
@ -44,49 +56,49 @@ it("should set fetchPriority", () => {
|
||||||
const script8 = document.head._children[7];
|
const script8 = document.head._children[7];
|
||||||
expect(script8._attributes.fetchpriority).toBeUndefined();
|
expect(script8._attributes.fetchpriority).toBeUndefined();
|
||||||
|
|
||||||
import(/* webpackFetchPriority: "auto" */ "./g");
|
abortable(() => import(/* webpackFetchPriority: "auto" */ "./g"));
|
||||||
expect(document.head._children).toHaveLength(9);
|
expect(document.head._children).toHaveLength(9);
|
||||||
const script9 = document.head._children[8];
|
const script9 = document.head._children[8];
|
||||||
expect(script9._attributes.fetchpriority).toBe("auto");
|
expect(script9._attributes.fetchpriority).toBe("auto");
|
||||||
|
|
||||||
import(/* webpackFetchPriority: "unknown" */ "./h.js");
|
abortable(() => import(/* webpackFetchPriority: "unknown" */ "./h.js"));
|
||||||
expect(document.head._children).toHaveLength(10);
|
expect(document.head._children).toHaveLength(10);
|
||||||
const script10 = document.head._children[9];
|
const script10 = document.head._children[9];
|
||||||
expect(script10._attributes.fetchpriority).toBeUndefined();
|
expect(script10._attributes.fetchpriority).toBeUndefined();
|
||||||
|
|
||||||
import(/* webpackFetchPriority: "high" */ "./i");
|
abortable(() => import(/* webpackFetchPriority: "high" */ "./i"));
|
||||||
import(/* webpackFetchPriority: "low" */ "./i");
|
abortable(() => import(/* webpackFetchPriority: "low" */ "./i"));
|
||||||
expect(document.head._children).toHaveLength(11);
|
expect(document.head._children).toHaveLength(11);
|
||||||
const script11 = document.head._children[10];
|
const script11 = document.head._children[10];
|
||||||
expect(script11._attributes.fetchpriority).toBe("high");
|
expect(script11._attributes.fetchpriority).toBe("high");
|
||||||
|
|
||||||
import(/* webpackFetchPriority: "low" */ "./j");
|
abortable(() => import(/* webpackFetchPriority: "low" */ "./j"));
|
||||||
import(/* webpackFetchPriority: "high" */ "./j");
|
abortable(() => import(/* webpackFetchPriority: "high" */ "./j"));
|
||||||
expect(document.head._children).toHaveLength(12);
|
expect(document.head._children).toHaveLength(12);
|
||||||
const script12 = document.head._children[11];
|
const script12 = document.head._children[11];
|
||||||
|
|
||||||
expect(script12._attributes.fetchpriority).toBe("low");
|
expect(script12._attributes.fetchpriority).toBe("low");
|
||||||
import(/* webpackFetchPriority: "low" */ "./k");
|
abortable(() => import(/* webpackFetchPriority: "low" */ "./k"));
|
||||||
import("./e");
|
abortable(() => import("./e"));
|
||||||
import(/* webpackFetchPriority: "high" */ "./k");
|
abortable(() => import(/* webpackFetchPriority: "high" */ "./k"));
|
||||||
expect(document.head._children).toHaveLength(13);
|
abortable(() => expect(document.head._children).toHaveLength(13));
|
||||||
const script13 = document.head._children[12];
|
const script13 = document.head._children[12];
|
||||||
expect(script13._attributes.fetchpriority).toBe("low");
|
expect(script13._attributes.fetchpriority).toBe("low");
|
||||||
|
|
||||||
__non_webpack_require__("./125.js");
|
__non_webpack_require__("./125.js");
|
||||||
import(/* webpackFetchPriority: "high" */ "./style.css");
|
abortable(() => import(/* webpackFetchPriority: "high" */ "./style.css"));
|
||||||
expect(document.head._children).toHaveLength(14);
|
expect(document.head._children).toHaveLength(14);
|
||||||
const link1 = document.head._children[13];
|
const link1 = document.head._children[13];
|
||||||
expect(link1._attributes.fetchpriority).toBe("high");
|
expect(link1._attributes.fetchpriority).toBe("high");
|
||||||
|
|
||||||
__non_webpack_require__("./499.js");
|
__non_webpack_require__("./499.js");
|
||||||
import("./style-1.css");
|
abortable(() => import("./style-1.css"));
|
||||||
expect(document.head._children).toHaveLength(15);
|
expect(document.head._children).toHaveLength(15);
|
||||||
const link2 = document.head._children[14];
|
const link2 = document.head._children[14];
|
||||||
expect(link2._attributes.fetchpriority).toBeUndefined();
|
expect(link2._attributes.fetchpriority).toBeUndefined();
|
||||||
|
|
||||||
__non_webpack_require__("./616.js");
|
__non_webpack_require__("./616.js");
|
||||||
import(/* webpackFetchPriority: "low" */ "./style-2.css");
|
abortable(() => import(/* webpackFetchPriority: "low" */ "./style-2.css"));
|
||||||
expect(document.head._children).toHaveLength(16);
|
expect(document.head._children).toHaveLength(16);
|
||||||
const link3 = document.head._children[15];
|
const link3 = document.head._children[15];
|
||||||
expect(link3._attributes.fetchpriority).toBe("low");
|
expect(link3._attributes.fetchpriority).toBe("low");
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ module.exports = {
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
new webpack.ProgressPlugin(),
|
new webpack.ProgressPlugin(() => {}),
|
||||||
{
|
{
|
||||||
apply(compiler) {
|
apply(compiler) {
|
||||||
compiler.hooks.done.tapPromise("CacheTest", async () => {
|
compiler.hooks.done.tapPromise("CacheTest", async () => {
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ module.exports = {
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
new webpack.ProgressPlugin(),
|
new webpack.ProgressPlugin(() => {}),
|
||||||
{
|
{
|
||||||
apply(compiler) {
|
apply(compiler) {
|
||||||
compiler.hooks.done.tapPromise("CacheTest", async () => {
|
compiler.hooks.done.tapPromise("CacheTest", async () => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue