gengyang d4c45d2dea fix: fix typescript related bugs, including the following:
1. fix bug of failing to resolve RFC components
    2. support transforming function args
    3. fix bug of oneOfType
    4. fix bug of crash when circular parsing
    5. fix bug of union
    6. support tuple
    7. fix bug of builtin type parsing
    8. fix bug of false positive component identification
    9. fix bug of entry resolving
2020-10-13 10:20:59 +08:00

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;