Skip to content

Commit

Permalink
feat: Support CommonJS javascript module for compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongsik Yoo committed Aug 8, 2017
1 parent 534c48d commit 6179db1
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 20 deletions.
4 changes: 3 additions & 1 deletion example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/package.json
Expand Up @@ -11,7 +11,7 @@
"author": "",
"license": "MIT",
"devDependencies": {
"assemblyscript-live-loader": "git+https://github.com/dongsik-yoo/assemblyscript-live-loader.git",
"assemblyscript-live-loader": "^0.1.0",
"babel": "^6.23.0",
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
Expand Down
79 changes: 77 additions & 2 deletions example/public/bundle.js
Expand Up @@ -32708,6 +32708,8 @@ module.exports = {
/* 199 */
/***/ (function(module, exports) {

var compatibleModule;
if (typeof WebAssembly !== "undefined") {
var buffer = new ArrayBuffer(1705);
var uint8 = new Uint8Array(buffer);
uint8.set([
Expand All @@ -32728,9 +32730,82 @@ var WebAssemblyModule = function(deps) {

return new WebAssembly.Instance(new WebAssembly.Module(buffer), deps || defaultDeps);
};
compatibleModule = WebAssemblyModule;
}
else {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function add(a, b) {
return a + b;
}
exports.add = add;
function subtract(a, b) {
return a - b;
}
exports.subtract = subtract;
function multiply(a, b) {
return a * b;
}
exports.multiply = multiply;
function divide(a, b) {
return a / b;
}
exports.divide = divide;
function factorial(num) {
var tmp = num;
if (num < 0) {
return -1;
}
else if (num === 0) {
return 1;
}
while (num > 2) {
tmp *= num;
num -= 1;
}
return tmp;
}
exports.factorial = factorial;
function addWithLoopCount(count, a, b) {
var i = 0;
for (; i < count; i += 1) {
add(a, b);
}
}
exports.addWithLoopCount = addWithLoopCount;
function subtractWithLoopCount(count, a, b) {
var i = 0;
for (; i < count; i += 1) {
subtract(a, b);
}
}
exports.subtractWithLoopCount = subtractWithLoopCount;
function multiplyWithLoopCount(count, a, b) {
var i = 0;
for (; i < count; i += 1) {
multiply(a, b);
}
}
exports.multiplyWithLoopCount = multiplyWithLoopCount;
function divideWithLoopCount(count, a, b) {
var i = 0;
for (; i < count; i += 1) {
divide(a, b);
}
}
exports.divideWithLoopCount = divideWithLoopCount;
function factorialWithLoopCount(count, num) {
var i = 0;
for (; i < count; i += 1) {
factorial(num);
}
}
exports.factorialWithLoopCount = factorialWithLoopCount;

module.exports = WebAssemblyModule;

compatibleModule = function() {}
compatibleModule.prototype.exports = exports;
}
module.exports = compatibleModule;

/***/ })
/******/ ]);
Expand Down
2 changes: 1 addition & 1 deletion example/public/bundle.js.map

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions example/public/index.html
Expand Up @@ -45,13 +45,7 @@
<script src="js/maps/world.js"></script>

<!-- Main -->
<!-- <script src="bundle.js"></script> -->

<div><img src="https://user-images.githubusercontent.com/26706716/28766417-6f76c5ae-760a-11e7-8123-efbaed56a7d5.png" alt="AssemblyScript"></div>
<br>
<div class="webpack-img"><img width="200" src="https://camo.githubusercontent.com/d18f4a7a64244f703efcb322bf298dcb4ca38856/68747470733a2f2f7765627061636b2e6a732e6f72672f6173736574732f69636f6e2d7371756172652d6269672e737667" alt="webpack MODULE BUNDLER"></div>
<div class="webpack">webpack</div>
<div class="webassembly"><img class="img-wa" src="http://webassembly.org/css/webassembly.svg" alt="WebAssembly"></div>
<script src="bundle.js"></script>
</body>

</html>
66 changes: 63 additions & 3 deletions lib/index.js
@@ -1,6 +1,7 @@
'use strict';

var fs = require('fs');
var ts = require('typescript');
var assemblyscript = require('assemblyscript');
var Compiler = assemblyscript.Compiler;
var CompilerTarget = assemblyscript.CompilerTarget;
Expand Down Expand Up @@ -39,12 +40,12 @@ function compile(source) {
}

/**
*
* Create a module using WebAssembly.Module
* @param {string} source assemblyscript source
* @param {Buffer} wasm WebAssembly Buffer
* @returns {string} module string
*/
function createModule(source, wasm) {
function createWasmModule(source, wasm) {
var i = 0;
var length = wasm.length;
var module = [];
Expand All @@ -63,19 +64,78 @@ function createModule(source, wasm) {
return module.join('\n');
}

/**
* Creates commonjs module for javascript
* @param {string} source assemblyscript source
* @returns {string} module string
*/
function createJsModule(source) {
var compilerOptions = {
compilerOptions: {
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS,
alwaysStrict: false
}
};
var transpiled = ts.transpileModule(source, compilerOptions);

return transpiled.outputText;
}

/**
* Creates compatible module with Javascript, WebAssembly both
* @param {string} jsModule - javascript module
* @param {string} wasmModule - WebAssembly module
* @example
* var compatibleModule;
* if (typeof WebAssembly !== "undefined") {
* // ... wasmModule ...
* compatibleModule = WebAssemblyModule;
* }
* else {
* // .. jsModule ...
* compatibleModule = function() {};
* compatibleModule.prototype.exports = exports;
* }
* module.exports = comptaibleModule;
* @returns {string} module string
*/
function createCompatibleModule(jsModule, wasmModule) {
var module = 'var compatibleModule;\n';
module += 'if (typeof WebAssembly !== "undefined") {\n';
module += wasmModule;
module += 'compatibleModule = WebAssemblyModule;\n';
module += '}\n';
module += 'else {\n';
module += jsModule;
module += '\n';
module += 'compatibleModule = function() {}\n';
module += 'compatibleModule.prototype.exports = exports;\n';
module += '}\n';
module += 'module.exports = compatibleModule;';

return module;
}

/**
* Webpack loader for assemblyscript to transform wasm and bundle it
* @param {string} source - assemblyscript source file
* @returns {string} module string
*/
function AssemblyScriptLiveLoader(source) {
var jsModule;
var wasmModule;

if (this.cacheable) {
this.cacheable();
}

this.addDependency(wasmFooter);

return createModule(source, compile(source));
jsModule = createJsModule(source);
wasmModule = createWasmModule(source, compile(source));

return createCompatibleModule(jsModule, wasmModule);
}

module.exports = AssemblyScriptLiveLoader;
2 changes: 0 additions & 2 deletions lib/wasmFooter.js
Expand Up @@ -13,5 +13,3 @@ var WebAssemblyModule = function(deps) {

return new WebAssembly.Instance(new WebAssembly.Module(buffer), deps || defaultDeps);
};

module.exports = WebAssemblyModule;
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "assemblyscript-live-loader",
"version": "0.1.0",
"version": "0.2.0",
"description": "A webpack loader that compiles assemblyscript to WebAssembly(wasm) and bundle it",
"main": "lib/index.js",
"scripts": {
Expand All @@ -26,7 +26,8 @@
"devDependencies": {
"eslint": "^4.3.0",
"eslint-config-tui": "^1.0.1",
"jasmine": "^2.6.0"
"jasmine": "^2.6.0",
"typescript": "^2.4.2"
},
"dependencies": {
"assemblyscript": "^0.1.0"
Expand Down
4 changes: 4 additions & 0 deletions test/source.js
@@ -1,3 +1,7 @@
export function add(a: int, b: int): int {
return a + b;
}

export function subtract(a: int, b: int): int {
return a - b;
}
2 changes: 1 addition & 1 deletion test/spec.js
Expand Up @@ -23,6 +23,6 @@ describe('assemblyscript', function() {
expect(loaderContext.addDependency).toHaveBeenCalled();
expect(module).toBeDefined();
expect(module.length).not.toBe(0);
expect(module).toMatch(/module.exports = WebAssemblyModule;/);
expect(module).toMatch(/module.exports = compatibleModule;/);
});
});

0 comments on commit 6179db1

Please sign in to comment.