Home > Web Front-end > JS Tutorial > Using ES Modules in the Browser Today

Using ES Modules in the Browser Today

Jennifer Aniston
Release: 2025-02-15 10:26:12
Original
221 people have browsed it

Using ES modules in browsers: Current status and future

Using ES Modules in the Browser Today

Core points:

  • ES6 (ES2015) introduced the JavaScript native module standard, but initially poor browser support led developers to use module loaders to bundle dependencies into a single ES5-compatible browser file.
  • Mainstream browsers such as Safari, Chrome, Firefox, and Edge now support the import syntax of the ES6 module, enabling a more efficient and standardized code structure.
  • While the ES6 module's support continues to improve, build tools such as Babel and Webpack are still indispensable as browsers continue to be optimized. Combining HTTP2's multi-resource streaming capabilities and browser preloading capabilities, the ES module is expected to bring significant performance improvements.
  • While now you can use the ES module directly in modern browsers without the need for a translator or bundler, it is still recommended to create separate packages for older browsers. More and more libraries are starting to be released as ES modules, but they are still primarily targeted at bundlers rather than direct imports.

This article will demonstrate how to use the ES module in today's browser.

Before recently, JavaScript did not have the concept of modules. It is impossible to directly reference or include a JavaScript file into another file. As application size and complexity grow, this makes writing JavaScript for browsers tricky.

A common solution is to use

// html.js
export function tag (tag, text) {
  const el = document.createElement(tag)
  el.textContent = text

  return el
}</pre>
Copy after login

Or as an external script:
<🎜></pre>
Copy after login
Copy after login
Copy after login
// app.js
import { tag } from './html.js'

const h1 = tag('h1', '? Hello Modules!')
document.body.appendChild(h1)</pre>
Copy after login
Copy after login

type="module"Simply add </pre> <p> <strong> to your

importRequirementsfile://npx serve

You need a server to use

for extraction, as it does not work with

protocol. You can use

to start a server in the current directory for local testing. browser-es-module-loader

If you want to load ES modules on a different domain, you need to enable CORS.

If you are bold enough to try this in production now, you still need to create separate packages for older browsers. A polyfill is provided in which follows the specification. However, this is not recommended for production environments at all.

Performance

Don't discard build tools like Babel and Webpack immediately, because browsers are still implementing methods for optimizing extraction. Nevertheless, there are still performance pitfalls and advantages in future use of ES modules.

Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template