4

I'm looking for a way to use ES6 Modules (import/export) syntax in my current RequireJS project. I can run my project directly in a recent browser, without build or Babel transformation. I build my project with r.js and Babel only for production.

We would like to start building ES6 modules and use it with requireJS. Is there a way to do that ?

NB : RequireJS does not load dependencies written in ES6.

1 Answer 1

4

Yes, RequireJS should be compatible with Babel's es2015-modules-umd plugin. So you should be able to use import and export in your files during development with a modern browser that supports modules, then use r.js and Babel w/that plugin for production.

Sign up to request clarification or add additional context in comments.

7 Comments

The problem is that RequireJS does not load dependencies written in ES6. Even in a modern browser
@Acidnuk: By the time RequireJS sees it, it's not in ES6 anymore (at least, not the import/export part). That's why Babel's involved at that stage, to transform import/export into require/define calls.
Yes it is actually in production. But my application will not work during development, right?
@Acidnuk: That's why I said "...with a modern browser that supports modules..." Recent versions of Chrome do, for instance; I think the latest Safari as well (Safari actually got there first, in their pre-release branch). You bootstrap with <script type="module" src="main.js"></script> and then main.js can use import { foo } from "./foo.js"; and such. So that's development. Then for test and production you run it through Babel, probably use Require et. al. to build a combined, minified file, et voila.
Ok, i got it. Sorry if my initial question was not precise enough. So that means I can not get an es6 module via requireJS. like: define (["myES6Module.js"], function () {});
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.