Skip to content
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

Preparing jsartoolkit5 to import it as a nodejs package #26

Open
kalwalt opened this issue Jan 9, 2020 · 17 comments
Open

Preparing jsartoolkit5 to import it as a nodejs package #26

kalwalt opened this issue Jan 9, 2020 · 17 comments
Assignees
Labels
enhancement New feature or request javascript all about javascript code npm

Comments

@kalwalt
Copy link
Owner

kalwalt commented Jan 9, 2020

I would do these changes as soon my branch fixing-nft #1 will be merged in the master of artoolkitx/jsartoolkit5, collecting here all the issue and infos needed for the process.
Started form this issue #24.

@kalwalt kalwalt added enhancement New feature or request javascript all about javascript code npm labels Jan 9, 2020
@kalwalt kalwalt self-assigned this Jan 9, 2020
@kalwalt
Copy link
Owner Author

kalwalt commented Jan 9, 2020

It need to be changed in artoolkit.api.js all the references to the window object and self ( we should use global instead for node) in detail at the beginning:

var scope;
if (typeof window !== 'undefined') {
scope = window;
} else {
scope = self;
};

and maybe in other parts of the lib.
In artoolkit.three.js inside the tick() function, all the window object need to be changed:
var tick = function() {
if (window.ARController && window.THREE) {
integrate();
if (window.ARThreeOnLoad) {
window.ARThreeOnLoad();

@kalwalt
Copy link
Owner Author

kalwalt commented Jan 18, 2020

i did some progress on this side, i have created a new repsitory https://github.com/kalwalt/jsartoolkit5-nodejs to test and make the required changes to jsartoolkit5 for use it with node. Actually artoolkit.api.js can be imported , not the minified lib. I will show you more details soon.

@kalwalt
Copy link
Owner Author

kalwalt commented Jan 25, 2020

I created a branch https://github.com/kalwalt/jsartoolkit5/tree/nodejs-feature in which i'm experimenting jsartoolkit5 as a module. I build the jsartollkit5 lib with the Modularize option

var MODULARIZE_FLAGS = ' -s EXPORT_ES6=1 -s MODULARIZE=1 '

now importing artoolkit.min.js into node i get this error:

node index.js
(node:10785) ExperimentalWarning: The ESM module loader is experimental.
we are here !
file:///home/walter/kalwalt-github/test/jsartoolkit5-nodejs/jsartoolkit5/build/artoolkit.min.js:9
var Module=typeof Module!=="undefined"?Module:{};(function(){"use strict";var scope;if(typeof window!=="undefined"){scope=window}else if(typeof window=="undefined"){scope=global}else{scope=global}if(scope.artoolkit_wasm_url){var downloadWasm=function(url){return new Promise(function(resolve,reject){var wasmXHR=new XMLHttpRequest;wasmXHR.open("GET",url,true);wasmXHR.responseType="arraybuffer";wasmXHR.onload=function(){resolve(wasmXHR.response)};wasmXHR.onerror=function(){reject("error "+wasmXHR.status)};wasmXHR.send(null)})};var wasm=downloadWasm(scope.artoolkit_wasm_url);Module.instantiateWasm=function(imports,successCallback){console.log("instantiateWasm: instantiating synchronously");wasm.then(function(wasmBinary){console.log("wasm download finished, begin instantiating");var wasmInstantiate=WebAssembly.instantiate(new Uint8Array(wasmBinary),imports).then(function(output){console.log("wasm instantiation succe

ReferenceError: module is not defined
    at file:///home/walter/kalwalt-github/test/jsartoolkit5-nodejs/jsartoolkit5/build/artoolkit.min.js:9:28048
    at file:///home/walter/kalwalt-github/test/jsartoolkit5-nodejs/jsartoolkit5/build/artoolkit.min.js:9:28141
    at file:///home/walter/kalwalt-github/test/jsartoolkit5-nodejs/index.js:5:13
    at ModuleJob.run (internal/modules/esm/module_job.js:109:37)
    at async Loader.import (internal/modules/esm/loader.js:133:24)

because this line:

module.exports = { artoolkit, ARController, ARCameraParam };

@andypotato
Copy link

You're trying to build an ES6 module but the API is exporting modules using CommonJS format (module.exports...).

I'd love to see jsartoolkit built as a module as it will enable dependent projects like AR.js to be built as modules too. Will give it a try myself.

@kalwalt
Copy link
Owner Author

kalwalt commented Feb 9, 2020

You're trying to build an ES6 module but the API is exporting modules using CommonJS format (module.exports...).

yes, probably has not much sense...

I'd love to see jsartoolkit built as a module as it will enable dependent projects like AR.js to be built as modules too. Will give it a try myself.

Me too, this is the idea! Ping me if you have questions or for help!

@andypotato
Copy link

I will start by installing the environment to build the artoolkit module using emscripten. Once I got that running I can have a deeper look into the API itself.

Probably it will need a rewrite as ES6 module and we could use babel / webpack to create different output formats for CommonJS / Browser etc.

Will give compiling a shot first!

@kalwalt
Copy link
Owner Author

kalwalt commented Feb 10, 2020

@andypotato You can also look at my branch https://github.com/kalwalt/jsartoolkit5/tree/nodejs-feature with some proposed modifications for nodejs. Good luck!

@andypotato
Copy link

I haven't worked with emscripten Modules before but it seems pretty straightforward. So far I've managed to compile the debug and minified versions of artoolkit (artoolkit.debug.js / artoolkit.min.js), also the wasm module and the JS wrapper.

If I understand your code correctly you created artoolkit.api.js as a kind of high-level API on top of the original artoolkit C-Module, correct? Also artoolkit.three.js as another set of convenience methods on top of Three.js?

@kalwalt
Copy link
Owner Author

kalwalt commented Feb 16, 2020

@andypotato artoolkit.api.js is used in combination with artoolkit.debug.js if you want a lib with debug symbols. Apart this aspect artoolkit.api.js is added in the minification version with a --pre-is flag so every changes/modifications you do in artoolkit.api.js code needs a rebuild of the entire project. and yes is a kind of high level API: all the methods you define in ARToolKitJS.cpp are exported in the bind file ARBindEM.cpp in artoolkit.api.js are imported as an array in the Module https://github.com/artoolkitx/jsartoolkit5/blob/244b2b23286403e78fa24805b34509dc5a88052f/js/artoolkit.api.js#L1798-L1859
Yes artoolkit.three.js is also another set to facilitate Three.js with jsartoolkit5.

@andypotato
Copy link

@kalwalt thanks for the input!
I believe to properly modularize this project we need to break it down into separate modules. My suggestion is to split the emscripten build into a separate repository and what you call "jsartoolkit5" then includes that module. Currently this is just a bit too messy with all the API, Three.js integration.

With a bit of luck I should have a modularized emscripten build of the original ARToolkit in a separate repository ready by tomorrow, then we can take it from there.

@kalwalt
Copy link
Owner Author

kalwalt commented Feb 17, 2020

@andypotato not understand well how you separate the two things but if you create a repository i will look at. 🙂

@andypotato
Copy link

andypotato commented Feb 17, 2020 via email

@kalwalt
Copy link
Owner Author

kalwalt commented Feb 18, 2020

Thank you @andypotato for the explanantion, it's very late now here i will answer tomorrow. 😄

@andypotato
Copy link

@kalwalt That's what I mean: https://github.com/andypotato/artoolkit5-js

It's plug & play. Include the module and you can remove all stuff related to ARToolkitX from your code base. Full WASM support is also included already.

@kalwalt
Copy link
Owner Author

kalwalt commented Feb 18, 2020

@kalwalt That's what I mean: https://github.com/andypotato/artoolkit5-js

It's plug & play. Include the module and you can remove all stuff related to ARToolkitX from your code base. Full WASM support is also included already.

That's magic! 🎉 💯 🙌 i will try your code when i have a bit of time....

BTW my repository was a fork of original artoolkit/jsartoolkit5 repository that was deleted recently because the owner went in bankrupt. @ThorstenBux may explain this to you. I'm sorry but It was not created by the user you wrote in the Readme

About Ar.js:
Maybe this could be part of the AR-js-org one day ? Have you seen this?

@andypotato
Copy link

Maybe this could be part of the AR-js-org one day ? Have you seen this?

Sure, actually that's more or less why I'm doing this. I really like AR.js and the original idea by @jeromeetienne to integrate jsartoolkit as a service / component for A-Frame was pretty genius.

However AR.js was never designed as a real library and this makes it hard to re-use it in projects outside of simple websites. Quite frankly, the codebase today is a complete mess and very much in need of a total refactoring. A good start would be to streamline the build process and load all external dependencies like A-Frame, Three.js and jsartoolkit as modules. A-Frame and Three.js are already available - jsartoolkit I can handle.

When it's ready and @nicolocarpignoli could use a hand with all the refactoring and integration then I can help. I believe without cleaning up the codebase first, this whole idea of the web based AR design tool won't work.

Taking the opportunity to say hi to @ThorstenBux too. I saw you're working on a pretty similar ES6 project, maybe we can somehow merge our work?

@kalwalt
Copy link
Owner Author

kalwalt commented Feb 18, 2020

Sure, actually that's more or less why I'm doing this. I really like AR.js and the original idea by @jeromeetienne to integrate jsartoolkit as a service / component for A-Frame was pretty genius

Ah ok! That is perfect!

However AR.js was never designed as a real library and this makes it hard to re-use it in projects outside of simple websites. Quite frankly, the codebase today is a complete mess and very much in need of a total refactoring. A good start would be to streamline the build process and load all external dependencies like A-Frame, Three.js and jsartoolkit as modules. A-Frame and Three.js are already available - jsartoolkit I can handle.

When it's ready and @nicolocarpignoli could use a hand with all the refactoring and integration then I can help. I believe without cleaning up the codebase first, this whole idea of the web based AR design tool won't work.

I agree with you. i think that is a long time that people are asking to load Ar.js as a module and other features. I realized that the coodebase need a restyling when i started with @nicolocarpignoli the NFT integration, but sincerely i have not the required skills, expertise and time to do this. I'm very happy that this now is happening.

Taking the opportunity to say hi to @ThorstenBux too. I saw you're working on a pretty similar ES6 project, maybe we can somehow merge our work?

yes @ThorstenBux did the awesome works to port artoolkitx to javascript with Emscripten: artoolkitX.js it has already ES6 and WASM. See also this discussion to publish it as a npm package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request javascript all about javascript code npm
Projects
None yet
Development

No branches or pull requests

2 participants