Skip to content

Commit 1db2f4d

Browse files
fix: the auto option for inline module syntax (#1274)
1 parent c13f369 commit 1db2f4d

File tree

7 files changed

+180
-136
lines changed

7 files changed

+180
-136
lines changed

package-lock.json

+115-112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"del": "^6.0.0",
6868
"del-cli": "^3.0.1",
6969
"es-check": "^5.2.3",
70-
"eslint": "^7.21.0",
70+
"eslint": "^7.22.0",
7171
"eslint-config-prettier": "^8.1.0",
7272
"eslint-plugin-import": "^2.22.1",
7373
"file-loader": "^6.2.0",
@@ -90,7 +90,7 @@
9090
"stylus": "^0.54.8",
9191
"stylus-loader": "^4.3.0",
9292
"url-loader": "^4.1.1",
93-
"webpack": "^5.24.4"
93+
"webpack": "^5.26.0"
9494
},
9595
"keywords": [
9696
"webpack",

src/utils.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,22 @@ function defaultGetLocalIdent(
6868
localName,
6969
options
7070
) {
71-
const { context, hashPrefix } = options;
72-
const { resourcePath } = loaderContext;
73-
const request = normalizePath(path.relative(context, resourcePath));
71+
let relativeMatchResource = "";
72+
73+
// eslint-disable-next-line no-underscore-dangle
74+
if (loaderContext._module.matchResource) {
75+
relativeMatchResource = `${normalizePath(
76+
// eslint-disable-next-line no-underscore-dangle
77+
path.relative(options.context, loaderContext._module.matchResource)
78+
)}\x00`;
79+
}
80+
81+
const relativeResourcePath = normalizePath(
82+
path.relative(options.context, loaderContext.resourcePath)
83+
);
7484

7585
// eslint-disable-next-line no-param-reassign
76-
options.content = `${hashPrefix + request}\x00${localName}`;
86+
options.content = `${options.hashPrefix}${relativeMatchResource}${relativeResourcePath}\x00${localName}`;
7787

7888
return interpolateName(loaderContext, localIdentName, options);
7989
}
@@ -126,7 +136,10 @@ const moduleRegExp = /\.module(s)?\.\w+$/i;
126136
const icssRegExp = /\.icss\.\w+$/i;
127137

128138
function getModulesOptions(rawOptions, loaderContext) {
129-
const { resourcePath } = loaderContext;
139+
const resourcePath =
140+
// eslint-disable-next-line no-underscore-dangle
141+
loaderContext._module.matchResource || loaderContext.resourcePath;
142+
130143
let isIcss;
131144

132145
if (typeof rawOptions.modules === "undefined") {

test/__snapshots__/loader.test.js.snap

+34-13
Original file line numberDiff line numberDiff line change
@@ -665,66 +665,66 @@ exports[`loader should work with inline module syntax: result 1`] = `
665665
Array [
666666
Array [
667667
"other.modules.css!=!../../src/index.js?[ident]!./index-loader-syntax.modules.css",
668-
"._1ZJhuRHDA53bY_Z4Qfm2b4 {
668+
"._14oM7qkZ6dgC62uK5h4NI0 {
669669
color: red;
670670
}
671671
672-
.j3CQSVq_DdDvo0Ew74yQi {
672+
._2NqyYysncZeLetATpl0xfW {
673673
color: white;
674674
}",
675675
"",
676676
],
677677
Array [
678-
"plain.scss!=!../../src/index.js?[ident]!./index-loader-syntax-sass.css",
679-
".baz {
678+
"button.modules.css!=!./index-loader-syntax-sass.css",
679+
"._8aapa4kQQdSsOoRODcrwe {
680680
width: 5px;
681681
}",
682682
"",
683683
],
684684
Array [
685685
"other.modules.scss!=!../../src/index.js?[ident]!./index-loader-syntax-sass.modules.css",
686-
"._1fGl5mRxLFCqIet0X3JYrB > ._2pVMnENaxk1YmKug-4EMYF {
686+
"._2yVYVQfmDwKLZn9Qj6k_Q5 > ._3rHlnRnPEotX4nTkd82-aE {
687687
color: red;
688688
}",
689689
"",
690690
],
691691
Array [
692692
"other.modules.css!=!../../src/index.js?[ident]!./index-loader-syntax.modules.css",
693-
"._1ZJhuRHDA53bY_Z4Qfm2b4 {
693+
"._14oM7qkZ6dgC62uK5h4NI0 {
694694
color: red;
695695
}
696696
697-
.j3CQSVq_DdDvo0Ew74yQi {
697+
._2NqyYysncZeLetATpl0xfW {
698698
color: white;
699699
}
700700
701-
.B7fdaUjwJ3YVou1v7dYEE {
701+
._1-Aa1c8UQML46IWY0FjGXT {
702702
from: custom;
703703
}",
704704
"",
705705
],
706706
Array [
707707
"other.modules.css!=!../../src/index.js?[ident]!./index-loader-syntax.modules.css",
708-
"._1ZJhuRHDA53bY_Z4Qfm2b4 {
708+
"._14oM7qkZ6dgC62uK5h4NI0 {
709709
color: red;
710710
}
711711
712-
.j3CQSVq_DdDvo0Ew74yQi {
712+
._2NqyYysncZeLetATpl0xfW {
713713
color: white;
714714
}
715715
716-
.B7fdaUjwJ3YVou1v7dYEE {
716+
._1-Aa1c8UQML46IWY0FjGXT {
717717
from: custom;
718718
}",
719719
"",
720720
],
721721
Array [
722722
"other.modules.scss!=!../../src/index.js?[ident]!./index-loader-syntax-sass.modules.css",
723-
"._1fGl5mRxLFCqIet0X3JYrB > ._2pVMnENaxk1YmKug-4EMYF {
723+
"._2yVYVQfmDwKLZn9Qj6k_Q5 > ._3rHlnRnPEotX4nTkd82-aE {
724724
color: red;
725725
}
726726
727-
._1kK_VYa-N303wnLgpvL-7d {
727+
._2kEhhupr-6tgHnCC_d4yO8 {
728728
from: custom;
729729
}",
730730
"",
@@ -733,6 +733,27 @@ Array [
733733
"./index-loader-syntax.css",
734734
".a {
735735
color: red;
736+
}",
737+
"",
738+
],
739+
Array [
740+
"button.modules.css!=!./index-loader-syntax-sass.css",
741+
"._8aapa4kQQdSsOoRODcrwe {
742+
width: 5px;
743+
}",
744+
"",
745+
],
746+
Array [
747+
"button.module.scss!=!./base64-loader/index.js?[ident]!./simple.js?foo=bar",
748+
"._2hfVrRfux-FOrqimHr-fzt {
749+
color: red;
750+
}",
751+
"",
752+
],
753+
Array [
754+
"other.module.scss!=!./base64-loader/index.js?[ident]!./simple.js?foo=baz",
755+
"._2jOyhMQwdpQ85V5dDLcfoG {
756+
color: red;
736757
}",
737758
"",
738759
],

test/fixtures/base64-loader/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function loader(content) {
2+
return Buffer.from(this.query.slice(1), 'base64').toString('ascii');
3+
};

test/fixtures/index-loader-syntax.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import css from './index-loader-syntax.css';
1+
import one from './index-loader-syntax.css';
2+
import two from 'button.modules.css!=!./index-loader-syntax-sass.css';
3+
// Hash should be different
4+
import three from './button.module.scss!=!./base64-loader?LmZvbyB7IGNvbG9yOiByZWQ7IH0=!./simple.js?foo=bar';
5+
import four from './other.module.scss!=!./base64-loader?LmZvbyB7IGNvbG9yOiByZWQ7IH0=!./simple.js?foo=baz';
26

3-
__export__ = css;
7+
__export__ = [...one, ...two, ...three, ...four];
48

5-
export default css;
9+
export default [...one, ...two, ...three, ...four];

test/sourceMap-option.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ describe('"sourceMap" option', () => {
501501

502502
expect(chunkName).toBe(
503503
webpack.version[0] === "5"
504-
? "main.89840c70616b788038a2.bundle.js"
504+
? "main.a531550ffe767c49e881.bundle.js"
505505
: "main.19efc497c5c37fc5e355.bundle.js"
506506
);
507507
expect(

0 commit comments

Comments
 (0)