From 61513c656722dcae5e4d1c8525680713f2d4d0da Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 3 Aug 2022 23:10:06 +0200 Subject: [PATCH 01/20] Use require hook and alias to resolve styled-jsx --- package.json | 1 - .../crates/core/tests/loader/front/attr-1/output.js | 2 +- packages/next-swc/crates/styled_jsx/src/utils.rs | 2 +- .../styled_jsx/tests/errors/nested-style-tags/output.js | 2 +- .../crates/styled_jsx/tests/errors/no-child/output.js | 2 +- .../styled_jsx/tests/errors/ts-with-css-resolve/output.js | 2 +- .../crates/styled_jsx/tests/errors/two-children/output.js | 2 +- .../styled_jsx/tests/errors/wrong-child-type/output.js | 2 +- .../tests/errors/wrong-jsx-expression-type/output.js | 2 +- .../attribute-generation-classname-rewriting/output.js | 2 +- .../tests/fixture/attribute-generation-modes/output.js | 2 +- .../crates/styled_jsx/tests/fixture/class/output.js | 2 +- .../crates/styled_jsx/tests/fixture/comments/output.js | 2 +- .../tests/fixture/component-attribute/output.js | 2 +- .../tests/fixture/css-selector-after-pseudo/output.js | 2 +- .../styled_jsx/tests/fixture/css-tag-same-file/output.js | 2 +- .../styled_jsx/tests/fixture/different-jsx-ids/output.js | 2 +- .../tests/fixture/dynamic-element-class/output.js | 2 +- .../tests/fixture/dynamic-element-external/output.js | 2 +- .../styled_jsx/tests/fixture/dynamic-element/output.js | 2 +- .../crates/styled_jsx/tests/fixture/expressions/output.js | 2 +- .../tests/fixture/external-nested-scope/output.js | 2 +- .../tests/fixture/external-stylesheet-global/output.js | 2 +- .../fixture/external-stylesheet-multi-line/output.js | 2 +- .../tests/fixture/external-stylesheet/output.js | 2 +- .../crates/styled_jsx/tests/fixture/fragment/output.js | 2 +- .../tests/fixture/global-child-selector/output.js | 2 +- .../styled_jsx/tests/fixture/global-redundant/output.js | 2 +- .../crates/styled_jsx/tests/fixture/global/output.js | 2 +- .../crates/styled_jsx/tests/fixture/issue-30480/output.js | 2 +- .../crates/styled_jsx/tests/fixture/issue-30570/output.js | 2 +- .../fixture/issue-31562-interpolation-in-mdea/output.js | 2 +- .../tests/fixture/mixed-global-scoped/output.js | 2 +- .../styled_jsx/tests/fixture/multiple-jsx/output.js | 2 +- .../tests/fixture/non-styled-jsx-style/output.js | 2 +- .../tests/fixture/number-after-placeholder/output.js | 2 +- .../tests/fixture/one-off-global-selectors/output.js | 2 +- .../crates/styled_jsx/tests/fixture/stateless/output.js | 2 +- .../crates/styled_jsx/tests/fixture/styles/output.js | 2 +- .../crates/styled_jsx/tests/fixture/too-many/output.js | 2 +- .../styled_jsx/tests/fixture/tpl-escape-1/output.js | 2 +- .../styled_jsx/tests/fixture/tpl-escape-2/output.js | 2 +- .../tests/fixture/tpl-placeholder-1-as-property/output.js | 2 +- .../fixture/tpl-placeholder-2-as-part-of-value/output.js | 2 +- .../tests/fixture/tpl-placeholder-3-as-value/output.js | 2 +- .../output.js | 2 +- .../output.js | 2 +- .../fixture/transform-css-complex-selector/output.js | 2 +- .../tests/fixture/transform-css-global/output.js | 2 +- .../tests/fixture/transform-css-media-query/output.js | 2 +- .../tests/fixture/transform-css-normal/output.js | 2 +- .../tests/fixture/transform-css-nth-1/output.js | 2 +- .../styled_jsx/tests/fixture/transform-css/output.js | 2 +- .../crates/styled_jsx/tests/fixture/whitespace/output.js | 2 +- packages/next/build/babel/preset.ts | 2 +- packages/next/build/webpack-config.ts | 8 +++++++- packages/next/build/webpack/require-hook.ts | 2 ++ packages/next/pages/_error.tsx | 4 ++-- packages/next/shared/lib/styled-jsx/index.d.ts | 1 + packages/next/shared/lib/styled-jsx/index.js | 1 + .../shared/lib/{styled-jsx.d.ts => styled-jsx/style.d.ts} | 0 .../shared/lib/{styled-jsx.js => styled-jsx/style.js} | 0 pnpm-lock.yaml | 2 -- test/e2e/app-dir/rsc-basic.test.ts | 1 - .../app-dir/rsc-basic/app/root-style-registry.client.js | 4 +--- 65 files changed, 68 insertions(+), 64 deletions(-) create mode 100644 packages/next/shared/lib/styled-jsx/index.d.ts create mode 100644 packages/next/shared/lib/styled-jsx/index.js rename packages/next/shared/lib/{styled-jsx.d.ts => styled-jsx/style.d.ts} (100%) rename packages/next/shared/lib/{styled-jsx.js => styled-jsx/style.js} (100%) diff --git a/package.json b/package.json index 5877847f2216..881653e3f2bc 100644 --- a/package.json +++ b/package.json @@ -191,7 +191,6 @@ "semver": "7.3.7", "shell-quote": "1.7.3", "styled-components": "5.3.3", - "styled-jsx": "link:packages/next/node_modules/styled-jsx", "styled-jsx-plugin-postcss": "3.0.2", "tailwindcss": "1.1.3", "taskr": "1.1.0", diff --git a/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js b/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js index f82bfb4e4a00..3c7b478d94b0 100644 --- a/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js +++ b/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js @@ -1,4 +1,4 @@ -import _JSXStyle from "next/dist/shared/lib/styled-jsx"; +import _JSXStyle from "next/dist/shared/lib/styled-jsx/style"; export default function Foo() { return /*#__PURE__*/ React.createElement("div", { render: function(v) { diff --git a/packages/next-swc/crates/styled_jsx/src/utils.rs b/packages/next-swc/crates/styled_jsx/src/utils.rs index 678762e3c4fa..e916ab37e325 100644 --- a/packages/next-swc/crates/styled_jsx/src/utils.rs +++ b/packages/next-swc/crates/styled_jsx/src/utils.rs @@ -310,7 +310,7 @@ pub fn styled_jsx_import_decl(style_import_name: &str) -> ModuleItem { })], src: Str { span: DUMMY_SP, - value: "next/dist/shared/lib/styled-jsx".into(), + value: "next/dist/shared/lib/styled-jsx/style".into(), raw: None, }, })) diff --git a/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js b/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js index 38d68136f2d8..d53f2bef9299 100644 --- a/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js +++ b/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js @@ -1,4 +1,4 @@ -import _JSXStyle from "next/dist/shared/lib/styled-jsx"; +import _JSXStyle from "next/dist/shared/lib/styled-jsx/style"; const Component = ()=><>
diff --git a/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js b/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js index 908b640b076a..f366d5daac81 100644 --- a/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js +++ b/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js @@ -1,4 +1,4 @@ -import _JSXStyle from "next/dist/shared/lib/styled-jsx"; +import _JSXStyle from "next/dist/shared/lib/styled-jsx/style"; export default (()=>
+

styled-jsx

This area is rendered by styled-jsx

+
) From 27c2d0134aca8c0d45a12de987c5952af3a9d879 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sun, 7 Aug 2022 17:39:07 +0200 Subject: [PATCH 05/20] setup require hook in server --- .../core/tests/loader/front/attr-1/output.js | 2 +- .../next-swc/crates/styled_jsx/src/utils.rs | 2 +- .../tests/errors/nested-style-tags/output.js | 2 +- .../tests/errors/no-child/output.js | 2 +- .../errors/ts-with-css-resolve/output.js | 2 +- .../tests/errors/two-children/output.js | 2 +- .../tests/errors/wrong-child-type/output.js | 2 +- .../wrong-jsx-expression-type/output.js | 2 +- .../output.js | 2 +- .../attribute-generation-modes/output.js | 2 +- .../styled_jsx/tests/fixture/class/output.js | 2 +- .../tests/fixture/comments/output.js | 2 +- .../fixture/component-attribute/output.js | 2 +- .../css-selector-after-pseudo/output.js | 2 +- .../tests/fixture/css-tag-same-file/output.js | 2 +- .../tests/fixture/different-jsx-ids/output.js | 2 +- .../fixture/dynamic-element-class/output.js | 2 +- .../dynamic-element-external/output.js | 2 +- .../tests/fixture/dynamic-element/output.js | 2 +- .../tests/fixture/expressions/output.js | 2 +- .../fixture/external-nested-scope/output.js | 2 +- .../external-stylesheet-global/output.js | 2 +- .../external-stylesheet-multi-line/output.js | 2 +- .../fixture/external-stylesheet/output.js | 2 +- .../tests/fixture/fragment/output.js | 2 +- .../fixture/global-child-selector/output.js | 2 +- .../tests/fixture/global-redundant/output.js | 2 +- .../styled_jsx/tests/fixture/global/output.js | 2 +- .../tests/fixture/issue-30480/output.js | 2 +- .../tests/fixture/issue-30570/output.js | 2 +- .../output.js | 2 +- .../fixture/mixed-global-scoped/output.js | 2 +- .../tests/fixture/multiple-jsx/output.js | 2 +- .../fixture/non-styled-jsx-style/output.js | 2 +- .../number-after-placeholder/output.js | 2 +- .../one-off-global-selectors/output.js | 2 +- .../tests/fixture/stateless/output.js | 2 +- .../styled_jsx/tests/fixture/styles/output.js | 2 +- .../tests/fixture/too-many/output.js | 2 +- .../tests/fixture/tpl-escape-1/output.js | 2 +- .../tests/fixture/tpl-escape-2/output.js | 2 +- .../tpl-placeholder-1-as-property/output.js | 2 +- .../output.js | 2 +- .../tpl-placeholder-3-as-value/output.js | 2 +- .../output.js | 2 +- .../output.js | 2 +- .../transform-css-complex-selector/output.js | 2 +- .../fixture/transform-css-global/output.js | 2 +- .../transform-css-media-query/output.js | 2 +- .../fixture/transform-css-normal/output.js | 2 +- .../fixture/transform-css-nth-1/output.js | 2 +- .../tests/fixture/transform-css/output.js | 2 +- .../tests/fixture/whitespace/output.js | 2 +- packages/next/build/babel/preset.ts | 2 +- packages/next/build/webpack-config.ts | 4 ---- packages/next/server/config-utils.ts | 23 +++++++++---------- packages/next/server/config.ts | 10 +++++++- packages/next/server/next-server.ts | 8 ++++++- .../next/shared/lib/styled-jsx/index.d.ts | 2 +- packages/next/shared/lib/styled-jsx/index.js | 2 +- .../next/shared/lib/styled-jsx/style.d.ts | 1 - packages/next/shared/lib/styled-jsx/style.js | 1 - 62 files changed, 83 insertions(+), 76 deletions(-) delete mode 100644 packages/next/shared/lib/styled-jsx/style.d.ts delete mode 100644 packages/next/shared/lib/styled-jsx/style.js diff --git a/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js b/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js index 3c7b478d94b0..f82bfb4e4a00 100644 --- a/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js +++ b/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js @@ -1,4 +1,4 @@ -import _JSXStyle from "next/dist/shared/lib/styled-jsx/style"; +import _JSXStyle from "next/dist/shared/lib/styled-jsx"; export default function Foo() { return /*#__PURE__*/ React.createElement("div", { render: function(v) { diff --git a/packages/next-swc/crates/styled_jsx/src/utils.rs b/packages/next-swc/crates/styled_jsx/src/utils.rs index e916ab37e325..678762e3c4fa 100644 --- a/packages/next-swc/crates/styled_jsx/src/utils.rs +++ b/packages/next-swc/crates/styled_jsx/src/utils.rs @@ -310,7 +310,7 @@ pub fn styled_jsx_import_decl(style_import_name: &str) -> ModuleItem { })], src: Str { span: DUMMY_SP, - value: "next/dist/shared/lib/styled-jsx/style".into(), + value: "next/dist/shared/lib/styled-jsx".into(), raw: None, }, })) diff --git a/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js b/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js index d53f2bef9299..38d68136f2d8 100644 --- a/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js +++ b/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js @@ -1,4 +1,4 @@ -import _JSXStyle from "next/dist/shared/lib/styled-jsx/style"; +import _JSXStyle from "next/dist/shared/lib/styled-jsx"; const Component = ()=><>
diff --git a/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js b/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js index f366d5daac81..908b640b076a 100644 --- a/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js +++ b/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js @@ -1,4 +1,4 @@ -import _JSXStyle from "next/dist/shared/lib/styled-jsx/style"; +import _JSXStyle from "next/dist/shared/lib/styled-jsx"; export default (()=>

hello world

+ {/* */}
) } diff --git a/test/e2e/styled-jsx/index.test.ts b/test/e2e/styled-jsx/index.test.ts index 06aed6c53f7d..82d272dd41d8 100644 --- a/test/e2e/styled-jsx/index.test.ts +++ b/test/e2e/styled-jsx/index.test.ts @@ -2,22 +2,32 @@ import path from 'path' import { createNext, FileRef } from 'e2e-utils' import { NextInstance } from 'test/lib/next-modes/base' import { renderViaHTTP } from 'next-test-utils' -import cheerio from 'cheerio' const appDir = path.join(__dirname, 'app') function runTest(packageManager?: string) { - describe(`styled-jsx${packageManager ? ' ' + packageManager : ''}`, () => { + describe(`styled-jsx with ${ + packageManager ? ' ' + packageManager : '' + }`, () => { let next: NextInstance beforeAll(async () => { next = await createNext({ files: { + node_modules_bak: new FileRef(path.join(appDir, 'node_modules_bak')), pages: new FileRef(path.join(appDir, 'pages')), + // 'next.config.js': new FileRef(path.join(appDir, 'next.config.js')), }, - dependencies: { - 'styled-jsx': '4.0.0', // add outdated invalid styled-jsx + packageJson: { + scripts: { + setup: `cp -r ./node_modules_bak/my-comps ./node_modules;`, + build: `yarn setup && next build`, + dev: `yarn setup && next dev`, + start: 'next start', + }, }, + startCommand: 'yarn ' + ((global as any).isNextDev ? 'dev' : 'start'), + buildCommand: `yarn build`, ...(packageManager ? { installCommand: `npx ${packageManager} install`, @@ -29,8 +39,9 @@ function runTest(packageManager?: string) { it('should contain styled-jsx styles in html', async () => { const html = await renderViaHTTP(next.url, '/') - const $ = cheerio.load(html) - expect($('html').text()).toMatch(/color:(\s)*red/) + expect(html).toMatch(/color:(\s)*red/) + // TODO: support styled-jsx from node-modules + // expect(html).toMatch(/color:(\s)*cyan/) }) }) } From eb5403dbd8c774b8bf3a5c33807bafd129037eba Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 8 Aug 2022 17:32:35 +0200 Subject: [PATCH 10/20] pre copy styled-jsx assets --- package.json | 1 + .../core/tests/loader/front/attr-1/output.js | 2 +- .../next-swc/crates/styled_jsx/src/utils.rs | 2 +- .../tests/errors/nested-style-tags/output.js | 2 +- .../tests/errors/no-child/output.js | 2 +- .../errors/ts-with-css-resolve/output.js | 2 +- .../tests/errors/two-children/output.js | 2 +- .../tests/errors/wrong-child-type/output.js | 2 +- .../wrong-jsx-expression-type/output.js | 2 +- .../output.js | 2 +- .../attribute-generation-modes/output.js | 2 +- .../styled_jsx/tests/fixture/class/output.js | 2 +- .../tests/fixture/comments/output.js | 2 +- .../fixture/component-attribute/output.js | 2 +- .../css-selector-after-pseudo/output.js | 2 +- .../tests/fixture/css-tag-same-file/output.js | 2 +- .../tests/fixture/different-jsx-ids/output.js | 2 +- .../fixture/dynamic-element-class/output.js | 2 +- .../dynamic-element-external/output.js | 2 +- .../tests/fixture/dynamic-element/output.js | 2 +- .../tests/fixture/expressions/output.js | 2 +- .../fixture/external-nested-scope/output.js | 2 +- .../external-stylesheet-global/output.js | 2 +- .../external-stylesheet-multi-line/output.js | 2 +- .../fixture/external-stylesheet/output.js | 2 +- .../tests/fixture/fragment/output.js | 2 +- .../fixture/global-child-selector/output.js | 2 +- .../tests/fixture/global-redundant/output.js | 2 +- .../styled_jsx/tests/fixture/global/output.js | 2 +- .../tests/fixture/issue-30480/output.js | 2 +- .../tests/fixture/issue-30570/output.js | 2 +- .../output.js | 2 +- .../fixture/mixed-global-scoped/output.js | 2 +- .../tests/fixture/multiple-jsx/output.js | 2 +- .../fixture/non-styled-jsx-style/output.js | 2 +- .../number-after-placeholder/output.js | 2 +- .../one-off-global-selectors/output.js | 2 +- .../tests/fixture/stateless/output.js | 2 +- .../styled_jsx/tests/fixture/styles/output.js | 2 +- .../tests/fixture/too-many/output.js | 2 +- .../tests/fixture/tpl-escape-1/output.js | 2 +- .../tests/fixture/tpl-escape-2/output.js | 2 +- .../tpl-placeholder-1-as-property/output.js | 2 +- .../output.js | 2 +- .../tpl-placeholder-3-as-value/output.js | 2 +- .../output.js | 2 +- .../output.js | 2 +- .../transform-css-complex-selector/output.js | 2 +- .../fixture/transform-css-global/output.js | 2 +- .../transform-css-media-query/output.js | 2 +- .../fixture/transform-css-normal/output.js | 2 +- .../fixture/transform-css-nth-1/output.js | 2 +- .../tests/fixture/transform-css/output.js | 2 +- .../tests/fixture/whitespace/output.js | 2 +- packages/next/build/babel/preset.ts | 2 +- packages/next/build/webpack-config.ts | 12 ++++------- packages/next/build/webpack/require-hook.ts | 4 ++-- packages/next/index.d.ts | 2 +- packages/next/server/render.tsx | 2 +- packages/next/taskfile.js | 18 ++++++++++++++--- packages/next/types/misc.d.ts | 5 +++++ pnpm-lock.yaml | 20 +++++++++++++++++++ .../app/node_modules_bak/my-comps/button.js | 2 -- test/e2e/styled-jsx/app/pages/index.js | 4 ++-- test/e2e/styled-jsx/index.test.ts | 8 +++++--- 65 files changed, 110 insertions(+), 76 deletions(-) diff --git a/package.json b/package.json index 881653e3f2bc..580bfac7d4f6 100644 --- a/package.json +++ b/package.json @@ -191,6 +191,7 @@ "semver": "7.3.7", "shell-quote": "1.7.3", "styled-components": "5.3.3", + "styled-jsx": "5.0.2", "styled-jsx-plugin-postcss": "3.0.2", "tailwindcss": "1.1.3", "taskr": "1.1.0", diff --git a/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js b/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js index 3c7b478d94b0..ed60ad648aa2 100644 --- a/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js +++ b/packages/next-swc/crates/core/tests/loader/front/attr-1/output.js @@ -1,4 +1,4 @@ -import _JSXStyle from "next/dist/shared/lib/styled-jsx/style"; +import _JSXStyle from "styled-jsx/style"; export default function Foo() { return /*#__PURE__*/ React.createElement("div", { render: function(v) { diff --git a/packages/next-swc/crates/styled_jsx/src/utils.rs b/packages/next-swc/crates/styled_jsx/src/utils.rs index e916ab37e325..bc720b16af38 100644 --- a/packages/next-swc/crates/styled_jsx/src/utils.rs +++ b/packages/next-swc/crates/styled_jsx/src/utils.rs @@ -310,7 +310,7 @@ pub fn styled_jsx_import_decl(style_import_name: &str) -> ModuleItem { })], src: Str { span: DUMMY_SP, - value: "next/dist/shared/lib/styled-jsx/style".into(), + value: "styled-jsx/style".into(), raw: None, }, })) diff --git a/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js b/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js index d53f2bef9299..6a801091839c 100644 --- a/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js +++ b/packages/next-swc/crates/styled_jsx/tests/errors/nested-style-tags/output.js @@ -1,4 +1,4 @@ -import _JSXStyle from "next/dist/shared/lib/styled-jsx/style"; +import _JSXStyle from "styled-jsx/style"; const Component = ()=><>
diff --git a/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js b/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js index f366d5daac81..90fb7d27831c 100644 --- a/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js +++ b/packages/next-swc/crates/styled_jsx/tests/errors/no-child/output.js @@ -1,4 +1,4 @@ -import _JSXStyle from "next/dist/shared/lib/styled-jsx/style"; +import _JSXStyle from "styled-jsx/style"; export default (()=>

hello world

- {/* */} +
) } diff --git a/test/e2e/styled-jsx/index.test.ts b/test/e2e/styled-jsx/index.test.ts index 82d272dd41d8..fd8610db133f 100644 --- a/test/e2e/styled-jsx/index.test.ts +++ b/test/e2e/styled-jsx/index.test.ts @@ -16,7 +16,7 @@ function runTest(packageManager?: string) { files: { node_modules_bak: new FileRef(path.join(appDir, 'node_modules_bak')), pages: new FileRef(path.join(appDir, 'pages')), - // 'next.config.js': new FileRef(path.join(appDir, 'next.config.js')), + 'next.config.js': new FileRef(path.join(appDir, 'next.config.js')), }, packageJson: { scripts: { @@ -26,6 +26,9 @@ function runTest(packageManager?: string) { start: 'next start', }, }, + dependencies: { + 'styled-jsx': '4.0.0', // incompatible styled-jsx on user side + }, startCommand: 'yarn ' + ((global as any).isNextDev ? 'dev' : 'start'), buildCommand: `yarn build`, ...(packageManager @@ -40,8 +43,7 @@ function runTest(packageManager?: string) { it('should contain styled-jsx styles in html', async () => { const html = await renderViaHTTP(next.url, '/') expect(html).toMatch(/color:(\s)*red/) - // TODO: support styled-jsx from node-modules - // expect(html).toMatch(/color:(\s)*cyan/) + expect(html).toMatch(/color:(\s)*cyan/) }) }) } From cea4f0fa2c3d51440bbc370cdacbb5893ad5c51e Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 8 Aug 2022 18:04:47 +0200 Subject: [PATCH 11/20] fix styled-jsx dts --- packages/next/taskfile.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/next/taskfile.js b/packages/next/taskfile.js index 800edee659db..198100f9a0cb 100644 --- a/packages/next/taskfile.js +++ b/packages/next/taskfile.js @@ -62,23 +62,26 @@ export async function copy_styled_jsx_assets(task, opts) { const content = await fs.readFile(join(styledJsxPath, file), 'utf8') const exportsIndex = content.indexOf('export') - await fs.writeFile( - join(outputDir, file), + let replacedContent = `${content.substring(0, exportsIndex)}\n` + - `declare module 'styled-jsx${ - file === 'index.d.ts' ? '' : '/' + fileNoExt - }' { + `declare module 'styled-jsx${ + file === 'index.d.ts' ? '' : '/' + fileNoExt + }' { ${content.substring(exportsIndex)} }` - ) + if (file === 'index.d.ts') { + replacedContent = replacedContent + .replace(/export function StyleRegistry/g, 'export function IRegistry') + .replace(/StyleRegistry/g, 'IStyleRegistry') + .replace(/IRegistry/g, 'Registry') + } + await fs.writeFile(join(outputDir, file), replacedContent) typeReferences += `/// \n` } await fs.writeFile(join(outputDir, 'global.d.ts'), typeReferences) - console.log('jsFiles', jsFiles) for (const file of jsFiles) { - console.log('file', file) const content = await fs.readFile(join(styledJsxPath, file), 'utf8') const distFile = join(outputDir, file) await fs.ensureDir(dirname(distFile)) From 7994c387eb76da31a23e05f691510da0f1943ad7 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 8 Aug 2022 18:26:01 +0200 Subject: [PATCH 12/20] add npmrc for styled-jsx e2e test --- test/e2e/styled-jsx/app/.npmrc | 1 + test/e2e/styled-jsx/index.test.ts | 1 + 2 files changed, 2 insertions(+) create mode 100644 test/e2e/styled-jsx/app/.npmrc diff --git a/test/e2e/styled-jsx/app/.npmrc b/test/e2e/styled-jsx/app/.npmrc new file mode 100644 index 000000000000..3e775efb0f40 --- /dev/null +++ b/test/e2e/styled-jsx/app/.npmrc @@ -0,0 +1 @@ +auto-install-peers=true diff --git a/test/e2e/styled-jsx/index.test.ts b/test/e2e/styled-jsx/index.test.ts index fd8610db133f..6b4682d3694e 100644 --- a/test/e2e/styled-jsx/index.test.ts +++ b/test/e2e/styled-jsx/index.test.ts @@ -17,6 +17,7 @@ function runTest(packageManager?: string) { node_modules_bak: new FileRef(path.join(appDir, 'node_modules_bak')), pages: new FileRef(path.join(appDir, 'pages')), 'next.config.js': new FileRef(path.join(appDir, 'next.config.js')), + '.npmrc': new FileRef(path.join(appDir, '.npmrc')), }, packageJson: { scripts: { From 616f1b242be2e2fb03ec2417acd8ca893320f8ba Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 8 Aug 2022 22:24:02 +0200 Subject: [PATCH 13/20] load require hook directly --- package.json | 1 - packages/next/build/utils.ts | 1 + packages/next/export/worker.ts | 5 ++- packages/next/package.json | 2 +- packages/next/server/config-utils.ts | 23 ++++++------ packages/next/server/config.ts | 12 +----- packages/next/server/next-server.ts | 8 +--- pnpm-lock.yaml | 21 +---------- test/e2e/styled-jsx/app/next.config.js | 3 -- test/e2e/styled-jsx/app/pages/amp.js | 12 ++++++ test/e2e/styled-jsx/app/pages/index.js | 2 +- test/e2e/styled-jsx/index.test.ts | 33 ++++++++++++++--- .../app/node_modules/my-comps/button.js | 37 +++++++++++++------ 13 files changed, 89 insertions(+), 71 deletions(-) delete mode 100644 test/e2e/styled-jsx/app/next.config.js create mode 100644 test/e2e/styled-jsx/app/pages/amp.js diff --git a/package.json b/package.json index 580bfac7d4f6..881653e3f2bc 100644 --- a/package.json +++ b/package.json @@ -191,7 +191,6 @@ "semver": "7.3.7", "shell-quote": "1.7.3", "styled-components": "5.3.3", - "styled-jsx": "5.0.2", "styled-jsx-plugin-postcss": "3.0.2", "tailwindcss": "1.1.3", "taskr": "1.1.0", diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index 95f8d413a000..bc65ff42fb10 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -1,6 +1,7 @@ import type { NextConfigComplete, ServerRuntime } from '../server/config-shared' import '../server/node-polyfill-fetch' +import '../build/webpack/require-hook' import chalk from 'next/dist/compiled/chalk' import getGzipSize from 'next/dist/compiled/gzip-size' import textTable from 'next/dist/compiled/text-table' diff --git a/packages/next/export/worker.ts b/packages/next/export/worker.ts index 08c3c14fcacd..803b3f3435d6 100644 --- a/packages/next/export/worker.ts +++ b/packages/next/export/worker.ts @@ -4,6 +4,10 @@ import type { GetStaticProps } from '../types' import type { IncomingMessage, ServerResponse } from 'http' import type { NextConfigComplete } from '../server/config-shared' import type { NextParsedUrlQuery } from '../server/request-meta' + +import '../server/node-polyfill-fetch' +import '../build/webpack/require-hook' + import url from 'url' import { extname, join, dirname, sep } from 'path' import { renderToHTML } from '../server/render' @@ -15,7 +19,6 @@ import { getRouteMatcher } from '../shared/lib/router/utils/route-matcher' import { getRouteRegex } from '../shared/lib/router/utils/route-regex' import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path' import { SERVER_PROPS_EXPORT_ERROR } from '../lib/constants' -import '../server/node-polyfill-fetch' import { requireFontManifest } from '../server/require' import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' import { trace } from '../trace' diff --git a/packages/next/package.json b/packages/next/package.json index 91b7bc8383e0..28f8955c54e0 100644 --- a/packages/next/package.json +++ b/packages/next/package.json @@ -74,7 +74,6 @@ "@swc/helpers": "0.4.3", "caniuse-lite": "^1.0.30001332", "postcss": "8.4.14", - "styled-jsx": "5.0.2", "use-sync-external-store": "1.2.0" }, "peerDependencies": { @@ -258,6 +257,7 @@ "string-hash": "1.1.3", "string_decoder": "1.3.0", "strip-ansi": "6.0.0", + "styled-jsx": "5.0.2", "tar": "6.1.11", "taskr": "1.1.0", "terser": "5.14.1", diff --git a/packages/next/server/config-utils.ts b/packages/next/server/config-utils.ts index 6d61b838597f..ff87e3e45c39 100644 --- a/packages/next/server/config-utils.ts +++ b/packages/next/server/config-utils.ts @@ -1,15 +1,16 @@ -export function createLoadRequireHook(callback?: () => void) { - let installed: boolean = false +import { init as initWebpack } from 'next/dist/compiled/webpack/webpack' - return () => { - if (installed) { - return - } - installed = true +let installed: boolean = false - callback?.() - - // load mapped modules - require('../build/webpack/require-hook') +export function loadWebpackHook() { + if (installed) { + return } + installed = true + + initWebpack() + + // hook the Node.js require so that webpack requires are + // routed to the bundled and now initialized webpack version + require('../build/webpack/require-hook') } diff --git a/packages/next/server/config.ts b/packages/next/server/config.ts index 8ecb5630c613..6371d93a9cec 100644 --- a/packages/next/server/config.ts +++ b/packages/next/server/config.ts @@ -3,7 +3,6 @@ import { pathToFileURL } from 'url' import { Agent as HttpAgent } from 'http' import { Agent as HttpsAgent } from 'https' import findUp from 'next/dist/compiled/find-up' -import { init as initWebpack } from 'next/dist/compiled/webpack/webpack' import chalk from '../lib/chalk' import * as Log from '../build/output/log' import { CONFIG_FILES, PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants' @@ -15,7 +14,7 @@ import { NextConfigComplete, validateConfig, } from './config-shared' -import { createLoadRequireHook } from './config-utils' +import { loadWebpackHook } from './config-utils' import { ImageConfig, imageConfigDefault, @@ -721,20 +720,13 @@ function assignDefaults(userConfig: { [key: string]: any }) { return result } -function loadWebpackHook() { - // hook the Node.js require so that webpack requires are - // routed to the bundled and now initialized webpack version - const initializeWebpackRequireHook = createLoadRequireHook(initWebpack) - initializeWebpackRequireHook() -} - export default async function loadConfig( phase: string, dir: string, customConfig?: object | null ): Promise { await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log) - await loadWebpackHook() + loadWebpackHook() let configFileName = 'next.config.js' diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 673e020ef01d..01f8bf070bf1 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -1,5 +1,6 @@ import './node-polyfill-fetch' import './node-polyfill-web-streams' +import '../build/webpack/require-hook' import type { TLSSocket } from 'tls' import type { Route } from './router' @@ -27,7 +28,6 @@ import fs from 'fs' import { join, relative, resolve, sep } from 'path' import { IncomingMessage, ServerResponse } from 'http' import { addRequestMeta, getRequestMeta } from './request-meta' -import { createLoadRequireHook } from './config-utils' import { PAGES_MANIFEST, BUILD_ID_FILE, @@ -86,12 +86,6 @@ import { checkIsManualRevalidate } from './api-utils' import { isDynamicRoute } from '../shared/lib/router/utils' import { shouldUseReactRoot } from './utils' -function loadRequireHook() { - return createLoadRequireHook() -} - -loadRequireHook() - if (shouldUseReactRoot) { ;(process.env as any).__NEXT_REACT_ROOT = 'true' } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f6247e2a2777..f771d955bd34 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -154,7 +154,6 @@ importers: semver: 7.3.7 shell-quote: 1.7.3 styled-components: 5.3.3 - styled-jsx: 5.0.2 styled-jsx-plugin-postcss: 3.0.2 tailwindcss: 1.1.3 taskr: 1.1.0 @@ -308,7 +307,6 @@ importers: semver: 7.3.7 shell-quote: 1.7.3 styled-components: 5.3.3_vtcxgy2wlmese7djxl6h7ok674 - styled-jsx: 5.0.2_uuaxwgga6hqycsez5ok7v2wg4i styled-jsx-plugin-postcss: 3.0.2 tailwindcss: 1.1.3 taskr: 1.1.0 @@ -586,7 +584,6 @@ importers: '@swc/helpers': 0.4.3 caniuse-lite: 1.0.30001332 postcss: 8.4.14 - styled-jsx: 5.0.2_@babel+core@7.18.0 use-sync-external-store: 1.2.0 devDependencies: '@ampproject/toolbox-optimizer': 2.8.3 @@ -751,6 +748,7 @@ importers: string_decoder: 1.3.0 string-hash: 1.1.3 strip-ansi: 6.0.0 + styled-jsx: 5.0.2_@babel+core@7.18.0 tar: 6.1.11 taskr: 1.1.0 terser: 5.14.1 @@ -20563,23 +20561,6 @@ packages: optional: true dependencies: '@babel/core': 7.18.0 - dev: false - - /styled-jsx/5.0.2_uuaxwgga6hqycsez5ok7v2wg4i: - resolution: {integrity: sha512-LqPQrbBh3egD57NBcHET4qcgshPks+yblyhPlH2GY8oaDgKs8SK4C3dBh3oSJjgzJ3G5t1SYEZGHkP+QEpX9EQ==} - engines: {node: '>= 12.0.0'} - peerDependencies: - '@babel/core': '*' - babel-plugin-macros: '*' - react: '>= 16.8.0 || 17.x.x || ^18.0.0-0' - peerDependenciesMeta: - '@babel/core': - optional: true - babel-plugin-macros: - optional: true - dependencies: - '@babel/core': 7.18.0 - react: 18.2.0 dev: true /stylehacks/4.0.3: diff --git a/test/e2e/styled-jsx/app/next.config.js b/test/e2e/styled-jsx/app/next.config.js deleted file mode 100644 index e97173b4b379..000000000000 --- a/test/e2e/styled-jsx/app/next.config.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - output: 'standalone', -} diff --git a/test/e2e/styled-jsx/app/pages/amp.js b/test/e2e/styled-jsx/app/pages/amp.js new file mode 100644 index 000000000000..8f71fdbc5ce3 --- /dev/null +++ b/test/e2e/styled-jsx/app/pages/amp.js @@ -0,0 +1,12 @@ +import { Button } from 'my-comps/button' + +export const config = { amp: true } + +export default function page() { + return ( + <> +

Hello world

+ + + ) +} diff --git a/test/e2e/styled-jsx/app/pages/index.js b/test/e2e/styled-jsx/app/pages/index.js index f59f1e3c318c..378661135fc9 100644 --- a/test/e2e/styled-jsx/app/pages/index.js +++ b/test/e2e/styled-jsx/app/pages/index.js @@ -1,4 +1,4 @@ -import { Button } from '../node_modules_bak/my-comps/button' +import { Button } from 'my-comps/button' export default function Page() { return ( diff --git a/test/e2e/styled-jsx/index.test.ts b/test/e2e/styled-jsx/index.test.ts index 6b4682d3694e..295e22ad6f0e 100644 --- a/test/e2e/styled-jsx/index.test.ts +++ b/test/e2e/styled-jsx/index.test.ts @@ -2,6 +2,7 @@ import path from 'path' import { createNext, FileRef } from 'e2e-utils' import { NextInstance } from 'test/lib/next-modes/base' import { renderViaHTTP } from 'next-test-utils' +import webdriver from 'next-webdriver' const appDir = path.join(__dirname, 'app') @@ -16,7 +17,6 @@ function runTest(packageManager?: string) { files: { node_modules_bak: new FileRef(path.join(appDir, 'node_modules_bak')), pages: new FileRef(path.join(appDir, 'pages')), - 'next.config.js': new FileRef(path.join(appDir, 'next.config.js')), '.npmrc': new FileRef(path.join(appDir, '.npmrc')), }, packageJson: { @@ -41,13 +41,36 @@ function runTest(packageManager?: string) { }) afterAll(() => next.destroy()) - it('should contain styled-jsx styles in html', async () => { + it('should contain styled-jsx styles during SSR', async () => { const html = await renderViaHTTP(next.url, '/') - expect(html).toMatch(/color:(\s)*red/) - expect(html).toMatch(/color:(\s)*cyan/) + expect(html).toMatch(/color:.*?red/) + expect(html).toMatch(/color:.*?cyan/) + }) + + it('should render styles during CSR', async () => { + const browser = await webdriver(next.url, '/') + const color = await browser.eval( + `getComputedStyle(document.querySelector('button')).color` + ) + + expect(color).toMatch('0, 255, 255') + }) + + it('should render styles during CSR (AMP)', async () => { + const browser = await webdriver(next.url, '/amp') + const color = await browser.eval( + `getComputedStyle(document.querySelector('button')).color` + ) + + expect(color).toMatch('0, 255, 255') + }) + + it('should render styles during SSR (AMP)', async () => { + const html = await renderViaHTTP(next.url, '/amp') + expect(html).toMatch(/color:.*?cyan/) }) }) } -runTest() +// runTest() runTest('pnpm') diff --git a/test/integration/styled-jsx-module/app/node_modules/my-comps/button.js b/test/integration/styled-jsx-module/app/node_modules/my-comps/button.js index 7176c6258a9d..b52364b1c12c 100644 --- a/test/integration/styled-jsx-module/app/node_modules/my-comps/button.js +++ b/test/integration/styled-jsx-module/app/node_modules/my-comps/button.js @@ -1,22 +1,37 @@ -'use strict'; +'use strict' -Object.defineProperty(exports, '__esModule', { value: true }); +Object.defineProperty(exports, '__esModule', { value: true }) -function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } +function _interopDefault(ex) { + return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex +} + +var _JSXStyle = _interopDefault(require('styled-jsx/style')) +var React = require('react') -var _JSXStyle = _interopDefault(require('styled-jsx/style')); -var React = require('react'); +if (typeof window === 'undefined') { + console.log('rr', require.resolve('styled-jsx')) +} function Button() { - return React.createElement("button", { - className: "jsx-451104437" - }, "hello", React.createElement(_JSXStyle, { - id: "451104437" - }, ".jsx-451104437{color:cyan;}\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkJ1dHRvbi50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBTWtCLEFBRW1CLFVBQ1oiLCJmaWxlIjoiQnV0dG9uLnRzeCIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCAqIGFzIFJlYWN0IGZyb20gJ3JlYWN0JztcblxuZXhwb3J0IGRlZmF1bHQgZnVuY3Rpb24gQnV0dG9uKCkge1xuICByZXR1cm4gKFxuICAgIDxidXR0b24+XG4gICAgICBoZWxsb1xuICAgICAgPHN0eWxlIGpzeD57YFxuICAgICAgICBjb2xybzogcmVkO1xuICAgICAgYH08L3N0eWxlPlxuICAgIDwvYnV0dG9uPlxuICApO1xufVxuIl19 */\n/*@ sourceURL=Button.tsx */")); + return React.createElement( + 'button', + { + className: 'jsx-451104437', + }, + 'hello', + React.createElement( + _JSXStyle, + { + id: '451104437', + }, + '.jsx-451104437{color:cyan;}\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkJ1dHRvbi50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBTWtCLEFBRW1CLFVBQ1oiLCJmaWxlIjoiQnV0dG9uLnRzeCIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCAqIGFzIFJlYWN0IGZyb20gJ3JlYWN0JztcblxuZXhwb3J0IGRlZmF1bHQgZnVuY3Rpb24gQnV0dG9uKCkge1xuICByZXR1cm4gKFxuICAgIDxidXR0b24+XG4gICAgICBoZWxsb1xuICAgICAgPHN0eWxlIGpzeD57YFxuICAgICAgICBjb2xybzogcmVkO1xuICAgICAgYH08L3N0eWxlPlxuICAgIDwvYnV0dG9uPlxuICApO1xufVxuIl19 */\n/*@ sourceURL=Button.tsx */' + ) + ) } /** * @class ExampleComponent */ -exports.Button = Button; \ No newline at end of file +exports.Button = Button From 96e2811052b3c142a2c86bac0a193485ad3bd5c6 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 8 Aug 2022 22:26:58 +0200 Subject: [PATCH 14/20] rm legacy test --- .../app/node_modules/my-comps/button.js | 37 ---------- .../app/node_modules/my-comps/package.json | 6 -- .../styled-jsx-module/app/package.json | 6 -- .../styled-jsx-module/app/pages/amp.js | 12 ---- .../styled-jsx-module/app/pages/index.js | 10 --- .../styled-jsx-module/test/index.test.js | 69 ------------------- 6 files changed, 140 deletions(-) delete mode 100644 test/integration/styled-jsx-module/app/node_modules/my-comps/button.js delete mode 100644 test/integration/styled-jsx-module/app/node_modules/my-comps/package.json delete mode 100644 test/integration/styled-jsx-module/app/package.json delete mode 100644 test/integration/styled-jsx-module/app/pages/amp.js delete mode 100644 test/integration/styled-jsx-module/app/pages/index.js delete mode 100644 test/integration/styled-jsx-module/test/index.test.js diff --git a/test/integration/styled-jsx-module/app/node_modules/my-comps/button.js b/test/integration/styled-jsx-module/app/node_modules/my-comps/button.js deleted file mode 100644 index b52364b1c12c..000000000000 --- a/test/integration/styled-jsx-module/app/node_modules/my-comps/button.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict' - -Object.defineProperty(exports, '__esModule', { value: true }) - -function _interopDefault(ex) { - return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex -} - -var _JSXStyle = _interopDefault(require('styled-jsx/style')) -var React = require('react') - -if (typeof window === 'undefined') { - console.log('rr', require.resolve('styled-jsx')) -} - -function Button() { - return React.createElement( - 'button', - { - className: 'jsx-451104437', - }, - 'hello', - React.createElement( - _JSXStyle, - { - id: '451104437', - }, - '.jsx-451104437{color:cyan;}\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkJ1dHRvbi50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBTWtCLEFBRW1CLFVBQ1oiLCJmaWxlIjoiQnV0dG9uLnRzeCIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCAqIGFzIFJlYWN0IGZyb20gJ3JlYWN0JztcblxuZXhwb3J0IGRlZmF1bHQgZnVuY3Rpb24gQnV0dG9uKCkge1xuICByZXR1cm4gKFxuICAgIDxidXR0b24+XG4gICAgICBoZWxsb1xuICAgICAgPHN0eWxlIGpzeD57YFxuICAgICAgICBjb2xybzogcmVkO1xuICAgICAgYH08L3N0eWxlPlxuICAgIDwvYnV0dG9uPlxuICApO1xufVxuIl19 */\n/*@ sourceURL=Button.tsx */' - ) - ) -} - -/** - * @class ExampleComponent - */ - -exports.Button = Button diff --git a/test/integration/styled-jsx-module/app/node_modules/my-comps/package.json b/test/integration/styled-jsx-module/app/node_modules/my-comps/package.json deleted file mode 100644 index c0952cffa5d6..000000000000 --- a/test/integration/styled-jsx-module/app/node_modules/my-comps/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "my-comps", - "version": "1.0.0", - "main": "index.js", - "license": "MIT" -} diff --git a/test/integration/styled-jsx-module/app/package.json b/test/integration/styled-jsx-module/app/package.json deleted file mode 100644 index b9506bee3387..000000000000 --- a/test/integration/styled-jsx-module/app/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "styled-jsx-module", - "version": "1.0.0", - "main": "index.js", - "license": "MIT" -} diff --git a/test/integration/styled-jsx-module/app/pages/amp.js b/test/integration/styled-jsx-module/app/pages/amp.js deleted file mode 100644 index 5e3645d14252..000000000000 --- a/test/integration/styled-jsx-module/app/pages/amp.js +++ /dev/null @@ -1,12 +0,0 @@ -import { Button } from 'my-comps/button' - -export const config = { amp: true } - -const Index = () => ( - <> -

Hello world

- - -) - -export default Index diff --git a/test/integration/styled-jsx-module/app/pages/index.js b/test/integration/styled-jsx-module/app/pages/index.js deleted file mode 100644 index 3ebb08835bd2..000000000000 --- a/test/integration/styled-jsx-module/app/pages/index.js +++ /dev/null @@ -1,10 +0,0 @@ -import { Button } from 'my-comps/button' - -const Index = () => ( - <> -

Hello world

- - -) - -export default Index diff --git a/test/integration/styled-jsx-module/test/index.test.js b/test/integration/styled-jsx-module/test/index.test.js deleted file mode 100644 index 3bad07cb13c4..000000000000 --- a/test/integration/styled-jsx-module/test/index.test.js +++ /dev/null @@ -1,69 +0,0 @@ -/* eslint-env jest */ - -import { join } from 'path' -import webdriver from 'next-webdriver' -import { - killApp, - findPort, - nextStart, - nextBuild, - launchApp, - renderViaHTTP, -} from 'next-test-utils' - -const appDir = join(__dirname, '../app') -let appPort -let app - -function runTests() { - it('should render styles during CSR', async () => { - const browser = await webdriver(appPort, '/') - const color = await browser.eval( - `getComputedStyle(document.querySelector('button')).color` - ) - - expect(color).toMatch('0, 255, 255') - }) - - it('should render styles during CSR (AMP)', async () => { - const browser = await webdriver(appPort, '/amp') - const color = await browser.eval( - `getComputedStyle(document.querySelector('button')).color` - ) - - expect(color).toMatch('0, 255, 255') - }) - - it('should render styles during SSR', async () => { - const html = await renderViaHTTP(appPort, '/') - expect(html).toMatch(/color:.*?cyan/) - }) - - it('should render styles during SSR (AMP)', async () => { - const html = await renderViaHTTP(appPort, '/amp') - expect(html).toMatch(/color:.*?cyan/) - }) -} - -describe('styled-jsx using in node_modules', () => { - describe('Production', () => { - beforeAll(async () => { - await nextBuild(appDir) - appPort = await findPort() - app = await nextStart(appDir, appPort) - }) - afterAll(() => killApp(app)) - - runTests() - }) - - describe('Development', () => { - beforeAll(async () => { - appPort = await findPort() - app = await launchApp(appDir, appPort) - }) - afterAll(() => killApp(app)) - - runTests() - }) -}) From e5f25ceb5e94d7493299fcb32780f1e98a1c086e Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 8 Aug 2022 22:36:47 +0200 Subject: [PATCH 15/20] fix lint --- packages/next/build/babel/preset.ts | 4 ++-- packages/next/shared/lib/styled-jsx/index.d.ts | 1 - packages/next/shared/lib/styled-jsx/index.js | 1 - packages/next/shared/lib/styled-jsx/style.d.ts | 1 - packages/next/shared/lib/styled-jsx/style.js | 1 - 5 files changed, 2 insertions(+), 6 deletions(-) delete mode 100644 packages/next/shared/lib/styled-jsx/index.d.ts delete mode 100644 packages/next/shared/lib/styled-jsx/index.js delete mode 100644 packages/next/shared/lib/styled-jsx/style.d.ts delete mode 100644 packages/next/shared/lib/styled-jsx/style.js diff --git a/packages/next/build/babel/preset.ts b/packages/next/build/babel/preset.ts index 5ea2a647a5e9..a0fb85846dab 100644 --- a/packages/next/build/babel/preset.ts +++ b/packages/next/build/babel/preset.ts @@ -185,8 +185,8 @@ export default ( ], [ isTest && options['styled-jsx'] && options['styled-jsx']['babel-test'] - ? require('styled-jsx/babel-test') - : require('styled-jsx/babel'), + ? require('next/dist/styled-jsx/babel-test') + : require('next/dist/styled-jsx/babel'), styledJsxOptions(options['styled-jsx']), ], require('./plugins/amp-attributes'), diff --git a/packages/next/shared/lib/styled-jsx/index.d.ts b/packages/next/shared/lib/styled-jsx/index.d.ts deleted file mode 100644 index 3ee1cf7cf4b4..000000000000 --- a/packages/next/shared/lib/styled-jsx/index.d.ts +++ /dev/null @@ -1 +0,0 @@ -export * from 'styled-jsx' diff --git a/packages/next/shared/lib/styled-jsx/index.js b/packages/next/shared/lib/styled-jsx/index.js deleted file mode 100644 index a28adad1cbf5..000000000000 --- a/packages/next/shared/lib/styled-jsx/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('styled-jsx') diff --git a/packages/next/shared/lib/styled-jsx/style.d.ts b/packages/next/shared/lib/styled-jsx/style.d.ts deleted file mode 100644 index 716fb641fed5..000000000000 --- a/packages/next/shared/lib/styled-jsx/style.d.ts +++ /dev/null @@ -1 +0,0 @@ -export default function JSXStyle(props: any): null diff --git a/packages/next/shared/lib/styled-jsx/style.js b/packages/next/shared/lib/styled-jsx/style.js deleted file mode 100644 index a349928dce20..000000000000 --- a/packages/next/shared/lib/styled-jsx/style.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('styled-jsx/style') From c02e0e06ae4e839ba97f2a8fed1ce15b17a53e43 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 8 Aug 2022 22:49:49 +0200 Subject: [PATCH 16/20] fix pnpm install error --- test/e2e/styled-jsx/app/.npmrc | 1 + test/e2e/styled-jsx/index.test.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/e2e/styled-jsx/app/.npmrc b/test/e2e/styled-jsx/app/.npmrc index 3e775efb0f40..4c2f52b3be71 100644 --- a/test/e2e/styled-jsx/app/.npmrc +++ b/test/e2e/styled-jsx/app/.npmrc @@ -1 +1,2 @@ auto-install-peers=true +strict-peer-dependencies=false diff --git a/test/e2e/styled-jsx/index.test.ts b/test/e2e/styled-jsx/index.test.ts index 295e22ad6f0e..beef99342927 100644 --- a/test/e2e/styled-jsx/index.test.ts +++ b/test/e2e/styled-jsx/index.test.ts @@ -28,7 +28,7 @@ function runTest(packageManager?: string) { }, }, dependencies: { - 'styled-jsx': '4.0.0', // incompatible styled-jsx on user side + 'styled-jsx': '5.0.0', // styled-jsx on user side }, startCommand: 'yarn ' + ((global as any).isNextDev ? 'dev' : 'start'), buildCommand: `yarn build`, From efe7832033a00ac1ef6cdb626e0549286ca585f0 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 9 Aug 2022 00:04:54 +0200 Subject: [PATCH 17/20] split require hook --- packages/next/build/utils.ts | 4 +- packages/next/build/webpack/require-hook.ts | 162 +++----------------- packages/next/export/worker.ts | 3 +- packages/next/server/config-utils.ts | 125 ++++++++++++++- packages/next/server/next-server.ts | 4 +- 5 files changed, 152 insertions(+), 146 deletions(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index bc65ff42fb10..638088ff7f0d 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -1,7 +1,7 @@ import type { NextConfigComplete, ServerRuntime } from '../server/config-shared' import '../server/node-polyfill-fetch' -import '../build/webpack/require-hook' +import loadRequireHook from '../build/webpack/require-hook' import chalk from 'next/dist/compiled/chalk' import getGzipSize from 'next/dist/compiled/gzip-size' import textTable from 'next/dist/compiled/text-table' @@ -60,6 +60,8 @@ const fsStat = (file: string) => { return (fileStats[file] = fileSize(file)) } +loadRequireHook() + export interface PageInfo { isHybridAmp?: boolean size: number diff --git a/packages/next/build/webpack/require-hook.ts b/packages/next/build/webpack/require-hook.ts index 60c72ebd613a..b3fa1a638956 100644 --- a/packages/next/build/webpack/require-hook.ts +++ b/packages/next/build/webpack/require-hook.ts @@ -2,147 +2,25 @@ // this is in order for userland plugins to attach to the same webpack instance as next.js // the individual compiled modules are as defined for the compilation in bundles/webpack/packages/* -const hookPropertyMap = new Map( - [ - ['webpack', 'next/dist/compiled/webpack/webpack-lib'], - ['webpack/package', 'next/dist/compiled/webpack/package'], - ['webpack/package.json', 'next/dist/compiled/webpack/package'], - ['webpack/lib/webpack', 'next/dist/compiled/webpack/webpack-lib'], - ['webpack/lib/webpack.js', 'next/dist/compiled/webpack/webpack-lib'], - [ - 'webpack/lib/node/NodeEnvironmentPlugin', - 'next/dist/compiled/webpack/NodeEnvironmentPlugin', - ], - [ - 'webpack/lib/node/NodeEnvironmentPlugin.js', - 'next/dist/compiled/webpack/NodeEnvironmentPlugin', - ], - [ - 'webpack/lib/BasicEvaluatedExpression', - 'next/dist/compiled/webpack/BasicEvaluatedExpression', - ], - [ - 'webpack/lib/BasicEvaluatedExpression.js', - 'next/dist/compiled/webpack/BasicEvaluatedExpression', - ], - [ - 'webpack/lib/node/NodeTargetPlugin', - 'next/dist/compiled/webpack/NodeTargetPlugin', - ], - [ - 'webpack/lib/node/NodeTargetPlugin.js', - 'next/dist/compiled/webpack/NodeTargetPlugin', - ], - [ - 'webpack/lib/node/NodeTemplatePlugin', - 'next/dist/compiled/webpack/NodeTemplatePlugin', - ], - [ - 'webpack/lib/node/NodeTemplatePlugin.js', - 'next/dist/compiled/webpack/NodeTemplatePlugin', - ], - [ - 'webpack/lib/LibraryTemplatePlugin', - 'next/dist/compiled/webpack/LibraryTemplatePlugin', - ], - [ - 'webpack/lib/LibraryTemplatePlugin.js', - 'next/dist/compiled/webpack/LibraryTemplatePlugin', - ], - [ - 'webpack/lib/SingleEntryPlugin', - 'next/dist/compiled/webpack/SingleEntryPlugin', - ], - [ - 'webpack/lib/SingleEntryPlugin.js', - 'next/dist/compiled/webpack/SingleEntryPlugin', - ], - [ - 'webpack/lib/optimize/LimitChunkCountPlugin', - 'next/dist/compiled/webpack/LimitChunkCountPlugin', - ], - [ - 'webpack/lib/optimize/LimitChunkCountPlugin.js', - 'next/dist/compiled/webpack/LimitChunkCountPlugin', - ], - [ - 'webpack/lib/webworker/WebWorkerTemplatePlugin', - 'next/dist/compiled/webpack/WebWorkerTemplatePlugin', - ], - [ - 'webpack/lib/webworker/WebWorkerTemplatePlugin.js', - 'next/dist/compiled/webpack/WebWorkerTemplatePlugin', - ], - [ - 'webpack/lib/ExternalsPlugin', - 'next/dist/compiled/webpack/ExternalsPlugin', - ], - [ - 'webpack/lib/ExternalsPlugin.js', - 'next/dist/compiled/webpack/ExternalsPlugin', - ], - [ - 'webpack/lib/web/FetchCompileWasmTemplatePlugin', - 'next/dist/compiled/webpack/FetchCompileWasmTemplatePlugin', - ], - [ - 'webpack/lib/web/FetchCompileWasmTemplatePlugin.js', - 'next/dist/compiled/webpack/FetchCompileWasmTemplatePlugin', - ], - [ - 'webpack/lib/web/FetchCompileWasmPlugin', - 'next/dist/compiled/webpack/FetchCompileWasmPlugin', - ], - [ - 'webpack/lib/web/FetchCompileWasmPlugin.js', - 'next/dist/compiled/webpack/FetchCompileWasmPlugin', - ], - [ - 'webpack/lib/web/FetchCompileAsyncWasmPlugin', - 'next/dist/compiled/webpack/FetchCompileAsyncWasmPlugin', - ], - [ - 'webpack/lib/web/FetchCompileAsyncWasmPlugin.js', - 'next/dist/compiled/webpack/FetchCompileAsyncWasmPlugin', - ], - [ - 'webpack/lib/ModuleFilenameHelpers', - 'next/dist/compiled/webpack/ModuleFilenameHelpers', - ], - [ - 'webpack/lib/ModuleFilenameHelpers.js', - 'next/dist/compiled/webpack/ModuleFilenameHelpers', - ], - ['webpack/lib/GraphHelpers', 'next/dist/compiled/webpack/GraphHelpers'], - ['webpack/lib/GraphHelpers.js', 'next/dist/compiled/webpack/GraphHelpers'], - ['webpack/lib/NormalModule', 'next/dist/compiled/webpack/NormalModule'], - ['webpack-sources', 'next/dist/compiled/webpack/sources'], - ['webpack-sources/lib', 'next/dist/compiled/webpack/sources'], - ['webpack-sources/lib/index', 'next/dist/compiled/webpack/sources'], - ['webpack-sources/lib/index.js', 'next/dist/compiled/webpack/sources'], - ['@babel/runtime', 'next/dist/compiled/@babel/runtime/package.json'], - [ - '@babel/runtime/package.json', - 'next/dist/compiled/@babel/runtime/package.json', - ], - ['node-fetch', 'next/dist/compiled/node-fetch'], - ['styled-jsx', 'next/dist/styled-jsx'], - ['styled-jsx/style', 'next/dist/styled-jsx/style'], - ].map(([request, replacement]) => [request, require.resolve(replacement)]) -) +export default function loadRequireHook(aliases: [string, string][] = []) { + const hookPropertyMap = new Map( + [ + ...aliases, + ['styled-jsx', 'next/dist/styled-jsx'], + ['styled-jsx/style', 'next/dist/styled-jsx/style'], + ].map(([request, replacement]) => [request, require.resolve(replacement)]) + ) -const mod = require('module') -const resolveFilename = mod._resolveFilename -mod._resolveFilename = function ( - request: string, - parent: any, - isMain: boolean, - options: any -) { - const hookResolved = hookPropertyMap.get(request) - if (hookResolved) request = hookResolved - return resolveFilename.call(mod, request, parent, isMain, options) + const mod = require('module') + const resolveFilename = mod._resolveFilename + mod._resolveFilename = function ( + request: string, + parent: any, + isMain: boolean, + options: any + ) { + const hookResolved = hookPropertyMap.get(request) + if (hookResolved) request = hookResolved + return resolveFilename.call(mod, request, parent, isMain, options) + } } - -// Flag as module for typescript -export {} diff --git a/packages/next/export/worker.ts b/packages/next/export/worker.ts index 803b3f3435d6..a23262711fec 100644 --- a/packages/next/export/worker.ts +++ b/packages/next/export/worker.ts @@ -6,7 +6,7 @@ import type { NextConfigComplete } from '../server/config-shared' import type { NextParsedUrlQuery } from '../server/request-meta' import '../server/node-polyfill-fetch' -import '../build/webpack/require-hook' +import loadRequireHook from '../build/webpack/require-hook' import url from 'url' import { extname, join, dirname, sep } from 'path' @@ -27,6 +27,7 @@ import { setHttpAgentOptions } from '../server/config' import RenderResult from '../server/render-result' import isError from '../lib/is-error' +loadRequireHook() const envConfig = require('../shared/lib/runtime-config') ;(global as any).__NEXT_DATA__ = { diff --git a/packages/next/server/config-utils.ts b/packages/next/server/config-utils.ts index ff87e3e45c39..4e83b432fab6 100644 --- a/packages/next/server/config-utils.ts +++ b/packages/next/server/config-utils.ts @@ -12,5 +12,128 @@ export function loadWebpackHook() { // hook the Node.js require so that webpack requires are // routed to the bundled and now initialized webpack version - require('../build/webpack/require-hook') + require('../build/webpack/require-hook').default([ + ['webpack', 'next/dist/compiled/webpack/webpack-lib'], + ['webpack/package', 'next/dist/compiled/webpack/package'], + ['webpack/package.json', 'next/dist/compiled/webpack/package'], + ['webpack/lib/webpack', 'next/dist/compiled/webpack/webpack-lib'], + ['webpack/lib/webpack.js', 'next/dist/compiled/webpack/webpack-lib'], + [ + 'webpack/lib/node/NodeEnvironmentPlugin', + 'next/dist/compiled/webpack/NodeEnvironmentPlugin', + ], + [ + 'webpack/lib/node/NodeEnvironmentPlugin.js', + 'next/dist/compiled/webpack/NodeEnvironmentPlugin', + ], + [ + 'webpack/lib/BasicEvaluatedExpression', + 'next/dist/compiled/webpack/BasicEvaluatedExpression', + ], + [ + 'webpack/lib/BasicEvaluatedExpression.js', + 'next/dist/compiled/webpack/BasicEvaluatedExpression', + ], + [ + 'webpack/lib/node/NodeTargetPlugin', + 'next/dist/compiled/webpack/NodeTargetPlugin', + ], + [ + 'webpack/lib/node/NodeTargetPlugin.js', + 'next/dist/compiled/webpack/NodeTargetPlugin', + ], + [ + 'webpack/lib/node/NodeTemplatePlugin', + 'next/dist/compiled/webpack/NodeTemplatePlugin', + ], + [ + 'webpack/lib/node/NodeTemplatePlugin.js', + 'next/dist/compiled/webpack/NodeTemplatePlugin', + ], + [ + 'webpack/lib/LibraryTemplatePlugin', + 'next/dist/compiled/webpack/LibraryTemplatePlugin', + ], + [ + 'webpack/lib/LibraryTemplatePlugin.js', + 'next/dist/compiled/webpack/LibraryTemplatePlugin', + ], + [ + 'webpack/lib/SingleEntryPlugin', + 'next/dist/compiled/webpack/SingleEntryPlugin', + ], + [ + 'webpack/lib/SingleEntryPlugin.js', + 'next/dist/compiled/webpack/SingleEntryPlugin', + ], + [ + 'webpack/lib/optimize/LimitChunkCountPlugin', + 'next/dist/compiled/webpack/LimitChunkCountPlugin', + ], + [ + 'webpack/lib/optimize/LimitChunkCountPlugin.js', + 'next/dist/compiled/webpack/LimitChunkCountPlugin', + ], + [ + 'webpack/lib/webworker/WebWorkerTemplatePlugin', + 'next/dist/compiled/webpack/WebWorkerTemplatePlugin', + ], + [ + 'webpack/lib/webworker/WebWorkerTemplatePlugin.js', + 'next/dist/compiled/webpack/WebWorkerTemplatePlugin', + ], + [ + 'webpack/lib/ExternalsPlugin', + 'next/dist/compiled/webpack/ExternalsPlugin', + ], + [ + 'webpack/lib/ExternalsPlugin.js', + 'next/dist/compiled/webpack/ExternalsPlugin', + ], + [ + 'webpack/lib/web/FetchCompileWasmTemplatePlugin', + 'next/dist/compiled/webpack/FetchCompileWasmTemplatePlugin', + ], + [ + 'webpack/lib/web/FetchCompileWasmTemplatePlugin.js', + 'next/dist/compiled/webpack/FetchCompileWasmTemplatePlugin', + ], + [ + 'webpack/lib/web/FetchCompileWasmPlugin', + 'next/dist/compiled/webpack/FetchCompileWasmPlugin', + ], + [ + 'webpack/lib/web/FetchCompileWasmPlugin.js', + 'next/dist/compiled/webpack/FetchCompileWasmPlugin', + ], + [ + 'webpack/lib/web/FetchCompileAsyncWasmPlugin', + 'next/dist/compiled/webpack/FetchCompileAsyncWasmPlugin', + ], + [ + 'webpack/lib/web/FetchCompileAsyncWasmPlugin.js', + 'next/dist/compiled/webpack/FetchCompileAsyncWasmPlugin', + ], + [ + 'webpack/lib/ModuleFilenameHelpers', + 'next/dist/compiled/webpack/ModuleFilenameHelpers', + ], + [ + 'webpack/lib/ModuleFilenameHelpers.js', + 'next/dist/compiled/webpack/ModuleFilenameHelpers', + ], + ['webpack/lib/GraphHelpers', 'next/dist/compiled/webpack/GraphHelpers'], + ['webpack/lib/GraphHelpers.js', 'next/dist/compiled/webpack/GraphHelpers'], + ['webpack/lib/NormalModule', 'next/dist/compiled/webpack/NormalModule'], + ['webpack-sources', 'next/dist/compiled/webpack/sources'], + ['webpack-sources/lib', 'next/dist/compiled/webpack/sources'], + ['webpack-sources/lib/index', 'next/dist/compiled/webpack/sources'], + ['webpack-sources/lib/index.js', 'next/dist/compiled/webpack/sources'], + ['@babel/runtime', 'next/dist/compiled/@babel/runtime/package.json'], + [ + '@babel/runtime/package.json', + 'next/dist/compiled/@babel/runtime/package.json', + ], + ['node-fetch', 'next/dist/compiled/node-fetch'], + ]) } diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 01f8bf070bf1..209e93d35890 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -1,6 +1,5 @@ import './node-polyfill-fetch' import './node-polyfill-web-streams' -import '../build/webpack/require-hook' import type { TLSSocket } from 'tls' import type { Route } from './router' @@ -56,6 +55,7 @@ import { RenderOpts, renderToHTML } from './render' import { renderToHTMLOrFlight as appRenderToHTMLOrFlight } from './app-render' import { ParsedUrl, parseUrl } from '../shared/lib/router/utils/parse-url' import * as Log from '../build/output/log' +import loadRequireHook from '../build/webpack/require-hook' import BaseServer, { Options, @@ -90,6 +90,8 @@ if (shouldUseReactRoot) { ;(process.env as any).__NEXT_REACT_ROOT = 'true' } +loadRequireHook() + export * from './base-server' type ExpressMiddleware = ( From b20dd63b4194590aed59d934004c4833ec00b841 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 9 Aug 2022 00:18:18 +0200 Subject: [PATCH 18/20] only alias styled-jsx --- packages/next/build/webpack/require-hook.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/next/build/webpack/require-hook.ts b/packages/next/build/webpack/require-hook.ts index b3fa1a638956..d8771be55b8d 100644 --- a/packages/next/build/webpack/require-hook.ts +++ b/packages/next/build/webpack/require-hook.ts @@ -4,11 +4,9 @@ export default function loadRequireHook(aliases: [string, string][] = []) { const hookPropertyMap = new Map( - [ - ...aliases, - ['styled-jsx', 'next/dist/styled-jsx'], - ['styled-jsx/style', 'next/dist/styled-jsx/style'], - ].map(([request, replacement]) => [request, require.resolve(replacement)]) + [...aliases, ['styled-jsx', 'next/dist/styled-jsx']].map( + ([request, replacement]) => [request, require.resolve(replacement)] + ) ) const mod = require('module') From e857da6d992c17e553477bf8218abc751908cbaf Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 9 Aug 2022 00:30:08 +0200 Subject: [PATCH 19/20] make styled-jsx resolving statically analyzable --- packages/next/build/webpack/require-hook.ts | 9 +- packages/next/server/config-utils.ts | 256 ++++++++++---------- test/e2e/styled-jsx/index.test.ts | 2 +- 3 files changed, 139 insertions(+), 128 deletions(-) diff --git a/packages/next/build/webpack/require-hook.ts b/packages/next/build/webpack/require-hook.ts index d8771be55b8d..74c2aa128de3 100644 --- a/packages/next/build/webpack/require-hook.ts +++ b/packages/next/build/webpack/require-hook.ts @@ -4,9 +4,12 @@ export default function loadRequireHook(aliases: [string, string][] = []) { const hookPropertyMap = new Map( - [...aliases, ['styled-jsx', 'next/dist/styled-jsx']].map( - ([request, replacement]) => [request, require.resolve(replacement)] - ) + [ + ...aliases, + // Use `require.resolve` explicitly to make them statically analyzable + ['styled-jsx', require.resolve('next/dist/styled-jsx')], + ['styled-jsx/style', require.resolve('next/dist/styled-jsx/style')], + ].map(([request, replacement]) => [request, replacement]) ) const mod = require('module') diff --git a/packages/next/server/config-utils.ts b/packages/next/server/config-utils.ts index 4e83b432fab6..90e763f7eb82 100644 --- a/packages/next/server/config-utils.ts +++ b/packages/next/server/config-utils.ts @@ -12,128 +12,136 @@ export function loadWebpackHook() { // hook the Node.js require so that webpack requires are // routed to the bundled and now initialized webpack version - require('../build/webpack/require-hook').default([ - ['webpack', 'next/dist/compiled/webpack/webpack-lib'], - ['webpack/package', 'next/dist/compiled/webpack/package'], - ['webpack/package.json', 'next/dist/compiled/webpack/package'], - ['webpack/lib/webpack', 'next/dist/compiled/webpack/webpack-lib'], - ['webpack/lib/webpack.js', 'next/dist/compiled/webpack/webpack-lib'], - [ - 'webpack/lib/node/NodeEnvironmentPlugin', - 'next/dist/compiled/webpack/NodeEnvironmentPlugin', - ], - [ - 'webpack/lib/node/NodeEnvironmentPlugin.js', - 'next/dist/compiled/webpack/NodeEnvironmentPlugin', - ], - [ - 'webpack/lib/BasicEvaluatedExpression', - 'next/dist/compiled/webpack/BasicEvaluatedExpression', - ], - [ - 'webpack/lib/BasicEvaluatedExpression.js', - 'next/dist/compiled/webpack/BasicEvaluatedExpression', - ], - [ - 'webpack/lib/node/NodeTargetPlugin', - 'next/dist/compiled/webpack/NodeTargetPlugin', - ], - [ - 'webpack/lib/node/NodeTargetPlugin.js', - 'next/dist/compiled/webpack/NodeTargetPlugin', - ], - [ - 'webpack/lib/node/NodeTemplatePlugin', - 'next/dist/compiled/webpack/NodeTemplatePlugin', - ], - [ - 'webpack/lib/node/NodeTemplatePlugin.js', - 'next/dist/compiled/webpack/NodeTemplatePlugin', - ], - [ - 'webpack/lib/LibraryTemplatePlugin', - 'next/dist/compiled/webpack/LibraryTemplatePlugin', - ], - [ - 'webpack/lib/LibraryTemplatePlugin.js', - 'next/dist/compiled/webpack/LibraryTemplatePlugin', - ], - [ - 'webpack/lib/SingleEntryPlugin', - 'next/dist/compiled/webpack/SingleEntryPlugin', - ], - [ - 'webpack/lib/SingleEntryPlugin.js', - 'next/dist/compiled/webpack/SingleEntryPlugin', - ], - [ - 'webpack/lib/optimize/LimitChunkCountPlugin', - 'next/dist/compiled/webpack/LimitChunkCountPlugin', - ], - [ - 'webpack/lib/optimize/LimitChunkCountPlugin.js', - 'next/dist/compiled/webpack/LimitChunkCountPlugin', - ], - [ - 'webpack/lib/webworker/WebWorkerTemplatePlugin', - 'next/dist/compiled/webpack/WebWorkerTemplatePlugin', - ], - [ - 'webpack/lib/webworker/WebWorkerTemplatePlugin.js', - 'next/dist/compiled/webpack/WebWorkerTemplatePlugin', - ], - [ - 'webpack/lib/ExternalsPlugin', - 'next/dist/compiled/webpack/ExternalsPlugin', - ], - [ - 'webpack/lib/ExternalsPlugin.js', - 'next/dist/compiled/webpack/ExternalsPlugin', - ], - [ - 'webpack/lib/web/FetchCompileWasmTemplatePlugin', - 'next/dist/compiled/webpack/FetchCompileWasmTemplatePlugin', - ], - [ - 'webpack/lib/web/FetchCompileWasmTemplatePlugin.js', - 'next/dist/compiled/webpack/FetchCompileWasmTemplatePlugin', - ], - [ - 'webpack/lib/web/FetchCompileWasmPlugin', - 'next/dist/compiled/webpack/FetchCompileWasmPlugin', - ], - [ - 'webpack/lib/web/FetchCompileWasmPlugin.js', - 'next/dist/compiled/webpack/FetchCompileWasmPlugin', - ], - [ - 'webpack/lib/web/FetchCompileAsyncWasmPlugin', - 'next/dist/compiled/webpack/FetchCompileAsyncWasmPlugin', - ], - [ - 'webpack/lib/web/FetchCompileAsyncWasmPlugin.js', - 'next/dist/compiled/webpack/FetchCompileAsyncWasmPlugin', - ], - [ - 'webpack/lib/ModuleFilenameHelpers', - 'next/dist/compiled/webpack/ModuleFilenameHelpers', - ], - [ - 'webpack/lib/ModuleFilenameHelpers.js', - 'next/dist/compiled/webpack/ModuleFilenameHelpers', - ], - ['webpack/lib/GraphHelpers', 'next/dist/compiled/webpack/GraphHelpers'], - ['webpack/lib/GraphHelpers.js', 'next/dist/compiled/webpack/GraphHelpers'], - ['webpack/lib/NormalModule', 'next/dist/compiled/webpack/NormalModule'], - ['webpack-sources', 'next/dist/compiled/webpack/sources'], - ['webpack-sources/lib', 'next/dist/compiled/webpack/sources'], - ['webpack-sources/lib/index', 'next/dist/compiled/webpack/sources'], - ['webpack-sources/lib/index.js', 'next/dist/compiled/webpack/sources'], - ['@babel/runtime', 'next/dist/compiled/@babel/runtime/package.json'], - [ - '@babel/runtime/package.json', - 'next/dist/compiled/@babel/runtime/package.json', - ], - ['node-fetch', 'next/dist/compiled/node-fetch'], - ]) + require('../build/webpack/require-hook').default( + [ + ['webpack', 'next/dist/compiled/webpack/webpack-lib'], + ['webpack/package', 'next/dist/compiled/webpack/package'], + ['webpack/package.json', 'next/dist/compiled/webpack/package'], + ['webpack/lib/webpack', 'next/dist/compiled/webpack/webpack-lib'], + ['webpack/lib/webpack.js', 'next/dist/compiled/webpack/webpack-lib'], + [ + 'webpack/lib/node/NodeEnvironmentPlugin', + 'next/dist/compiled/webpack/NodeEnvironmentPlugin', + ], + [ + 'webpack/lib/node/NodeEnvironmentPlugin.js', + 'next/dist/compiled/webpack/NodeEnvironmentPlugin', + ], + [ + 'webpack/lib/BasicEvaluatedExpression', + 'next/dist/compiled/webpack/BasicEvaluatedExpression', + ], + [ + 'webpack/lib/BasicEvaluatedExpression.js', + 'next/dist/compiled/webpack/BasicEvaluatedExpression', + ], + [ + 'webpack/lib/node/NodeTargetPlugin', + 'next/dist/compiled/webpack/NodeTargetPlugin', + ], + [ + 'webpack/lib/node/NodeTargetPlugin.js', + 'next/dist/compiled/webpack/NodeTargetPlugin', + ], + [ + 'webpack/lib/node/NodeTemplatePlugin', + 'next/dist/compiled/webpack/NodeTemplatePlugin', + ], + [ + 'webpack/lib/node/NodeTemplatePlugin.js', + 'next/dist/compiled/webpack/NodeTemplatePlugin', + ], + [ + 'webpack/lib/LibraryTemplatePlugin', + 'next/dist/compiled/webpack/LibraryTemplatePlugin', + ], + [ + 'webpack/lib/LibraryTemplatePlugin.js', + 'next/dist/compiled/webpack/LibraryTemplatePlugin', + ], + [ + 'webpack/lib/SingleEntryPlugin', + 'next/dist/compiled/webpack/SingleEntryPlugin', + ], + [ + 'webpack/lib/SingleEntryPlugin.js', + 'next/dist/compiled/webpack/SingleEntryPlugin', + ], + [ + 'webpack/lib/optimize/LimitChunkCountPlugin', + 'next/dist/compiled/webpack/LimitChunkCountPlugin', + ], + [ + 'webpack/lib/optimize/LimitChunkCountPlugin.js', + 'next/dist/compiled/webpack/LimitChunkCountPlugin', + ], + [ + 'webpack/lib/webworker/WebWorkerTemplatePlugin', + 'next/dist/compiled/webpack/WebWorkerTemplatePlugin', + ], + [ + 'webpack/lib/webworker/WebWorkerTemplatePlugin.js', + 'next/dist/compiled/webpack/WebWorkerTemplatePlugin', + ], + [ + 'webpack/lib/ExternalsPlugin', + 'next/dist/compiled/webpack/ExternalsPlugin', + ], + [ + 'webpack/lib/ExternalsPlugin.js', + 'next/dist/compiled/webpack/ExternalsPlugin', + ], + [ + 'webpack/lib/web/FetchCompileWasmTemplatePlugin', + 'next/dist/compiled/webpack/FetchCompileWasmTemplatePlugin', + ], + [ + 'webpack/lib/web/FetchCompileWasmTemplatePlugin.js', + 'next/dist/compiled/webpack/FetchCompileWasmTemplatePlugin', + ], + [ + 'webpack/lib/web/FetchCompileWasmPlugin', + 'next/dist/compiled/webpack/FetchCompileWasmPlugin', + ], + [ + 'webpack/lib/web/FetchCompileWasmPlugin.js', + 'next/dist/compiled/webpack/FetchCompileWasmPlugin', + ], + [ + 'webpack/lib/web/FetchCompileAsyncWasmPlugin', + 'next/dist/compiled/webpack/FetchCompileAsyncWasmPlugin', + ], + [ + 'webpack/lib/web/FetchCompileAsyncWasmPlugin.js', + 'next/dist/compiled/webpack/FetchCompileAsyncWasmPlugin', + ], + [ + 'webpack/lib/ModuleFilenameHelpers', + 'next/dist/compiled/webpack/ModuleFilenameHelpers', + ], + [ + 'webpack/lib/ModuleFilenameHelpers.js', + 'next/dist/compiled/webpack/ModuleFilenameHelpers', + ], + ['webpack/lib/GraphHelpers', 'next/dist/compiled/webpack/GraphHelpers'], + [ + 'webpack/lib/GraphHelpers.js', + 'next/dist/compiled/webpack/GraphHelpers', + ], + ['webpack/lib/NormalModule', 'next/dist/compiled/webpack/NormalModule'], + ['webpack-sources', 'next/dist/compiled/webpack/sources'], + ['webpack-sources/lib', 'next/dist/compiled/webpack/sources'], + ['webpack-sources/lib/index', 'next/dist/compiled/webpack/sources'], + ['webpack-sources/lib/index.js', 'next/dist/compiled/webpack/sources'], + ['@babel/runtime', 'next/dist/compiled/@babel/runtime/package.json'], + [ + '@babel/runtime/package.json', + 'next/dist/compiled/@babel/runtime/package.json', + ], + ['node-fetch', 'next/dist/compiled/node-fetch'], + ].map( + // Use dynamic require.resolve to avoid statically analyzable since they're only for build time + ([request, replacement]) => [request, require.resolve(replacement)] + ) + ) } diff --git a/test/e2e/styled-jsx/index.test.ts b/test/e2e/styled-jsx/index.test.ts index beef99342927..8f0b30958d39 100644 --- a/test/e2e/styled-jsx/index.test.ts +++ b/test/e2e/styled-jsx/index.test.ts @@ -72,5 +72,5 @@ function runTest(packageManager?: string) { }) } -// runTest() +runTest() runTest('pnpm') From 7c8482e08130df9014bb6d2e24d5a25ca218663d Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 8 Aug 2022 19:33:33 -0500 Subject: [PATCH 20/20] update test --- test/e2e/styled-jsx/index.test.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/test/e2e/styled-jsx/index.test.ts b/test/e2e/styled-jsx/index.test.ts index 8f0b30958d39..aa22928ece66 100644 --- a/test/e2e/styled-jsx/index.test.ts +++ b/test/e2e/styled-jsx/index.test.ts @@ -6,10 +6,8 @@ import webdriver from 'next-webdriver' const appDir = path.join(__dirname, 'app') -function runTest(packageManager?: string) { - describe(`styled-jsx with ${ - packageManager ? ' ' + packageManager : '' - }`, () => { +function runTest() { + describe(`styled-jsx`, () => { let next: NextInstance beforeAll(async () => { @@ -32,11 +30,6 @@ function runTest(packageManager?: string) { }, startCommand: 'yarn ' + ((global as any).isNextDev ? 'dev' : 'start'), buildCommand: `yarn build`, - ...(packageManager - ? { - installCommand: `npx ${packageManager} install`, - } - : {}), }) }) afterAll(() => next.destroy()) @@ -73,4 +66,3 @@ function runTest(packageManager?: string) { } runTest() -runTest('pnpm')