@jscad/core 2.6.6 → 3.0.0-alpha.0

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +9 -228
  2. package/README.md +1 -1
  3. package/dist/jscad-core.min.js +465 -477
  4. package/package.json +15 -16
  5. package/src/code-evaluation/index.js +4 -6
  6. package/src/code-evaluation/instanciateDesign.js +5 -9
  7. package/src/code-evaluation/rebuildGeometry.js +4 -6
  8. package/src/code-evaluation/rebuildGeometryCli.js +14 -11
  9. package/src/code-evaluation/rebuildGeometryWorker.js +3 -5
  10. package/src/code-evaluation/serializeSolids.js +2 -4
  11. package/src/code-loading/index.js +2 -4
  12. package/src/code-loading/isCommonJsModule.js +1 -3
  13. package/src/code-loading/loadDesign.js +8 -10
  14. package/src/code-loading/makeFakeFs.js +2 -3
  15. package/src/code-loading/modulifySource.js +6 -24
  16. package/src/code-loading/normalizeDesignModule.js +1 -3
  17. package/src/code-loading/requireDesignFromModule.js +4 -9
  18. package/src/code-loading/requireDesignUtilsFs.js +5 -9
  19. package/src/code-loading/transformSources.js +2 -4
  20. package/src/code-loading/validateDesignModule.js +1 -3
  21. package/src/code-loading/vtreeApi.js +1 -1
  22. package/src/code-loading/webRequire.js +36 -28
  23. package/src/code-loading/webRequire.test.js +8 -9
  24. package/src/index.js +14 -7
  25. package/src/io/index.js +2 -4
  26. package/src/io/registerExtensions.js +5 -9
  27. package/src/parameters/applyParameterDefinitions.js +1 -4
  28. package/src/parameters/getParameterDefinitionsAndValues.js +5 -6
  29. package/src/parameters/getParameterDefinitionsFromSource.js +10 -12
  30. package/src/parameters/getParameterDefinitionsFromSource.test.js +2 -3
  31. package/src/parameters/getParameterValuesFromParameters.js +2 -4
  32. package/src/parameters/getParameterValuesFromUIControls.js +2 -4
  33. package/src/parameters/index.js +4 -6
  34. package/src/utils/getFileExtensionFromString.js +1 -3
  35. package/src/utils/index.js +2 -4
  36. package/src/utils/version.js +1 -3
  37. package/src/web/index.js +1 -3
  38. package/src/web/walkFileTree.js +7 -9
package/package.json CHANGED
@@ -1,29 +1,30 @@
1
1
  {
2
2
  "name": "@jscad/core",
3
- "version": "2.6.6",
3
+ "version": "3.0.0-alpha.0",
4
4
  "description": "Core functionality for JSCAD Applications",
5
5
  "homepage": "https://openjscad.xyz/",
6
6
  "repository": "https://github.com/jscad/OpenJSCAD.org",
7
7
  "main": "src/index.js",
8
+ "type": "module",
8
9
  "unpkg": "dist/jscad-core.min.js",
10
+ "module": "dist/jscad-core.es.js",
9
11
  "scripts": {
10
- "build": "browserify src/index.js -o dist/jscad-core.min.js -g uglifyify --standalone jscadCore",
11
- "version": "npm run build && git add dist",
12
- "coverage": "nyc --all --reporter=html --reporter=text npm test",
12
+ "build": "rollup --config",
13
+ "coverage": "c8 --all --reporter=html --reporter=text pnpm test",
13
14
  "test": "ava './src/**/*.test.js' --verbose --timeout 2m"
14
15
  },
15
16
  "contributors": [
16
17
  {
17
18
  "name": "Rene K. Mueller",
18
- "url": "http://renekmueller.com"
19
+ "url": "https://github.com/Spiritdude"
19
20
  },
20
21
  {
21
22
  "name": "z3dev",
22
- "url": "http://www.z3d.jp"
23
+ "url": "https://github.com/z3dev"
23
24
  },
24
25
  {
25
26
  "name": "Mark 'kaosat-dev' Moissette",
26
- "url": "http://kaosat.net"
27
+ "url": "https://github.com/kaosat-dev"
27
28
  }
28
29
  ],
29
30
  "keywords": [
@@ -35,23 +36,21 @@
35
36
  ],
36
37
  "license": "MIT",
37
38
  "dependencies": {
38
- "@jscad/array-utils": "2.1.4",
39
- "@jscad/io": "2.4.5",
40
- "@jscad/io-utils": "2.0.24",
41
- "@jscad/modeling": "2.11.1",
39
+ "@jscad/array-utils": "3.0.0-alpha.0",
40
+ "@jscad/io": "3.0.0-alpha.0",
41
+ "@jscad/io-utils": "3.0.0-alpha.0",
42
+ "@jscad/modeling": "3.0.0-alpha.0",
42
43
  "json5": "2.2.3",
43
44
  "strip-bom": "4.0.0"
44
45
  },
45
46
  "devDependencies": {
46
- "ava": "3.15.0",
47
- "browserify": "16.5.1",
48
- "nyc": "15.1.0",
49
- "uglifyify": "5.0.2"
47
+ "ava": "^4.3.3",
48
+ "c8": "^8.0.0"
50
49
  },
51
50
  "collective": {
52
51
  "type": "opencollective",
53
52
  "url": "https://opencollective.com/openjscad",
54
53
  "logo": "https://opencollective.com/openjscad/logo.txt"
55
54
  },
56
- "gitHead": "4313974b50957018d2edd010e3a251f59bea46a4"
55
+ "gitHead": "3656d36ed9cd738ab884e86aae5a2ce08d52adf7"
57
56
  }
@@ -1,6 +1,4 @@
1
- module.exports = {
2
- rebuildGeometry: require('./rebuildGeometry'),
3
- rebuildGeometryCli: require('./rebuildGeometryCli'),
4
- rebuildGeometryWorker: require('./rebuildGeometryWorker'),
5
- serializeSolids: require('./serializeSolids')
6
- }
1
+ export { rebuildGeometry } from './rebuildGeometry.js'
2
+ export { rebuildGeometryCli } from './rebuildGeometryCli.js'
3
+ export { rebuildGeometryWorker } from './rebuildGeometryWorker.js'
4
+ export { serializeSolids } from './serializeSolids.js'
@@ -1,22 +1,20 @@
1
- const isGeom2 = require('@jscad/modeling').geometries.geom2.isA
2
- const isGeom3 = require('@jscad/modeling').geometries.geom3.isA
3
- const isPath2 = require('@jscad/modeling').geometries.path2.isA
1
+ import { geom2, geom3, path2 } from '@jscad/modeling'
4
2
 
5
- const { flatten, toArray } = require('@jscad/array-utils')
3
+ import { flatten, toArray } from '@jscad/array-utils'
6
4
 
7
- const serializeSolids = require('./serializeSolids')
5
+ import { serializeSolids } from './serializeSolids.js'
8
6
 
9
7
  /*
10
8
  * determine if the given results contain valid geometry
11
9
  */
12
10
  const isResultGeometry = (results) => {
13
11
  if (Array.isArray(results) && results.length > 0) {
14
- return results.reduce((acc, result) => acc || (isGeom3(result) || isGeom2(result) || isPath2(result)), false)
12
+ return results.reduce((acc, result) => acc || (geom3.isA(result) || geom2.isA(result) || path2.isA(result)), false)
15
13
  }
16
14
  return false
17
15
  }
18
16
 
19
- const instanciateDesign = (rootModule, parameterValues, options) => {
17
+ export const instanciateDesign = (rootModule, parameterValues, options) => {
20
18
  const { serialize } = options
21
19
  // deal with the actual solids generation
22
20
  let solids
@@ -29,5 +27,3 @@ const instanciateDesign = (rootModule, parameterValues, options) => {
29
27
  throw new Error('bad output from script: expected geom3/geom2/path2 objects')
30
28
  }
31
29
  }
32
-
33
- module.exports = instanciateDesign
@@ -1,6 +1,6 @@
1
- const loadDesign = require('../code-loading/loadDesign')
2
- const instanciateDesign = require('./instanciateDesign')
3
- const applyParameterDefinitions = require('../parameters/applyParameterDefinitions')
1
+ import { loadDesign } from '../code-loading/loadDesign.js'
2
+ import { instanciateDesign } from './instanciateDesign.js'
3
+ import { applyParameterDefinitions } from '../parameters/index.js'
4
4
 
5
5
  /**
6
6
  * Rebuild JSCAD solids from the given filesAndFolders.
@@ -26,7 +26,7 @@ const applyParameterDefinitions = require('../parameters/applyParameterDefinitio
26
26
  * Everything is together in a single function, because this is usually run in the context of a web worker
27
27
  * And transfering data back & forth is both complex (see transferables) and costly (time)
28
28
  **/
29
- const rebuildSolids = (data, callback) => {
29
+ export const rebuildGeometry = (data, callback) => {
30
30
  const defaults = {
31
31
  mainPath: '',
32
32
  apiMainPath: '@jscad/modeling',
@@ -83,5 +83,3 @@ const rebuildSolids = (data, callback) => {
83
83
  }, null)
84
84
  }
85
85
  }
86
-
87
- module.exports = rebuildSolids
@@ -1,17 +1,19 @@
1
+ import path from 'path'
2
+ import { createRequire } from 'module'
1
3
 
2
- const path = require('path')
3
- const { toArray } = require('@jscad/array-utils')
4
- const requireDesignFromModule = require('../code-loading/requireDesignFromModule')
5
- const getAllParameterDefintionsAndValues = require('../parameters/getParameterDefinitionsAndValues')
6
- const makeWebRequire = require('../code-loading/webRequire')
4
+ import { toArray } from '@jscad/array-utils'
7
5
 
8
- const rebuildSolids = (data) => {
6
+ import { requireDesignFromModule } from '../code-loading/requireDesignFromModule.js'
7
+ import { getParameterDefinitionsAndValues } from '../parameters/index.js'
8
+ import { makeWebRequire } from '../code-loading/webRequire.js'
9
+
10
+ export const rebuildGeometryCli = async (data) => {
9
11
  const defaults = {
10
12
  apiMainPath: '@jscad/modeling'
11
13
  }
12
14
  let { apiMainPath, mainPath, parameterValues, useFakeFs } = Object.assign({}, defaults, data)
13
15
  // we need to update the source for our module
14
- let requireFn = require
16
+ let requireFn = createRequire(import.meta.url)
15
17
 
16
18
  // source came from conversion, i.e. not from file system
17
19
  if (useFakeFs) {
@@ -32,13 +34,14 @@ const rebuildSolids = (data) => {
32
34
  }
33
35
 
34
36
  // rootModule should contain exported main and getParameterDefinitions functions
35
- const rootModule = requireDesignFromModule(mainPath, requireFn)
37
+ // const rootModule = requireDesignFromModule(mainPath, requireFn)
38
+ // FIXME HACK for designs with import / export
39
+ const rootModule = await import(mainPath)
40
+
36
41
  // the design (module tree) has been loaded at this stage
37
42
  // now we can get our usefull data (definitions and values/defaults)
38
- const parameters = getAllParameterDefintionsAndValues(rootModule, parameterValues)
43
+ const parameters = getParameterDefinitionsAndValues(rootModule, parameterValues)
39
44
 
40
45
  const rawResults = toArray(rootModule.main(parameters.parameterValues))
41
46
  return rawResults
42
47
  }
43
-
44
- module.exports = rebuildSolids
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * evaluate script & rebuild solids, in seperate thread/webworker
4
3
  * @param {String} script the script
@@ -7,8 +6,9 @@
7
6
  * @param {Object} callback the callback to call once evaluation is done /failed
8
7
  * @param {Object} options the settings to use when rebuilding the solid
9
8
  */
10
- const rebuildGeometryWorker = (self) => {
11
- const rebuildGeometry = require('./rebuildGeometry')
9
+ import { rebuildGeometry } from './rebuildGeometry.js'
10
+
11
+ export const rebuildGeometryWorker = (self) => {
12
12
  self.onmessage = function (event) {
13
13
  if (event.data instanceof Object) {
14
14
  const { data } = event
@@ -21,5 +21,3 @@ const rebuildGeometryWorker = (self) => {
21
21
  }
22
22
  }
23
23
  }
24
-
25
- module.exports = rebuildGeometryWorker
@@ -1,11 +1,11 @@
1
- const { geom2, geom3, path2 } = require('@jscad/modeling').geometries
1
+ import { geom2, geom3, path2 } from '@jscad/modeling'
2
2
 
3
3
  /*
4
4
  * Serialize the given solids/objects into web worker message content.
5
5
  * @param {Array} solids - list of solids to serialize
6
6
  * @return {Array} web worker message contents
7
7
  */
8
- const serializeSolids = (solids) => {
8
+ export const serializeSolids = (solids) => {
9
9
  // NOTE: the use of compactBinary formats was removed due to
10
10
  // that lack of support for additional attributes, as well as
11
11
  // imcomplete support for transfering objects via web workers
@@ -22,5 +22,3 @@ const serializeSolids = (solids) => {
22
22
  })
23
23
  return solids
24
24
  }
25
-
26
- module.exports = serializeSolids
@@ -1,4 +1,2 @@
1
- module.exports = {
2
- makeFakeFs: require('./makeFakeFs.js'),
3
- requireDesignUtilsFs: require('./requireDesignUtilsFs')
4
- }
1
+ export { makeFakeFs } from './makeFakeFs.js'
2
+ export { getDesignEntryPoint, getDesignName } from './requireDesignUtilsFs.js'
@@ -1,3 +1 @@
1
- const isCommonJsModule = (string) => string.includes('module.exports') || string.includes('require(')
2
-
3
- module.exports = isCommonJsModule
1
+ export const isCommonJsModule = (string) => string.includes('module.exports') || string.includes('require(')
@@ -1,10 +1,10 @@
1
- const { registerAllExtensions } = require('../io/registerExtensions')
1
+ import { registerAllExtensions } from '../io/registerExtensions.js'
2
2
 
3
- const transformSources = require('./transformSources')
4
- const makeFakeFs = require('./makeFakeFs')
5
- const makeWebRequire = require('./webRequire')
6
- const normalizeDesignModule = require('./normalizeDesignModule')
7
- const getAllParameterDefintionsAndValues = require('../parameters/getParameterDefinitionsAndValues')
3
+ import { transformSources } from './transformSources.js'
4
+ import { makeFakeFs } from './makeFakeFs.js'
5
+ import { makeWebRequire } from './webRequire.js'
6
+ import { normalizeDesignModule } from './normalizeDesignModule.js'
7
+ import { getParameterDefinitionsAndValues } from '../parameters/index.js'
8
8
 
9
9
  /**
10
10
  * load a jscad script, injecting the basic dependencies if necessary
@@ -14,7 +14,7 @@ const getAllParameterDefintionsAndValues = require('../parameters/getParameterDe
14
14
  * @param {Array} filesAndFolders - array of files and folders to use
15
15
  * @param {Object} parameterValuesOverride - the values to use to override the defaults for the current design
16
16
  */
17
- const loadDesign = (mainPath, apiMainPath, filesAndFolders, parameterValuesOverride) => {
17
+ export const loadDesign = (mainPath, apiMainPath, filesAndFolders, parameterValuesOverride) => {
18
18
  // transform the source if passed non-javascript content, i.e. stl
19
19
  filesAndFolders = transformSources({ apiMainPath }, filesAndFolders)
20
20
 
@@ -45,9 +45,7 @@ const loadDesign = (mainPath, apiMainPath, filesAndFolders, parameterValuesOverr
45
45
  // rootModule SHOULD contain a main() entry and optionally a getParameterDefinitions entry
46
46
  // the design (module tree) has been loaded at this stage
47
47
  // now we can get our usefull data (definitions and values/defaults)
48
- const parameters = getAllParameterDefintionsAndValues(rootModule, parameterValuesOverride)
48
+ const parameters = getParameterDefinitionsAndValues(rootModule, parameterValuesOverride)
49
49
 
50
50
  return { rootModule, ...parameters }
51
51
  }
52
-
53
- module.exports = loadDesign
@@ -1,4 +1,4 @@
1
- const makeFakeFs = (filesAndFolders) => {
1
+ export const makeFakeFs = (filesAndFolders) => {
2
2
  const findMatch = (path, inputs = filesAndFolders) => {
3
3
  for (let i = 0; i < inputs.length; i++) {
4
4
  const entry = inputs[i]
@@ -22,6 +22,7 @@ const makeFakeFs = (filesAndFolders) => {
22
22
  isDirectory: (_) => (entry && (!('source' in entry) && ('children' in entry)))
23
23
  }
24
24
  }
25
+
25
26
  const fakeFs = {
26
27
  statSync,
27
28
  existsSync: (path) => {
@@ -61,5 +62,3 @@ const makeFakeFs = (filesAndFolders) => {
61
62
  }
62
63
  return fakeFs
63
64
  }
64
-
65
- module.exports = makeFakeFs
@@ -1,37 +1,19 @@
1
- /** transform an old type 'implicit imports' jscad script into one with explicit imports
1
+ /**
2
+ * Transform an old type 'implicit imports' jscad script into one with explicit imports
2
3
  * @param {} source
3
4
  * @param {} apiMainPath
4
5
  */
5
- const modulifySource = (source, apiMainPath) => {
6
+ export const modulifySource = (source, apiMainPath) => {
6
7
  const getParamsString = source.includes('getParameterDefinitions')
7
- ? 'module.exports.getParameterDefinitions = getParameterDefinitions'
8
+ ? 'export getParameterDefinitions'
8
9
  : ''
9
10
  const updatedSource = `
10
- const {geom2, geom3, path2, pol2, poly3} = require('${apiMainPath}').geometries
11
-
12
- const {square, circle, polygon, rectangle, ellipse, roundedRectangle} = require('${apiMainPath}').primitives
13
- const {cube, cylinder, sphere, polyhedron, torus} = require('${apiMainPath}').primitives
14
- const {colorize, hsl2rgb} = require('${apiMainPath}').colors
15
-
16
- const {extrudeFromSlices, extrudeLinear, extrudeRectangular, extrudeRotate, slice} = require('${apiMainPath}').extrusions
17
- const {rotate, translate, scale, mirror, transform} = require('${apiMainPath}').transforms
18
- const {hull, hullChain} = require('${apiMainPath}').hulls
19
- const {expand, offset} = require('${apiMainPath}').expansions
20
- const {union, difference, intersection} = require('${apiMainPath}').booleans
21
- const { measureArea, measureBounds, measureVolume} = require('${apiMainPath}').measurements
22
-
23
- const connectors = require('${apiMainPath}').connectors
24
-
25
- const {vec2, vec3, vec4, constants, line2, line3, mat4, plane, utils} = require('${apiMainPath}').maths
26
-
27
- const {vectorText, vectorChar} = require('${apiMainPath}').text
11
+ import * from '${apiMainPath}'
28
12
 
29
13
  ${source}
30
14
 
31
- module.exports = {main}
15
+ export main
32
16
  ${getParamsString}
33
17
  `
34
18
  return updatedSource
35
19
  }
36
-
37
- module.exports = modulifySource
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * Normalize the given design module for internal use.
3
3
  */
4
- const normalizeDesignModule = (rootModule) => {
4
+ export const normalizeDesignModule = (rootModule) => {
5
5
  if (!rootModule) {
6
6
  throw new Error('no root module found, please check the project structure')
7
7
  }
@@ -17,5 +17,3 @@ const normalizeDesignModule = (rootModule) => {
17
17
  }
18
18
  return rootModule
19
19
  }
20
-
21
- module.exports = normalizeDesignModule
@@ -1,19 +1,14 @@
1
-
2
- const validateDesignModule = require('./validateDesignModule')
3
- const normalizeDesignModule = require('./normalizeDesignModule')
1
+ import { validateDesignModule } from './validateDesignModule.js'
2
+ import { normalizeDesignModule } from './normalizeDesignModule.js'
4
3
 
5
4
  /** load a jscad script, injecting the basic dependencies if necessary
6
5
  * @param {string} filePath
7
- * @param {function} requireFn : the 'require' function to use: defaults to the standard 'require' under node.js
6
+ * @param {function} requireFn : the 'require' function to use; Node require or webRequire
8
7
  */
9
- const requireDesignFromModule = (filePath, requireFn = require) => {
10
- // const requireUncached = require('../code-loading/requireUncached')
11
- // TODO: only uncache when needed
8
+ export const requireDesignFromModule = (filePath, requireFn) => {
12
9
  // requireUncached(mainPath)
13
10
  const designRootModule = requireFn(filePath)
14
11
  // make sure everything is ok
15
12
  validateDesignModule(designRootModule)
16
13
  return normalizeDesignModule(designRootModule)
17
14
  }
18
-
19
- module.exports = requireDesignFromModule
@@ -1,5 +1,6 @@
1
- const path = require('path')
2
- const { toArray } = require('@jscad/array-utils')
1
+ import path from 'path'
2
+
3
+ import { toArray } from '@jscad/array-utils'
3
4
 
4
5
  // NOTE/ path.parse is NOT included by browserify & co , hence this function ...
5
6
  // https://github.com/substack/path-browserify/pull/3
@@ -36,7 +37,7 @@ const assertPath = (path) => {
36
37
  * statSync, existSync, readFileSync, readdirSync
37
38
  * @param {} paths
38
39
  */
39
- const getDesignEntryPoint = (fs, paths) => {
40
+ export const getDesignEntryPoint = (fs, paths) => {
40
41
  if (!paths) {
41
42
  return
42
43
  }
@@ -91,7 +92,7 @@ const packageNameFromDir = (fs, dirName, filePath) => {
91
92
  * @param {Object} fs a file-system like object (either node's fs or some other) providing at least statSync, existSync, readFileSync
92
93
  * @param {Array} paths an array of paths (strings) or a single path
93
94
  */
94
- const getDesignName = (fs, paths) => {
95
+ export const getDesignName = (fs, paths) => {
95
96
  if (!paths) {
96
97
  return 'undefined'
97
98
  }
@@ -105,8 +106,3 @@ const getDesignName = (fs, paths) => {
105
106
  return packageNameFromDir(fs, mainPath)
106
107
  }
107
108
  }
108
-
109
- module.exports = {
110
- getDesignEntryPoint,
111
- getDesignName
112
- }
@@ -1,4 +1,4 @@
1
- const { deserializers } = require('@jscad/io')
1
+ import { deserializers } from '@jscad/io'
2
2
 
3
3
  /*
4
4
  * Transform the entry into a ready-to-use module.
@@ -21,7 +21,7 @@ const createJscadEntry = (entry, source) => {
21
21
  * Transforms are only applied to single files as current deserializers create source with a main() function. Only one.
22
22
  * Transforms are NOT applied to projects.
23
23
  */
24
- const transformSources = (options, filesAndFolders) => {
24
+ export const transformSources = (options, filesAndFolders) => {
25
25
  if (filesAndFolders && filesAndFolders.length > 1) return filesAndFolders // skip projects
26
26
 
27
27
  const codeTransforms = {
@@ -51,5 +51,3 @@ const transformSources = (options, filesAndFolders) => {
51
51
  }
52
52
  return filesAndFolders
53
53
  }
54
-
55
- module.exports = transformSources
@@ -1,4 +1,4 @@
1
- const validateDesignModule = (scriptRootModule) => {
1
+ export const validateDesignModule = (scriptRootModule) => {
2
2
  if (!scriptRootModule) {
3
3
  throw new Error('undefined root module passed !')
4
4
  }
@@ -9,5 +9,3 @@ const validateDesignModule = (scriptRootModule) => {
9
9
  throw new Error('no main() function found in the input script')
10
10
  }
11
11
  }
12
-
13
- module.exports = validateDesignModule
@@ -1 +1 @@
1
- module.exports = require('@jscad/vtree').api
1
+ export api from '@jscad/vtree'
@@ -1,11 +1,18 @@
1
- const path = require('path')
1
+ import path from 'path'
2
+ import { createRequire } from 'module'
3
+
4
+ import stripBom from 'strip-bom'
5
+
6
+ import * as jscadModule from '@jscad/modeling'
7
+
8
+ import { getFileExtensionFromString } from '../utils/getFileExtensionFromString.js'
9
+ import { combineParameterDefinitions, getParameterDefinitionsFromSource } from '../parameters/getParameterDefinitionsFromSource.js'
10
+
11
+ import * as fsModule from './makeFakeFs.js'
2
12
 
3
13
  // use posix versions of path, even in the browser
4
14
  const posix = path.posix ? path.posix : path
5
15
 
6
- const getFileExtensionFromString = require('../utils/getFileExtensionFromString')
7
- const { combineParameterDefinitions, getParameterDefinitionsFromSource } = require('../parameters/getParameterDefinitionsFromSource')
8
-
9
16
  /* find matching path in inputs
10
17
  * @param {} path
11
18
  * @param {} inputs=filesAndFolders
@@ -27,7 +34,6 @@ const findMatch = (path, inputs) => {
27
34
  }
28
35
 
29
36
  const registerJsExtension = (fs, _require) => {
30
- const stripBom = require('strip-bom')
31
37
  _require.extensions['.js'] = (module, filename) => {
32
38
  const content = fs.readFileSync(filename, 'utf8')
33
39
  module._compile(stripBom(content), filename)
@@ -41,36 +47,46 @@ const registerJsonExtension = (fs, _require) => {
41
47
  }
42
48
  }
43
49
 
44
- /* Make require callback functions based on the given file system.
50
+ /*
51
+ * Make require callback functions based on the given file system.
45
52
  */
46
- const makeWebRequire = (filesAndFolders, options) => {
53
+ export const makeWebRequire = (filesAndFolders, options) => {
47
54
  const defaults = {
48
55
  apiMainPath: '@jscad/modeling',
49
- fakeFs: require('./makeFakeFs')(filesAndFolders)
56
+ fakeFs: null
50
57
  }
51
58
  const { apiMainPath, fakeFs } = Object.assign({}, defaults, options)
52
- const apiModule = apiMainPath === '@jscad/modeling' ? require('@jscad/modeling') : require(apiMainPath)
59
+
60
+ // HACK create the require function if necessary
61
+ if (typeof self === 'undefined') {
62
+ // create require via Node API
63
+ const require = createRequire(import.meta.url)
64
+ }
65
+
66
+ const { makeFakeFs } = fsModule
67
+
68
+ // FIXME const apiModule = apiMainPath === '@jscad/modeling' ? jscadModule : require(apiMainPath)
69
+ const apiModule = apiMainPath === '@jscad/modeling' ? jscadModule : import(apiMainPath)
70
+ // const fsModule = fakeFs ? fakeFs : makeFakeFs
53
71
 
54
72
  // preset core modules
55
73
  // FIXME this list of modules should be an option, replacing apiMainPath
56
74
  const coreModules = {
57
- '@jscad/io': {
58
- exports: require('@jscad/io')
59
- },
60
- '@jscad/array-utils': {
61
- exports: require('@jscad/array-utils')
62
- },
75
+ // '@jscad/io': {
76
+ // exports: require('@jscad/io')
77
+ // },
78
+ // '@jscad/array-utils': {
79
+ // exports: require('@jscad/array-utils')
80
+ // },
63
81
  '@jscad/modeling': {
64
82
  exports: apiModule
65
83
  },
66
84
  // expose the fake fs module
67
85
  fs: {
68
- exports: fakeFs
86
+ exports: fsModule
69
87
  }
70
88
  }
71
89
 
72
- // console.log('*****\n',filesAndFolders,'\n*****')
73
-
74
90
  const extensions = {}
75
91
  const moduleCache = {}
76
92
 
@@ -79,8 +95,6 @@ const makeWebRequire = (filesAndFolders, options) => {
79
95
  * @see https://nodejs.org/dist/latest-v12.x/docs/api/modules.html#modules_all_together
80
96
  */
81
97
  const _require = (currentPath, requirePath) => {
82
- // console.log('***** require: cur:', currentPath, ' req:', requirePath)
83
-
84
98
  // core modules
85
99
  const directModule = coreModules[requirePath]
86
100
  if (directModule) {
@@ -92,7 +106,6 @@ const makeWebRequire = (filesAndFolders, options) => {
92
106
  }
93
107
 
94
108
  const loadAsFile = (requirePath) => {
95
- // console.log('***** load as file', requirePath)
96
109
  let baseExt = getFileExtensionFromString(requirePath)
97
110
  if (!baseExt) {
98
111
  baseExt = 'js' // for lookups
@@ -146,7 +159,6 @@ const makeWebRequire = (filesAndFolders, options) => {
146
159
  }
147
160
 
148
161
  const loadIndex = (requirePath) => {
149
- // console.log('***** load index', requirePath)
150
162
  const entry = findMatch(requirePath, filesAndFolders)
151
163
  if (!entry) return null
152
164
 
@@ -164,7 +176,6 @@ const makeWebRequire = (filesAndFolders, options) => {
164
176
  }
165
177
 
166
178
  const loadAsDirectory = (requirePath) => {
167
- // console.log('***** load as directory', requirePath)
168
179
  let entry = findMatch(requirePath, filesAndFolders)
169
180
  if (!entry) return null
170
181
 
@@ -223,7 +234,6 @@ const makeWebRequire = (filesAndFolders, options) => {
223
234
  }
224
235
 
225
236
  const loadNodeModules = (requirePath, basePath) => {
226
- // console.log('loadNodeModules',requirePath, basePath)
227
237
  const dirs = nodeModulesPaths(basePath)
228
238
  for (let i = 0; i < dirs.length; i++) {
229
239
  const dir = dirs[i]
@@ -250,9 +260,7 @@ const makeWebRequire = (filesAndFolders, options) => {
250
260
  req.extensions = extensions
251
261
  req.resolve = () => {}
252
262
 
253
- registerJsExtension(fakeFs, req)
254
- registerJsonExtension(fakeFs, req)
263
+ registerJsExtension(makeFakeFs(filesAndFolders), req)
264
+ registerJsonExtension(makeFakeFs(filesAndFolders), req)
255
265
  return req
256
266
  }
257
-
258
- module.exports = makeWebRequire
@@ -1,9 +1,9 @@
1
- const test = require('ava')
1
+ import test from 'ava'
2
2
 
3
- const { registerAllExtensions } = require('../io/registerExtensions')
3
+ import { registerAllExtensions } from '../io/registerExtensions.js'
4
4
 
5
- const makeWebRequire = require('./webRequire')
6
- const makeFakeFs = require('./makeFakeFs')
5
+ import { makeWebRequire } from './webRequire.js'
6
+ import { makeFakeFs } from './makeFakeFs.js'
7
7
 
8
8
  test.beforeEach((t) => {
9
9
  })
@@ -76,10 +76,9 @@ test('webRequire: should support require, from a directory with dependent files'
76
76
 
77
77
  test('webRequire: should allow using require.extensions like the native node require (simple)', (t) => {
78
78
  const registerJscadExtension = (fs, _require) => {
79
- const stripBom = require('strip-bom')
80
79
  _require.extensions['.jscad'] = (module, filename) => {
81
80
  const content = fs.readFileSync(filename, 'utf8')
82
- module._compile(stripBom(content), filename)
81
+ module._compile(content, filename)
83
82
  }
84
83
  }
85
84
 
@@ -107,7 +106,7 @@ test('webRequire: should allow using require.extensions like the native node req
107
106
  t.true(designRootModule.main instanceof Function)
108
107
  })
109
108
 
110
- test('webRequire: should allow using require.extensions like the native node require (parser)', (t) => {
109
+ test.skip('webRequire: should allow using require.extensions like the native node require (parser)', (t) => {
111
110
  const registerStlExtension = (fs, _require) => {
112
111
  const { deserializers } = require('@jscad/io')
113
112
  const deserializer = deserializers.stl
@@ -168,7 +167,7 @@ const singleFileJs = [
168
167
  fullPath: '/logo.js',
169
168
  name: 'logo.js',
170
169
  source: `
171
- const { cube } = require('@jscad/modeling').primitives
170
+ const { cube } = require('@jscad/modeling')
172
171
 
173
172
  const main = () => {
174
173
  return cube()
@@ -317,7 +316,7 @@ const directoryWithDependencies = [
317
316
  fullPath: '/project/file2.js',
318
317
  name: 'file2.js',
319
318
  source: `
320
- const { cube } = require('@jscad/modeling').primitives
319
+ const { cube } = require('@jscad/modeling')
321
320
 
322
321
  const file2 = () => {
323
322
  return cube()