1
+ // @ts -check
2
+
1
3
/*
2
4
MIT License http://www.opensource.org/licenses/mit-license.php
3
5
Author Tobias Koppers @sokra
4
6
Modified by Evan You @yyx990803
5
7
*/
6
8
9
+ /**
10
+ * @typedef {{manualInject?: boolean; shadowMode?: boolean; ssrId?: string} } LoaderOptions
11
+ * @typedef {import('webpack').LoaderContext<LoaderOptions> } LoaderContext
12
+ */
13
+
7
14
const path = require ( 'node:path' )
8
15
const qs = require ( 'node:querystring' )
9
16
@@ -13,28 +20,45 @@ const loaderUtils = require('loader-utils')
13
20
// eslint-disable-next-line no-empty-function
14
21
module . exports = function ( ) { }
15
22
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
+ */
16
40
module . exports . pitch = function ( remainingRequest ) {
17
41
const isServer = this . target === 'node'
18
42
const isProduction = this . minimize || process . env . NODE_ENV === 'production'
19
- const addStylesClientPath = loaderUtils . stringifyRequest (
43
+ const addStylesClientPath = stringifyRequest (
20
44
this ,
21
45
'!' + path . join ( __dirname , 'lib/addStylesClient.js' ) ,
22
46
)
23
- const addStylesServerPath = loaderUtils . stringifyRequest (
47
+ const addStylesServerPath = stringifyRequest (
24
48
this ,
25
49
'!' + path . join ( __dirname , 'lib/addStylesServer.js' ) ,
26
50
)
27
- const addStylesShadowPath = loaderUtils . stringifyRequest (
51
+ const addStylesShadowPath = stringifyRequest (
28
52
this ,
29
53
'!' + path . join ( __dirname , 'lib/addStylesShadow.js' ) ,
30
54
)
31
55
32
- const request = loaderUtils . stringifyRequest ( this , '!!' + remainingRequest )
56
+ const request = stringifyRequest ( this , '!!' + remainingRequest )
33
57
const relPath = path
34
58
. relative ( __dirname , this . resourcePath )
35
59
. replaceAll ( '\\' , '/' )
36
60
const id = JSON . stringify ( hash ( request + relPath ) )
37
- const options = loaderUtils . getOptions ( this ) || { }
61
+ const options = this . getOptions ?. ( ) || loaderUtils . getOptions ( this ) || { }
38
62
39
63
// direct css import from js --> direct, or manually call `styles.__inject__(ssrContext)` with `manualInject` option
40
64
// css import from react file --> component lifecycle linked
0 commit comments