jsreport / jsreport-core

The minimalist jsreport rendering core
GNU Lesser General Public License v3.0
85 stars 24 forks source link

Not working with typescript #51

Closed Nogostradamus closed 4 years ago

Nogostradamus commented 4 years ago

With a minimal example:

let jsreport = require('jsreport-core')();
let fs = require('fs');

jsreport.init().then(() => {
    return jsreport.render({
        template: {
            content: '<h1>Hello {{foo}}</h1>',
            engine: 'handlebars',
            recipe: 'chrome-pdf'
        },
        data: {
            foo: "world"
        }
    }).then((resp) => {
        fs.writeFile('test.pdf', resp, function(data){
            console.log(data);
        });
    });
}).catch((e) => {
    console.error(e)
});

I'm able to generate pdf all right. When I try to use it with typescript with webpack I have an error: { "errorType": "TypeError", "errorMessage": "The \"path\" argument must be of type string. Received undefined", "trace": [ "TypeError [ERR_INVALID_ARG_TYPE]: The \"path\" argument must be of type string. Received undefined", " at validateString (internal/validators.js:117:11)", " at Object.dirname (path.js:1128:5)", " at e.exports (/var/task/reporting-engine.js:198:1060)", " at e.exports (/var/task/reporting-engine.js:198:222)", " at Object. (/var/task/reporting-engine.js:198:138)", " at r (/var/task/reporting-engine.js:1:124)", " at Object. (/var/task/reporting-engine.js:59:424)", " at Object. (/var/task/reporting-engine.js:59:17244)", " at r (/var/task/reporting-engine.js:1:124)", " at Object. (/var/task/reporting-engine.js:52:2818)", " at Object. (/var/task/reporting-engine.js:52:3077)", " at r (/var/task/reporting-engine.js:1:124)", " at Module. (/var/task/reporting-engine.js:52:3216)", " at r (/var/task/reporting-engine.js:1:124)", " at /var/task/reporting-engine.js:1:923", " at Object. (/var/task/reporting-engine.js:1:934)", " at Module._compile (internal/modules/cjs/loader.js:1158:30)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)", " at Module.load (internal/modules/cjs/loader.js:1002:32)", " at Function.Module._load (internal/modules/cjs/loader.js:901:14)", " at Module.require (internal/modules/cjs/loader.js:1044:19)", " at require (internal/modules/cjs/helpers.js:77:18)" ] } My package:

"dependencies": {
    "aws-sdk": "^2.651.0",
    "emitter": "^0.0.5",
    "handlebars": "^4.7.4",
    "jsreport-chrome-pdf": "^1.7.1",
    "jsreport-core": "^2.7.2",
    "jsreport-handlebars": "^2.1.0",
    "middy": "^0.34.0",
    "puppeteer": "^2.1.1"
  },
  "devDependencies": {
    "@types/aws-lambda": "^8.10.15",
    "@types/jsreport-core": "^1.5.1",
    "@types/node": "^10.12.2",
    "@types/request-promise-native": "^1.0.15",
    "lambda-local": "^1.7.1",
    "mocha": "5.0.0",
    "supertest": "3.0.0",
    "ts-loader": "^5.3.0",
    "typescript": "^3.1.6",
    "unit.js": "2.0.0",
    "webpack": "^4.24.0",
    "webpack-cli": "^3.1.2"
  },

Does anyone use JSreport-core with TS. Can't find any examples.

pofider commented 4 years ago

Maybe use it as any other typeless library as we don't maintain the types currently.

pofider commented 4 years ago

It can get complicated with webpack, because jsreport dynamically reads some files/scripts. Is there a reason for using webpack on the server side?

Nogostradamus commented 4 years ago

I'm building AWS lambda function to generate pdf reports. I use cloud formation to setup lambda, roles, and s3 bucket. So my cloud formation script builds js handler using webpack aith packages, and typescript. The output is index.js. That is uploaded as lambda to the AWS.

pofider commented 4 years ago

Hm, you should be able to do it without webpack. Like upload zip with node_modules. In every case, you won't be able to put the whole chrome (puppeteer dependency) to the index.js

Btw we have lambda tutorial here, you likely want a different flow, but still, maybe it can be interesting for you. https://jsreport.net/learn/aws-lambda-serverless

Nogostradamus commented 4 years ago

Yes, I saw that kit there and have implemented that manually on AWS with layers. I need to figure out how to do it with the could formation and my company guidelines.