Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nextjs): Add package for nextjs plugin #85

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

brijeshb42
Copy link
Contributor

@brijeshb42 brijeshb42 commented May 14, 2024

Motivation

Using WyW (Linaria) with Next.js, in the absence of official integration, has been a big pain so far. People have been relying on next-linaria (not maintained anymore) or next-with-linaria (has it's own issue of not being able to write global and local css in the same file). Mainly the issue is that these are not official. This change opens the way to having an official plugin that others can also contribute to and fix issues (whenever found).

Summary

  • There is an important change in the webpack-loader package that specifically handles nextjs integration. The support for virtual modules is not the best when it comes to Next.js. That's why, the current webpack-loader package does not work directly with Next. There is also an issue with custom css loader that gets removed during one of the three phases of Next.js build process. This change allows the nextjs plugin package to then hook into the usual webpack loader process.
  • 2nd change is the plugin itself which is the new @wyw-in-js/nextjs package. It handles stuff like adding the two loaders (the webpack loader and the css injector loader) to the existing webpack configuration. It also handles resolving some of the next.js modules that are part of it's compile time like next/image or next/font.
  • 3rd change adds a Next.js demo app in the examples directory that uses @linaria/core with the @wyw-in-js/nextjs plugin integration.

What this PR does not cover is support for Turbopack which has some key APIs missing as of now for us to support it.

Test plan

Since it'll be hard to test the Next.js plugin itself, I was planning on using the nextjs-demo app as the testing playground by adding various combinations of the @linaria/core or @linaria/react usage and try to build the app.

There is an issue right now in the new package that only allows apps with ESM files, ie, you can have next.config.mjs or next.config.js with "type": "module" in package.json. Commonjs doesn't work. This is something that I am debugging and will make the PR ready to review once that is done.

This code has been extracted out of Pigment CSS's Next.js implementation for the community.

To test the Next.js integration, go into the examples/nextjs-demo app and run pnpm build to build the app.
The review can be done commit-wise since each commit builds on top of the previous one.

Copy link

changeset-bot bot commented May 14, 2024

🦋 Changeset detected

Latest commit: 6e67255

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@brijeshb42 brijeshb42 force-pushed the nextjs branch 4 times, most recently from 55a2fd1 to 38e0032 Compare May 15, 2024 05:41
@brijeshb42
Copy link
Contributor Author

Note that there is an issue when using :global(). Seems related to callstack/linaria#1388

@nicholasio
Copy link

Is there anything I can do to help move this forward? We provide an official Linaria integration for Pages router on https://github.com/10up/headstartwp and we're now adding support to app router, I'd love to stick with Linaria as the default styling solution.

@austinbiggs
Copy link

Is there anything I can do to help move this forward?

I second this; I'd love to help push this along. 🙂

@Anber
Copy link
Owner

Anber commented Aug 19, 2024

Hi @brijeshb42!
Is it ready? I want to merge it before #96 because #96 has some breaking changes in the transform API.

@nicholasio
Copy link

I wasn't able to build the example app

> next build

  ▲ Next.js 14.2.3

   Creating an optimized production build ...
Failed to compile.

../../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/api/image.js
Module parse failed: Duplicate export 'default' (2:9)
File was processed with these loaders:
 * ../../packages/webpack-loader/lib/index.js
 * ../../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/build/webpack/loaders/next-flight-loader/index.js
 * ../../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/build/webpack/loaders/next-swc-loader.js
You may need an additional loader to handle the result of these loaders.
| export { default } from "../shared/lib/image-external";
> export { default, getImageProps } from "../shared/lib/image-external"; //# sourceMappingURL=image.js.map

Import trace for requested module:
../../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/api/image.js
./src/app/page.tsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants