question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage with Nextjs

See original GitHub issue

Hi, thanks for creating this library! I was eager to try it with Nextjs, but faced some issues. I’m using Nextjs v 10.0.7 and latest vanilla-extract. I have this setup:

next.config.js:

const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.plugins.push(new VanillaExtractPlugin({
      // allowRuntime: true
    }));
    config.plugins.push(new MiniCssExtractPlugin())
    return config;
  },
};

.babelrc:

{
  "presets": ["next/babel"],
  "plugins": [
    "@vanilla-extract/babel-plugin"
  ]
}

When I try to run yarn dev, I get:

Error: Cannot find module 'webpack/lib/NormalModule'
Require stack:
- /{projectpath}/node_modules/mini-css-extract-plugin/dist/index.js
- /{projectpath}/node_modules/mini-css-extract-plugin/dist/cjs.js
- /{projectpath}/next.config.js

Do you perhaps an idea how to solve this: Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
Mokshit06commented, Mar 31, 2021

I’ve got vanilla-extract working with Next.js with hot reload. This setup works but it shows You did not set any plugins, parser, or stringifier. Right now, PostCSS does nothing. Pick plugins for your case on https://www.postcss.parts/ and use them in postcss.config.js warning.

.babelrc

{
  "presets": ["next/babel"],
  "plugins": ["@vanilla-extract/babel-plugin"]
}

next.config.js

const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin');
const {
  getGlobalCssLoader,
} = require('next/dist/build/webpack/config/blocks/css/loaders');
const {
  default: MiniCssExtractPlugin,
} = require('next/dist/build/webpack/plugins/mini-css-extract-plugin/src');

/**
 * @param {ConstructorParameters<typeof VanillaExtractPlugin>[0]} pluginOptions Custom config for vanilla-extract
 */
function withVanillaExtract(pluginOptions = {}) {
  /**
   * @param {any} nextConfig Custom config for Next.js
   */
  return (nextConfig = {}) => {
    return {
      // For Webpack 4, you'll need to install it seperately
      future: {
        webpack5: true,
      },
      webpack(config, options) {
        const { dev, isServer } = options;

        config.module.rules.push({
          test: /\.css$/i,
          sideEffects: true,
          use: dev
            ? getGlobalCssLoader(
                {
                  assetPrefix: options.config.assetPrefix,
                  future: {
                    webpack5: true,
                  },
                  isClient: !isServer,
                  isServer,
                  isDevelopment: dev,
                },
                [],
                []
              )
            : [MiniCssExtractPlugin.loader, 'css-loader'],
        });

        const plugins = [];

        plugins.push(new VanillaExtractPlugin(pluginOptions));

        if (!dev) {
          plugins.push(
            new MiniCssExtractPlugin({
              filename: 'static/css/[contenthash].css',
              chunkFilename: 'static/css/[contenthash].css',
              ignoreOrder: true,
            })
          );
        }

        config.plugins.push(...plugins);

        if (typeof nextConfig.webpack === 'function') {
          return nextConfig.webpack(config, options);
        }

        return config;
      },
    };
  };
}

module.exports = withVanillaExtract()();
0reactions
danielberndtcommented, Apr 22, 2021

Ah thanks, downgrading to "@vanilla-extract/babel-plugin": "0.2.1" resolves this issue 👍

Update: "@vanilla-extract/babel-plugin": "0.3.1" now works as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is Next.js?
Next.js is a React framework that gives you building blocks to create web applications. By framework, we mean Next.js handles the tooling and...
Read more >
Nextjs for everyone — with some basic knowledge of React
Next.js is a JavaScript framework created by Zeit. It lets you build server-side rendering and static web applications using React. It's a great ......
Read more >
What is Next JS and Why Should You Use it in 2022? - Pagepro
Next.js is widely used by the biggest and most popular companies all over the world like Netflix, Uber, Starbucks, or Twitch. It's also ......
Read more >
What is Next.js & Why & How To Use It in 2022? - Emizentech
No, Next.js is a framework primarily used for server-side rendering of react applications. Next.js provides a backend that can be used by server ......
Read more >
Usage with Next.js - SWR
Together with SWR, you can pre-render the page for SEO, and also have features such as caching, revalidation, focus tracking, refetching on ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found