riot/compiler

[TypeError: cleanSource(...).replace is not a function]

Closed this issue · 3 comments

When I call compiler.compile within node, the compile fails.
[TypeError: cleanSource(...).replace is not a function]

The same tag compiles fine in the browser.
node v4.4.7
riot-compiler: v2.5.2

@philmaker1 , please give us more details, mainly the tag that is generating the error.
any task runner?

@philmaker1 with no infos we can help you on this issue. I will be glad to reopen it whenever you will find a bit of time for us

@philmaker1 Were you reading the file with the 'fs' module? You may have inadvertently been passing a Buffer into riot.compile(). If you read the documentation for the fs.readFile callback, you'll see how the type of the second parameter changes depending on how readFile() is called.

test.tag:

<test>
    <h2>Hello, World!</h2>

    <script>
        this.f = function() {
        };
        this.v = opts.value;
    </script>
</test>

riot.js:

var riot = require('riot');
var fs = require('fs');

fs.readFile('test.tag', function(err, tag) {
    if (err) {
        console.error(err);
    } else {
        console.log(riot.compile(tag));
    }
});

Error

$ node riot.js
./node_modules/riot-compiler/lib/compiler.js:925
    .replace(CUST_TAG, function (_, indent, tagName, attribs, body, body2) {
     ^

TypeError: cleanSource(...).replace is not a function
    at Object.compile (./node_modules/riot-compiler/lib/compiler.js:925:6)
    at ./riot.js:8:20
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:415:3)});

Solution 1:

// Specify the encoding and tag will be a string
fs.readFile('test.tag', 'utf8', function(err, tag) {

Solution 2:

// Call toString() on the buffer
console.log(riot.compile(tag.toString()));

Either way, no more error

$ node riot.js
riot.tag2('test', '<h2>Hello, World!</h2>', '', '', function(opts) {
        this.f = function() {
        };
        this.v = opts.value;
});