question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert to CommonJS-style require syntax, but produce current syntax as build output

See original GitHub issue

RequireJS and most other AMD loaders support a so-called “CommonJS-style” syntax for requiring in dependencies: http://requirejs.org/docs/commonjs.html

Essentially,

define(function (require) {
    var foo = require('foo');
    var bar = require('bar');
    //etc
});

Not only is this syntax far easier to read, but it would let us more easily detect unused dependencies, since they become unused local variables, rather than unused parameters. The problem with this syntax is that some build tools, notably the Dojo build system, don’t properly detect dependencies when modules are required using this syntax (bug).

We can use the more convenient syntax directly for development and testing, where we load in individual modules asynchronously directly from the source files, and then as part of our build process produce versions of the modules with the dependencies listed in the more widely-supported conventional manner. These would be the set of modules that we ship with releases, and build Dojo-based applications against.

Either by solely adding the dependency array:

define(['require', 'foo', 'bar'], function (require) {
    var foo = require('foo');
    var bar = require('bar');
    //etc
});

(once the dependencies are declared explicitly, we can require them synchronously knowing that they will already be available)

Or, by adjusting the code to even more closely match our current style:

define(['require', 'foo', 'bar'], function (require, foo, bar) {
    //etc
});

The RequireJS loader has a regex to find require('') calls, but I think it may be better in our case to be more specific and look for larger var a = require('b'); statements, since in the future I would also like to start moving towards dynamically requiring materials, visualizers, etc. to make it more feasible to produce custom builds that exclude unused code. (#351)

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
ggetzcommented, Feb 23, 2018
0reactions
jpiersoncommented, Feb 23, 2018

@mramato, where are the plans to move to ES6 modules being tracked? As far as I can tell from some brief searching this hasn’t been completed in the 3 years since this issue was closed and I was unable to find any other tracking open tracking issue covering the movement to ES modules specifically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Codemod for Babel `import` into commonjs `require`
I dig up the source code of babel-plugin-transform-modules-commonjs . Looks like it's impossible to config babel to output your desired ...
Read more >
Use ES6 modules instead of CommonJS · Issue #1206 - GitHub
It's only converting the module and export syntax. Neither of which are supported by any current browser except for a dev build of...
Read more >
API - esbuild
It assumes the environment contains exports , require , and module . Entry points with exports in ECMAScript module syntax will be converted...
Read more >
RequireJS API
The RequireJS syntax for modules allows them to be loaded as fast as possible, even out of order, but evaluated in the correct...
Read more >
CommonJS vs. ES modules in Node.js - LogRocket Blog
js that only support CommonJS modules (that is, the require() syntax). But with the new conditional exports, we can build dual-mode libraries.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found