Skip to content

Commit 88a5f26

Browse files
authored
Move resolve-url-loader into Next.js (#32932)
* Move resolve-url-loader into Next.js Fixes #32157 Moves resolve-url-loader into Next.js and strips out all features that are not used like `rework` support. Will reduce install size as well as allow for optimizing the approach in the near future. * Update precompiled * Use loader-utils 2 * Update trace test * Revert "Update trace test" This reverts commit 7c09a07. * Add es5-ext as it's used in trace tests * Update join-function.js * Update bundle5.js
1 parent a5bfc1e commit 88a5f26

File tree

26 files changed

+804
-114
lines changed

26 files changed

+804
-114
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"critters": "0.0.6",
9595
"cross-env": "6.0.3",
9696
"cross-spawn": "6.0.5",
97+
"es5-ext": "0.10.53",
9798
"escape-string-regexp": "2.0.0",
9899
"eslint": "7.24.0",
99100
"eslint-plugin-import": "2.22.1",

packages/next/build/webpack/config/blocks/css/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export const css = curry(async function css(
139139
...sassOptions
140140
} = ctx.sassOptions
141141

142-
const lazyPostCSSInitalizer = () =>
142+
const lazyPostCSSInitializer = () =>
143143
lazyPostCSS(
144144
ctx.rootDirectory,
145145
ctx.supportedBrowsers,
@@ -165,8 +165,9 @@ export const css = curry(async function css(
165165
// To fix this, we use `resolve-url-loader` to rewrite the CSS
166166
// imports to real file paths.
167167
{
168-
loader: require.resolve('next/dist/compiled/resolve-url-loader'),
168+
loader: require.resolve('../../../loaders/resolve-url-loader/index'),
169169
options: {
170+
postcss: lazyPostCSSInitializer,
170171
// Source maps are not required here, but we may as well emit
171172
// them.
172173
sourceMap: true,
@@ -216,7 +217,7 @@ export const css = curry(async function css(
216217
and: [ctx.rootDirectory],
217218
not: [/node_modules/],
218219
},
219-
use: getCssModuleLoader(ctx, lazyPostCSSInitalizer),
220+
use: getCssModuleLoader(ctx, lazyPostCSSInitializer),
220221
}),
221222
],
222223
})
@@ -241,7 +242,7 @@ export const css = curry(async function css(
241242
},
242243
use: getCssModuleLoader(
243244
ctx,
244-
lazyPostCSSInitalizer,
245+
lazyPostCSSInitializer,
245246
sassPreprocessors
246247
),
247248
}),
@@ -303,7 +304,7 @@ export const css = curry(async function css(
303304
and: [ctx.rootDirectory],
304305
not: [/node_modules/],
305306
},
306-
use: getGlobalCssLoader(ctx, lazyPostCSSInitalizer),
307+
use: getGlobalCssLoader(ctx, lazyPostCSSInitializer),
307308
}),
308309
],
309310
})
@@ -321,7 +322,7 @@ export const css = curry(async function css(
321322
sideEffects: true,
322323
test: regexCssGlobal,
323324
issuer: { and: [ctx.customAppFile] },
324-
use: getGlobalCssLoader(ctx, lazyPostCSSInitalizer),
325+
use: getGlobalCssLoader(ctx, lazyPostCSSInitializer),
325326
}),
326327
],
327328
})
@@ -339,7 +340,7 @@ export const css = curry(async function css(
339340
issuer: { and: [ctx.customAppFile] },
340341
use: getGlobalCssLoader(
341342
ctx,
342-
lazyPostCSSInitalizer,
343+
lazyPostCSSInitializer,
343344
sassPreprocessors
344345
),
345346
}),
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2016 Ben Holloway
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
import { SourceMapConsumer } from 'next/dist/compiled/source-map'
26+
import valueProcessor from './lib/value-processor'
27+
import { defaultJoin } from './lib/join-function'
28+
import process from './lib/postcss'
29+
/**
30+
* A webpack loader that resolves absolute url() paths relative to their original source file.
31+
* Requires source-maps to do any meaningful work.
32+
* @param {string} content Css content
33+
* @param {object} sourceMap The source-map
34+
* @returns {string|String}
35+
*/
36+
export default async function resolveUrlLoader(content, sourceMap) {
37+
const options = Object.assign(
38+
{
39+
sourceMap: this.sourceMap,
40+
silent: false,
41+
absolute: false,
42+
keepQuery: false,
43+
root: false,
44+
debug: false,
45+
join: defaultJoin,
46+
},
47+
this.getOptions()
48+
)
49+
50+
let sourceMapConsumer
51+
if (sourceMap) {
52+
sourceMapConsumer = new SourceMapConsumer(sourceMap)
53+
}
54+
55+
const callback = this.async()
56+
const { postcss } = await options.postcss()
57+
process(postcss, this.resourcePath, content, {
58+
outputSourceMap: Boolean(options.sourceMap),
59+
transformDeclaration: valueProcessor(this.resourcePath, options),
60+
inputSourceMap: sourceMap,
61+
sourceMapConsumer: sourceMapConsumer,
62+
})
63+
.catch(onFailure)
64+
.then(onSuccess)
65+
66+
function onFailure(error) {
67+
callback(encodeError('CSS error', error))
68+
}
69+
70+
function onSuccess(reworked) {
71+
if (reworked) {
72+
// complete with source-map
73+
// source-map sources are relative to the file being processed
74+
if (options.sourceMap) {
75+
callback(null, reworked.content, reworked.map)
76+
}
77+
// complete without source-map
78+
else {
79+
callback(null, reworked.content)
80+
}
81+
}
82+
}
83+
84+
function encodeError(label, exception) {
85+
return new Error(
86+
[
87+
'resolve-url-loader',
88+
': ',
89+
[label]
90+
.concat(
91+
(typeof exception === 'string' && exception) ||
92+
(exception instanceof Error && [
93+
exception.message,
94+
exception.stack.split('\n')[1].trim(),
95+
]) ||
96+
[]
97+
)
98+
.filter(Boolean)
99+
.join('\n '),
100+
].join('')
101+
)
102+
}
103+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2016 Ben Holloway
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
/**
26+
* Prepend file:// protocol to source path string or source-map sources.
27+
*/
28+
function prepend(candidate) {
29+
if (typeof candidate === 'string') {
30+
return 'file://' + candidate
31+
} else if (
32+
candidate &&
33+
typeof candidate === 'object' &&
34+
Array.isArray(candidate.sources)
35+
) {
36+
return Object.assign({}, candidate, {
37+
sources: candidate.sources.map(prepend),
38+
})
39+
} else {
40+
throw new Error('expected string|object')
41+
}
42+
}
43+
44+
exports.prepend = prepend
45+
46+
/**
47+
* Remove file:// protocol from source path string or source-map sources.
48+
*/
49+
function remove(candidate) {
50+
if (typeof candidate === 'string') {
51+
return candidate.replace(/^file:\/{2}/, '')
52+
} else if (
53+
candidate &&
54+
typeof candidate === 'object' &&
55+
Array.isArray(candidate.sources)
56+
) {
57+
return Object.assign({}, candidate, {
58+
sources: candidate.sources.map(remove),
59+
})
60+
} else {
61+
throw new Error('expected string|object')
62+
}
63+
}
64+
65+
exports.remove = remove

0 commit comments

Comments
 (0)