kanasimi / wikiapi

JavaScript MediaWiki API for node.js
https://kanasimi.github.io/wikiapi/
BSD 3-Clause "New" or "Revised" License
47 stars 5 forks source link

Snowpack support #45

Closed KararTY closed 3 years ago

KararTY commented 3 years ago

Does this module support browsers?

I'm specifically using it together with Snowpack (& Skypack CDN) to make it work on the browser but I get the following errors:

[23:33:25] [snowpack] + wikiapi@1.15.1
[23:33:26] [esinstall:wikiapi] Home\DEV\ppsl-app-v1.5\node_modules\wikiapi\Wikiapi.js
   Import "./_CeL.loader.nodejs.js" could not be resolved from file.
[23:33:26] [esinstall:wikiapi] Home\DEV\ppsl-app-v1.5\node_modules\wikiapi\Wikiapi.js
   Import "./_CeL.loader.nodejs.js" could not be resolved from file.
[23:33:26] [esinstall:wikiapi]  Home\DEV\ppsl-app-v1.5\node_modules\wikiapi\_CeL.loader.nodejs.js?commonjs-external
   Module "Home\DEV\ppsl-app-v1.5\node_modules\wikiapi\_CeL.loader.nodejs.js" could not be resolved by Snowpack (Is it installed?).
[23:33:26] [snowpack] Install failed.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

This is how I import it:

import wikiapi from 'wikiapi'
kanasimi commented 3 years ago

The module needs cejs. Do you also import cejs?

KararTY commented 3 years ago

The module needs cejs. Do you also import cejs?

I've imported "cejs" but I still get the same issue:

[24:36:56] [snowpack] + wikiapi@1.15.1
[24:36:57] [esinstall:wikiapi] Home\DEV\ppsl-app-v1.5\node_modules\wikiapi\Wikiapi.js
   Import "./_CeL.loader.nodejs.js" could not be resolved from file.
[24:36:57] [esinstall:wikiapi] Home\DEV\ppsl-app-v1.5\node_modules\wikiapi\Wikiapi.js
   Import "./_CeL.loader.nodejs.js" could not be resolved from file.
[24:36:57] [esinstall:wikiapi]  Home\DEV\ppsl-app-v1.5\node_modules\wikiapi\_CeL.loader.nodejs.js?commonjs-external
   Module "Home\DEV\ppsl-app-v1.5\node_modules\wikiapi\_CeL.loader.nodejs.js" could not be resolved by Snowpack (Is it installed?).
[24:36:57] [snowpack] Install failed.
kanasimi commented 3 years ago

hmm... Do you have codes to test?

KararTY commented 3 years ago

hmm... Do you have codes to test?

I've made a reproducible Github repository over here: https://github.com/KararTY/wikiapi-test

KararTY commented 3 years ago

This is still broken, I've updated https://github.com/KararTY/wikiapi-test

The new error I'm getting is:

cejs.v4.2.2.js:91 TypeError: fs.readFileSync is not a function
    at cejs.v4.2.2.js:55
    at cejs.v4.2.2.js:79
    at createCommonjsModule (cejs.v4.2.2.js:10)
    at cejs.v4.2.2.js:30
(anonymous) @ cejs.v4.2.2.js:91
createCommonjsModule @ cejs.v4.2.2.js:10
(anonymous) @ cejs.v4.2.2.js:30
wikiapi.v1.15.1.js:530 Uncaught TypeError: Cannot read property 'match' of undefined
    at wikiapi.v1.15.1.js:530
    at wikiapi.v1.15.1.js:643
    at createCommonjsModule (wikiapi.v1.15.1.js:239)
    at wikiapi.v1.15.1.js:509
(anonymous) @ wikiapi.v1.15.1.js:530
(anonymous) @ wikiapi.v1.15.1.js:643
createCommonjsModule @ wikiapi.v1.15.1.js:239
(anonymous) @ wikiapi.v1.15.1.js:509

image

Are you sure this module works in the browser? There's a lot of filesystem (fs) calls and module calls.

kanasimi commented 3 years ago

This looks a bit complicated... still trying...

KararTY commented 3 years ago

This looks a bit complicated... still trying...

What's the usual way of importing this module into the browser?

kanasimi commented 3 years ago

It is a pure npm now. For browser, I using the method list in https://github.com/kanasimi/CeJS/tree/master/application/net/wiki#usage-in-mediawiki-user-script-userexamplecommonjs

kanasimi commented 3 years ago
// Since Snowpack cannot read utf-8 or utf-16 script file now,
// you MUST create a web server, and set main_script_URL.
//window.CeL = { main_script_URL: 'https://localhost/path/to/ce.js' };
// Default URL to ce.js is https://kanasimi.github.io/CeJS/ce.js

import cejs from 'cejs';
cejs.then(CeL_module => import('wikiapi')).then(Wikiapi_module => main_process(Wikiapi_module.default));

async function main_process(Wikiapi) {
    const wiki = new Wikiapi({ project: 'en', origin: '*' });
    const page_data = await wiki.page('ABC');
    console.log(page_data);
}