Core points:
import
syntax of the ES6 module, enabling a more efficient and standardized code structure. 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
// app.js import { tag } from './html.js' const h1 = tag('h1', '? Hello Modules!') document.body.appendChild(h1)</pre>Copy after login
type="module"
Simply add </pre>
<p>
<strong>
to your
import
Requirementsfile://
npx serve
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 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.
PerformanceDon'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.