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

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

Closed
ghost opened this issue Aug 11, 2016 · 3 comments
Closed

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

ghost opened this issue Aug 11, 2016 · 3 comments

Comments

@ghost
Copy link

ghost commented Aug 11, 2016

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

@aMarCruz
Copy link
Contributor

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

@GianlucaGuarini
Copy link
Member

@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

@speckins
Copy link

speckins commented Nov 5, 2016

@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;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants