Nuxt.js: Support for .mjs files and native ESM

Created on 23 Oct 2017  路  4Comments  路  Source: nuxt/nuxt.js

Most of the code in nuxt projects can now be written in modern ES6 syntax. Even more is going to be supported when 8 is next LTS (Such as async/await). However, there are parts currently are not fully ES6 compatible because are being required directly with node and thus depends on pure node functionalities. Also using babel-register to polyfill this gap may introduce many many inconsistencies and perf/stability issues which is not ideal. Current parts which can potentially leverage .mjs:

  • Modules (to be used without any transpiler like roll-up)
  • Server Middleware
  • nuxt.config.js
  • [potentailly] Nuxt.js core source code itself for perf and maintenance reasons

This article is useful for getting started with native esm. Currently, esm support can be enabled behind a flag so we would delay this feature for Nuxt until it gets stable enough.

PS: Any ideas, feedbacks, and contribution are welcome. our only limitation is that PRs should target v2 as this is almost breaking change in the entire ecosystem.

XREFs:

  • #1912
  • #2087

This feature request is available on Nuxt.js community (#c1727)
available soon

Most helpful comment

I'm experimenting with @std/esm to share code between client & server. How about wrapping all serverMiddleware code with this?

// serverMiddleware.js
const { Router } = require('express')
const router = Router()

const esm = require('@std/esm')(module, { esm: 'js', cjs: true })

router.use(esm('./some-api-module').router)
router.use(esm('./another-one').router)

module.exports = router
// some-api-module.js
import { Router } from 'express'
export const router = Router()

router.post('/...')

All 4 comments

I think we can experiment most of the parts by using something like this to start Node.js process:

node --experimental-modules node_modules/.bin/nuxt --config ./nuxt.config.mjs

Again this article would be really helpful to get started :)

I'm experimenting with @std/esm to share code between client & server. How about wrapping all serverMiddleware code with this?

// serverMiddleware.js
const { Router } = require('express')
const router = Router()

const esm = require('@std/esm')(module, { esm: 'js', cjs: true })

router.use(esm('./some-api-module').router)
router.use(esm('./another-one').router)

module.exports = router
// some-api-module.js
import { Router } from 'express'
export const router = Router()

router.post('/...')

Shipped! (#1935)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vadimsg picture vadimsg  路  3Comments

mikekidder picture mikekidder  路  3Comments

vadimsg picture vadimsg  路  3Comments

jaredreich picture jaredreich  路  3Comments

o-alexandrov picture o-alexandrov  路  3Comments