3

Is it possible to import a "base script" from a module-like directory with SystemJS, like a Python's __init__.py?

Example:

Directory structure

app/
  module1/
    __init__.js       // or module1.js
    module1.weeble.js
    module1.thingy.js
  module2/
    __init__.js       // or module2.js
    module2.weeble.js
    module2.thingy.js
  bootstrap.js

module1/__init__.js

import Weeble from "./module1.weeble"
import Thingy from "./module1.thingy"

class Module1 {
  constructor() {
    this.weeble = new Weeble();
    this.thingy = new Thingy();
  }
}

export default Module1

bootstrap.js

Import directory to import the base script (__init__.js or module1.js) or something:

import Module1 from "./module1"
import Module2 from "./module2"

let module1 = new Module1();
let module2 = new Module2();

// INSTEAD OF
// import Module1 from "./module1/__init__"
// OR
// import Module1 from "./module1/module1"

Is this possible? Python imports a directory as a module if __init__.py is present in that directory

# so one doesn't have to type
from module/__init__.py import module_part

# but only
from module import module_part

# or to import the entire module 
import module
Richard de Wit
  • 5,772
  • 7
  • 42
  • 50

2 Answers2

0

Yup it's possible. Use a index.js file for that:

module1/index.js

Then you can import "./module1";

Richard de Wit
  • 5,772
  • 7
  • 42
  • 50
-1

JavaScript itself isn't a module-based language language (not yet). In order to do this you will need to include another library like RequireJS

Ryan Ransford
  • 3,145
  • 26
  • 33
Erik Lucio
  • 828
  • 7
  • 8