Unverified Commit 55b8502d authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

chore: compile `style-loader` dep (#21355)

This PR compiles `style-loader` to resolve a peer dep warning.

---

Closes #21359
parent 0d0db45c
......@@ -875,6 +875,7 @@ export default async function getBaseWebpackConfig(
'next-serverless-loader',
'noop-loader',
'next-plugin-loader',
'next-style-loader',
].reduce((alias, loader) => {
// using multiple aliases to replace `resolveLoader.modules`
alias[loader] = path.join(__dirname, 'webpack', 'loaders', loader)
......
......@@ -10,7 +10,7 @@ export function getClientStyleLoader({
}): webpack.RuleSetUseItem {
return isDevelopment
? {
loader: require.resolve('style-loader'),
loader: 'next-style-loader',
options: {
// By default, style-loader injects CSS into the bottom
// of <head>. This causes ordering problems between dev
......
Copyright JS Foundation and other contributors
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import loaderUtils from 'next/dist/compiled/loader-utils'
import path from 'path'
import { validate } from 'next/dist/compiled/schema-utils3'
import isEqualLocals from './runtime/isEqualLocals'
const schema = {
type: 'object',
properties: {
injectType: {
description:
'Allows to setup how styles will be injected into DOM (https://github.com/webpack-contrib/style-loader#injecttype).',
enum: [
'styleTag',
'singletonStyleTag',
'lazyStyleTag',
'lazySingletonStyleTag',
'linkTag',
],
},
attributes: {
description:
'Adds custom attributes to tag (https://github.com/webpack-contrib/style-loader#attributes).',
type: 'object',
},
insert: {
description:
'Inserts `<style>`/`<link>` at the given position (https://github.com/webpack-contrib/style-loader#insert).',
anyOf: [
{
type: 'string',
},
{
instanceof: 'Function',
},
],
},
base: {
description:
'Sets module ID base for DLLPlugin (https://github.com/webpack-contrib/style-loader#base).',
type: 'number',
},
esModule: {
description:
'Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule).',
type: 'boolean',
},
},
additionalProperties: false,
}
const loaderApi = () => {}
loaderApi.pitch = function loader(request) {
const options = loaderUtils.getOptions(this)
validate(schema, options, {
name: 'Style Loader',
baseDataPath: 'options',
})
const insert =
typeof options.insert === 'undefined'
? '"head"'
: typeof options.insert === 'string'
? JSON.stringify(options.insert)
: options.insert.toString()
const injectType = options.injectType || 'styleTag'
const esModule =
typeof options.esModule !== 'undefined' ? options.esModule : false
delete options.esModule
switch (injectType) {
case 'linkTag': {
const hmrCode = this.hot
? `
if (module.hot) {
module.hot.accept(
${loaderUtils.stringifyRequest(this, `!!${request}`)},
function() {
${
esModule
? 'update(content);'
: `content = require(${loaderUtils.stringifyRequest(
this,
`!!${request}`
)});
content = content.__esModule ? content.default : content;
update(content);`
}
}
);
module.hot.dispose(function() {
update();
});
}`
: ''
return `${
esModule
? `import api from ${loaderUtils.stringifyRequest(
this,
`!${path.join(__dirname, 'runtime/injectStylesIntoLinkTag.js')}`
)};
import content from ${loaderUtils.stringifyRequest(
this,
`!!${request}`
)};`
: `var api = require(${loaderUtils.stringifyRequest(
this,
`!${path.join(__dirname, 'runtime/injectStylesIntoLinkTag.js')}`
)});
var content = require(${loaderUtils.stringifyRequest(
this,
`!!${request}`
)});
content = content.__esModule ? content.default : content;`
}
var options = ${JSON.stringify(options)};
options.insert = ${insert};
var update = api(content, options);
${hmrCode}
${esModule ? 'export default {}' : ''}`
}
case 'lazyStyleTag':
case 'lazySingletonStyleTag': {
const isSingleton = injectType === 'lazySingletonStyleTag'
const hmrCode = this.hot
? `
if (module.hot) {
if (!content.locals || module.hot.invalidate) {
var isEqualLocals = ${isEqualLocals.toString()};
var oldLocals = content.locals;
module.hot.accept(
${loaderUtils.stringifyRequest(this, `!!${request}`)},
function () {
${
esModule
? `if (!isEqualLocals(oldLocals, content.locals)) {
module.hot.invalidate();
return;
}
oldLocals = content.locals;
if (update && refs > 0) {
update(content);
}`
: `content = require(${loaderUtils.stringifyRequest(
this,
`!!${request}`
)});
content = content.__esModule ? content.default : content;
if (!isEqualLocals(oldLocals, content.locals)) {
module.hot.invalidate();
return;
}
oldLocals = content.locals;
if (update && refs > 0) {
update(content);
}`
}
}
)
}
module.hot.dispose(function() {
if (update) {
update();
}
});
}`
: ''
return `${
esModule
? `import api from ${loaderUtils.stringifyRequest(
this,
`!${path.join(__dirname, 'runtime/injectStylesIntoStyleTag.js')}`
)};
import content from ${loaderUtils.stringifyRequest(
this,
`!!${request}`
)};`
: `var api = require(${loaderUtils.stringifyRequest(
this,
`!${path.join(__dirname, 'runtime/injectStylesIntoStyleTag.js')}`
)});
var content = require(${loaderUtils.stringifyRequest(
this,
`!!${request}`
)});
content = content.__esModule ? content.default : content;
if (typeof content === 'string') {
content = [[module.id, content, '']];
}`
}
var refs = 0;
var update;
var options = ${JSON.stringify(options)};
options.insert = ${insert};
options.singleton = ${isSingleton};
var exported = {};
exported.locals = content.locals || {};
exported.use = function() {
if (!(refs++)) {
update = api(content, options);
}
return exported;
};
exported.unuse = function() {
if (refs > 0 && !--refs) {
update();
update = null;
}
};
${hmrCode}
${esModule ? 'export default' : 'module.exports ='} exported;`
}
case 'styleTag':
case 'singletonStyleTag':
default: {
const isSingleton = injectType === 'singletonStyleTag'
const hmrCode = this.hot
? `
if (module.hot) {
if (!content.locals || module.hot.invalidate) {
var isEqualLocals = ${isEqualLocals.toString()};
var oldLocals = content.locals;
module.hot.accept(
${loaderUtils.stringifyRequest(this, `!!${request}`)},
function () {
${
esModule
? `if (!isEqualLocals(oldLocals, content.locals)) {
module.hot.invalidate();
return;
}
oldLocals = content.locals;
update(content);`
: `content = require(${loaderUtils.stringifyRequest(
this,
`!!${request}`
)});
content = content.__esModule ? content.default : content;
if (typeof content === 'string') {
content = [[module.id, content, '']];
}
if (!isEqualLocals(oldLocals, content.locals)) {
module.hot.invalidate();
return;
}
oldLocals = content.locals;
update(content);`
}
}
)
}
module.hot.dispose(function() {
update();
});
}`
: ''
return `${
esModule
? `import api from ${loaderUtils.stringifyRequest(
this,
`!${path.join(__dirname, 'runtime/injectStylesIntoStyleTag.js')}`
)};
import content from ${loaderUtils.stringifyRequest(
this,
`!!${request}`
)};`
: `var api = require(${loaderUtils.stringifyRequest(
this,
`!${path.join(__dirname, 'runtime/injectStylesIntoStyleTag.js')}`
)});
var content = require(${loaderUtils.stringifyRequest(
this,
`!!${request}`
)});
content = content.__esModule ? content.default : content;
if (typeof content === 'string') {
content = [[module.id, content, '']];
}`
}
var options = ${JSON.stringify(options)};
options.insert = ${insert};
options.singleton = ${isSingleton};
var update = api(content, options);
${hmrCode}
${esModule ? 'export default' : 'module.exports ='} content.locals || {};`
}
}
}
module.exports = loaderApi
const getTarget = (function getTarget() {
const memo = {}
return function memorize(target) {
if (typeof memo[target] === 'undefined') {
let styleTarget = document.querySelector(target)
// Special case to return head of iframe instead of iframe itself
if (
window.HTMLIFrameElement &&
styleTarget instanceof window.HTMLIFrameElement
) {
try {
// This will throw an exception if access to iframe is blocked
// due to cross-origin restrictions
styleTarget = styleTarget.contentDocument.head
} catch (e) {
// istanbul ignore next
styleTarget = null
}
}
memo[target] = styleTarget
}
return memo[target]
}
})()
module.exports = (url, options) => {
options = options || {}
options.attributes =
typeof options.attributes === 'object' ? options.attributes : {}
if (typeof options.attributes.nonce === 'undefined') {
const nonce =
// eslint-disable-next-line no-undef
typeof __webpack_nonce__ !== 'undefined' ? __webpack_nonce__ : null
if (nonce) {
options.attributes.nonce = nonce
}
}
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = url
Object.keys(options.attributes).forEach((key) => {
link.setAttribute(key, options.attributes[key])
})
if (typeof options.insert === 'function') {
options.insert(link)
} else {
const target = getTarget(options.insert || 'head')
if (!target) {
throw new Error(
"Couldn't find a style target. This probably means that the value for the 'insert' parameter is invalid."
)
}
target.appendChild(link)
}
return (newUrl) => {
if (typeof newUrl === 'string') {
link.href = newUrl
} else {
link.parentNode.removeChild(link)
}
}
}
const isOldIE = (function isOldIE() {
let memo
return function memorize() {
if (typeof memo === 'undefined') {
// Test for IE <= 9 as proposed by Browserhacks
// @see http://browserhacks.com/#hack-e71d8692f65334173fee715c222cb805
// Tests for existence of standard globals is to allow style-loader
// to operate correctly into non-standard environments
// @see https://github.com/webpack-contrib/style-loader/issues/177
memo = Boolean(window && document && document.all && !window.atob)
}
return memo
}
})()
const getTarget = (function getTarget() {
const memo = {}
return function memorize(target) {
if (typeof memo[target] === 'undefined') {
let styleTarget = document.querySelector(target)
// Special case to return head of iframe instead of iframe itself
if (
window.HTMLIFrameElement &&
styleTarget instanceof window.HTMLIFrameElement
) {
try {
// This will throw an exception if access to iframe is blocked
// due to cross-origin restrictions
styleTarget = styleTarget.contentDocument.head
} catch (e) {
// istanbul ignore next
styleTarget = null
}
}
memo[target] = styleTarget
}
return memo[target]
}
})()
const stylesInDom = []
function getIndexByIdentifier(identifier) {
let result = -1
for (let i = 0; i < stylesInDom.length; i++) {
if (stylesInDom[i].identifier === identifier) {
result = i
break
}
}
return result
}
function modulesToDom(list, options) {
const idCountMap = {}
const identifiers = []
for (let i = 0; i < list.length; i++) {
const item = list[i]
const id = options.base ? item[0] + options.base : item[0]
const count = idCountMap[id] || 0
const identifier = `${id} ${count}`
idCountMap[id] = count + 1
const index = getIndexByIdentifier(identifier)
const obj = {
css: item[1],
media: item[2],
sourceMap: item[3],
}
if (index !== -1) {
stylesInDom[index].references++
stylesInDom[index].updater(obj)
} else {
stylesInDom.push({
identifier,
updater: addStyle(obj, options),
references: 1,
})
}
identifiers.push(identifier)
}
return identifiers
}
function insertStyleElement(options) {
const style = document.createElement('style')
const attributes = options.attributes || {}
if (typeof attributes.nonce === 'undefined') {
const nonce =
// eslint-disable-next-line no-undef
typeof __webpack_nonce__ !== 'undefined' ? __webpack_nonce__ : null
if (nonce) {
attributes.nonce = nonce
}
}
Object.keys(attributes).forEach((key) => {
style.setAttribute(key, attributes[key])
})
if (typeof options.insert === 'function') {
options.insert(style)
} else {
const target = getTarget(options.insert || 'head')
if (!target) {
throw new Error(
"Couldn't find a style target. This probably means that the value for the 'insert' parameter is invalid."
)
}
target.appendChild(style)
}
return style
}
function removeStyleElement(style) {
// istanbul ignore if
if (style.parentNode === null) {
return false
}
style.parentNode.removeChild(style)
}
/* istanbul ignore next */
const replaceText = (function replaceText() {
const textStore = []
return function replace(index, replacement) {
textStore[index] = replacement
return textStore.filter(Boolean).join('\n')
}
})()
function applyToSingletonTag(style, index, remove, obj) {
const css = remove
? ''
: obj.media
? `@media ${obj.media} {${obj.css}}`
: obj.css
// For old IE
/* istanbul ignore if */
if (style.styleSheet) {
style.styleSheet.cssText = replaceText(index, css)
} else {
const cssNode = document.createTextNode(css)
const childNodes = style.childNodes
if (childNodes[index]) {
style.removeChild(childNodes[index])
}
if (childNodes.length) {
style.insertBefore(cssNode, childNodes[index])
} else {
style.appendChild(cssNode)
}
}
}
function applyToTag(style, options, obj) {
let css = obj.css
const media = obj.media
const sourceMap = obj.sourceMap
if (media) {
style.setAttribute('media', media)
} else {
style.removeAttribute('media')
}
if (sourceMap && typeof btoa !== 'undefined') {
css += `\n/*# sourceMappingURL=data:application/json;base64,${btoa(
unescape(encodeURIComponent(JSON.stringify(sourceMap)))
)} */`
}
// For old IE
/* istanbul ignore if */
if (style.styleSheet) {
style.styleSheet.cssText = css
} else {
while (style.firstChild) {
style.removeChild(style.firstChild)
}
style.appendChild(document.createTextNode(css))
}
}
let singleton = null
let singletonCounter = 0
function addStyle(obj, options) {
let style
let update
let remove
if (options.singleton) {
const styleIndex = singletonCounter++
style = singleton || (singleton = insertStyleElement(options))
update = applyToSingletonTag.bind(null, style, styleIndex, false)
remove = applyToSingletonTag.bind(null, style, styleIndex, true)
} else {
style = insertStyleElement(options)
update = applyToTag.bind(null, style, options)
remove = () => {
removeStyleElement(style)
}
}
update(obj)
return function updateStyle(newObj) {
if (newObj) {
if (
newObj.css === obj.css &&
newObj.media === obj.media &&
newObj.sourceMap === obj.sourceMap
) {
return
}
update((obj = newObj))
} else {
remove()
}
}
}
module.exports = (list, options) => {
options = options || {}
// Force single-tag solution on IE6-9, which has a hard limit on the # of <style>
// tags it will allow on a page
if (!options.singleton && typeof options.singleton !== 'boolean') {
options.singleton = isOldIE()
}
list = list || []
let lastIdentifiers = modulesToDom(list, options)
return function update(newList) {
newList = newList || []
if (Object.prototype.toString.call(newList) !== '[object Array]') {
return
}
for (let i = 0; i < lastIdentifiers.length; i++) {
const identifier = lastIdentifiers[i]
const index = getIndexByIdentifier(identifier)
stylesInDom[index].references--
}
const newLastIdentifiers = modulesToDom(newList, options)
for (let i = 0; i < lastIdentifiers.length; i++) {
const identifier = lastIdentifiers[i]
const index = getIndexByIdentifier(identifier)
if (stylesInDom[index].references === 0) {
stylesInDom[index].updater()
stylesInDom.splice(index, 1)
}
}
lastIdentifiers = newLastIdentifiers
}
}
function isEqualLocals(a, b, isNamedExport) {
if ((!a && b) || (a && !b)) {
return false
}
let p
for (p in a) {
if (isNamedExport && p === 'default') {
// eslint-disable-next-line no-continue
continue
}
if (a[p] !== b[p]) {
return false
}
}
for (p in b) {
if (isNamedExport && p === 'default') {
// eslint-disable-next-line no-continue
continue
}
if (!a[p]) {
return false
}
}
return true
}
module.exports = isEqualLocals
......@@ -94,7 +94,6 @@
"react-is": "16.13.1",
"react-refresh": "0.8.3",
"stream-browserify": "3.0.0",
"style-loader": "1.2.1",
"styled-jsx": "3.3.2",
"use-subscription": "1.5.1",
"vm-browserify": "1.1.2",
......
......@@ -14079,7 +14079,7 @@ scheduler@^0.20.1:
loose-envify "^1.1.0"
object-assign "^4.1.1"
schema-utils@2.7.1, schema-utils@^2.0.0, schema-utils@^2.6.5, schema-utils@^2.6.6, schema-utils@^2.7.1:
schema-utils@2.7.1, schema-utils@^2.0.0, schema-utils@^2.6.5, schema-utils@^2.7.1:
version "2.7.1"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-2.7.1.tgz#1ca4f32d1b24c590c203b8e7a50bf0ea4cd394d7"
integrity sha512-SHiNtMOUGWBQJwzISiVYKu82GiV4QYGePp3odlY1tuKO7gPtphAT5R/py0fA6xtbgLL/RvtJZnU9b8s0F1q0Xg==
......@@ -14917,13 +14917,6 @@ style-inject@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/style-inject/-/style-inject-0.3.0.tgz#d21c477affec91811cc82355832a700d22bf8dd3"
style-loader@1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/style-loader/-/style-loader-1.2.1.tgz#c5cbbfbf1170d076cfdd86e0109c5bba114baa1a"
dependencies:
loader-utils "^2.0.0"
schema-utils "^2.6.6"
styled-components@5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/styled-components/-/styled-components-5.1.0.tgz#2e3985b54f461027e1c91af3229e1c2530872a4e"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment