Skip to content

Commit

Permalink
feat(loader): add baseUri option when using loaderContext.importModule
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Feb 22, 2022
1 parent 01f3585 commit f031cdb
Show file tree
Hide file tree
Showing 11 changed files with 843 additions and 817 deletions.
10 changes: 10 additions & 0 deletions src/index.js
Expand Up @@ -13,6 +13,7 @@ const {
SINGLE_DOT_PATH_SEGMENT,
compareModulesByIdentifier,
getUndoPath,
BASE_URI,
} = require("./utils");

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

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

// replacements
content = content.replace(new RegExp(ABSOLUTE_PUBLIC_PATH, "g"), "");
content = content.replace(
new RegExp(SINGLE_DOT_PATH_SEGMENT, "g"),
"."
);
content = content.replace(new RegExp(AUTO_PUBLIC_PATH, "g"), undoPath);

const entryOptions = chunk.getEntryOptions();
const baseUriReplacement =
(entryOptions && entryOptions.baseUri) || undoPath;
content = content.replace(
new RegExp(BASE_URI, "g"),
baseUriReplacement
);

if (module.sourceMap) {
source.add(
new SourceMapSource(
Expand Down
2 changes: 2 additions & 0 deletions src/loader.js
Expand Up @@ -5,6 +5,7 @@ const {
evalModuleCode,
AUTO_PUBLIC_PATH,
ABSOLUTE_PUBLIC_PATH,
BASE_URI,
SINGLE_DOT_PATH_SEGMENT,
stringifyRequest,
} = require("./utils");
Expand Down Expand Up @@ -300,6 +301,7 @@ function pitch(request) {
{
layer: options.layer,
publicPath: /** @type {string} */ (publicPathForExtract),
baseUri: BASE_URI,
},
/**
* @param {Error | null | undefined} error
Expand Down
2 changes: 2 additions & 0 deletions src/utils.js
Expand Up @@ -86,6 +86,7 @@ function compareModulesByIdentifier(a, b) {
const MODULE_TYPE = "css/mini-extract";
const AUTO_PUBLIC_PATH = "__mini_css_extract_plugin_public_path_auto__";
const ABSOLUTE_PUBLIC_PATH = "webpack:///mini-css-extract-plugin/";
const BASE_URI = "webpack:///mini-css-extract-plugin-base-uri";
const SINGLE_DOT_PATH_SEGMENT =
"__mini_css_extract_plugin_single_dot_path_segment__";

Expand Down Expand Up @@ -212,6 +213,7 @@ module.exports = {
MODULE_TYPE,
AUTO_PUBLIC_PATH,
ABSOLUTE_PUBLIC_PATH,
BASE_URI,
SINGLE_DOT_PATH_SEGMENT,
stringifyRequest,
getUndoPath,
Expand Down
Binary file not shown.
7 changes: 7 additions & 0 deletions test/cases/base-uri/expected/index.css
@@ -0,0 +1,7 @@
@font-face {
font-family: Roboto-plp;
font-style: normal;
font-weight: 400;
src: url(asset/roboto-v18-latin-300.ttf) format("truetype");
}

3 changes: 3 additions & 0 deletions test/cases/base-uri/expected/index.mjs
@@ -0,0 +1,3 @@
var __webpack_exports__ = {};


Binary file not shown.
1 change: 1 addition & 0 deletions test/cases/base-uri/index.js
@@ -0,0 +1 @@
import "./main.css";
6 changes: 6 additions & 0 deletions test/cases/base-uri/main.css
@@ -0,0 +1,6 @@
@font-face {
font-family: Roboto-plp;
font-style: normal;
font-weight: 400;
src: url("./fonts/roboto-v18-latin-300.ttf") format("truetype");
}
42 changes: 42 additions & 0 deletions test/cases/base-uri/webpack.config.js
@@ -0,0 +1,42 @@
import Self from "../../../src";

/**
* @type {import('webpack').Configuration}
*/
module.exports = {
mode: "production",
devtool: false,
entry: {
index: "./index.js",
},
optimization: {
minimize: false,
},
output: {
module: true,
assetModuleFilename: "asset/[name][ext]",
chunkFormat: "module",
chunkLoading: "import",
},
experiments: {
outputModule: true,
},
module: {
rules: [
{
test: /\.css$/i,
use: [
{
loader: Self.loader,
},
"css-loader",
],
},
{
test: /\.ttf$/i,
type: "asset/resource",
},
],
},
plugins: [new Self({ experimentalUseImportModule: true })],
};

0 comments on commit f031cdb

Please sign in to comment.