Skip to content

Commit 1c4909a

Browse files
authored
fix: better compatible with webpack v4/v5 (#17)
1 parent d0dc617 commit 1c4909a

File tree

9 files changed

+541
-25
lines changed

9 files changed

+541
-25
lines changed

.changeset/spotty-turkeys-melt.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-style-loader": patch
3+
---
4+
5+
fix: better compatible with webpack v4/v5

.codesandbox/ci.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"node": "18",
33
"buildCommand": false,
4+
"installCommand": "codesandbox:install",
45
"sandboxes": []
56
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
coverage
33
node_modules
44
.*cache
5+
/.yarn/*
6+
!/.yarn/plugins
57
!/.*.cjs

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lts/*

.yarn/install-state.gz

-1.44 MB
Binary file not shown.

index.cjs

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
// @ts-check
2+
13
/*
24
MIT License http://www.opensource.org/licenses/mit-license.php
35
Author Tobias Koppers @sokra
46
Modified by Evan You @yyx990803
57
*/
68

9+
/**
10+
* @typedef {{manualInject?: boolean; shadowMode?: boolean; ssrId?: string}} LoaderOptions
11+
* @typedef {import('webpack').LoaderContext<LoaderOptions>} LoaderContext
12+
*/
13+
714
const path = require('node:path')
815
const qs = require('node:querystring')
916

@@ -13,28 +20,45 @@ const loaderUtils = require('loader-utils')
1320
// eslint-disable-next-line no-empty-function
1421
module.exports = function () {}
1522

23+
/**
24+
* @param {LoaderContext} loaderContext
25+
* @param {string} request
26+
* @returns {string} stringified request
27+
*/
28+
const stringifyRequest = (loaderContext, request) =>
29+
loaderContext.utils
30+
? JSON.stringify(
31+
loaderContext.utils.contextify(loaderContext.context, request),
32+
)
33+
: loaderUtils.stringifyRequest(loaderContext, request)
34+
35+
/**
36+
* @this {LoaderContext & {minimize?: boolean}}
37+
* @param {string} remainingRequest
38+
* @returns {string} transformed codes
39+
*/
1640
module.exports.pitch = function (remainingRequest) {
1741
const isServer = this.target === 'node'
1842
const isProduction = this.minimize || process.env.NODE_ENV === 'production'
19-
const addStylesClientPath = loaderUtils.stringifyRequest(
43+
const addStylesClientPath = stringifyRequest(
2044
this,
2145
'!' + path.join(__dirname, 'lib/addStylesClient.js'),
2246
)
23-
const addStylesServerPath = loaderUtils.stringifyRequest(
47+
const addStylesServerPath = stringifyRequest(
2448
this,
2549
'!' + path.join(__dirname, 'lib/addStylesServer.js'),
2650
)
27-
const addStylesShadowPath = loaderUtils.stringifyRequest(
51+
const addStylesShadowPath = stringifyRequest(
2852
this,
2953
'!' + path.join(__dirname, 'lib/addStylesShadow.js'),
3054
)
3155

32-
const request = loaderUtils.stringifyRequest(this, '!!' + remainingRequest)
56+
const request = stringifyRequest(this, '!!' + remainingRequest)
3357
const relPath = path
3458
.relative(__dirname, this.resourcePath)
3559
.replaceAll('\\', '/')
3660
const id = JSON.stringify(hash(request + relPath))
37-
const options = loaderUtils.getOptions(this) || {}
61+
const options = this.getOptions?.() || loaderUtils.getOptions(this) || {}
3862

3963
// direct css import from js --> direct, or manually call `styles.__inject__(ssrContext)` with `manualInject` option
4064
// css import from react file --> component lifecycle linked

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,30 @@
1616
"lib"
1717
],
1818
"scripts": {
19+
"codesandbox:install": "yarn --ignore-engines",
1920
"lint": "eslint . --cache",
2021
"prepare": "simple-git-hooks",
2122
"release": "clean-pkg-json && changeset publish",
2223
"test": "node --experimental-vm-modules node_modules/.bin/jest"
2324
},
2425
"dependencies": {
2526
"hash-sum": "^2.0.0",
26-
"loader-utils": "^3.2.1"
27+
"loader-utils": "^2.0.4"
2728
},
2829
"devDependencies": {
2930
"@1stg/common-config": "^10.0.0",
3031
"@changesets/changelog-github": "^0.5.0",
3132
"@changesets/cli": "^2.27.1",
3233
"@commitlint/cli": "^18.4.4",
34+
"@types/loader-utils": "^2.0.6",
3335
"clean-pkg-json": "^1.2.0",
3436
"eslint": "^8.56.0",
3537
"jest": "^29.7.0",
3638
"jest-environment-jsdom": "^29.7.0",
3739
"lint-staged": "^15.2.0",
3840
"prettier": "^3.1.1",
39-
"simple-git-hooks": "^2.9.0"
41+
"simple-git-hooks": "^2.9.0",
42+
"webpack": "^5.89.0"
4043
},
4144
"resolutions": {
4245
"prettier": "^3.1.1"

test/test.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import addStylesClient from '../lib/addStylesClient'
2-
import addStylesServer from '../lib/addStylesServer'
1+
import addStylesClient from '../lib/addStylesClient.js'
2+
import addStylesServer from '../lib/addStylesServer.js'
33

44
const mockedList = [
55
[1, 'h1 { color: red; }', ''],

0 commit comments

Comments
 (0)