monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

[Bug] Package currently doesn't work when used with NextJS

Open KTSnowy opened this issue 2 years ago • 1 comments

Reproducible in vscode.dev or in VS Code Desktop?

  • [X] Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Code

No response

Actual Behavior

Screenshot 2022-04-03 201145

Package is currently broken when used with NextJS, there seems to be no way to fix without modifying the package's source code.

Expected Behavior

Expected to be able to use the package with NextJS, specially since there's a ReactJS sample in the samples folder

Additional Context

It seems that NextJS requires that css files be imported as ".module.css"

KTSnowy avatar Apr 03 '22 23:04 KTSnowy

I think the reason might also be related to: https://github.com/vercel/next.js/issues/10975

Solution:
make use of next-transpile-modules inside your next.config.js.

As an example:

const config = {
  webpack: config => {
    config.plugins.push(
      new MonacoWebpackPlugin({
        languages: ['javascript'],
        filename: 'static/[name].worker.js',
      }),
    );
    return config;
  },
[…]
}


const withTM = require('next-transpile-modules')(['monaco-editor']);

module.exports = withTM(config);

chrison94 avatar May 20 '22 14:05 chrison94

Has anyone worked this out? Any guide on how to use monaco-editor with Next.js 13?

harshcut avatar Dec 24 '22 17:12 harshcut

The above example doesn't work when adapted to the latest next@13

const MonacoWebpackPlugin = require("monaco-editor-webpack-plugin");

/** @type {import('next').NextConfig} */
const nextConfig = {
  transpilePackages: ['monaco-editor'],
  webpack(config, options) {
    config.plugins.push(new MonacoWebpackPlugin({
      languages: ['json'],
      filename: 'static/[name].worker.js',
    }));
    return config;
  },
};

module.exports = nextConfig;

I get this error in the console/error boundary and next fails

setting getter-only property "Lexer"

worth noting - despite using the above config with json instead of javascript, only static/editor.worker.ts shows up. it appears this failure happens in the transpilation stage

acao avatar Feb 15 '23 13:02 acao

@chrison94 what version of next.js and monaco editor were you using where you got this working?

acao avatar Feb 15 '23 14:02 acao

@acao have you already solved this problem ?

vinoMamba avatar Feb 24 '23 16:02 vinoMamba