Skip to content

Commit f031cdb

Browse files
committed
feat(loader): add baseUri option when using loaderContext.importModule
1 parent 01f3585 commit f031cdb

File tree

11 files changed

+843
-817
lines changed

11 files changed

+843
-817
lines changed

src/index.js

+10
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

+2
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

+2
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:///mini-css-extract-plugin-base-uri";
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,
Binary file not shown.
+7
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(asset/roboto-v18-latin-300.ttf) format("truetype");
6+
}
7+
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var __webpack_exports__ = {};
2+
3+
Binary file not shown.

test/cases/base-uri/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./main.css";

test/cases/base-uri/main.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@font-face {
2+
font-family: Roboto-plp;
3+
font-style: normal;
4+
font-weight: 400;
5+
src: url("./fonts/roboto-v18-latin-300.ttf") format("truetype");
6+
}

test/cases/base-uri/webpack.config.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Self from "../../../src";
2+
3+
/**
4+
* @type {import('webpack').Configuration}
5+
*/
6+
module.exports = {
7+
mode: "production",
8+
devtool: false,
9+
entry: {
10+
index: "./index.js",
11+
},
12+
optimization: {
13+
minimize: false,
14+
},
15+
output: {
16+
module: true,
17+
assetModuleFilename: "asset/[name][ext]",
18+
chunkFormat: "module",
19+
chunkLoading: "import",
20+
},
21+
experiments: {
22+
outputModule: true,
23+
},
24+
module: {
25+
rules: [
26+
{
27+
test: /\.css$/i,
28+
use: [
29+
{
30+
loader: Self.loader,
31+
},
32+
"css-loader",
33+
],
34+
},
35+
{
36+
test: /\.ttf$/i,
37+
type: "asset/resource",
38+
},
39+
],
40+
},
41+
plugins: [new Self({ experimentalUseImportModule: true })],
42+
};

0 commit comments

Comments
 (0)