@jscad/core 2.4.0 → 2.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jscad/core",
3
- "version": "2.4.0",
3
+ "version": "2.5.3",
4
4
  "description": "Core functionality for JSCAD Applications",
5
5
  "repository": "https://github.com/jscad/OpenJSCAD.org",
6
6
  "main": "src/index.js",
@@ -34,11 +34,10 @@
34
34
  ],
35
35
  "license": "MIT",
36
36
  "dependencies": {
37
- "@jscad/array-utils": "2.1.0",
38
- "@jscad/io": "2.1.0",
39
- "@jscad/io-utils": "2.0.9",
40
- "@jscad/modeling": "2.6.0",
41
- "@jscad/vtree": "2.0.10",
37
+ "@jscad/array-utils": "2.1.1",
38
+ "@jscad/io": "2.2.3",
39
+ "@jscad/io-utils": "2.0.12",
40
+ "@jscad/modeling": "2.7.1",
42
41
  "json5": "2.2.0",
43
42
  "strip-bom": "4.0.0"
44
43
  },
@@ -53,5 +52,5 @@
53
52
  "url": "https://opencollective.com/openjscad",
54
53
  "logo": "https://opencollective.com/openjscad/logo.txt"
55
54
  },
56
- "gitHead": "3d1d02f2863c65fd95406c7098c09297b3be1c10"
55
+ "gitHead": "95f05c5f11a53bff469a305d69b99f674dc7d6d3"
57
56
  }
@@ -1,5 +1,3 @@
1
- const makeBuildCachedGeometryFromTree = require('@jscad/vtree').buildCachedGeometry
2
-
3
1
  const isGeom2 = require('@jscad/modeling').geometries.geom2.isA
4
2
  const isGeom3 = require('@jscad/modeling').geometries.geom3.isA
5
3
  const isPath2 = require('@jscad/modeling').geometries.path2.isA
@@ -18,98 +16,17 @@ const isResultGeometry = (results) => {
18
16
  return false
19
17
  }
20
18
 
21
- const lookupFromCompactBinary = (compactLookup = {}) => {
22
- // console.log('lookupFromCompactBinary', compactLookup)
23
- // TODO: optimise this !!
24
- const lookup = {}
25
- Object.keys(compactLookup).forEach((key) => {
26
- const object = compactLookup[key]
27
- let result
28
- if (object[0] === 0) { // Geom2
29
- result = require('@jscad/modeling').geometries.geom2.fromCompactBinary(object)
30
- }
31
- if (object[0] === 1) { // Geom3
32
- result = require('@jscad/modeling').geometries.geom3.fromCompactBinary(object)
33
- }
34
- if (object[0] === 2) { // Path2
35
- result = require('@jscad/modeling').geometries.path2.fromCompactBinary(object)
36
- }
37
- lookup[key] = result
38
- })
39
- return lookup
40
- }
41
-
42
- const toJSON = (data) => JSON.stringify(data, (key, value) => {
43
- if (value instanceof Int8Array ||
44
- value instanceof Uint8Array ||
45
- value instanceof Uint8ClampedArray ||
46
- value instanceof Int16Array ||
47
- value instanceof Uint16Array ||
48
- value instanceof Int32Array ||
49
- value instanceof Uint32Array ||
50
- value instanceof Float32Array ||
51
- value instanceof Float64Array) {
52
- const replacement = {
53
- constructor: value.constructor.name,
54
- data: Array.apply([], value),
55
- flag: 'FLAG_TYPED_ARRAY'
56
- }
57
- return replacement
58
- }
59
- return value
60
- })
61
-
62
- const lookupToCompactBinary = (lookup) => {
63
- // FIXME: optimise this !!
64
- const compactLookup = {}
65
- Object.keys(lookup).forEach((key) => {
66
- const object = lookup[key]
67
- let result = object
68
- if (isGeom2(object)) {
69
- compactLookup[key] = require('@jscad/modeling').geometries.geom2.toCompactBinary(object)
70
- } else if (isGeom3(object)) {
71
- compactLookup[key] = require('@jscad/modeling').geometries.geom3.toCompactBinary(object)
72
- } else if (isPath2(object)) {
73
- compactLookup[key] = require('@jscad/modeling').geometries.path2.toCompactBinary(object)
74
- } else {
75
- result = toJSON(object)
76
- compactLookup[key] = result
77
- }
78
- })
79
- return compactLookup
80
- }
81
-
82
19
  const instanciateDesign = (rootModule, parameterValues, options) => {
83
- const { vtreeMode, serialize } = options
20
+ const { serialize } = options
84
21
  // deal with the actual solids generation
85
22
  let solids
86
23
  const rawResults = flatten(toArray(rootModule.main(parameterValues)))
87
24
 
88
- if (vtreeMode) {
89
- console.log('input lookup', options.lookup)
90
- let lookup = lookupFromCompactBinary(options.lookup)
91
- const lookupCounts = options.lookupCounts || {}
92
-
93
- console.log('lookup after', lookup)
94
- // const start = new Date()
95
- const buildCachedGeometryFromTree = makeBuildCachedGeometryFromTree({ passesBeforeElimination: 5, lookup, lookupCounts })
96
- solids = buildCachedGeometryFromTree({}, rawResults)
97
- console.log('created lookup', lookup, lookupCounts)
98
- // console.log('solids, vtree', solids, 'lookup', lookup)
99
- // console.warn(`buildCachedGeometryFromTree`, new Date() - start)//, rawResults, solids)
100
- // TODO: return both solids and cache instead of mutating ?
101
- lookup = lookupToCompactBinary(lookup)
102
- console.log('compact lookup', lookup)
103
-
104
- solids = serialize ? serializeSolids(solids) : solids
105
- return { solids, lookup, lookupCounts }
25
+ if (isResultGeometry(rawResults)) {
26
+ solids = serialize ? serializeSolids(rawResults) : rawResults
27
+ return { solids }
106
28
  } else {
107
- if (isResultGeometry(rawResults)) {
108
- solids = serialize ? serializeSolids(rawResults) : rawResults
109
- return { solids }
110
- } else {
111
- throw new Error('bad output from script: expected geom3/geom2/path2 objects')
112
- }
29
+ throw new Error('bad output from script: expected geom3/geom2/path2 objects')
113
30
  }
114
31
  }
115
32
 
@@ -12,7 +12,6 @@ const applyParameterDefinitions = require('../parameters/applyParameterDefinitio
12
12
  * @param {Array} data.filesAndFolders - array of files / directories
13
13
  * @param {String} [data.mainPath] - path of the file containing the main function (optional)
14
14
  * @param {Boolean} [data.serialize] - true to serialize the solids into JSON
15
- * @param {Boolean} [data.vtreeMode] - true to use the experimental Vtree caching (optional)
16
15
  * @param {Object} [data.lookup] - geometry cache lookup (optional)
17
16
  * @param {Object} [data.lookupCounts] - geometry cache lookup counts (optional)
18
17
  * @param {Object} [data.parameterValues] - over-rides of parameter values (optional)
@@ -28,15 +27,14 @@ const rebuildSolids = (data, callback) => {
28
27
  console.log('rebuildSolids',data)
29
28
  const defaults = {
30
29
  mainPath: '',
31
- vtreeMode: false,
30
+ apiMainPath: '@jscad/modeling',
32
31
  serialize: false,
33
32
  lookup: null,
34
33
  lookupCounts: null,
35
34
  parameterValues: {}
36
35
  }
37
- let { mainPath, vtreeMode, serialize, lookup, lookupCounts, parameterValues } = Object.assign({}, defaults, data)
36
+ let { mainPath, apiMainPath, serialize, lookup, lookupCounts, parameterValues } = Object.assign({}, defaults, data)
38
37
 
39
- const apiMainPath = '@jscad/modeling'// vtreeMode ? '../code-loading/vtreeApi' : '@jscad/modeling'
40
38
  const filesAndFolders = data.filesAndFolders
41
39
 
42
40
  // let start = new Date()
@@ -56,7 +54,6 @@ const rebuildSolids = (data, callback) => {
56
54
  parameterValues = Object.assign({}, designData.parameterValues, parameterValues)
57
55
  // start = new Date()
58
56
  const options = {
59
- vtreeMode,
60
57
  lookup,
61
58
  lookupCounts,
62
59
  serialize
@@ -6,9 +6,11 @@ const getAllParameterDefintionsAndValues = require('../parameters/getParameterDe
6
6
  const makeWebRequire = require('../code-loading/webRequire')
7
7
 
8
8
  const rebuildSolids = (data) => {
9
- const defaults = { vtreeMode: false, serialize: false }
10
- let { mainPath, vtreeMode, parameterValues, useFakeFs } = Object.assign({}, defaults, data)
11
- const apiMainPath = vtreeMode ? '../code-loading/vtreeApi' : '@jscad/modeling'
9
+ const defaults = {
10
+ apiMainPath: '@jscad/modeling',
11
+ serialize: false
12
+ }
13
+ let { apiMainPath, serialize, mainPath, parameterValues, useFakeFs } = Object.assign({}, defaults, data)
12
14
  // we need to update the source for our module
13
15
  let requireFn = require
14
16
 
@@ -3,17 +3,17 @@
3
3
  */
4
4
  const normalizeDesignModule = (rootModule) => {
5
5
  if (!rootModule) {
6
- throw new Error('no root module found')
6
+ throw new Error('no root module found, please check the project structure')
7
7
  }
8
8
  if (typeof (rootModule) === 'function') {
9
- console.warn('please use named exports for the main() function')
9
+ console.warn('please refactor the exports, assigning main() as a property, i.e. module.exports = { main }')
10
10
  rootModule = { main: rootModule }
11
11
  }
12
12
  if (!rootModule.main) {
13
- throw new Error('no main() function, check the exports')
13
+ throw new Error('no main() function found, please check the module.exports')
14
14
  }
15
- if (!(typeof (rootModule.main) === 'function')) {
16
- throw new Error('main is not a function, check the exports')
15
+ if (typeof (rootModule.main) !== 'function') {
16
+ throw new Error('main is not a function, please check the module.exports')
17
17
  }
18
18
  return rootModule
19
19
  }
@@ -1,18 +1,10 @@
1
1
  const { deserializers } = require('@jscad/io')
2
2
 
3
- const isCommonJsModule = require('./isCommonJsModule')
4
- const modulifySource = require('./modulifySource')
5
-
6
- const passThroughTransform = (options, entry) => entry
7
-
8
- /* function to turn old style jscad code with implicit imports
9
- into code with explicit exports/imports */
10
- // FIXME wouldn't a javascript error be better then 'hacking' the users code?
3
+ /*
4
+ * Transform the entry into a ready-to-use module.
5
+ */
11
6
  const modulifyTransform = (options, entry) => {
12
- const { apiMainPath } = options
13
- const isFileCommonJs = isCommonJsModule(entry.source)
14
- const source = !isFileCommonJs ? modulifySource(entry.source, apiMainPath) : entry.source
15
- return Object.assign({}, entry, { source })
7
+ return Object.assign({}, entry, { source: entry.source })
16
8
  }
17
9
 
18
10
  /*
@@ -49,7 +49,7 @@ const makeWebRequire = (filesAndFolders, options) => {
49
49
  fakeFs: require('./makeFakeFs')(filesAndFolders)
50
50
  }
51
51
  const { apiMainPath, fakeFs } = Object.assign({}, defaults, options)
52
- const apiModule = apiMainPath === '@jscad/modeling' ? require('@jscad/modeling') : require('./vtreeApi')
52
+ const apiModule = apiMainPath === '@jscad/modeling' ? require('@jscad/modeling') : require(apiMainPath)
53
53
 
54
54
  // preset core modules
55
55
  // FIXME this list of modules should be an option, replacing apiMainPath
@@ -72,6 +72,7 @@ const makeWebRequire = (filesAndFolders, options) => {
72
72
  // console.log('*****\n',filesAndFolders,'\n*****')
73
73
 
74
74
  const extensions = {}
75
+ const moduleCache = {}
75
76
 
76
77
  /* Require (obtain) the exports for the given require path, relative to the given current path.
77
78
  * The logic is based on the original NODE require() function.
@@ -105,6 +106,7 @@ const makeWebRequire = (filesAndFolders, options) => {
105
106
  if (entry.children) return null // directory
106
107
 
107
108
  if (extensions[baseExt]) {
109
+ if(moduleCache[requirePath]) return moduleCache[requirePath]
108
110
  // evaluate the content
109
111
  const matchingModule = {
110
112
  exports: {},
@@ -121,7 +123,7 @@ const makeWebRequire = (filesAndFolders, options) => {
121
123
  }
122
124
  }
123
125
  extensions[baseExt](matchingModule, entry.fullPath)
124
- return matchingModule.exports
126
+ return moduleCache[requirePath] = matchingModule.exports
125
127
  }
126
128
  return null
127
129
  }
@@ -13,10 +13,14 @@ test('webRequire: should support require, from a single file', (t) => {
13
13
 
14
14
  let requireFn = makeWebRequire(singleFileJs, { apiMainPath })
15
15
  let designRootModule = requireFn(singleFileJs[0].fullPath)
16
+ const designRootModule2 = requireFn(singleFileJs[0].fullPath)
16
17
 
17
18
  t.true('main' in designRootModule)
18
19
  t.true(designRootModule.main instanceof Function)
19
20
 
21
+ t.is(designRootModule, designRootModule2)
22
+ t.deepEqual(designRootModule, designRootModule2)
23
+
20
24
  // NOTE: 'jscad' must be registered as an extension
21
25
  const fakeFs = makeFakeFs(singleFileJscad)
22
26
  requireFn = makeWebRequire(singleFileJscad, { apiMainPath })
@@ -32,7 +36,6 @@ test('webRequire: should support require, from a directory with index.js', (t) =
32
36
 
33
37
  const requireFn = makeWebRequire(directoryWithIndexJs, { apiMainPath })
34
38
  const designRootModule = requireFn('/project')
35
-
36
39
  t.true('main' in designRootModule)
37
40
  t.true(designRootModule.main instanceof Function)
38
41
  })