Skip to content

Commit febbb7e

Browse files
committed
feat(loader): add baseUri option when using loaderContext.importModule
1 parent b2261c4 commit febbb7e

File tree

21 files changed

+152
-0
lines changed

21 files changed

+152
-0
lines changed

src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
SINGLE_DOT_PATH_SEGMENT,
1414
compareModulesByIdentifier,
1515
getUndoPath,
16+
BASE_URI,
1617
} = require("./utils");
1718

1819
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
@@ -1372,13 +1373,22 @@ class MiniCssExtractPlugin {
13721373

13731374
const undoPath = getUndoPath(filename, compiler.outputPath, false);
13741375

1376+
// replacements
13751377
content = content.replace(new RegExp(ABSOLUTE_PUBLIC_PATH, "g"), "");
13761378
content = content.replace(
13771379
new RegExp(SINGLE_DOT_PATH_SEGMENT, "g"),
13781380
"."
13791381
);
13801382
content = content.replace(new RegExp(AUTO_PUBLIC_PATH, "g"), undoPath);
13811383

1384+
const entryOptions = chunk.getEntryOptions();
1385+
const baseUriReplacement =
1386+
(entryOptions && entryOptions.baseUri) || undoPath;
1387+
content = content.replace(
1388+
new RegExp(BASE_URI, "g"),
1389+
baseUriReplacement
1390+
);
1391+
13821392
if (module.sourceMap) {
13831393
source.add(
13841394
new SourceMapSource(

src/loader.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
evalModuleCode,
66
AUTO_PUBLIC_PATH,
77
ABSOLUTE_PUBLIC_PATH,
8+
BASE_URI,
89
SINGLE_DOT_PATH_SEGMENT,
910
stringifyRequest,
1011
} = require("./utils");
@@ -300,6 +301,7 @@ function pitch(request) {
300301
{
301302
layer: options.layer,
302303
publicPath: /** @type {string} */ (publicPathForExtract),
304+
baseUri: `${BASE_URI}/`,
303305
},
304306
/**
305307
* @param {Error | null | undefined} error

src/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ function compareModulesByIdentifier(a, b) {
8686
const MODULE_TYPE = "css/mini-extract";
8787
const AUTO_PUBLIC_PATH = "__mini_css_extract_plugin_public_path_auto__";
8888
const ABSOLUTE_PUBLIC_PATH = "webpack:///mini-css-extract-plugin/";
89+
const BASE_URI = "webpack://";
8990
const SINGLE_DOT_PATH_SEGMENT =
9091
"__mini_css_extract_plugin_single_dot_path_segment__";
9192

@@ -212,6 +213,7 @@ module.exports = {
212213
MODULE_TYPE,
213214
AUTO_PUBLIC_PATH,
214215
ABSOLUTE_PUBLIC_PATH,
216+
BASE_URI,
215217
SINGLE_DOT_PATH_SEGMENT,
216218
stringifyRequest,
217219
getUndoPath,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@font-face {
2+
font-family: Roboto-plp;
3+
font-style: normal;
4+
font-weight: 400;
5+
src: url(my-scheme://uri/assets/asset/roboto-v18-latin-300.ttf) format("truetype");
6+
}
7+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var __webpack_exports__ = {};
2+
3+
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@font-face {
2+
font-family: Roboto-plp;
3+
font-style: normal;
4+
font-weight: 400;
5+
src: url(/assets/asset/roboto-v18-latin-300.ttf) format("truetype");
6+
}
7+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var __webpack_exports__ = {};
2+
3+
Binary file not shown.

0 commit comments

Comments
 (0)