@bildit-platform/engine 0.1.2 → 0.1.5
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/README.md +17 -2
- package/dist/cjs/react/createModuleWithDependencies.d.ts +6 -4
- package/dist/cjs/react/createModuleWithDependencies.js +17 -9
- package/dist/cjs/react/index.d.ts +1 -0
- package/dist/cjs/react/interpretModuleString.d.ts +6 -4
- package/dist/cjs/react/interpretModuleString.js +9 -45
- package/dist/cjs/react/registerGlobalModuleCreator.d.ts +7 -0
- package/dist/cjs/react/registerGlobalModuleCreator.js +69 -0
- package/dist/cjs/react/registerGlobalModuleCreator.test.d.ts +4 -0
- package/dist/cjs/types/index.d.ts +4 -0
- package/dist/esm/react/createModuleWithDependencies.d.ts +6 -4
- package/dist/esm/react/createModuleWithDependencies.js +17 -9
- package/dist/esm/react/index.d.ts +1 -0
- package/dist/esm/react/interpretModuleString.d.ts +6 -4
- package/dist/esm/react/interpretModuleString.js +9 -26
- package/dist/esm/react/registerGlobalModuleCreator.d.ts +7 -0
- package/dist/esm/react/registerGlobalModuleCreator.js +46 -0
- package/dist/esm/react/registerGlobalModuleCreator.test.d.ts +4 -0
- package/dist/esm/types/index.d.ts +4 -0
- package/package.json +9 -2
package/README.md
CHANGED
|
@@ -65,11 +65,16 @@ Interprets JavaScript module code from a string and returns the specified export
|
|
|
65
65
|
|
|
66
66
|
**Signature:**
|
|
67
67
|
```javascript
|
|
68
|
-
function interpretModuleString<T = unknown>(
|
|
68
|
+
function interpretModuleString<T = unknown>(
|
|
69
|
+
moduleCode: string,
|
|
70
|
+
exportName?: string,
|
|
71
|
+
extraDependenciesConfig: Record<string, ExtraDependencyConfig>
|
|
72
|
+
): T | null;
|
|
69
73
|
```
|
|
70
74
|
**Parameters:**
|
|
71
75
|
- `moduleCode`: The JavaScript module code as a string.
|
|
72
76
|
- `exportName` (optional): The name of the export to retrieve. Defaults to 'default'.
|
|
77
|
+
- `extraDependenciesConfig` (optional): Additional customer-specific dependencies.
|
|
73
78
|
|
|
74
79
|
**Returns:**
|
|
75
80
|
- The specified export from the module code, or `null` if not found.
|
|
@@ -81,12 +86,22 @@ Creates a module context with controlled dependencies.
|
|
|
81
86
|
```javascript
|
|
82
87
|
function createModuleWithDependencies<T extends Record<string, unknown>>(
|
|
83
88
|
dependencies: string[],
|
|
84
|
-
moduleFactory: (...args: unknown[]) => void
|
|
89
|
+
moduleFactory: (...args: unknown[]) => void,
|
|
90
|
+
extraDependenciesConfig: Record<string, ExtraDependencyConfig>
|
|
85
91
|
): T;
|
|
86
92
|
```
|
|
87
93
|
**Parameters:**
|
|
88
94
|
- `dependencies`: An array of dependency names required by the module.
|
|
89
95
|
- `moduleFactory`: A factory function that receives the resolved dependencies as arguments and populates the module’s exports.
|
|
96
|
+
- `extraDependenciesConfig` (optional): Extra dependencies configuration to set global name and module.
|
|
90
97
|
|
|
91
98
|
**Returns:**
|
|
92
99
|
- An object containing the module exports.
|
|
100
|
+
|
|
101
|
+
## Types
|
|
102
|
+
```javascript
|
|
103
|
+
export type ExtraDependencyConfig = {
|
|
104
|
+
module: any;
|
|
105
|
+
globalName?: string;
|
|
106
|
+
};
|
|
107
|
+
```
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { ExtraDependencyConfig } from "../types";
|
|
1
2
|
/**
|
|
2
3
|
* Creates a module context with controlled dependencies.
|
|
3
4
|
* This function is used internally by the dynamic code interpreter
|
|
4
5
|
* to provide specific dependencies to the interpreted module.
|
|
5
6
|
*
|
|
6
|
-
* @param dependencies - Array of dependency names required by the module
|
|
7
|
-
* @param moduleFactory - Factory function that creates the module using the dependencies
|
|
8
|
-
* @
|
|
7
|
+
* @param {string[]} dependencies - Array of dependency names required by the module
|
|
8
|
+
* @param {(...args: unknown[]) => void} moduleFactory - Factory function that creates the module using the dependencies
|
|
9
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - Additional customer-specific dependencies
|
|
10
|
+
* @returns {T extends Record<string, unknown>} The exports object from the module
|
|
9
11
|
*/
|
|
10
|
-
export default function createModuleWithDependencies<T extends Record<string, unknown>>(dependencies: string[], moduleFactory: (...args: unknown[]) => void): T;
|
|
12
|
+
export default function createModuleWithDependencies<T extends Record<string, unknown>>(dependencies: string[], moduleFactory: (...args: unknown[]) => void, extraDependenciesConfig?: Record<string, ExtraDependencyConfig>): T;
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
|
|
21
21
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
22
22
|
|
|
23
|
-
var
|
|
23
|
+
var React = require('react');
|
|
24
24
|
var jsxRuntime = require('react/jsx-runtime');
|
|
25
25
|
|
|
26
26
|
/**
|
|
@@ -28,18 +28,26 @@ var jsxRuntime = require('react/jsx-runtime');
|
|
|
28
28
|
* This function is used internally by the dynamic code interpreter
|
|
29
29
|
* to provide specific dependencies to the interpreted module.
|
|
30
30
|
*
|
|
31
|
-
* @param dependencies - Array of dependency names required by the module
|
|
32
|
-
* @param moduleFactory - Factory function that creates the module using the dependencies
|
|
33
|
-
* @
|
|
31
|
+
* @param {string[]} dependencies - Array of dependency names required by the module
|
|
32
|
+
* @param {(...args: unknown[]) => void} moduleFactory - Factory function that creates the module using the dependencies
|
|
33
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - Additional customer-specific dependencies
|
|
34
|
+
* @returns {T extends Record<string, unknown>} The exports object from the module
|
|
34
35
|
*/
|
|
35
|
-
function createModuleWithDependencies(dependencies, moduleFactory) {
|
|
36
|
+
function createModuleWithDependencies(dependencies, moduleFactory, extraDependenciesConfig = {}) {
|
|
36
37
|
// Collect all data exported from the remote code.
|
|
37
38
|
const exports = {};
|
|
39
|
+
const coreDependencies = {
|
|
40
|
+
react: React,
|
|
41
|
+
'react/jsx-runtime': jsxRuntime,
|
|
42
|
+
};
|
|
43
|
+
const extraDependencies = Object.keys(extraDependenciesConfig).reduce((acc, key) => {
|
|
44
|
+
const { module } = extraDependenciesConfig[key];
|
|
45
|
+
acc[key] = module;
|
|
46
|
+
return acc;
|
|
47
|
+
}, {});
|
|
38
48
|
const availableDependencies = {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// 'styled-components': styled,
|
|
42
|
-
'react/jsx-runtime': jsxRuntime
|
|
49
|
+
...coreDependencies,
|
|
50
|
+
...extraDependencies,
|
|
43
51
|
};
|
|
44
52
|
const resolvedDependencies = dependencies.map((dependencyName) => {
|
|
45
53
|
if (dependencyName === 'exports') {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ExtraDependencyConfig } from '../types';
|
|
1
2
|
import createModuleWithDependencies from './createModuleWithDependencies';
|
|
2
3
|
type DefineFunction = typeof createModuleWithDependencies;
|
|
3
4
|
declare global {
|
|
@@ -19,9 +20,10 @@ declare global {
|
|
|
19
20
|
* limited access to dependencies. It's designed for dynamic code loading scenarios
|
|
20
21
|
* where the code comes from a trusted source.
|
|
21
22
|
*
|
|
22
|
-
* @param moduleCode - JavaScript module code to interpret
|
|
23
|
-
* @param exportName - Name of the export to return (defaults to 'default')
|
|
24
|
-
* @
|
|
23
|
+
* @param {string} moduleCode - JavaScript module code to interpret
|
|
24
|
+
* @param {string} exportName - Name of the export to return (defaults to 'default')
|
|
25
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - Additional customer-specific dependencies
|
|
26
|
+
* @returns {T | null} The requested module export or null if interpretation fails
|
|
25
27
|
*/
|
|
26
|
-
declare const interpretModuleString: <T = unknown>(moduleCode: string, exportName?: string) => T | null;
|
|
28
|
+
declare const interpretModuleString: <T = unknown>(moduleCode: string, exportName?: string, extraDependenciesConfig?: Record<string, ExtraDependencyConfig>) => T | null;
|
|
27
29
|
export default interpretModuleString;
|
|
@@ -20,48 +20,8 @@
|
|
|
20
20
|
|
|
21
21
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
22
22
|
|
|
23
|
-
var
|
|
24
|
-
var createModuleWithDependencies = require('./createModuleWithDependencies.js');
|
|
25
|
-
|
|
26
|
-
function _interopNamespaceDefault(e) {
|
|
27
|
-
var n = Object.create(null);
|
|
28
|
-
if (e) {
|
|
29
|
-
Object.keys(e).forEach(function (k) {
|
|
30
|
-
if (k !== 'default') {
|
|
31
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
32
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
get: function () { return e[k]; }
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
n.default = e;
|
|
40
|
-
return Object.freeze(n);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
var ReactImport__namespace = /*#__PURE__*/_interopNamespaceDefault(ReactImport);
|
|
23
|
+
var registerGlobalModuleCreator = require('./registerGlobalModuleCreator.js');
|
|
44
24
|
|
|
45
|
-
/**
|
|
46
|
-
* Registers the module creator in the global scope for use by evaluated code
|
|
47
|
-
*/
|
|
48
|
-
const registerGlobalModuleCreator = () => {
|
|
49
|
-
// Check if we're in a Node.js environment
|
|
50
|
-
if (typeof process !== 'undefined' &&
|
|
51
|
-
process.versions != null &&
|
|
52
|
-
process.versions.node != null) {
|
|
53
|
-
// We're in Node.js
|
|
54
|
-
globalThis.define = createModuleWithDependencies.default;
|
|
55
|
-
globalThis.React = ReactImport__namespace;
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
// We're in a browser or other environment
|
|
59
|
-
window.define = createModuleWithDependencies.default;
|
|
60
|
-
window.React = ReactImport__namespace;
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
// Execute the registration
|
|
64
|
-
registerGlobalModuleCreator();
|
|
65
25
|
/**
|
|
66
26
|
* Interprets JavaScript module code from a string and returns the specified export.
|
|
67
27
|
*
|
|
@@ -69,12 +29,16 @@ registerGlobalModuleCreator();
|
|
|
69
29
|
* limited access to dependencies. It's designed for dynamic code loading scenarios
|
|
70
30
|
* where the code comes from a trusted source.
|
|
71
31
|
*
|
|
72
|
-
* @param moduleCode - JavaScript module code to interpret
|
|
73
|
-
* @param exportName - Name of the export to return (defaults to 'default')
|
|
74
|
-
* @
|
|
32
|
+
* @param {string} moduleCode - JavaScript module code to interpret
|
|
33
|
+
* @param {string} exportName - Name of the export to return (defaults to 'default')
|
|
34
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - Additional customer-specific dependencies
|
|
35
|
+
* @returns {T | null} The requested module export or null if interpretation fails
|
|
75
36
|
*/
|
|
76
|
-
const interpretModuleString = (moduleCode, exportName = 'default') => {
|
|
37
|
+
const interpretModuleString = (moduleCode, exportName = 'default', extraDependenciesConfig) => {
|
|
77
38
|
try {
|
|
39
|
+
if (!moduleCode)
|
|
40
|
+
return null;
|
|
41
|
+
registerGlobalModuleCreator.default(extraDependenciesConfig);
|
|
78
42
|
const compiledModule = eval === null || eval === void 0 ? void 0 : eval(`"use strict"; ${moduleCode}`);
|
|
79
43
|
if (!compiledModule)
|
|
80
44
|
return null;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ExtraDependencyConfig } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Registers the module creator in the global scope for use by evaluated code
|
|
4
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - extra dependencies config map
|
|
5
|
+
*/
|
|
6
|
+
declare const registerGlobalModuleCreator: (extraDependenciesConfig?: Record<string, ExtraDependencyConfig>) => void;
|
|
7
|
+
export default registerGlobalModuleCreator;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2022 BILDIT, INC.
|
|
3
|
+
*
|
|
4
|
+
* This file, and the software contained herein, are the exclusive property of BILDIT, INC.
|
|
5
|
+
* Unauthorized copying, distribution, or modification of this software is strictly prohibited.
|
|
6
|
+
* All rights reserved.
|
|
7
|
+
*
|
|
8
|
+
* This file is licensed under the ENT License ("License"). Use of this file is subject to the
|
|
9
|
+
* terms and conditions specified in the License. You may obtain a copy of the License at:
|
|
10
|
+
*
|
|
11
|
+
* https://bildit.co/ENTLicense
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
|
14
|
+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
|
|
15
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
16
|
+
* DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
|
|
17
|
+
* OUT OF, OR IN CONNECTION WITH, THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
18
|
+
*/
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
22
|
+
|
|
23
|
+
var React = require('react');
|
|
24
|
+
var createModuleWithDependencies = require('./createModuleWithDependencies.js');
|
|
25
|
+
|
|
26
|
+
function _interopNamespaceDefault(e) {
|
|
27
|
+
var n = Object.create(null);
|
|
28
|
+
if (e) {
|
|
29
|
+
Object.keys(e).forEach(function (k) {
|
|
30
|
+
if (k !== 'default') {
|
|
31
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
32
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () { return e[k]; }
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
n.default = e;
|
|
40
|
+
return Object.freeze(n);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Registers the module creator in the global scope for use by evaluated code
|
|
47
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - extra dependencies config map
|
|
48
|
+
*/
|
|
49
|
+
const registerGlobalModuleCreator = (extraDependenciesConfig = {}) => {
|
|
50
|
+
const define = (dependencies, moduleFactory) => {
|
|
51
|
+
return createModuleWithDependencies.default(dependencies, moduleFactory, extraDependenciesConfig);
|
|
52
|
+
};
|
|
53
|
+
const nodeJsEnvironment = typeof process !== 'undefined' &&
|
|
54
|
+
process.versions != null &&
|
|
55
|
+
process.versions.node != null;
|
|
56
|
+
if (nodeJsEnvironment || typeof window !== 'undefined') {
|
|
57
|
+
const globalScope = nodeJsEnvironment ? globalThis : window;
|
|
58
|
+
globalScope.define = define;
|
|
59
|
+
globalScope.React = React__namespace;
|
|
60
|
+
for (const [_, value] of Object.entries(extraDependenciesConfig)) {
|
|
61
|
+
const { module, globalName } = value;
|
|
62
|
+
if (globalName) {
|
|
63
|
+
globalScope[globalName] = module;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
exports.default = registerGlobalModuleCreator;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { ExtraDependencyConfig } from "../types";
|
|
1
2
|
/**
|
|
2
3
|
* Creates a module context with controlled dependencies.
|
|
3
4
|
* This function is used internally by the dynamic code interpreter
|
|
4
5
|
* to provide specific dependencies to the interpreted module.
|
|
5
6
|
*
|
|
6
|
-
* @param dependencies - Array of dependency names required by the module
|
|
7
|
-
* @param moduleFactory - Factory function that creates the module using the dependencies
|
|
8
|
-
* @
|
|
7
|
+
* @param {string[]} dependencies - Array of dependency names required by the module
|
|
8
|
+
* @param {(...args: unknown[]) => void} moduleFactory - Factory function that creates the module using the dependencies
|
|
9
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - Additional customer-specific dependencies
|
|
10
|
+
* @returns {T extends Record<string, unknown>} The exports object from the module
|
|
9
11
|
*/
|
|
10
|
-
export default function createModuleWithDependencies<T extends Record<string, unknown>>(dependencies: string[], moduleFactory: (...args: unknown[]) => void): T;
|
|
12
|
+
export default function createModuleWithDependencies<T extends Record<string, unknown>>(dependencies: string[], moduleFactory: (...args: unknown[]) => void, extraDependenciesConfig?: Record<string, ExtraDependencyConfig>): T;
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
|
|
17
17
|
* OUT OF, OR IN CONNECTION WITH, THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
18
18
|
*/
|
|
19
|
-
import
|
|
19
|
+
import React__default from 'react';
|
|
20
20
|
import jsxRuntime from 'react/jsx-runtime';
|
|
21
21
|
|
|
22
22
|
/**
|
|
@@ -24,18 +24,26 @@ import jsxRuntime from 'react/jsx-runtime';
|
|
|
24
24
|
* This function is used internally by the dynamic code interpreter
|
|
25
25
|
* to provide specific dependencies to the interpreted module.
|
|
26
26
|
*
|
|
27
|
-
* @param dependencies - Array of dependency names required by the module
|
|
28
|
-
* @param moduleFactory - Factory function that creates the module using the dependencies
|
|
29
|
-
* @
|
|
27
|
+
* @param {string[]} dependencies - Array of dependency names required by the module
|
|
28
|
+
* @param {(...args: unknown[]) => void} moduleFactory - Factory function that creates the module using the dependencies
|
|
29
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - Additional customer-specific dependencies
|
|
30
|
+
* @returns {T extends Record<string, unknown>} The exports object from the module
|
|
30
31
|
*/
|
|
31
|
-
function createModuleWithDependencies(dependencies, moduleFactory) {
|
|
32
|
+
function createModuleWithDependencies(dependencies, moduleFactory, extraDependenciesConfig = {}) {
|
|
32
33
|
// Collect all data exported from the remote code.
|
|
33
34
|
const exports = {};
|
|
35
|
+
const coreDependencies = {
|
|
36
|
+
react: React__default,
|
|
37
|
+
'react/jsx-runtime': jsxRuntime,
|
|
38
|
+
};
|
|
39
|
+
const extraDependencies = Object.keys(extraDependenciesConfig).reduce((acc, key) => {
|
|
40
|
+
const { module } = extraDependenciesConfig[key];
|
|
41
|
+
acc[key] = module;
|
|
42
|
+
return acc;
|
|
43
|
+
}, {});
|
|
34
44
|
const availableDependencies = {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// 'styled-components': styled,
|
|
38
|
-
'react/jsx-runtime': jsxRuntime
|
|
45
|
+
...coreDependencies,
|
|
46
|
+
...extraDependencies,
|
|
39
47
|
};
|
|
40
48
|
const resolvedDependencies = dependencies.map((dependencyName) => {
|
|
41
49
|
if (dependencyName === 'exports') {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ExtraDependencyConfig } from '../types';
|
|
1
2
|
import createModuleWithDependencies from './createModuleWithDependencies';
|
|
2
3
|
type DefineFunction = typeof createModuleWithDependencies;
|
|
3
4
|
declare global {
|
|
@@ -19,9 +20,10 @@ declare global {
|
|
|
19
20
|
* limited access to dependencies. It's designed for dynamic code loading scenarios
|
|
20
21
|
* where the code comes from a trusted source.
|
|
21
22
|
*
|
|
22
|
-
* @param moduleCode - JavaScript module code to interpret
|
|
23
|
-
* @param exportName - Name of the export to return (defaults to 'default')
|
|
24
|
-
* @
|
|
23
|
+
* @param {string} moduleCode - JavaScript module code to interpret
|
|
24
|
+
* @param {string} exportName - Name of the export to return (defaults to 'default')
|
|
25
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - Additional customer-specific dependencies
|
|
26
|
+
* @returns {T | null} The requested module export or null if interpretation fails
|
|
25
27
|
*/
|
|
26
|
-
declare const interpretModuleString: <T = unknown>(moduleCode: string, exportName?: string) => T | null;
|
|
28
|
+
declare const interpretModuleString: <T = unknown>(moduleCode: string, exportName?: string, extraDependenciesConfig?: Record<string, ExtraDependencyConfig>) => T | null;
|
|
27
29
|
export default interpretModuleString;
|
|
@@ -16,29 +16,8 @@
|
|
|
16
16
|
* DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
|
|
17
17
|
* OUT OF, OR IN CONNECTION WITH, THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
18
18
|
*/
|
|
19
|
-
import
|
|
20
|
-
import createModuleWithDependencies from './createModuleWithDependencies.js';
|
|
19
|
+
import registerGlobalModuleCreator from './registerGlobalModuleCreator.js';
|
|
21
20
|
|
|
22
|
-
/**
|
|
23
|
-
* Registers the module creator in the global scope for use by evaluated code
|
|
24
|
-
*/
|
|
25
|
-
const registerGlobalModuleCreator = () => {
|
|
26
|
-
// Check if we're in a Node.js environment
|
|
27
|
-
if (typeof process !== 'undefined' &&
|
|
28
|
-
process.versions != null &&
|
|
29
|
-
process.versions.node != null) {
|
|
30
|
-
// We're in Node.js
|
|
31
|
-
globalThis.define = createModuleWithDependencies;
|
|
32
|
-
globalThis.React = ReactImport;
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
// We're in a browser or other environment
|
|
36
|
-
window.define = createModuleWithDependencies;
|
|
37
|
-
window.React = ReactImport;
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
// Execute the registration
|
|
41
|
-
registerGlobalModuleCreator();
|
|
42
21
|
/**
|
|
43
22
|
* Interprets JavaScript module code from a string and returns the specified export.
|
|
44
23
|
*
|
|
@@ -46,12 +25,16 @@ registerGlobalModuleCreator();
|
|
|
46
25
|
* limited access to dependencies. It's designed for dynamic code loading scenarios
|
|
47
26
|
* where the code comes from a trusted source.
|
|
48
27
|
*
|
|
49
|
-
* @param moduleCode - JavaScript module code to interpret
|
|
50
|
-
* @param exportName - Name of the export to return (defaults to 'default')
|
|
51
|
-
* @
|
|
28
|
+
* @param {string} moduleCode - JavaScript module code to interpret
|
|
29
|
+
* @param {string} exportName - Name of the export to return (defaults to 'default')
|
|
30
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - Additional customer-specific dependencies
|
|
31
|
+
* @returns {T | null} The requested module export or null if interpretation fails
|
|
52
32
|
*/
|
|
53
|
-
const interpretModuleString = (moduleCode, exportName = 'default') => {
|
|
33
|
+
const interpretModuleString = (moduleCode, exportName = 'default', extraDependenciesConfig) => {
|
|
54
34
|
try {
|
|
35
|
+
if (!moduleCode)
|
|
36
|
+
return null;
|
|
37
|
+
registerGlobalModuleCreator(extraDependenciesConfig);
|
|
55
38
|
const compiledModule = eval === null || eval === void 0 ? void 0 : eval(`"use strict"; ${moduleCode}`);
|
|
56
39
|
if (!compiledModule)
|
|
57
40
|
return null;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ExtraDependencyConfig } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Registers the module creator in the global scope for use by evaluated code
|
|
4
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - extra dependencies config map
|
|
5
|
+
*/
|
|
6
|
+
declare const registerGlobalModuleCreator: (extraDependenciesConfig?: Record<string, ExtraDependencyConfig>) => void;
|
|
7
|
+
export default registerGlobalModuleCreator;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2022 BILDIT, INC.
|
|
3
|
+
*
|
|
4
|
+
* This file, and the software contained herein, are the exclusive property of BILDIT, INC.
|
|
5
|
+
* Unauthorized copying, distribution, or modification of this software is strictly prohibited.
|
|
6
|
+
* All rights reserved.
|
|
7
|
+
*
|
|
8
|
+
* This file is licensed under the ENT License ("License"). Use of this file is subject to the
|
|
9
|
+
* terms and conditions specified in the License. You may obtain a copy of the License at:
|
|
10
|
+
*
|
|
11
|
+
* https://bildit.co/ENTLicense
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
|
14
|
+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
|
|
15
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
16
|
+
* DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
|
|
17
|
+
* OUT OF, OR IN CONNECTION WITH, THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
18
|
+
*/
|
|
19
|
+
import * as React from 'react';
|
|
20
|
+
import createModuleWithDependencies from './createModuleWithDependencies.js';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Registers the module creator in the global scope for use by evaluated code
|
|
24
|
+
* @param {Record<string, ExtraDependencyConfig>} extraDependenciesConfig - extra dependencies config map
|
|
25
|
+
*/
|
|
26
|
+
const registerGlobalModuleCreator = (extraDependenciesConfig = {}) => {
|
|
27
|
+
const define = (dependencies, moduleFactory) => {
|
|
28
|
+
return createModuleWithDependencies(dependencies, moduleFactory, extraDependenciesConfig);
|
|
29
|
+
};
|
|
30
|
+
const nodeJsEnvironment = typeof process !== 'undefined' &&
|
|
31
|
+
process.versions != null &&
|
|
32
|
+
process.versions.node != null;
|
|
33
|
+
if (nodeJsEnvironment || typeof window !== 'undefined') {
|
|
34
|
+
const globalScope = nodeJsEnvironment ? globalThis : window;
|
|
35
|
+
globalScope.define = define;
|
|
36
|
+
globalScope.React = React;
|
|
37
|
+
for (const [_, value] of Object.entries(extraDependenciesConfig)) {
|
|
38
|
+
const { module, globalName } = value;
|
|
39
|
+
if (globalName) {
|
|
40
|
+
globalScope[globalName] = module;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export { registerGlobalModuleCreator as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bildit-platform/engine",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "BILDIT Render Engine",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
"dev": "rollup -c -w",
|
|
25
25
|
"lint": "eslint src/**/*.{ts,tsx}",
|
|
26
26
|
"clean": "rimraf dist",
|
|
27
|
-
"prepare": "npm run build"
|
|
27
|
+
"prepare": "npm run build",
|
|
28
|
+
"test": "jest"
|
|
28
29
|
},
|
|
29
30
|
"keywords": [
|
|
30
31
|
"bildit",
|
|
@@ -41,11 +42,13 @@
|
|
|
41
42
|
],
|
|
42
43
|
"license": "UNLICENSED",
|
|
43
44
|
"peerDependencies": {
|
|
45
|
+
"next": "^15.0.0",
|
|
44
46
|
"react": ">=18.0.0",
|
|
45
47
|
"react-dom": ">=18.0.0"
|
|
46
48
|
},
|
|
47
49
|
"devDependencies": {
|
|
48
50
|
"@rollup/plugin-commonjs": "^24.0.0",
|
|
51
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
49
52
|
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
50
53
|
"@rollup/plugin-typescript": "^11.0.0",
|
|
51
54
|
"@types/jest": "^29.5.14",
|
|
@@ -54,9 +57,13 @@
|
|
|
54
57
|
"@types/react-dom": ">=18.0.0",
|
|
55
58
|
"@types/react-is": ">=18.0.0",
|
|
56
59
|
"eslint": "^8.0.0",
|
|
60
|
+
"jest": "^29.7.0",
|
|
61
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
57
62
|
"rimraf": "^4.0.0",
|
|
58
63
|
"rollup": "^3.0.0",
|
|
59
64
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
65
|
+
"ts-jest": "^29.3.2",
|
|
66
|
+
"tslib": "^2.8.1",
|
|
60
67
|
"typescript": "^5.0.0"
|
|
61
68
|
},
|
|
62
69
|
"dependencies": {
|