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

Next.js 15 and experimental import attributes #21

Closed
arnemolland opened this issue Jun 19, 2024 · 5 comments
Closed

Next.js 15 and experimental import attributes #21

arnemolland opened this issue Jun 19, 2024 · 5 comments

Comments

@arnemolland
Copy link

The generated files from contentlayer(2) makes use of the experimental import attributes feature when running Node > 16 which worked fine using Babel plugins with previous Next.js versions.

With Next.js 15, Babel has been replaced by swc which does not have any documented way to enable the experimental syntax. Next throws and suggests installing the Babel plugin, which didn't work for me at least (I use Turbopack). The quick fix for me was to patch the @contentlayer2/core package to remove the assert statement.

Removing the statement outright probably breaks stuff, providing a config option to disable them would be a quick fix but I'm not sure if it's the best way to do it either.

Note: The proposed syntax differs from the contentlayer(2) implementation and this will probably need to be updated in the near future anyway; the proposed keyword is with instead of assert.

@timlrx
Copy link
Owner

timlrx commented Jun 22, 2024

Hmm... I would have thought that it should run fine given the swc supports import attributes and the default next.js swc options seems to support it as well.

Let me try changing the code from assert to with and see if that solves the problem.

@timlrx
Copy link
Owner

timlrx commented Jun 23, 2024

The latest canary version of Next 15 seems to work without issues - https://stackblitz.com/edit/github-ekmxur-ba82kk?file=package.json

@arnemolland
Copy link
Author

arnemolland commented Jul 2, 2024

I just tried with the latest canary version and contentlayer2 and I'm still able to reproduce. The reason why the stackblitz example is working is because it's not running contentlayer2 build, just next build. To clarify, Next only complains about it when building the application after the contentlayer files has been generated. next dev does not complain. This is the error message that's being spit out:

.contentlayer/generated/index.mjs:7:42
Syntax error: Support for the experimental syntax 'importAttributes' isn't currently enabled (7:43):

   5 | // NOTE During development Contentlayer imports from `.mjs` files to improve HMR speeds.
   6 | // During (production) builds Contentlayer it imports from `.json` files to improve build performance.
>  7 | import allPosts from './Post/_index.json' assert { type: 'json' }
     |                                           ^
   8 |
   9 | export { allPosts }
  10 |

Add @babel/plugin-syntax-import-attributes (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-import-attributes) to the 'plugins' section of your Babel config to enable parsing.


> Build failed because of webpack errors
error: script "build" exited with code 1

@lishaduck
Copy link
Contributor

While I agree that this is strange, it really should use with, assert is deprecated. I'll send in a PR.

@timlrx
Copy link
Owner

timlrx commented Jul 13, 2024

@arnemolland I have published a fix in v0.5.0. Give it a try and let me know if the problem is fixed. Thanks!

@timlrx timlrx closed this as completed Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants