ForceUser
ForceUser

Reputation: 1119

Common module format for isomorphic/universal javascript application

I'm trying to write isomorphic/universal app and there are many complications to tie all stuff together.

What i need is common (not to be confused with commonjs) modules format for client and server with posibility of dynamic loading and es6/7 scripting (like async/await etc)

Also i want it to transpile in runtime without any packaging tools like webpack

I've tried systemjs, but the problem is that Systemjs cant load nodejs modules in the same way as it makes node require, for example

System.import("express")

not working the same way as

require("express")

So i'm trying to use commonjs format on both server side (+babel for transpiling), and in the browser

In browser i'm using steal.js, it also uses systemjs under the hood and can load npm modules on client side and transpile it in runtime, but it can't automaticaly transpile commonjs modules, because systemjs automaticaly transpiles only 'esm/es6' format :(

I need the way to force-transpile cjs modules with systemjs/stealjs or to make System.import work on server in acceptable way

Or maybe i moving in wrong direction?

Upvotes: 0

Views: 320

Answers (1)

DBrowne
DBrowne

Reputation: 131

System Js can import CJS modules. I'm currently creating an isomorphic express/react/system js/jspm app myself.

The documentation rightly says here: https://github.com/systemjs/systemjs/blob/master/docs/module-formats.md

try adding something like this to your system config:

System.config({
  baseURL: "/",
  defaultJSExtensions: true,
    meta: {
        "*js": {
            format: "cjs"
        }
    }
})

Upvotes: 0

Related Questions