-
Notifications
You must be signed in to change notification settings - Fork 6
4 Modules
- A template can import/include many file types:
-
Mask — import components or nodes from other
*.mask
files. -
Javascript —
CommonJS
modules are used, but via the configuration a javascript module loader can be defined to support any module system. - JSON — load data from files or APIs to use it within templates
-
HTML — load html and parse it to
Mask AST
, so that it can be used as genericmask
nodes. -
Style — load
css
styles.
-
Mask — import components or nodes from other
- A template can export components or nodes.
- Imports are loaded in parallel.
📦 Use Mask Optimizer to create single style, markup and script files from many sources.
-
1
Export -
2
Import-
2.1
Path -
2.2
Asynchronous import-
2.2.1
Async Mask -
2.2.2
Async Javascript/Json -
2.2.3
Async Styles -
2.2.4
Progress
-
-
2.3
Mask module-
2.3.1
Embed -
2.3.2
Import nodes -
2.3.3
Import components
-
-
2.4
Javascript module -
2.5
Data module -
2.6
Style module -
2.7
HTML module
-
-
3
Loaders-
3.1
Mask, Html, Json -
3.2
Javascript -
3.3
Style
-
There are no keywords and no specific syntax to export things, thats why any template can be rendered as a root template. But when a template is requested as a module, then exported are:
- Components, which are created with
define
directive - Nodes. All generic Mask Nodes.
Basic examples
-
Export generic nodes
// baz.mask h4 #baz > 'Baz'
-
Create and export components
// foo.mask define foo { /* definition */ }
-
Create and export components and nodes
// foo.mask define foo { /* definition */ } define baz { /* definition */ } h4 #qux > 'Lorem ~[name]' h4 #quux > 'Ipsum'
// mask examples
import from 'foo';
import foo from 'foo.mask';
import foo as baz from './foo';
import foo as xxx, baz as yyy from 'foo';
import * as Titles from 'foo.mask';
import * as Titles, foo, baz from 'foo.mask';
import h4#qux as Title from './baz';
import async Baz as Title, Foo from './baz';
// javascript example
import User from '/models/User.js';
Type of the imported module is parsed by extension, or is mask
per default.
To specify the type manually use is
keywoard.
import * as Data from '/api/v1/application/data' is json;
log(Data);
File extension defines the type of a module. When no extension is provided then mask
extension will be automatically added.
-
Mask:
mask
or empty file extension -
Javascript:
js
,es6
,coffee
-
Style:
css
,sass
,less
-
Html:
html
-
Data:
json
In NodeJS this is process.cwd()
. In Browser this is the baseURI
of the document. You can also set custom root path:
-
via Javascript:
mask.Module.cfg('base', EXPRESSION); mask.Module.cfg('base', 'foo/'); // e.g
-
via Mask
import:base (EXPRESSION); import:base ('foo/'); // e.g
/path
- is an absolute path and is concatenated with a root path to get the file path.
path
, ./path
- are relative paths to the parents module location or parents component location. But if the template is the root module, then the context
is checked for filename
/dirname
properties, if they are not set, then the root path is used.
Setting the current templates location via context object:
mask.render(template, module, {
dirname: '/foo/baz/'
})
Use async
keyword to define particular import
to be loaded asynchronously. That means the module is not waiting for the dependency to be loaded.
To use this dependency later in the view use await
component (if you are not sure that it will be loaded until then).
import async Foo from 'Foo';
h4 > 'Header Sample'
await Foo;
import async Users from '/api/users' is json;
h4 > 'Header'
// Waits until the json is loaded, and then renders children
await (Users) {
each (Users) > 'Username: ~[userName]'
}
Styles are asynchonous per default
Use @progress
placeholder inside the await
component.
import async Users from '/api/users' is json;
await Foo {
@progress {
'Loading'
// Any nodes or components, which are removed
// after the load process is ended,
// and before actual `Foo` render
}
}
For simple cases you can just embed everything from a module into the requested template. Embedding means the imported template is rendered in-place.
import from 'foo';
-
Import all nodes as alias
import * as XXX from 'foo'; // now `XXX` can be used as normal tag name within the template XXX; //e.g: use `XXX` nodes to render each item in `foos` array in the model ul > each (foos) > li > XXX;
-
Use css selector to get specific node for an import
filter
method is used to get the node, not thefind
, so it should be the root node in the set.import h4#qux as lorem from 'foo'; lorem;
-
By defined name
import foo from 'foo'; ul > each (foos) > li > foo;
-
By defined name with alias
import foo as FOO from 'foo'; ul > each (foos) > li > FOO;
Imported object is set to the current controllers scope. So it can be accessed in interpolations and in define
extendings.
-
Scope access examples
import * as App from '/configs/application.js'; h4 > '~[App.name]'
import name from '/configs/application.js'; h4 > '~[name]'
-
Component definition
import * as Utils from '/helpers/baseutils.js'; define foo extends Utils { /* definition */ }
Similar to Javascript
{
"application": "Foo App"
}
import * as Config from 'config.json';
h4 > '~[Config.application]'
Speaking about the UI it is always needed for a component to load stylesheets. Import
can load them for you.
#foo { background: red; }
import from 'foo.css';
section #foo > 'Lorem ipsum' // has then red background color
Similar to *.mask
HTML is loaded and parsed to Mask AST
Lorem <i>ipsum</i>
import * as MyTemplate from 'lorem.html';
section #foo {
MyTemplate;
}
fs
module is used to load the mask files
XMLHttpRequest
is used to load the files
mask.cfg('getFile', function (path) {
// should return Promise or Deferred
// and when ready resolve with the string, or reject with an `Error`.
})
❕
JSON.parse
is used forjson
data
require
is used to load the javascript module
<script src>
tag is used to load the module. MaskJS also provides global module
object to simulate CommonJS
exports.
mask.cfg('getScript', function (path) {
// should return Promise or Deferred
// and when ready resolve with `exported` object, or reject with an `Error`.
})
Not loading.
<link href>
tag is used.
mask.cfg('getStyle', function(path) {
// should return Promise or Deferred
// and when ready just resolve it
})
🏁 If you have any questions, please open an issue or mail us.