Skip to content

Proposal: switch to a real module system for better modular designs #245

@kaosat-dev

Description

@kaosat-dev

Curent system

Hi everyone ! right now we are using a custom and rather limited include system to enable modular design & part reuse, with a few major flaws:

pros

  • simple
  • custom

cons

  • reinventing the wheel: all the issues we face have already been solved elsewhere, better
  • global pollution : all variables declared in ANY include() are pushed to the global scope
  • unclear syntax for export & import : ie what is exported ?? what if we have mutlple functions with the same name in different modules etc (hint, not possible)
const someValue = 42
const someText = 'foo'
coolPart = function() { 
   return cube({size:someValue}).union(text(someText)
}
  • non idiomatic : the syntax teaches bad practices
  • no relative path resolution
  • greedy evaluation : any code in an imported module is immediatly evaluated & run (careful what you import!)
  • not integrated with the rest of the javascript ecosystem

Proposal

The node.js & js ecosystem have been very successfully using modules for years, let's use that tech and knowledge to our advantage :

Note: this applies to both common.js & es6 modules as well

pros

(in addition to all the cons of the current system, which are all resolved in commonjs/es6 modules)

  • clear , explicit and proven syntax :
    export: (coolPart.jscad)
const someValue = 42
const someText = 'foo' // this one is not exported, yet still available at the module level

function coolPart(){
  return cube({size:someValue}).union(text(someText)
}
module.exports = { //these are exported and available from any consumer
  someValue,
  coolPart
}

import

const {coolPart} = require('./coolPart.jscad') //we can selectively import features, etc
  • all the existing javascript packages & tools at our disposal
  • simpler to distribute package and use them (distribution with current syntax is not problematic, but use is)
  • OpenJSCAD is javascript, we should embrace it , instead of half heartedly hiding it :)

cons

  • higher complexity in browser (but less in node.js), there are many open source tools to code with modules in the browsers, they can be self hosted , but is more complex.

note: the commonjs require function / module loading is very simple and could also be emulated for a limited browser use

  • possible dependency issues (as in , too much)

Of course this could be rolled out progressively, tested & tried, and would not need to impact the current implementation of include.

As you can likely see, I am quite passionate about the subject .
Any thoughts ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions