Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(loader): add baseUri option when using loaderContext.importModule #915

Merged
merged 1 commit into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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://";
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.
@@ -0,0 +1,7 @@
@font-face {
font-family: Roboto-plp;
font-style: normal;
font-weight: 400;
src: url(my-scheme://uri/assets/asset/roboto-v18-latin-300.ttf) format("truetype");
}

@@ -0,0 +1,3 @@
var __webpack_exports__ = {};


Binary file not shown.
7 changes: 7 additions & 0 deletions test/cases/base-uri-in-entry/expected/webpack-5/index.css
@@ -0,0 +1,7 @@
@font-face {
font-family: Roboto-plp;
font-style: normal;
font-weight: 400;
src: url(/assets/asset/roboto-v18-latin-300.ttf) format("truetype");
}

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


Binary file not shown.
1 change: 1 addition & 0 deletions test/cases/base-uri-in-entry/index.js
@@ -0,0 +1 @@
import "./main.css";
6 changes: 6 additions & 0 deletions test/cases/base-uri-in-entry/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");
}
48 changes: 48 additions & 0 deletions test/cases/base-uri-in-entry/webpack.config.js
@@ -0,0 +1,48 @@
import Self from "../../../src";

/**
* @type {import('webpack').Configuration}
*/
module.exports = {
mode: "production",
devtool: false,
entry: {
index: {
import: "./index.js",
baseUri: "my-scheme://uri",
},
},
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",
generator: {
publicPath: "/assets/",
},
},
],
},
plugins: [new Self({ experimentalUseImportModule: true })],
};
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(/assets/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");
}
45 changes: 45 additions & 0 deletions test/cases/base-uri/webpack.config.js
@@ -0,0 +1,45 @@
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",
generator: {
publicPath: "/assets/",
},
},
],
},
plugins: [new Self({ experimentalUseImportModule: true })],
};
1 change: 1 addition & 0 deletions types/utils.d.ts
Expand Up @@ -37,6 +37,7 @@ export function compareModulesByIdentifier(a: Module, b: Module): 0 | 1 | -1;
export const MODULE_TYPE: "css/mini-extract";
export const AUTO_PUBLIC_PATH: "__mini_css_extract_plugin_public_path_auto__";
export const ABSOLUTE_PUBLIC_PATH: "webpack:///mini-css-extract-plugin/";
export const BASE_URI: "webpack://";
export const SINGLE_DOT_PATH_SEGMENT: "__mini_css_extract_plugin_single_dot_path_segment__";
/**
* @param {LoaderContext} loaderContext
Expand Down