-
Notifications
You must be signed in to change notification settings - Fork 687
Implement ES2015 module system #2599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
LaszloLango
merged 1 commit into
jerryscript-project:master
from
vincedani:feature/es6-module-system
Mar 18, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # ES6 module support for JerryScript | ||
|
|
||
| The module system allows users to write import and export statements in scripts. Therefore the logic of the application could be separated in custom modules. | ||
| The standard's relevant part can be found [here](https://www.ecma-international.org/ecma-262/6.0/#sec-modules). | ||
|
|
||
| ## General | ||
|
|
||
| If the main script contains import statements, then Jerry opens and runs the appropriate scripts before the main script (as the standard says). The script's and the module's extension is `.js`, custom extensions are unnecessary. | ||
|
|
||
| main.js | ||
|
|
||
| ```js | ||
| import { secret_number } from "./module.js" | ||
|
|
||
| print (secret_number); | ||
| ``` | ||
|
|
||
| module.js | ||
|
|
||
| ```js | ||
| var secret_number = 42; | ||
|
|
||
| export secret_number; | ||
| ``` | ||
|
|
||
| ## Supported features | ||
|
|
||
| * import variable or function | ||
| * add alias name to the imported variable (function) | ||
| * export variable or function | ||
| * add alias name to the exported variable (function) | ||
|
|
||
| ### Example | ||
|
|
||
| ```js | ||
| import { | ||
| engine, | ||
| version as v | ||
| } from "./module.js" | ||
|
|
||
| import { getFeatureDetails } from "./module_2.js" | ||
|
|
||
| var version = "v3.1415"; | ||
|
|
||
| print("> main.js"); | ||
|
|
||
| print(">> Engine: " + engine); | ||
| print(">> Version: " + v); | ||
|
|
||
| print (">> " + getFeatureDetails()); | ||
| print (">> Script version: " + version); | ||
| ``` | ||
|
|
||
| ```js | ||
| // module.js | ||
| var _engine = "JerryScript"; | ||
| export _engine as engine; | ||
|
|
||
| export var version = "1.0 (e92ae0fb)"; | ||
| ``` | ||
|
|
||
| ```js | ||
| // module_2.js | ||
| var featureName = "EcmaScript 2015 modules"; | ||
| var year = 2018; | ||
|
|
||
| export function getFeatureDetails() { | ||
| return "Feature name: " + featureName + " | developed in " + year; | ||
| } | ||
| ``` | ||
|
|
||
| ## Unsupported features | ||
|
|
||
| * **snapshot** | ||
| * errors from the imported scripts | ||
| * redirection ( `export { a, b } from 'module.js'` ) | ||
| * default import and export | ||
| * `import b from 'module.js'` | ||
| * `export default b`, | ||
| * whole module import statements | ||
| * `import * from 'module.js` | ||
| * `import { * as module } from 'module.js` | ||
| * object freezing ( `Object.freeze (this)` ) | ||
|
|
||
| ### Redirection | ||
|
|
||
| An export statement can import variables from a custom module and export it directly from the current script. This statement is called redirection. In this case the `export { b } from 'module2.js'` works as the `b` was imported before then exported as a local variable. | ||
|
|
||
| ```js | ||
| import { a, b } from 'module.js' | ||
|
|
||
| print (a + b); | ||
| ``` | ||
|
|
||
| ```js | ||
| // module.js | ||
| export var a = 2; | ||
| export { b } from 'module2.js' | ||
| ``` | ||
|
|
||
| ```js | ||
| // module2.js | ||
| export var b = 40; | ||
| ``` | ||
|
|
||
| ### Default imports and exports | ||
|
|
||
| TODO: This part is going to be written in the next part of the patch. | ||
|
|
||
| ### Import the whole module | ||
|
|
||
| The whole module can be imported. In this case the `m` object would contain the exported parts of the module. If the import is not aliased, the `global object` would contain the exports. | ||
|
|
||
| ```js | ||
| import { * as m } from "./module.js" | ||
|
|
||
| print (m.secret_number); | ||
| print (m.getPrettifiedNumber()); | ||
| print (m.api.version); | ||
| ``` | ||
|
|
||
| ```js | ||
| // module.js | ||
| var secret_number = 42; | ||
| export secret_number; | ||
|
|
||
| export function getPrettifiedNumber() { | ||
| return "*** " + secret_number + " ***"; | ||
| } | ||
|
|
||
| export { ble as api } from "./ble.js"; | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.