You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

57 lines
2.5 KiB

<a name="writing-c-modules"></a> <!-- for old links -->
<h1 id="modules">Modules</h1>
<h2>CommonJS module loading frameworks</h2>
<p>There's no built-in module loading framework because it's difficult for a
single framework to match a wide variety of different module loading use cases.
The Duktape distributable includes several optional module loader frameworks,
for example:</p>
<table>
<tr>
<td><a href="https://github.com/svaarala/duktape/tree/master/extras/module-duktape">module-duktape</a></td>
<td>A Duktape 1.x compatible loader based on
<a href="http://wiki.commonjs.org/wiki/Modules/1.1.1">CommonJS modules version 1.1.1</a>,
with additional support for <code>module.exports</code> and a few Duktape specific
<code>module</code> object properties. The internals are documented in
<a href="https://github.com/svaarala/duktape/blob/master/doc/modules.rst">modules.rst</a>,
see <a href="http://wiki.duktape.org/HowtoModules.html">How to use modules</a> for examples.
This loader was a built-in in Duktape 1.x but was moved into an optional extra in Duktape 2.x.</td>
</tr>
<tr>
<td><a href="https://github.com/svaarala/duktape/tree/master/extras/module-node">module-node</a></td>
<td>A <a href="https://nodejs.org/api/modules.html">Node.js modules</a> compatible
loader. See <a href="http://wiki.duktape.org/HowtoNodejsModules.html">How to use Node.js-like modules</a>
for examples.</td>
</tr>
</table>
<p>You can also implement your own module loader from scratch: the above
loaders are implemented using the Duktape public API with no special access
to internals.</p>
<p>The module loaders provide a <code>require()</code> function which allows
modules to be loaded as follows:</p>
<pre class="ecmascript-code">
var mod = require('foo/bar');
mod.hello();
</pre>
<p>The loaders abstract actual module resolution/loading to user-provided hook(s)
to allow the loader to be embedded in wide variety of environments. For example:</p>
<ul>
<li>Module loading can be used in environments with no file system by loading
modules from a set of virtual built-in files.</li>
<li>On-the-fly transpiling can be done during loading, for example using
<a href="https://babeljs.io/">Babel</a>.</li>
</ul>
<h2>ES2015 modules</h2>
<p>There's currently no support for ES2015 import/export and ES2015 modules.</p>
<h2>C module convention</h2>
<p>There's a recommended (but not mandatory) C module convention which allows
C modules to be loaded and initialized from DLLs:
<a href="https://github.com/svaarala/duktape/blob/master/doc/c-module-convention.rst">c-module-convention.rst</a>.</p>