Skip to content

Commit

Permalink
fix: experimentalUseImportModule works with new URL(...) syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Sep 1, 2021
1 parent e70a0a9 commit cf81c4b
Show file tree
Hide file tree
Showing 172 changed files with 832 additions and 238 deletions.
192 changes: 109 additions & 83 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"babel-jest": "^27.0.6",
"bootstrap": "^4.6.0",
"cross-env": "^7.0.3",
"css-loader": "^5.2.6",
"css-loader": "^6.2.0",
"del": "^6.0.0",
"del-cli": "^4.0.0",
"es-check": "^6.0.0",
Expand Down
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
trueFn,
MODULE_TYPE,
AUTO_PUBLIC_PATH,
ABSOLUTE_PUBLIC_PATH,
SINGLE_DOT_PATH_SEGMENT,
DOUBLE_DOT_PATH_SEGMENT,
compareModulesByIdentifier,
} from "./utils";

Expand Down Expand Up @@ -1034,6 +1037,15 @@ class MiniCssExtractPlugin {

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

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

if (module.sourceMap) {
Expand Down
19 changes: 15 additions & 4 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
findModuleById,
evalModuleCode,
AUTO_PUBLIC_PATH,
ABSOLUTE_PUBLIC_PATH,
SINGLE_DOT_PATH_SEGMENT,
DOUBLE_DOT_PATH_SEGMENT,
stringifyRequest,
} from "./utils";
import schema from "./loader-options.json";
Expand Down Expand Up @@ -193,15 +196,23 @@ export function pitch(request) {
return;
}

const isAbsolutePublicPath = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/.test(publicPath);
const publicPathForExtract = isAbsolutePublicPath
? publicPath
: `${ABSOLUTE_PUBLIC_PATH}${publicPath
.replace(/\./g, SINGLE_DOT_PATH_SEGMENT)
.replace(/\.\./g, DOUBLE_DOT_PATH_SEGMENT)}`;

This comment has been minimized.

Copy link
@CrOrc

CrOrc Sep 2, 2021

Replacement with DOUBLE_DOT_PATH_SEGMENT will never work

"./../...".replace(/\./g,"a").replace(/\.\./g,"b") === "a/aa/aaa"

This comment has been minimized.

Copy link
@alexander-akait

alexander-akait Sep 2, 2021

Author Member

good catch


this.importModule(
`${this.resourcePath}.webpack[javascript/auto]!=!${request}`,
{
layer: options.layer,
publicPath,
publicPath: publicPathForExtract,
},
(err, exports) => {
if (err) {
callback(err);
(error, exports) => {
if (error) {
callback(error);

return;
}

Expand Down
10 changes: 9 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ function compareModulesByIdentifier(a, b) {
}

const MODULE_TYPE = "css/mini-extract";
const AUTO_PUBLIC_PATH = "__MINI_CSS_EXTRACT_PLUGIN_PUBLIC_PATH__";
const AUTO_PUBLIC_PATH = "__mini_css_extract_plugin_public_path_auto__";
const ABSOLUTE_PUBLIC_PATH = "webpack:///mini-css-extract-plugin/";
const SINGLE_DOT_PATH_SEGMENT =
"__mini_css_extract_plugin_single_dot_path_segment__";
const DOUBLE_DOT_PATH_SEGMENT =
"__mini_css_extract_plugin_double_dot_path_segment__";

function isAbsolutePath(str) {
return path.posix.isAbsolute(str) || path.win32.isAbsolute(str);
Expand Down Expand Up @@ -106,5 +111,8 @@ export {
compareModulesByIdentifier,
MODULE_TYPE,
AUTO_PUBLIC_PATH,
ABSOLUTE_PUBLIC_PATH,
SINGLE_DOT_PATH_SEGMENT,
DOUBLE_DOT_PATH_SEGMENT,
stringifyRequest,
};
5 changes: 4 additions & 1 deletion test/TestMemoryFS.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const assetsNames = (assets) => assets.map((asset) => asset.name);
describe("TestMemoryFS", () => {
it("should preserve asset even if not emitted", (done) => {
const casesDirectory = path.resolve(__dirname, "cases");
const directoryForCase = path.resolve(casesDirectory, "simple-publicpath");
const directoryForCase = path.resolve(
casesDirectory,
"publicpath-default-auto"
);
// eslint-disable-next-line import/no-dynamic-require, global-require
const webpackConfig = require(path.resolve(
directoryForCase,
Expand Down
18 changes: 9 additions & 9 deletions test/__snapshots__/emit-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ exports[`emit option should work with locals and invalidate cache when emit opti
<style id=\\"existing-style\\">.existing { color: yellow }</style>
</head>
<body>
css.foo: _1F17fUrdX_cbcyDHpFqmAD
css.bar: _3TZVxmkrZR2cTfifKhDoZn
css.baz: M-pzkE59YBii7EXzFyxE6
css.foo: J2lpabaeIrMw_95s_HME
css.bar: V4_dkftIz80Idb9RVpj3
css.baz: BhqK6bijhySVfFKg1Jj5
</body></html>"
`;
Expand All @@ -71,9 +71,9 @@ exports[`emit option should work with locals and invalidate cache when emit opti
<style id=\\"existing-style\\">.existing { color: yellow }</style>
</head>
<body>
css.foo-bar: VWLDg_rByGM4Rdhz5T3Ns
css.bar: _3TZVxmkrZR2cTfifKhDoZn
css.baz: M-pzkE59YBii7EXzFyxE6
css.foo-bar: XDzcxRg1NQGrYrn90Pnq
css.bar: V4_dkftIz80Idb9RVpj3
css.baz: BhqK6bijhySVfFKg1Jj5
</body></html>"
`;
Expand Down Expand Up @@ -115,9 +115,9 @@ exports[`emit option should work with locals when emit option is "false": DOM 1`
<style id=\\"existing-style\\">.existing { color: yellow }</style>
</head>
<body>
css.foo: _1F17fUrdX_cbcyDHpFqmAD
css.bar: _3TZVxmkrZR2cTfifKhDoZn
css.baz: M-pzkE59YBii7EXzFyxE6
css.foo: J2lpabaeIrMw_95s_HME
css.bar: V4_dkftIz80Idb9RVpj3
css.baz: BhqK6bijhySVfFKg1Jj5
</body></html>"
`;
Expand Down
3 changes: 0 additions & 3 deletions test/cases/asset-modules/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ module.exports = {
use: [
{
loader: Self.loader,
options: {
publicPath: "",
},
},
"css-loader",
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function loader() {
const callback = this.async();

callback(
null,
`export default [
[module.id, ".foo {background: url(" + new URL("./img.png", import.meta.url) + ")}", ""],
[module.id, ".bar {background: url(" + new URL("../outer-img.png", import.meta.url) + ")}", ""],
[module.id, ".baz {background: url(" + new URL("./nested/nested-img.png", import.meta.url) + ")}", ""]
]`
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {background: url(public/img.png)}
.bar {background: url(outer-img.png)}
.baz {background: url(public/nested/nested-img.png)}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function loader() {
const callback = this.async();

callback(
null,
`export default [
[module.id, ".foo {background: url(" + new URL("./img.png", import.meta.url) + ")}", ""],
[module.id, ".bar {background: url(" + new URL("../outer-img.png", import.meta.url) + ")}", ""],
[module.id, ".baz {background: url(" + new URL("./nested/nested-img.png", import.meta.url) + ")}", ""]
]`
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function loader() {
const callback = this.async();

callback(
null,
`export default [
[module.id, ".foo {background: url(" + new URL("./img.png", import.meta.url) + ")}", ""],
[module.id, ".bar {background: url(" + new URL("../outer-img.png", import.meta.url) + ")}", ""],
[module.id, ".baz {background: url(" + new URL("./nested/nested-img.png", import.meta.url) + ")}", ""]
]`
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.class {
background: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {background: url(img.png)}
.bar {background: url(../outer-img.png)}
.baz {background: url(nested/nested-img.png)}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from "path";

import Self from "../../../src";

module.exports = {
entry: "./index.js",
context: path.resolve(__dirname, "app"),
module: {
rules: [
{
test: /\.css$/,
use: [Self.loader, "./mockLoader"],
},
{
test: /\.png$/,
type: "asset/resource",
generator: {
filename: "[path][name][ext]",
},
},
],
},
plugins: [
new Self({
filename: "[name].css",
}),
],
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function loader() {
const callback = this.async();

callback(
null,
`export default [
[module.id, ".foo {background: url(" + new URL("./img.png", import.meta.url) + ")}", ""],
[module.id, ".bar {background: url(" + new URL("../outer-img.png", import.meta.url) + ")}", ""],
[module.id, ".baz {background: url(" + new URL("./nested/nested-img.png", import.meta.url) + ")}", ""]
]`
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.class {
background: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {background: url(/public/img.png)}
.bar {background: url(/outer-img.png)}
.baz {background: url(/public/nested/nested-img.png)}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {background: url(/public/img.png)}
.bar {background: url(/public/../outer-img.png)}
.baz {background: url(/public/nested/nested-img.png)}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from "path";

import Self from "../../../src";

module.exports = {
entry: "./index.js",
context: path.resolve(__dirname, "app"),
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: Self.loader,
options: {
publicPath: "/public/",
},
},
"./mockLoader",
],
},
{
test: /\.png$/,
type: "asset/resource",
generator: {
filename: "[path][name][ext]",
},
},
],
},
plugins: [
new Self({
filename: "[name].css",
}),
],
};
7 changes: 5 additions & 2 deletions test/cases/file-loader/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ module.exports = {
use: [
{
loader: Self.loader,
},
{
loader: "css-loader",
options: {
publicPath: "",
esModule: false,
},
},
"css-loader",
],
},
{
test: /\.svg$/,
type: "javascript/auto",
use: [
{
loader: "file-loader",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

.dDtFvCqivX5dncL54de5D {
.Z_E0RPEyV1Psn6SZX9qd {
background: green;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Z_E0RPEyV1Psn6SZX9qd {
background: red;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

.Hm__zqGZU_G0Lp44yFFz {
background: green;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Hm__zqGZU_G0Lp44yFFz {
background: red;
}

12 changes: 0 additions & 12 deletions test/cases/new-url-with-public-path-auto/app/mockLoader.js

This file was deleted.

1 change: 0 additions & 1 deletion test/cases/new-url-with-public-path-auto/test.filter.js

This file was deleted.

12 changes: 0 additions & 12 deletions test/cases/new-url-with-public-path/app/mockLoader.js

This file was deleted.

1 change: 0 additions & 1 deletion test/cases/new-url-with-public-path/test.filter.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
background: red;
background-image: url(https://webpack.js.org/foo/c9e192c015437a21dea1.svg);
}

Loading

0 comments on commit cf81c4b

Please sign in to comment.