Skip to content

Commit 26f3a1d

Browse files
committed
add base uri runtime module if chunkLoading=false
1 parent 5a26b7c commit 26f3a1d

File tree

7 files changed

+79
-0
lines changed

7 files changed

+79
-0
lines changed

lib/RuntimePlugin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirement
1111
const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
1212
const AsyncModuleRuntimeModule = require("./runtime/AsyncModuleRuntimeModule");
1313
const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModule");
14+
const BaseUriRuntimeModule = require("./runtime/BaseUriRuntimeModule");
1415
const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
1516
const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
1617
const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
@@ -413,6 +414,15 @@ class RuntimePlugin {
413414
);
414415
return true;
415416
});
417+
compilation.hooks.runtimeRequirementInTree
418+
.for(RuntimeGlobals.baseURI)
419+
.tap("RuntimePlugin", chunk => {
420+
const { chunkLoading } = chunk.getEntryOptions();
421+
if (chunkLoading === false) {
422+
compilation.addRuntimeModule(chunk, new BaseUriRuntimeModule());
423+
return true;
424+
}
425+
});
416426
// TODO webpack 6: remove CompatRuntimeModule
417427
compilation.hooks.additionalTreeRuntimeRequirements.tap(
418428
"RuntimePlugin",

lib/runtime/BaseUriRuntimeModule.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Ivan Kopeykin @vankop
4+
*/
5+
6+
"use strict";
7+
8+
const RuntimeGlobals = require("../RuntimeGlobals");
9+
const RuntimeModule = require("../RuntimeModule");
10+
11+
class BaseUriRuntimeModule extends RuntimeModule {
12+
constructor() {
13+
super("base uri", RuntimeModule.STAGE_ATTACH);
14+
}
15+
16+
/**
17+
* @returns {string} runtime code
18+
*/
19+
generate() {
20+
const { chunk } = this;
21+
22+
const options = chunk.getEntryOptions();
23+
return `${RuntimeGlobals.baseURI} = ${
24+
options.baseUri === undefined
25+
? "undefined"
26+
: JSON.stringify(options.baseUri)
27+
};`;
28+
}
29+
}
30+
31+
module.exports = BaseUriRuntimeModule;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"a":1}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import content from "./loader!!";
2+
3+
it("should compile", () => {
4+
expect(typeof content).toBe("string");
5+
expect(content.startsWith("webpack://")).toBe(true);
6+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
/** @type {import("../../../../").LoaderDefinition} */
6+
module.exports = function () {
7+
const callback = this.async();
8+
this.importModule(
9+
path.resolve(__dirname, "module.js"),
10+
{ baseUri: "webpack://" },
11+
(error, exports) => {
12+
if (error) {
13+
callback(error);
14+
return;
15+
}
16+
17+
callback(
18+
null,
19+
`module.exports = ${
20+
exports.asset ? JSON.stringify(exports.asset) : undefined
21+
}`
22+
);
23+
}
24+
);
25+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const asset = new URL("./a.json", import.meta.url);
2+
3+
export { asset }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = config => {
2+
return !config.module;
3+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy