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.

Trouble creating TypeScript extensions (within Next.js SSR)

See original GitHub issue

My problem is an intersection between remirror and module loading (with Next.js). It may not be a bug per-se, but it is something I’ve only encountered with this library.

I realize I risk getting this issue closed and told to open it at Next.js, but my hope in posting here is that we can clear up how one should ingest the core types to write extensions in TypeScript.

The core idea: I’m running into a situation where importing modules from e.g. @remirror/core the objects are exported classes. But with e.g. the default babel/typescript setup with Next.js, the classes are compiled to function classes, and we get

TypeError: Class constructor NodeExtension cannot be invoked without 'new'

Here’s some code that shows what I mean:

// A Next.js app with Typescript as described here: https://github.com/zeit/next-plugins/tree/master/packages/next-typescript
// pages/index.tsx
import { NodeExtension } from "@remirror/core"; // loads two different modules

class TitlePlaceholder extends NodeExtension {}

console.log("NodeExtension: ", NodeExtension); // is an ES6 `class` during SSR
console.log("TitlePlaceholder: ", TitlePlaceholder); // is a function class

// const a = new TitlePlaceholder();
// gives => TypeError: Class constructor NodeExtension cannot be invoked without 'new'

const Index = () => (
  <div>
    <p>Hello Next.js</p>
  </div>
);

export default Index;

You can find this complete repo here.


One thing I tried was using a custom webpack module rule to use babel-loader for remirror:

// see: https://github.com/zeit/next-plugins/tree/master/packages/next-typescript
// next.config.js
const webpack = (config, { dev, isServer }) => {
  config.module.rules.push({
    test: /\.js$/,
    include: /node_modules\/@remirror/,
    use: {
      loader: "babel-loader",
      options: {
        presets: [
          [
            "@babel/preset-env",
            {
              debug: true,
              forceAllTransforms: true,
              targets: {
                ie: "11"
              }
            }
          ]
        ]
      }
    }
  });

  return config;
};

const withTypescript = require("@zeit/next-typescript");
module.exports = withTypescript({ webpack });

The result is:

  • babel classTransformer runs over all remirror classes such as NodeExtension
  • however the initial SSR still receives a class (causing an error)
  • but the client-side actually receives NodeExtension as a function class (!) (the desired result) (runnable branch here)

I know I’m on thin ice asking about this, because at that point it seems to be related to the Next.js SSR.

That said it’s a vanilla Next.js setup and I can’t help but wonder if these issues could be solved by publishing an ES5-style package.

If anyone has any suggestions on how to resolve these module issues, I’d be deeply grateful.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ifiokjrcommented, Mar 29, 2019

@jashmenn I’ve noticed that an unintended side effect of the changes made to fix this bug, is a 30% increase in the size of the @remirror/core library, from 37.5kb to 48.8kb.

I’ll keep an eye on this as it may be possible to achieve the desired outcome (build compatibility with next.js and other platforms), without adding the unwanted extra build code.

image

1reaction
jashmenncommented, Mar 22, 2019

🙏 Thank you so much. I struggled w/ this for hours and your fix works perfectly.

Remirror is amazing, btw! It’s a near-perfect structuring of Prosemirror with React. Thanks for sharing it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trouble creating TypeScript extensions (within Next.js SSR) #59
My problem is an intersection between remirror and module loading (with Next.js). It may not be a bug per-se, but it is something...
Read more >
Basic Features: TypeScript - Next.js
Next.js provides an integrated TypeScript experience, including zero-configuration set up and built-in types for Pages, APIs, and more.
Read more >
Using Next.js with TypeScript - LogRocket Blog
In this tutorial, learn how to use Next.js with TypeScript to build a modern stack for high-quality, search-optimized apps.
Read more >
NEXTJS Snippets tsx and jsx - Visual Studio Marketplace
js. nexterrorcustom→, Custom error component Next JS. You must create a file "_error.js" inside your "pages" folder.
Read more >
Building Application using Next JS + Typescript - YouTube
Next JS SSR || Building Application using Next JS + Typescript.
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