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

Feature: add next.js integration #29

Open
mulfyx opened this issue Dec 11, 2023 · 8 comments
Open

Feature: add next.js integration #29

mulfyx opened this issue Dec 11, 2023 · 8 comments
Labels
cat: extend support 🤝 Support for a new bundler/platform feature: approved ✅ New feature that has been approved needs: investigation 🔎 Issue has to be investigated for reason or solution platform: next.js 🛠️ Issue related to next.js

Comments

@mulfyx
Copy link

mulfyx commented Dec 11, 2023

The current version of wyw-in-js does not provide compatibility with Next.js 14.

@mulfyx mulfyx added the feature: proposal 💬 New feature proposal that needs to be discussed label Dec 11, 2023
@Anber Anber added platform: next.js 🛠️ Issue related to next.js needs: triage 🏷 Issue needs to be checked and prioritized labels Dec 11, 2023
@layershifter layershifter removed the needs: triage 🏷 Issue needs to be checked and prioritized label Dec 29, 2023
@layershifter layershifter changed the title Add next.js integration Feature: add next.js integration Dec 29, 2023
@layershifter layershifter added the cat: extend support 🤝 Support for a new bundler/platform label Dec 29, 2023
@Anber Anber added the needs: triage 🏷 Issue needs to be checked and prioritized label Dec 29, 2023
@layershifter layershifter added feature: approved ✅ New feature that has been approved and removed feature: proposal 💬 New feature proposal that needs to be discussed needs: triage 🏷 Issue needs to be checked and prioritized labels Dec 29, 2023
@glassdimlygr
Copy link

Is Next.js 13 or 12 supported?

@siriwatknp
Copy link
Contributor

@layershifter MUI is working on the zero-runtime engine (on top of linaria). Would you be able to share what's to be done for Next.js integration? I can submit a PR once the plan is clear.

@layershifter
Copy link
Collaborator

@siriwatknp first, I would check if setup the existing Webpack loader & plugin works with Next.js 13/14 and debug what breaks. So far, I was not able to do that, but Next.js must be supported, so I appreciate if somebody will at least trace the problem 🐱

@layershifter
Copy link
Collaborator

Looks like the problem has repro callstack/linaria#1387.

Needs to be debugged to get understanding what went wrong.

@layershifter layershifter added the needs: investigation 🔎 Issue has to be investigated for reason or solution label Feb 11, 2024
@nihgwu
Copy link
Contributor

nihgwu commented Feb 17, 2024

As we still don't have official plugin for Next.js, and I've tried different solutions mentioned in this and other threads, none of them works for me (wyw-in-js + Next.js v14), so I chose another approach and works pretty good for us: Extracting stylesheet in a different process via esbuild, and esbuild is already super fast

  1. create babel.config.js, we still need the babel plugin to compile wyw-in-js to classNames
    module.exports = {
      presets: ['next/babel', '@wyw-in-js'],
    }
  2. create script (e.g. extract-styles.mjs) to extract style via esbuild
    import wyw from '@wyw-in-js/esbuild'
    import * as esbuild from 'esbuild'
    import fs from 'fs'
    
    import wywConfig from './wyw-in-js.config.js'
    
    const args = process.argv.slice(2)
    
    /** @type {import('esbuild').Plugin} */
    const myPlugin = {
      name: 'my-plugin',
      setup(build) {
        build.onEnd(() => {
          const cssText = fs.readFileSync('./dist/index.css', 'utf-8')
          fs.writeFileSync('./styles.css', cssText, 'utf-8')
          fs.rmSync('./dist', { recursive: true })
        })
      },
    }
    
    /** @type {import('esbuild').BuildOptions} */
    const options = {
      entryPoints: ['src/index.ts'],
      outdir: 'dist',
      bundle: true,
      minify: false,
      logOverride: {
        'ignored-bare-import': 'silent',
      },
      plugins: [
        wyw({
          filter: wywConfig.include,
          ...wywConfig,
        }),
        myPlugin,
      ],
    }
    
    if (args.includes('-w') || args.includes('--watch')) {
      const context = await esbuild.context(options)
      await context.watch()
    
      // eslint-disable-next-line no-console
      console.log('👀 watching changes to generate styles.css')
    } else {
      await esbuild.build(options)
    
      // eslint-disable-next-line no-console
      console.log('✅ styles.css generated')
    }
  3. import styles.css in _app.tsx of Next.js app
  4. build styles while starting Next.js app
    "dev": "node extract-styles.mjs & next dev
    

Note, if you are using Linaria in a different folder as us, you will need to do some trick to make sure babel plugin and esbuild plugin generate the same classNames, overriding classNameSlug in wyw.config.js

const { slugify } = require('@wyw-in-js/shared')
const { toValidCSSIdentifier } = require('@wyw-in-js/processor-utils')

module.exports = {
  ...
  // TODO: we can remove this once https://github.com/Anber/wyw-in-js/pull/63 been merged, and only override it in website
  classNameSlug: (hash, title, args) => {
    const slug = toValidCSSIdentifier(
      `${title.charAt(0).toLowerCase()}${slugify(
        args.file.replace(/(.*\/ui-kit\/)src/, 'src'),
      )}`,
    )

    const className = `${toValidCSSIdentifier(title)}_${slug}`
    return className
  },
}

@mulfyx
Copy link
Author

mulfyx commented Mar 14, 2024

@brijeshb42
Copy link
Contributor

You can try and use @pigment-css/nextjs-plugin with linaria and it should work. It has some MUI specific handling as well but that should not affect the usage of linaria.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat: extend support 🤝 Support for a new bundler/platform feature: approved ✅ New feature that has been approved needs: investigation 🔎 Issue has to be investigated for reason or solution platform: next.js 🛠️ Issue related to next.js
Projects
None yet
Development

No branches or pull requests

7 participants