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

Include #3406

Open
dettlafferetein opened this issue Feb 23, 2023 · 0 comments
Open

Include #3406

dettlafferetein opened this issue Feb 23, 2023 · 0 comments

Comments

@dettlafferetein
Copy link

dettlafferetein commented Feb 23, 2023

Pug Version: 3.0.2

Node Version: 16.13.0

I encountered an weird issue. When including a library (I was using math.js), pug.render would fail on the first attempt and succeed on all the latter attempts (within single execution). Here's my test code:

const pug = require('pug');

const template =
    "include /jslibraries/a808a2a1544736cc444b233aa52d4dc5.pug\n" +
    "\n" +
    "html\n" +
    "    body\n" +
    "        h1 Test Template\n" +
    "        p math.sqrt(4) = #{math.sqrt(4).toString()}";

const options = {
    basedir: process.cwd()
};

console.log('one');
try {
    console.log(pug.render(template, options));
} catch (err) {
    console.log(err.stack);
}
console.log('two');
try {
    console.log(pug.render(template, options));
} catch (err) {
    console.log(err.stack);
}

What I get as a result is:

PS C:\mypath> node test.js
one
TypeError: C:\mypath\jslibraries\mathjs.pug:6


Cannot read properties of undefined (reading 'sqrt')
    at eval (eval at wrap (C:\mypath\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:22:62)
    at template (eval at wrap (C:\mypath\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:23:7)
    at Object.exports.render (C:\mypath\node_modules\pug\lib\index.js:423:43)
    at Object.<anonymous> (C:\mypath\test.js:20:13)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
two
<html><body><h1>Test Template</h1><p>math.sqrt(4) = 2</p></body></html>

/jslibraries/mathjs.pug contains minified math.js with a dash, newline and 4 spaces in front of it. Start of the file looks like this, to give you a better idea:

-
    !function(e,t){"object"==typeof exports&&"object"==typeof module?module.............................

I have also tried prepending contents of /jslibraries/mathjs.pug to the beginning of the template, results are identical.

The only possible explanation I can see is that due to math.js being a fairly large script there is some async issue, which allowes the template to continue rendering before the included script is evaluated. I haven't had a chance to investigate further myself unfortunately.

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

1 participant