Skip to content

Commit 6004d95

Browse files
authored
feat: added baseUri option support (from entry options) (#915)
1 parent b2261c4 commit 6004d95

21 files changed

+152
-0
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://";
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,
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var __webpack_exports__ = {};
2+
3+
Binary file not shown.
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+
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-in-entry/index.js

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

test/cases/base-uri-in-entry/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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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: {
11+
import: "./index.js",
12+
baseUri: "my-scheme://uri",
13+
},
14+
},
15+
optimization: {
16+
minimize: false,
17+
},
18+
output: {
19+
module: true,
20+
assetModuleFilename: "asset/[name][ext]",
21+
chunkFormat: "module",
22+
chunkLoading: "import",
23+
},
24+
experiments: {
25+
outputModule: true,
26+
},
27+
module: {
28+
rules: [
29+
{
30+
test: /\.css$/i,
31+
use: [
32+
{
33+
loader: Self.loader,
34+
},
35+
"css-loader",
36+
],
37+
},
38+
{
39+
test: /\.ttf$/i,
40+
type: "asset/resource",
41+
generator: {
42+
publicPath: "/assets/",
43+
},
44+
},
45+
],
46+
},
47+
plugins: [new Self({ experimentalUseImportModule: true })],
48+
};
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(/assets/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

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
generator: {
39+
publicPath: "/assets/",
40+
},
41+
},
42+
],
43+
},
44+
plugins: [new Self({ experimentalUseImportModule: true })],
45+
};

types/utils.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function compareModulesByIdentifier(a: Module, b: Module): 0 | 1 | -1;
3737
export const MODULE_TYPE: "css/mini-extract";
3838
export const AUTO_PUBLIC_PATH: "__mini_css_extract_plugin_public_path_auto__";
3939
export const ABSOLUTE_PUBLIC_PATH: "webpack:///mini-css-extract-plugin/";
40+
export const BASE_URI: "webpack://";
4041
export const SINGLE_DOT_PATH_SEGMENT: "__mini_css_extract_plugin_single_dot_path_segment__";
4142
/**
4243
* @param {LoaderContext} loaderContext

0 commit comments

Comments
 (0)