Require "everything" from a node.js module
Problem
I'm writing a DSL, and I want to put everything from a required module into the current namespace to be able to write something like this
// I know it's not working.
// In python, I'd do: from mydsl import *
{*} = require('./mydsl');
node('London');
node('Paris');
edge('London', 'Paris');
Here are some versions I tried as a workaround
// In python: import mydsl as dsl
dsl = require('./mydsl');
dsl.node('London');
// In python: from mydsl import node, edge
{node, edge} = require('./mydsl');
node('London');
// Extend `this` with imported functions
_ = require('underscore');
_.extend(this, require('./mydsl'));
this.node('London');
Since my DSL has many keywords, using {node,edge,...} = require
would be clumsy. I'd prefer a solution that can be ported to the web with browserify.
Problem courtesy of: Adam Schmideg
Solution
Did you consider using with
?
var mydsl = require('./mydsl');
with (mydsl) {
node('London');
node('Paris');
edge('London', 'Paris');
}
Solution courtesy of: Trevor Dixon
Discussion
View additional discussion.