1

I was going through this module, and found many of the files using the import stament.ex: this one. can anybody tell me how the import statement is replacing the standerd require statement of nodejs and how are they working?

EDIT: this is not the duplicate, because the import syntax is different form the ES6 syntax

isnvi23h4
  • 1,910
  • 1
  • 27
  • 45
  • Possible duplicate of [Using Node.js require vs. ES6 import/export](http://stackoverflow.com/questions/31354559/using-node-js-require-vs-es6-import-export) – Roman Kiselenko Jan 01 '16 at 19:15
  • is this the es6 module feature? because the systax seems different, in the example i have mentioned, it whereas in es6 it – isnvi23h4 Jan 01 '16 at 19:37

1 Answers1

2

The import statement is provided by js.io - A module system that the repository is using.

Quoting from the README of the project:

js.io is a multi-platform package management and module system for JavaScript. js.io modules can be evaluated in a JavaScript runtime (e.g. node.js) or precompiled into a single package for use on the client side.

js.io provides the following:

A module system. Dependency graph that works in the client and the browser. Support and networking libraries that can be used on either platform.

The import statement as in the linked example does not confirm to ES6 spec.

From MDN, the syntax for ES6 imports follow the following patterns:

import name from "module-name";
import * as name from "module-name";
import { member } from "module-name";
import { member as alias } from "module-name";
import { member1 , member2 } from "module-name";
import { member1 , member2 as alias2 , [...] } from "module-name";
import defaultMember, { member [ , [...] ] } from "module-name";
import defaultMember, * as alias from "module-name";
import defaultMember from "module-name";
import "module-name";

The usage import AudioManager as exports; is not a valid usage as per the above rules.

I could not deduce from the README of js.io if confirmance with ES6 modules is a goal of the project.

lorefnon
  • 12,875
  • 6
  • 61
  • 93
  • thanks for the explanation, i have one more question, that is, is there any way that this file can be browserified without the jsio lib? – isnvi23h4 Jan 01 '16 at 22:57
  • i dont think so. you should be able to compile the library using jsio once and use browserify for the rest of your application though. – lorefnon Jan 02 '16 at 00:17
  • do you have any idea about which part code is responsible for the import statement in the jsio? – isnvi23h4 Jan 05 '16 at 12:48
  • 1
    Code here is responsible for parsing of import statement: https://github.com/gameclosure/js.io/blob/bf8cdfa2c19fd610b179ce47ca7101f36988c7e9/packages/preprocessors/import.js The export resolution and building functionality is available here: https://github.com/gameclosure/js.io/blob/bf8cdfa2c19fd610b179ce47ca7101f36988c7e9/packages/preprocessors/compiler.js#L14 – lorefnon Jan 05 '16 at 13:20
  • can you tell me what is the syntax "/^(\s*)(import\s+[^=+*"'\r\n;\/]+|from\s+[^=+"'\r\n;\/ ]+\s+import\s+[^=+"'\r\n;\/]+)(;|\/|$)/gm;" used in the importExpr variable called? – isnvi23h4 Jan 07 '16 at 15:54
  • 1
    This is a javascript regular expression used to parse the import syntax. Details about usage of regular expressions in javascript are available here: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions and to understand this particular regular expression you can use the following online regex tester: https://regex101.com/r/mS3wW4/1 – lorefnon Jan 07 '16 at 18:53