DasRed / js-bbcode-parser

24 stars 7 forks source link

Having a heck of a time getting it to run #23

Closed mark8044 closed 4 months ago

mark8044 commented 4 months ago

Im having a heck of a time getting this to run.

// this imports the default parser
import bbCodeParser from 'js-bbcode-parser';
// this import the class constructor of the parser
import BBCodeParser from 'js-bbcode-parser/src/index.js';

// use to create a clean parser
const parserA = new BBCodeParser({});

// configure the default parser with
bbCodeParser.setCodes({});

Leads to error:

Uncaught SyntaxError: Cannot use import statement outside a module

After some googling it seems I might need require.js?

Tried it and after more errors I get to this point:

         require(['/js-bbcode-parser/src/index.js'], function (BBCodeParser) {
const parser = new BBCodeParser({
    '\\[br\\]': '<br>'
});
alert(parser.parse('This is a text[br]with HTML Break.'));
            });

Which then brings me to error:

Uncaught TypeError: BBCodeParser is not a constructor

Totally confused here. Is this an issue that I need to have node.js installed on a server? Never used it before and prefer not to

DasRed commented 4 months ago

Hey,

you don't need requirejs. You have to import it as ES module. If your structure does not support modules, you should use the commonjs import.

File A with ES module import:

File B with commonjs import:

Bye :)

mark8044 commented 4 months ago

Thank you for the direction, I think there is some progress. Since I do not use node.js, It sounds like File A -> Bullet 2 is the correct one for me

Doing:

// this imports the default parser
const bbCodeParser = require('js-bbcode-parser');
// this import the class constructor of the parser
const BBCodeParser = require('js-bbcode-parser/src/index.js');

I end up with the following browser error:

Uncaught SyntaxError: Cannot use import statement outside a module

So then doing what you said it sounds like my core JS that does the module load, itself must be a module, so I added type="module" to my core JS file

<script type="module" src="index.js"></script> <--- index.js does: import bbCodeParser from 'js-bbcode-parser';

Now I end up with error:

Uncaught SyntaxError: Unexpected identifier 'bbCodeParser'

In case it matters, this is all done in the context of jQuery:


 $(document).ready(function() {

//this imports the default parser
import bbCodeParser from '/forums/bpeditor/htmlbbcode/js-bbcode-parser';
//this import the class constructor of the parser
import BBCodeParser from '/forums/bpeditor/htmlbbcode/js-bbcode-parser/src/index.js';

.... etc
});
mark8044 commented 4 months ago

Ok it looks like loading all this inside jQuery was the issue, I just moved the import lines outside the document.ready and it all works.

Thank you on the education with modules! 👍 🥇

DasRed commented 4 months ago

You are welcome.

imports are always at the top of a module and out of any blocks.

But you can use dynamic imports, if you want to import something inside a block

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import