mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-03-01 22:10:27 +00:00
38 lines
1005 B
TypeScript
38 lines
1005 B
TypeScript
import { readFileSync } from 'fs-extra';
|
|
import { NodeVM } from 'vm2';
|
|
// import PropTypes from 'prop-types';
|
|
|
|
const cssPattern = /\.(css|scss|sass|less)$/;
|
|
function requireInSandbox(filePath: string, PropTypes: any) {
|
|
const vm = new NodeVM({
|
|
sandbox: {},
|
|
sourceExtensions: ['js', 'css', 'scss', 'sass', 'less'],
|
|
compiler: (code, filename) => {
|
|
if (filename.match(cssPattern)) {
|
|
return `
|
|
const handler = {
|
|
get() {
|
|
return new Proxy({}, handler);
|
|
},
|
|
};
|
|
const proxiedObject = new Proxy({}, handler);
|
|
module.exports = proxiedObject;
|
|
`;
|
|
} else {
|
|
return code;
|
|
}
|
|
},
|
|
require: {
|
|
external: true,
|
|
context: 'sandbox',
|
|
mock: {
|
|
'prop-types': PropTypes,
|
|
},
|
|
},
|
|
});
|
|
const fileContent = readFileSync(filePath, { encoding: 'utf8' });
|
|
return vm.run(fileContent, filePath);
|
|
}
|
|
|
|
export default requireInSandbox;
|