Skip to content

Commit f8c8055

Browse files
refactor: drop webpack@4 (#912)
BREAKING CHANGE: minimum supported `webpack` version is `5`
1 parent ca6eca0 commit f8c8055

File tree

16 files changed

+1247
-979
lines changed

16 files changed

+1247
-979
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
matrix:
5757
os: [ubuntu-latest, windows-latest, macos-latest]
5858
node-version: [10.x, 12.x, 14.x]
59-
webpack-version: [4, latest]
59+
webpack-version: [latest]
6060

6161
runs-on: ${{ matrix.os }}
6262

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,16 @@ Finally run `webpack` via your preferred method.
8383

8484
Webpack provides an [advanced mechanism to resolve files](https://webpack.js.org/concepts/module-resolution/).
8585

86-
The `sass-loader` uses Sass's custom importer feature to pass all queries to the Webpack resolving engine. Thus you can import your Sass modules from `node_modules`. Just prepend them with a `~` to tell Webpack that this is not a relative import:
86+
The `sass-loader` uses Sass's custom importer feature to pass all queries to the Webpack resolving engine.
87+
Thus you can import your Sass modules from `node_modules`.
88+
89+
```scss
90+
@import "bootstrap";
91+
```
92+
93+
Using `~` is deprecated and can be removed from your code (**we recommend it**), but we still support it for historical reasons.
94+
Why you can removed it? The loader will first try to resolve `@import` as relative, if it cannot be resolved, the loader will try to resolve `@import` inside [`node_modules`](https://webpack.js.org/configuration/resolve/#resolve-modules).
95+
Just prepend them with a `~` which tells webpack to look up the [`modules`](https://webpack.js.org/configuration/resolve/#resolve-modules).
8796

8897
```scss
8998
@import "~bootstrap";
@@ -569,7 +578,7 @@ For production builds it's recommended to extract the CSS from your bundle being
569578

570579
There are two possibilities to extract a style sheet from the bundle:
571580

572-
- [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) (use this, when using webpack 4 configuration. Works in all use-cases)
581+
- [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin)
573582
- [extract-loader](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)
574583

575584
**webpack.config.js**

package-lock.json

Lines changed: 715 additions & 676 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"dist"
4040
],
4141
"peerDependencies": {
42-
"webpack": "^4.36.0 || ^5.0.0",
42+
"webpack": "^5.0.0",
4343
"node-sass": "^4.0.0 || ^5.0.0",
4444
"sass": "^1.3.0",
4545
"fibers": ">= 3.1.0"
@@ -57,23 +57,21 @@
5757
},
5858
"dependencies": {
5959
"klona": "^2.0.4",
60-
"loader-utils": "^2.0.0",
6160
"neo-async": "^2.6.2",
62-
"schema-utils": "^3.0.0",
63-
"semver": "^7.3.2"
61+
"semver": "^7.3.4"
6462
},
6563
"devDependencies": {
66-
"@babel/cli": "^7.12.1",
67-
"@babel/core": "^7.12.3",
68-
"@babel/preset-env": "^7.12.1",
64+
"@babel/cli": "^7.12.10",
65+
"@babel/core": "^7.12.10",
66+
"@babel/preset-env": "^7.12.11",
6967
"@commitlint/cli": "^11.0.0",
7068
"@commitlint/config-conventional": "^11.0.0",
7169
"@webpack-contrib/defaults": "^6.3.0",
7270
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
7371
"babel-jest": "^26.6.3",
7472
"bootstrap": "^4.5.3",
7573
"bootstrap-sass": "^3.4.1",
76-
"cross-env": "^7.0.2",
74+
"cross-env": "^7.0.3",
7775
"css-loader": "^5.0.1",
7876
"del": "^6.0.0",
7977
"del-cli": "^3.0.1",
@@ -84,18 +82,18 @@
8482
"fibers": "^5.0.0",
8583
"file-loader": "^6.2.0",
8684
"foundation-sites": "^6.6.3",
87-
"husky": "^4.3.0",
85+
"husky": "^4.3.6",
8886
"jest": "^26.6.3",
89-
"lint-staged": "^10.5.1",
87+
"lint-staged": "^10.5.3",
9088
"material-components-web": "^8.0.0",
9189
"memfs": "^3.2.0",
9290
"node-sass": "^5.0.0",
9391
"npm-run-all": "^4.1.5",
94-
"prettier": "^2.1.2",
95-
"sass": "^1.29.0",
96-
"standard-version": "^9.0.0",
92+
"prettier": "^2.2.1",
93+
"sass": "^1.32.0",
94+
"standard-version": "^9.1.0",
9795
"style-loader": "^2.0.0",
98-
"webpack": "^5.12.2"
96+
"webpack": "^5.20.0"
9997
},
10098
"keywords": [
10199
"sass",

src/index.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import path from "path";
22

3-
import { validate } from "schema-utils";
4-
import { getOptions } from "loader-utils";
5-
63
import schema from "./options.json";
74
import {
85
getSassImplementation,
@@ -20,13 +17,7 @@ import SassError from "./SassError";
2017
* @param {string} content
2118
*/
2219
async function loader(content) {
23-
const options = getOptions(this);
24-
25-
validate(schema, options, {
26-
name: "Sass Loader",
27-
baseDataPath: "options",
28-
});
29-
20+
const options = this.getOptions(schema);
3021
const callback = this.async();
3122
const implementation = getSassImplementation(this, options.implementation);
3223

src/options.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"title": "Sass Loader options",
23
"type": "object",
34
"properties": {
45
"implementation": {

src/utils.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import path from "path";
33

44
import semver from "semver";
55
import { klona } from "klona/full";
6-
import { urlToRequest } from "loader-utils";
76
import async from "neo-async";
87

98
function getDefaultSassImplementation() {
@@ -231,6 +230,7 @@ async function getSassOptions(
231230
// - ~@org/package
232231
// - ~@org/package/
233232
const isModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
233+
const moduleRequestRegex = /^[^?]*~/;
234234

235235
/**
236236
* When `sass`/`node-sass` tries to resolve an import, it uses a special algorithm.
@@ -249,18 +249,19 @@ const isModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+
249249
function getPossibleRequests(
250250
// eslint-disable-next-line no-shadow
251251
url,
252-
forWebpackResolver = false,
253-
rootContext = false
252+
forWebpackResolver = false
254253
) {
255-
const request = urlToRequest(
256-
url,
257-
// Maybe it is server-relative URLs
258-
forWebpackResolver && rootContext
259-
);
254+
let request = url;
260255

261256
// In case there is module request, send this to webpack resolver
262-
if (forWebpackResolver && isModuleImport.test(url)) {
263-
return [...new Set([request, url])];
257+
if (forWebpackResolver) {
258+
request = request.replace(moduleRequestRegex, "");
259+
260+
if (isModuleImport.test(url)) {
261+
request = request[request.length - 1] === "/" ? request : `${request}/`;
262+
263+
return [...new Set([request, url])];
264+
}
264265
}
265266

266267
// Keep in mind: ext can also be something like '.datepicker' when the true extension is omitted and the filename contains a dot.
@@ -376,6 +377,7 @@ function getWebpackResolver(
376377
mainFiles: ["_index", "index"],
377378
modules: [],
378379
restrictions: [/\.((sa|sc|c)ss)$/i],
380+
preferRelative: true,
379381
})
380382
);
381383
const webpackResolve = promiseResolve(
@@ -385,6 +387,7 @@ function getWebpackResolver(
385387
mainFiles: ["_index", "index", "..."],
386388
extensions: [".sass", ".scss", ".css"],
387389
restrictions: [/\.((sa|sc|c)ss)$/i],
390+
preferRelative: true,
388391
})
389392
);
390393

0 commit comments

Comments
 (0)