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

fix: the auto option for inline module syntax #1274

Merged
merged 7 commits into from Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion src/utils.js
Expand Up @@ -126,7 +126,10 @@ const moduleRegExp = /\.module(s)?\.\w+$/i;
const icssRegExp = /\.icss\.\w+$/i;

function getModulesOptions(rawOptions, loaderContext) {
const { resourcePath } = loaderContext;
const resourcePath =
// eslint-disable-next-line no-underscore-dangle
loaderContext._module.matchResource || loaderContext.resourcePath;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woohoo! should this also be the logic for local ident generation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you are right, otherwise we will get the same class names.

After fix local ident generation should we merge it and release? All is fixed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think so, seems like this covers it all. hard to know for sure until i try it out on a code base. Thanks again for helping me out here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, here interesting case, when we have:

import three from './button.module.scss!=!./base64-loader?LmZvbyB7IGNvbG9yOiByZWQ7IH0=!./simple.js';
import four from './other.module.scss!=!./base64-loader?LmZvbyB7IGNvbG9yOiByZWQ7IH0=!./simple.js';

We run the loader only once, why? Because ES modules executed only once. So I don't think here bug. But we can solve it using:

// i.e. adding `?` with different values
import three from './button.module.scss!=!./base64-loader?LmZvbyB7IGNvbG9yOiByZWQ7IH0=!./simple.js?foo=bar';
import four from './other.module.scss!=!./base64-loader?LmZvbyB7IGNvbG9yOiByZWQ7IH0=!./simple.js?foo=baz';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah interesting, so the query param acts like a cache buster. That solution seems fine to me

Copy link
Contributor

@jquense jquense Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah in my case it'd be easy to use a stable identifier here. This wouldn't solve the className hash generation issue tho right? I'm guessing you discovered this writing a test for that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solved, please check code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arg, i'm trying this out now and _module.matchResource is null locally. Going to debug a bit to see why will report back

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't have matchResource in request, it will be nullish

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is for requests with it in it. For some reason i am seeing the module get created twice, one with the matchResource and one without, the one without is what makes it to the css-loader. It maybe has to do with ESM, still trying to figure it out


let isIcss;

if (typeof rawOptions.modules === "undefined") {
Expand Down
18 changes: 16 additions & 2 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -675,8 +675,8 @@ Array [
"",
],
Array [
"plain.scss!=!../../src/index.js?[ident]!./index-loader-syntax-sass.css",
".baz {
"button.modules.css!=!./index-loader-syntax-sass.css",
".iuzmhXWrT3_Z-zKS-rMGM {
width: 5px;
}",
"",
Expand Down Expand Up @@ -733,6 +733,20 @@ Array [
"./index-loader-syntax.css",
".a {
color: red;
}",
"",
],
Array [
"button.modules.css!=!./index-loader-syntax-sass.css",
".iuzmhXWrT3_Z-zKS-rMGM {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now they are CSS moduled

width: 5px;
}",
"",
],
Array [
"button.module.scss!=!./base64-loader/index.js?[ident]!./simple.js",
"._1DaPRMj4jImbX3XSIzjaR4 {
color: red;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see here: hash for classes are different

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But note: you should use different matchResource, otherwise hash will be same

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect!

}",
"",
],
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/base64-loader/index.js
@@ -0,0 +1,3 @@
module.exports = function loader(content) {
return Buffer.from(this.query.slice(1), 'base64').toString('ascii');
};
8 changes: 5 additions & 3 deletions test/fixtures/index-loader-syntax.js
@@ -1,5 +1,7 @@
import css from './index-loader-syntax.css';
import one from './index-loader-syntax.css';
import two from 'button.modules.css!=!./index-loader-syntax-sass.css';
import three from './button.module.scss!=!./base64-loader?LmZvbyB7IGNvbG9yOiByZWQ7IH0=!./simple.js';

__export__ = css;
__export__ = [...one, ...two, ...three];

export default css;
export default [...one, ...two, ...three];
2 changes: 1 addition & 1 deletion test/sourceMap-option.test.js
Expand Up @@ -501,7 +501,7 @@ describe('"sourceMap" option', () => {

expect(chunkName).toBe(
webpack.version[0] === "5"
? "main.89840c70616b788038a2.bundle.js"
? "main.a531550ffe767c49e881.bundle.js"
: "main.19efc497c5c37fc5e355.bundle.js"
);
expect(
Expand Down