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

fix(framework): Resolve CJS issues this time with json-schema-faker #6766

Merged
merged 3 commits into from
Oct 29, 2024

Conversation

SokratisVidros
Copy link
Contributor

@SokratisVidros SokratisVidros commented Oct 24, 2024

What changed? Why was the change needed?

Apply more configuration changes for better ESM/CJS support. Also try to remove the Node https dependency from json-schema-faker

Fixes #6701
Fixes #6332

Copy link

netlify bot commented Oct 24, 2024

Deploy Preview for novu-stg-vite-dashboard-poc ready!

Name Link
🔨 Latest commit cb6c2e5
🔍 Latest deploy log https://app.netlify.com/sites/novu-stg-vite-dashboard-poc/deploys/671f3a7be8efe30008f15b53
😎 Deploy Preview https://deploy-preview-6766--novu-stg-vite-dashboard-poc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

pkg-pr-new bot commented Oct 24, 2024

Open in Stackblitz

@novu/client

pnpm add https://pkg.pr.new/novuhq/novu/@novu/client@6766

@novu/framework

pnpm add https://pkg.pr.new/novuhq/novu/@novu/framework@6766

@novu/headless

pnpm add https://pkg.pr.new/novuhq/novu/@novu/headless@6766

@novu/js

pnpm add https://pkg.pr.new/novuhq/novu/@novu/js@6766

@novu/nextjs

pnpm add https://pkg.pr.new/novuhq/novu/@novu/nextjs@6766

@novu/nest

pnpm add https://pkg.pr.new/novuhq/novu/@novu/nest@6766

@novu/node

pnpm add https://pkg.pr.new/novuhq/novu/@novu/node@6766

@novu/notification-center

pnpm add https://pkg.pr.new/novuhq/novu/@novu/notification-center@6766

novu

pnpm add https://pkg.pr.new/novuhq/novu@6766

@novu/providers

pnpm add https://pkg.pr.new/novuhq/novu/@novu/providers@6766

@novu/react

pnpm add https://pkg.pr.new/novuhq/novu/@novu/react@6766

@novu/react-native

pnpm add https://pkg.pr.new/novuhq/novu/@novu/react-native@6766

@novu/shared

pnpm add https://pkg.pr.new/novuhq/novu/@novu/shared@6766

@novu/stateless

pnpm add https://pkg.pr.new/novuhq/novu/@novu/stateless@6766

commit: cb6c2e5

@SokratisVidros SokratisVidros changed the title fix(framework): Try with dynamic import to resolve CJS issues this time. fix(framework): Resolve CJS issues this time with json-schema-faker Oct 25, 2024
@SokratisVidros SokratisVidros marked this pull request as ready for review October 28, 2024 07:16
Json-schema-faker is causing big HMR and Webpack headaches when @novu/framework is used in Next.js.
To address the issue, we decided to go old-school and hardcode the IIFE version of the source code in our package.
The code was copied for https://unpkg.com/browse/json-schema-faker@0.5.6/dist/main.iife.js.

PLEASE NOTE THE CODE WAS SLIGHTLY MODIFIED TO MAKE IT WORK IN @novu/framework. See the end of this file.

See json-schema-faker/json-schema-faker#796 (comment).
@@ -1,5 +1,3 @@
{
"main": "../dist/cjs/servers/express.cjs",
Copy link
Contributor Author

@SokratisVidros SokratisVidros Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After careful testing, these are not required as ESM sub-exports will always be fetched from the top-level package.json

@@ -239,7 +239,6 @@
"better-ajv-errors": "^1.2.0",
"chalk": "^4.1.2",
"cross-fetch": "^4.0.0",
"json-schema-faker": "^0.5.6",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package is causing a lot of ESM/CJS trouble. It was decided to hardcode its UMD version in the framework.

@@ -37,7 +37,7 @@
"format:fix": "prettier --write --ignore-path .gitignore .",
"build": "NODE_ENV=production tsup",
"build:watch": "tsup --watch",
"postbuild": "./scripts/copy-package-json.sh && pnpm run check:exports && pnpm check:circulars",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy-package.json is not anymore assuming package exports work correctly. Attw confirms the removal.


const frameworks: SupportedFrameworkName[] = ['h3', 'express', 'next', 'nuxt', 'sveltekit', 'remix', 'lambda', 'nest'];

const baseConfig: Options = {
entry: ['src/index.ts', 'src/internal/index.ts', ...frameworks.map((framework) => `src/servers/${framework}.ts`)],
sourcemap: false,
clean: true,
treeshake: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Libraries need to be treeshaken by the host application bundler.


const frameworks: SupportedFrameworkName[] = ['h3', 'express', 'next', 'nuxt', 'sveltekit', 'remix', 'lambda', 'nest'];

const baseConfig: Options = {
entry: ['src/index.ts', 'src/internal/index.ts', ...frameworks.map((framework) => `src/servers/${framework}.ts`)],
sourcemap: false,
clean: true,
treeshake: true,
dts: true,
minify: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Libraries shouldn't be minified. The host application bundler should handle that.

However, setting bundle: false and minify: false in tsup config, breaks Nest.js loading @novu/framework.

The hypothesis is that building through ESBuild alone is not enough. Minify triggers a rollup processing that, for some reason, satisfies Nest.js CommonJS requirements.

We will get back to this in a separate PR, as this one is already big enough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed explanation. Sounds like a good plan 👍

dts: true,
minify: true,
minifyWhitespace: true,
minifyIdentifiers: true,
minifySyntax: true,
define: {
SDK_VERSION: `"${version}"`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static code replacements allow us to eliminate importing package.json on runtime that can cause some CJS/ESM issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! 👍

@rifont rifont self-requested a review October 28, 2024 08:23
@SokratisVidros SokratisVidros merged commit d6d4250 into next Oct 29, 2024
45 checks passed
@SokratisVidros SokratisVidros deleted the framework_in_edge_runtimes branch October 29, 2024 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants