Unverified Commit 7a09f88d authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Reexports styled-jsx JSXStyle in nextjs (#36585)

When using pnpm / yarnPnP to install next.js, styled-jsx as dependency is not hoisted in the top level node_modules, it will fail when nodejs is trying to resolve `styled-jsx/style` from project directory. Re-export `styled-jsx/style` in next.js and let swc/babel plugin compile the import path it to `next/dist/shared/lib/styled-jsx`

Resolves #10149
Closes #21320
Closes #9325
Co-authored-by: 's avatarTim Neutkens <6324199+timneutkens@users.noreply.github.com>
parent 948128ae
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default function Foo() { export default function Foo() {
return /*#__PURE__*/ React.createElement("div", { return /*#__PURE__*/ React.createElement("div", {
render: function(v) { render: function(v) {
......
...@@ -310,7 +310,7 @@ pub fn styled_jsx_import_decl(style_import_name: &str) -> ModuleItem { ...@@ -310,7 +310,7 @@ pub fn styled_jsx_import_decl(style_import_name: &str) -> ModuleItem {
})], })],
src: Str { src: Str {
span: DUMMY_SP, span: DUMMY_SP,
value: "styled-jsx/style".into(), value: "next/dist/shared/lib/styled-jsx".into(),
raw: None, raw: None,
}, },
})) }))
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div > export default (()=><div >
<style jsx> <style jsx>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default { export default {
styles: <_JSXStyle id={"71f03d42ea0ec6"}>{".container.jsx-71f03d42ea0ec6{background:#000;color:white;font-weight:700;height:100px}"}</_JSXStyle>, styles: <_JSXStyle id={"71f03d42ea0ec6"}>{".container.jsx-71f03d42ea0ec6{background:#000;color:white;font-weight:700;height:100px}"}</_JSXStyle>,
className: "jsx-71f03d42ea0ec6" className: "jsx-71f03d42ea0ec6"
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div > export default (()=><div >
<style jsx> <style jsx>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div > export default (()=><div >
<style jsx> <style jsx>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div > export default (()=><div >
<style jsx> <style jsx>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=>{ export default (()=>{
const Element = 'div'; const Element = 'div';
return <div className={"jsx-abb4c2202db1a207"}> return <div className={"jsx-abb4c2202db1a207"}>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
import styles from './styles'; import styles from './styles';
const styles2 = require('./styles2'); const styles2 = require('./styles2');
// external only // external only
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default class { export default class {
render() { render() {
return <div className={"jsx-b2b86d63f35d25ee"}> return <div className={"jsx-b2b86d63f35d25ee"}>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default class { export default class {
render() { render() {
return <div className={"jsx-1952086b0a5ae64c"}> return <div className={"jsx-1952086b0a5ae64c"}>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
const Test = ()=><div className={"jsx-a9535d7d5f32c3c4"}> const Test = ()=><div className={"jsx-a9535d7d5f32c3c4"}>
<span className={"jsx-a9535d7d5f32c3c4"}>test</span> <span className={"jsx-a9535d7d5f32c3c4"}>test</span>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
function NavigationItem({ active , className , }) { function NavigationItem({ active , className , }) {
return <span className={"jsx-2342aae4628612c6" + " " + (cn({ return <span className={"jsx-2342aae4628612c6" + " " + (cn({
active active
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (({ children })=><div className={`jsx-${styles.__hash}`}> export default (({ children })=><div className={`jsx-${styles.__hash}`}>
<p className={`jsx-${styles.__hash}`}>{children}</p> <p className={`jsx-${styles.__hash}`}>{children}</p>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
const color = 'red'; const color = 'red';
const otherColor = 'green'; const otherColor = 'green';
const A = ()=><div className={"jsx-498d4e86e548e679"}> const A = ()=><div className={"jsx-498d4e86e548e679"}>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default class { export default class {
render() { render() {
const Element = 'div'; const Element = 'div';
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
import styles from './styles2'; import styles from './styles2';
export default (({ level =1 })=>{ export default (({ level =1 })=>{
const Element = `h${level}`; const Element = `h${level}`;
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (({ level =1 })=>{ export default (({ level =1 })=>{
const Element = `h${level}`; const Element = `h${level}`;
return <Element className={"jsx-fca64cc3f069b519" + " " + "root"}> return <Element className={"jsx-fca64cc3f069b519" + " " + "root"}>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
const darken = (c)=>c const darken = (c)=>c
; ;
const color = 'red'; const color = 'red';
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
function test() { function test() {
({ ({
styles: <_JSXStyle id={"abb4c2202db1a207"}>{"div.jsx-abb4c2202db1a207{color:red}"}</_JSXStyle>, styles: <_JSXStyle id={"abb4c2202db1a207"}>{"div.jsx-abb4c2202db1a207{color:red}"}</_JSXStyle>,
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
import styles, { foo as styles3 } from './styles'; import styles, { foo as styles3 } from './styles';
const styles2 = require('./styles2'); const styles2 = require('./styles2');
export default (()=><div > export default (()=><div >
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
import styles from './styles'; import styles from './styles';
export default (()=><div className={`jsx-${styles.__hash}`}> export default (()=><div className={`jsx-${styles.__hash}`}>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
import styles from './styles'; import styles from './styles';
const styles2 = require('./styles2'); const styles2 = require('./styles2');
import { foo as styles3 } from './styles'; import { foo as styles3 } from './styles';
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
import React from 'react'; import React from 'react';
export default (()=><> export default (()=><>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
const Test = ()=><div className={"jsx-5a206f122d1cb32e"}> const Test = ()=><div className={"jsx-5a206f122d1cb32e"}>
<span className={"jsx-5a206f122d1cb32e"}>test</span> <span className={"jsx-5a206f122d1cb32e"}>test</span>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default function IndexPage() { export default function IndexPage() {
return <div className={"jsx-b6abd0684ba81871"}> return <div className={"jsx-b6abd0684ba81871"}>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
const Test = ()=><div className={"jsx-d47d6adadf14e957"}> const Test = ()=><div className={"jsx-d47d6adadf14e957"}>
<_JSXStyle id={"d47d6adadf14e957"}>{"body{color:red}:hover{color:red;display:-webkit-box;display:-webkit-flex;display:-moz-box;display:-ms-flexbox;display:flex;-webkit-animation:foo 1s ease-out;-moz-animation:foo 1s ease-out;-o-animation:foo 1s ease-out;animation:foo 1s ease-out}div a{display:none}[data-test]>div{color:red}"}</_JSXStyle> <_JSXStyle id={"d47d6adadf14e957"}>{"body{color:red}:hover{color:red;display:-webkit-box;display:-webkit-flex;display:-moz-box;display:-ms-flexbox;display:flex;-webkit-animation:foo 1s ease-out;-moz-animation:foo 1s ease-out;-o-animation:foo 1s ease-out;animation:foo 1s ease-out}div a{display:none}[data-test]>div{color:red}"}</_JSXStyle>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (({ breakPoint })=><div className={_JSXStyle.dynamic([ export default (({ breakPoint })=><div className={_JSXStyle.dynamic([
[ [
"1568e0a11702cf40", "1568e0a11702cf40",
......
...@@ -2,7 +2,7 @@ export default function IndexPage() { ...@@ -2,7 +2,7 @@ export default function IndexPage() {
return ( return (
<div> <div>
<h1>Hello World.</h1> <h1>Hello World.</h1>
<style jsx global>{` <style jsx global>{`
@supports (display: flex) { @supports (display: flex) {
h1 { h1 {
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default function IndexPage() { export default function IndexPage() {
return <div className={"jsx-bbdada4ef17d18ef"}> return <div className={"jsx-bbdada4ef17d18ef"}>
<h1 className={"jsx-bbdada4ef17d18ef"}>Hello World.</h1> <h1 className={"jsx-bbdada4ef17d18ef"}>Hello World.</h1>
<_JSXStyle id={"bbdada4ef17d18ef"}>{"@supports(display:flex){h1{color:hotpink}}"}</_JSXStyle> <_JSXStyle id={"bbdada4ef17d18ef"}>{"@supports(display:flex){h1{color:hotpink}}"}</_JSXStyle>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default class { export default class {
render() { render() {
return <div className={_JSXStyle.dynamic([ return <div className={_JSXStyle.dynamic([
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
const Test = ()=><_JSXStyle id={"94239b6d6b42c9b5"}>{"p{color:red}"}</_JSXStyle> const Test = ()=><_JSXStyle id={"94239b6d6b42c9b5"}>{"p{color:red}"}</_JSXStyle>
; ;
export default (()=><div className={"jsx-3822e6e1fb9fa41a"}> export default (()=><div className={"jsx-3822e6e1fb9fa41a"}>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
const attrs = { const attrs = {
id: 'test' id: 'test'
}; };
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div className={"jsx-94239b6d6b42c9b5"}> export default (()=><div className={"jsx-94239b6d6b42c9b5"}>
<p className={"jsx-94239b6d6b42c9b5"}>woot</p> <p className={"jsx-94239b6d6b42c9b5"}>woot</p>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
import Link from "next/link"; import Link from "next/link";
export default function IndexPage() { export default function IndexPage() {
return <div className={"jsx-2339e3d61ab4470c"}> return <div className={"jsx-2339e3d61ab4470c"}>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div className={"jsx-c7c3a8e231c9215a"}> export default (()=><div className={"jsx-c7c3a8e231c9215a"}>
<p className={"jsx-c7c3a8e231c9215a"}>test</p> <p className={"jsx-c7c3a8e231c9215a"}>test</p>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div className={"jsx-94239b6d6b42c9b5"}> export default (()=><div className={"jsx-94239b6d6b42c9b5"}>
<p className={"jsx-94239b6d6b42c9b5"}>test</p> <p className={"jsx-94239b6d6b42c9b5"}>test</p>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
import colors, { size } from './constants'; import colors, { size } from './constants';
const color = 'red'; const color = 'red';
const bar = new String("div.jsx-aaed0341accea8f{font-size:3em}"); const bar = new String("div.jsx-aaed0341accea8f{font-size:3em}");
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export const Red = ({ Component ='button' , })=>{ export const Red = ({ Component ='button' , })=>{
return <Component className={_JSXStyle.dynamic([ return <Component className={_JSXStyle.dynamic([
[ [
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default class { export default class {
render() { render() {
return <div className={"jsx-1f6cef12199c3a8f"}> return <div className={"jsx-1f6cef12199c3a8f"}>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default function Home({ fontFamily }) { export default function Home({ fontFamily }) {
return <div className={_JSXStyle.dynamic([ return <div className={_JSXStyle.dynamic([
[ [
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default class { export default class {
render() { render() {
return <div className={_JSXStyle.dynamic([ return <div className={_JSXStyle.dynamic([
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default class { export default class {
render() { render() {
return <div className={_JSXStyle.dynamic([ return <div className={_JSXStyle.dynamic([
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default class { export default class {
render() { render() {
return <div className={_JSXStyle.dynamic([ return <div className={_JSXStyle.dynamic([
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default class { export default class {
render() { render() {
return <div className={_JSXStyle.dynamic([ return <div className={_JSXStyle.dynamic([
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default class { export default class {
render() { render() {
return <div className={_JSXStyle.dynamic([ return <div className={_JSXStyle.dynamic([
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div className={"jsx-713499aa363d6373"}> export default (()=><div className={"jsx-713499aa363d6373"}>
<p className={"jsx-713499aa363d6373"}>test</p> <p className={"jsx-713499aa363d6373"}>test</p>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div className={"jsx-53fd644ab080300c"}> export default (()=><div className={"jsx-53fd644ab080300c"}>
<p className={"jsx-53fd644ab080300c"}>test</p> <p className={"jsx-53fd644ab080300c"}>test</p>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div className={"jsx-1f7963ae04c6466a"}> export default (()=><div className={"jsx-1f7963ae04c6466a"}>
<p className={"jsx-1f7963ae04c6466a"}>test</p> <p className={"jsx-1f7963ae04c6466a"}>test</p>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div className={"jsx-1a19bb4817c105dd"}> export default (()=><div className={"jsx-1a19bb4817c105dd"}>
<p className={"jsx-1a19bb4817c105dd"}>test</p> <p className={"jsx-1a19bb4817c105dd"}>test</p>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div className={"jsx-938ca197692ef624"}> export default (()=><div className={"jsx-938ca197692ef624"}>
<p className={"jsx-938ca197692ef624"}>test</p> <p className={"jsx-938ca197692ef624"}>test</p>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div className={"jsx-768337a97aceabd1"}> export default (()=><div className={"jsx-768337a97aceabd1"}>
<p className={"jsx-768337a97aceabd1"}>test</p> <p className={"jsx-768337a97aceabd1"}>test</p>
......
import _JSXStyle from "styled-jsx/style"; import _JSXStyle from "next/dist/shared/lib/styled-jsx";
export default (()=><div className={"jsx-94239b6d6b42c9b5"}> export default (()=><div className={"jsx-94239b6d6b42c9b5"}>
<p className={"jsx-94239b6d6b42c9b5"}>test</p> <p className={"jsx-94239b6d6b42c9b5"}>test</p>
......
...@@ -8,15 +8,15 @@ type StyledJsxPlugin = [string, any] | string ...@@ -8,15 +8,15 @@ type StyledJsxPlugin = [string, any] | string
type StyledJsxBabelOptions = type StyledJsxBabelOptions =
| { | {
plugins?: StyledJsxPlugin[] plugins?: StyledJsxPlugin[]
styleModule?: string
'babel-test'?: boolean 'babel-test'?: boolean
} }
| undefined | undefined
// Resolve styled-jsx plugins // Resolve styled-jsx plugins
function styledJsxOptions(options: StyledJsxBabelOptions) { function styledJsxOptions(options: StyledJsxBabelOptions) {
if (!options) { options = options || {}
return {} options.styleModule = 'next/dist/shared/lib/styled-jsx'
}
if (!Array.isArray(options.plugins)) { if (!Array.isArray(options.plugins)) {
return options return options
......
export default function JSXStyle(props: any): null
module.exports = require('styled-jsx/style')
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
import cheerio from 'cheerio'
function runTest(packageManager?: string) {
describe(`styled-jsx${packageManager ? ' ' + packageManager : ''}`, () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return (
<div>
<style jsx>{'p { color: red; }'}</style>
<p>hello world</p>
</div>
)
}
`,
},
...(packageManager
? {
installCommand: `npx ${packageManager} install`,
}
: {}),
})
})
afterAll(() => next.destroy())
it('should contain styled-jsx styles in html', async () => {
const html = await renderViaHTTP(next.url, '/')
const $ = cheerio.load(html)
expect($('head').text()).toContain('color:red')
})
})
}
runTest()
runTest('pnpm')
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