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: resolve style fields in package json #17346

Merged
merged 29 commits into from Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
32 changes: 30 additions & 2 deletions lib/config/defaults.js
Expand Up @@ -298,7 +298,10 @@ const applyWebpackOptionsDefaults = options => {
cache,
context: /** @type {Context} */ (options.context),
targetProperties,
mode: /** @type {Mode} */ (options.mode)
mode: /** @type {Mode} */ (options.mode),
css:
/** @type {NonNullable<ExperimentsNormalized["css"]>} */
(options.experiments.css)
}),
options.resolve
);
Expand Down Expand Up @@ -1398,9 +1401,16 @@ const applyOptimizationDefaults = (
* @param {string} options.context build context
* @param {TargetProperties | false} options.targetProperties target properties
* @param {Mode} options.mode mode
* @param {CssExperimentOptions|false} options.css is css enabled
* @returns {ResolveOptions} resolve options
*/
const getResolveDefaults = ({ cache, context, targetProperties, mode }) => {
const getResolveDefaults = ({
cache,
context,
targetProperties,
mode,
css
}) => {
/** @type {string[]} */
const conditions = ["webpack"];

Expand Down Expand Up @@ -1468,6 +1478,24 @@ const getResolveDefaults = ({ cache, context, targetProperties, mode }) => {
}
};

if (css) {
const styleConditions = [];

styleConditions.push("webpack");
styleConditions.push(mode === "development" ? "development" : "production");
styleConditions.push("style");

resolveOptions.byDependency["css-import"] = {
// We avoid using any main files because we have to be consistent with CSS `@import`
// and CSS `@import` does not handle `main` files in directories,
// you should always specify the full URL for styles
mainFiles: [],
mainFields: ["style", "..."],
conditionNames: styleConditions,
extensions: [".css"]
};
}

return resolveOptions;
};

Expand Down
21 changes: 18 additions & 3 deletions test/Defaults.unittest.js
Expand Up @@ -2207,8 +2207,6 @@ describe("snapshots", () => {
+ },
+ "futureDefaults": true,
@@ ... @@
+ },
+ Object {
+ "rules": Array [
+ Object {
+ "descriptionData": Object {
Expand Down Expand Up @@ -2261,7 +2259,8 @@ describe("snapshots", () => {
+ "fullySpecified": true,
+ },
+ "type": "css/module",
@@ ... @@
+ },
+ Object {
+ "mimetype": "text/css",
+ "resolve": Object {
+ "fullySpecified": true,
Expand All @@ -2287,6 +2286,22 @@ describe("snapshots", () => {
+ "hashDigestLength": 16,
+ "hashFunction": "xxhash64",
@@ ... @@
+ "css-import": Object {
+ "conditionNames": Array [
+ "webpack",
+ "production",
+ "style",
+ ],
+ "extensions": Array [
+ ".css",
+ ],
+ "mainFields": Array [
+ "style",
+ "...",
+ ],
+ "mainFiles": Array [],
+ },
@@ ... @@
- "<cwd>/node_modules/",
+ /^(.+?[\\\\/]node_modules[\\\\/])/,
`)
Expand Down
73 changes: 72 additions & 1 deletion test/__snapshots__/ConfigCacheTestCases.longtest.js.snap

Large diffs are not rendered by default.

73 changes: 72 additions & 1 deletion test/__snapshots__/ConfigTestCases.basictest.js.snap

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions test/configCases/css/css-import/directory/index.css
@@ -0,0 +1,3 @@
.directory-css {
color: red;
}
6 changes: 6 additions & 0 deletions test/configCases/css/css-import/errors.js
@@ -0,0 +1,6 @@
module.exports = [
/Can't resolve 'non-exported-css'/,
/Can't resolve '\.\/directory'/,
/Can't resolve 'condition-names-subpath\/non-valid\.css'/,
/Can't resolve '\.\/no-extension-in-request'/
];
3 changes: 3 additions & 0 deletions test/configCases/css/css-import/extensions-imported.mycss
@@ -0,0 +1,3 @@
.custom-extension{
color: green;
}
5 changes: 5 additions & 0 deletions test/configCases/css/css-import/file.less
@@ -0,0 +1,5 @@
@link-color: #428bca;

.link {
color: @link-color;
}
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/configCases/css/css-import/prefer-relative.css
@@ -0,0 +1,3 @@
.relative {
color: red;
}
3 changes: 3 additions & 0 deletions test/configCases/css/css-import/some-file.js
@@ -0,0 +1,3 @@
function doNotImportCss() {
return 'doNotImportCss';
}
1 change: 0 additions & 1 deletion test/configCases/css/css-import/string-loader.js
@@ -1,4 +1,3 @@
module.exports = function loader(content) {
return content + `.using-loader { color: red; }`;
};

28 changes: 28 additions & 0 deletions test/configCases/css/css-import/style-import.css
@@ -0,0 +1,28 @@
@import "style-library";
@import "main-field";
@import "package-with-exports";
@import "./extensions-imported.mycss";
@import "./with-less-import.css";
@import "prefer-relative.css";
@import "condition-names-style";
@import "condition-names-style-mode";
@import "condition-names-subpath/custom.css";
@import "condition-names-subpath-extra/custom.css";
@import "condition-names-style-less";
@import "condition-names-custom-name";
@import "style-and-main-library";
@import "condition-names-webpack";
@import "condition-names-style-nested";

/* Technically, this is not entirely true, but we allow it because the final file can be processed by the loader and return the CSS code */

@import "js-import";
@import "condition-names-webpack-js";
@import url("some-file.js");

/* Failed */

@import "non-exported-css";
@import "./directory";
@import "condition-names-subpath/non-valid.css";
@import "./no-extension-in-request";
2 changes: 2 additions & 0 deletions test/configCases/css/css-import/style.css
@@ -1,3 +1,4 @@
@import "./style-import.css";
@import "print.css?foo=1";
@import url("print.css?foo=2");
@import "print.css?foo=3" layer(default);
Expand Down Expand Up @@ -124,6 +125,7 @@ le3.css?=bar4');
@import url(' ./string-loader.js?esModule=false!./test.css ') screen and (orientation: landscape);
@import url(data:text/css;charset=utf-8,a%20%7B%0D%0A%20%20color%3A%20red%3B%0D%0A%7D);
@import url(data:text/css;charset=utf-8,a%20%7B%0D%0A%20%20color%3A%20blue%3B%0D%0A%7D) screen and (orientation:landscape);
@import url("data:text/css;charset=utf-8;base64,YSB7DQogIGNvbG9yOiByZWQ7DQp9");

@import url("./style5.css?foo=1") supports();
@import url("./style5.css?foo=2") supports( );
Expand Down