Is it possible to use paperjs in nodejs without using Cairo? - node.js

I need to "read" an array of some paperjs paths in nodejs and get their dimension. I wanted to use paper npm module but saw that it has a dependency to Cairo.
As I'm deploying to heroku it is a little difficult to use Cairo. I know its possible but I want to know if its really necessary just for "reading" the dimensions of a path group.

A present day answer: Yes it's possible.
———
To quote from the docs:
Paper.js comes in three different versions on NPM: paper, paper-jsdom and paper-jsdom-canvas. Depending on your use case, you need to require a different one:
paper is the main library, and can be used directly in a browser context, e.g. a web browser or worker.
paper-jsdom is a shim module for Node.js, offering headless use with SVG importing and exporting through jsdom.
paper-jsdom-canvas is a shim module for Node.js, offering canvas rendering through Node-Canvas as well as SVG importing and exporting through jsdom.
→ If you don't require rendering to canvas, paper-jsdom should do the trick (which doesn't need Cairo)
———
Important note:
If I understood correctly this means you can't use PaperScript, but have to use JavaScript directly. This will affect how you have to write code:
new paper.Path() instead of new Path()
Create project manually using paper.setup()
No mathematical operators (+ - * / %)
... (More info in the docs)
———
A simple example to create a line, log its bounding box size (and also export the SVG):
Note: I used this with the current Node LTS (v14.16.1)
package.json
{
"name": "paper-jsdom-example",
"main": "index.js",
"dependencies": {
"paper-jsdom": "^0.12.15"
}
}
index.js
const paper = require('paper');
const fs = require('fs');
var size = new paper.Size(300, 300)
paper.setup(size);
var path = new paper.Path();
path.strokeColor = '#348BF0';
var start = new paper.Point(100, 100);
var end = new paper.Point(200, 200);
path.moveTo(start);
path.lineTo(end);
console.log('width', path.bounds.width, 'height', path.bounds.height);
var svg = paper.project.exportSVG({asString:true});
fs.writeFileSync('punchline.svg', svg);

Related

ag-grid-community vs ag-grid-enterprise new Grid

I have a Node client-side application with the latest ag-grid version.
I was using ag-grid-community without any issues with this require line
const {Grid} = require('ag-grid-community');
and this new
new Grid(agGridDiv, agGridOptions);
but if I change the require to
const {Grid} = require('ag-grid-enterprise');
the new fails with exception 'Grid is not a constructor'
How can I fix this? I have tried various changes such as new Grid.Grid etc but nothing seems to work.
For latest 23.1.1 version this page:
// ECMA 5 - using nodes require() method
const AgGrid = require('ag-grid-enterprise');
Another way to follow this guide, it all depends on which repository you download the dependencies from.
import {Grid, GridOptions} from '#ag-grid-community/core';
import {LicenseManager} from '#ag-grid-enterprise/core';
// or
const {Grid, GridOptions} = require('#ag-grid-community/core');
I used core and it worked for import.
For old version:
Grid, like everything else, needs to be imported from ag-grid-community.
1) ag-grid-enterprise is pure additive functionality for ag-grid-community.
2) You will use ag-grid-enterprise via the ag-grid-community api not explicit. Use ag-grid-enterprise for LicenseManager only.
Off-topic:
I would recommend starting with the old version, since the source code of the new version is minified and it will be more difficult for you to understand many nontrivial nuances.

Angular, electron, typescript and robotjs

I'm trying to use Typescript together with Electron and RobotJS.
I'm a beginner with all of these technologies so I lack the deep understanding of what is happening behind the scenes so I cannot really connect the dots on this issue.
Electron and my app are working fine, everything compiles, but RobotJS won't work. The error I get is the following:
ERROR in ./node_modules/robotjs/build/Release/robotjs.node 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
I tried rebuilding RobotJS for Electron but it still doesn't work.
Is this a native node module? I'm not sure, gotta read up on it first to figure out what I have to do with it if it indeed is.
Is this even achievable?
Thanks and have a lovely day!
LE: It would appear that I was trying to use robotjs that relies on node (main process) from angular (rendered process). When I moved what I was trying to do with robotjs from the angular component to main.ts that is ran by electron, it worked. I will try and find a way to use it from angular, I guess Inter-process Communication or something, since I couldn't find any other way for now.
Still waiting for ideas since I'm kind of in the dark right now.
Thanks!
Ok, figured this out.
Basically you cannot access Electron's Node.js API straight from Angular.
For that you need an awesome tool called ngx-electron. Read about it here or just Google it.
After you install this tool you can just DI its service as most guides instruct you and then use Electron remote to get access to robotjs.
Basically this:
const robot = this._electronService.remote.require('robotjs');
// The example supplied by robotjs
robot.setMouseDelay(2);
const twoPI = Math.PI * 2.0;
const screenSize = robot.getScreenSize();
const height = (screenSize.height / 2) - 10;
const width = screenSize.width;
for (let x = 0; x < width; x++) {
const y = height * Math.sin((twoPI * x) / width) + height;
robot.moveMouse(x, y);
}
Might not be the best solution but with my current limited knowledge about these technologies this will have to do.
I'm open to opinions tho.
Thanks for your time, have a nice one!

Webpack Aliases in Node JS Server code

I'm building an isomorphic React/React-Router/Redux/Webpack application and I'm attempting to implement server side rendering.
My directory looks like:
/client
/actions
/components
/containers
/server
/server.js
In my webpack config, I have aliases set up for all the folders inside client:
var path_base = path.resolve(__dirname, '..');
const resolve = path.resolve;
const base = function() {
var args = [path_base];
args.push.apply(args, arguments);
return resolve.apply(resolve,args);
};
const resolve_alias = base.bind(null, 'src/client');
const aliases = [
'actions',
'components',
'constants',
'containers',
'middleware',
'reducers',
'routes',
'store',
'styles',
'utils',
'validation'
];
so that inside the code that gets bundled by webpack, I can do:
import { Widget } from 'components';
and that import gets resolved by webpack.
Now in my server code, in order to do the rendering, I have to import some of my client files, like routes/index.js. The problem I'm running into when I import my routes file, it's using a webpack alias to another file, say components or containers so naturally, the node js require system can't resolve it.
How do I fix something like that? I looked at this question and it talks about essentially setting up the same aliases that exist in webpack with mock-require. But then the issue becomes that my routes file imports all my components which then all import things like stylesheets, images, etc. Should I then be using something like webpack-isomorphic-tools?
The guides I've been looking at (this for example) are all great at showing how server side rendering is accomplished but none of them really talk about how to resolve all the requires and whatnot.
After battling with this issue for 2 days I settled on babel-plugin-webpack-alias.
What you need to do to resolve paths with that is:
$ npm install --save-dev babel-plugin-webpack-alias
Add the plugin to your .babelrc
Add the aliases to your webpack.config (make sure you use path.join())
Refer to this post if you have problems loading styles
The other option I tried was universal-webpack but I found it to be a bit verbose. If you want to see roughly how the whole server-side loading works, you can check out this video.
If you really want them, run your server side code through babel and use this plugin: https://www.npmjs.com/package/babel-plugin-module-alias which will let you do the same thing as webpack.
Edit: This one works a lot better: https://github.com/jagrem/babel-resolve-relative-module it allows multiple paths
Try to use NODE_PATH. Node will always look for a module in this path during require calls. It allows to short cut your relative paths as you want.
// turn this
import {Widget} from '../../components';
// into this
import {Widget} from 'components';
See Node.js docs for more information.
P.S. this thing is very sensitive, so use it carefully. Now your code tightly depends from the environment and may break somewhere.
If you use webpack-isomorphic-tools then it'll take your webpack config into account for your server side which will make all your aliases work.
https://www.npmjs.com/package/webpack-isomorphic-tools

How to reference local files in a npm module?

I wrote a simple npm module to precompile my handlebars templates when using django compressor to do post-processing for some client side components and found that I need to ship the npm module with a few js files.
Currently I just assume no one is installing this with the global flag because I've "hard coded" the path to these dependencies in the npm module itself
example layout of my npm module
/
* /bin
* /lib/main.js
* /vendor/ember.js
Now inside main.js I want to use the ember.js file ... currently my hard coded approach looks like this
var emberjs = fs.readFileSync('node_modules/django-ember-precompile/vendor/ember.js', 'utf8');
Again -this only works because I assume you install it local but I'd like to think node.js has a more legit way to get locally embedded files
Anyone know how I can improve this to be more "global" friendly?
What you can do is get the directory of the current file and make your file paths relative to that.
var path = require('path')
, fs = require('fs');
var vendor = path.join(path.dirname(fs.realpathSync(__filename)), '../vendor');
var emberjs = fs.readFileSync(vendor + '/ember.js', 'utf8');
Hope that helps!
One of the great strengths of Node.js is how quickly you can get up and running. The downside to this approach is that you are forced to fit the design patterns it was build around.
This is an example where your approach differs too much from Nodes approach.
Node expects everything in a module to be exposed from the modules exports, including templates.
Move the readFileSync into the django-ember-precompile module, then expose the returned value via a module export in lib/main.js.
Example:
package.json
{
"name": "django-ember-precompile",
"main": "lib/main.js"
}
lib/main.js
module.exports.ember = readFileSync('vendor/ember.js')
vendor/ember.js
You obtain your template via
var template = require('django-ember-precompile').ember
This example can be refined, but the core idea is the same.

Nodejs + CoffeeScript + Mongoose : Define Module?

I'm trying to create a little application to store snippets of code using nodejs and mongodb
I'm using Coffeescript to write the app.
The problem is, i want to separate the code in modules
so i create this folder structure
/app
/lib
/models
/routes
core.coffee
The core.coffe is the "server" app using expressjs
so in this file i have
mongoose = module.exports.mongoose = require 'mongoose'
app = module.exports.app = express.createServer()
Snippet = module.exports.Snippet = require __dirname+'/lib/models/Snippet'
#App configurations
routes = require(__dirname+'/lib/routes/general')
In lib/models/Snippet
mongoose = module.parent.exports.mongoose
Snippet = new mongoose.Schema
title:
type: String
default:'Title'
mongoose.model 'Snippet',Snippet
exports.Snippet = mongoose.model 'Snippet'
In /lib/routes/general.coffee
app = module.parent.exports.app
mongoose = module.parent.exports.mongoose
Snippet = module.parent.exports.Snippet
app.get '/test', (req,res)->
snip = new Snippet()
res.send snip
But this don't work i get the following error message
TypeError: object is not a function
at Object.CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
How can I accomplish that?
I see a noteworthy typo:
Snippet = module.exports.Snippt = require __dirname+'/lib/models/Snippet'
Change module.exports.Snippt to module.exports.Snippet.
Let's start by looking at how you're using require. It looks like you're trying to load all the project's requirements in core.coffee, and then re-export them elsewhere. That's an odd way of doing it, most people just require those libraries in each module that needs them (for now at least, see the end of my answer).
For example, you need mongoose in lib/models/Snippet, so just require it there:
lib/models/Snippet:
mongoose = require 'mongoose'
Next, there's no need to use __dirname to require a relative path, require copes fine with a path starting with ./:
require './lib/models/Snippet'
I still wasn't able to get the code to work cleanly (I'm guessing we're not seeing the full code), but it might be enough to set you on the right path.
Finally, if you want to go down the route of exporting everything on the main module can I suggest taking a look at dave-elkan's layers project. The plain version doesn't support coffeescript, but I've created a fork that does.
It's very lightweight, and makes almost no assumptions about your project structure. The basic idea is that you give layers() your express app object and a directory. Layers will scan that directory and set up any subdirectories as layers on your app object.
In your case you'd pass in a rootPath: __dirname + '/lib' and your app object would get app.models.Snippet and app.routes.general added onto it. That's still not quite how I'd structure it, but you might be able to come up with something that matches your style from there.

Resources