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

OJET support for ESM modules #93

Open
markericson opened this issue Oct 31, 2023 · 1 comment
Open

OJET support for ESM modules #93

markericson opened this issue Oct 31, 2023 · 1 comment

Comments

@markericson
Copy link

Are there any plans to move forward from AMD modules to ESM modules with OJET?

AMD modules are considered antiquated by many with ESM modules becoming the default standard for almost all browsers.

@marke-apexit
Copy link

ECMAScript Modules (ESM) are clearly superior to "requirejs" AMD modules and now supported by all web browsers. I'm curious why this JET project hasn't migrated to ESM?

  • ESM supports static module structure enabling static analysis and tree-shaking
  • ESM also supports dynamic and conditional module loading
  • ESM better handles circular dependencies
  • ESM has a simple and intuitive syntax using simple import and export statements or functions

But I think most importantly:

  • ESM is a standard, part of the ECMAScript language specification
  • ESM does not require importing any javascript for the module functionality (it is native)
  • all browsers have been supporting ESM since ~2018

Attempting to illustrate the more intuitive and concise code with ESM, I created a slightly contrived example, creating a module mathUtils that supports add(), multiply() and trig functions which are imported from another module, but exported as part of mathUtils.

**Example of ESM export and import **

Note: there is no extra code that must be loaded for module support as ESM modules are native to the ECMAScript standard supported by browsers since ~2017-2018

// trig.mjs (ESM)
export const sin = x => Math.sin(x);
export const cos = x => Math.cos(x);
export const tan = x => Math.tan(x);
// calc.mjs (ESM)
export { sin, cos, tan } from './trig.mjs';
export const add = (a, b) => a + b;
export const multiply = (a, b) => a * b;
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<!-- No additional JavaScript necessary for module support, 
it is natively supported by ECMAScript in browsers -->
<body>
    <script>
        import * from './calc.mjs';
        console.log('Result:', add(sin(0), cos(0)));
    </script>
</body>
</html>

Example of AMD (RequireJS) export and import :

Note: excluding comments 73% more lines of code (excluding comments) than the ESM implementation, and less intuitive than the ESM import / export statements

// trig.js (AMD)
// assumes that the client has already have loaded the Require.js module before this module
define(() => ({
    sin: x => Math.sin(x),
    cos: x => Math.cos(x),
    tan: x => Math.tan(x)
}));
// calc.js (AMD)
// assumes that the client has already have loaded the Require.js module before this module
define(['trig'], trig => ({
    add: (a, b) => a + b,
    multiply: (a, b) => a * b,
    sin: trig.sin,
    cos: trig.cos,
    tan: trig.tan
}));
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Include RequireJS to support AMD modules -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
</head>
<body>
    <!-- Your application content here -->
    <script>
        // Load your main module
        require(['calc'], function (calc) {
            console.log('Result:', math.add(math.sin(0), math.cos(0)));
        });
    </script>
</body>
</html>

Can anyone explain why JET has not migrated to ESM?

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

2 participants