Kayote
Kayote

Reputation: 15617

How Does Babel Know To Treat A Module As A Module

I have gone through 2ality article on Modules, however, I am confused, how does the system know to treat a module as a module?

import React from "react";
import { hello } from "./hello.js";

var Ctrl = React.createClass ({
    render : function () {
        return (
            <div .... random data
            ></div>
        );
    }
});
export default Ctrl;

To me, a module is just a file with global code written in it. We reference the module via its filename.

However, I wonder how does this all gel together when we build the production build.

How does the system know that this is a module & not just global varaibles being declared?

Is it the import / export commands that actually make it say: 'aha, this is a module!'

Excuse the newbie question.

Upvotes: 0

Views: 71

Answers (2)

Muhammad Asif Javed
Muhammad Asif Javed

Reputation: 618

if by system you mean the browser, it doesn't know, you normally use another tool that implements the idea of modules for you, and it transforms (puts in code) that act as a module system for you that the browser understands.

some tools like do this is require in node, browserify, webpack that*, not like

Upvotes: 0

Amit
Amit

Reputation: 46323

Is it the import / export commands that actually make it say: 'aha, this is a module!'

Yes!

More precisely, the import keyword instructs the JavaScript engine (or transpiler) to load an external file as a module.

Upvotes: 1

Related Questions