* Wraps the given content in a script and runs it in a new context. * @param {string} filename The name of the file being loaded * @param {string} content The content of the file being loaded * @param {Module|undefined} cjsModuleInstance The CommonJS loader instance * @param {'commonjs'|undefine
(filename, content, cjsModuleInstance, format)
| 1832 | * @returns {object} |
| 1833 | */ |
| 1834 | function wrapSafe(filename, content, cjsModuleInstance, format) { |
| 1835 | assert(format !== 'module', 'ESM should be handled in loadESMFromCJS()'); |
| 1836 | const hostDefinedOptionId = vm_dynamic_import_default_internal; |
| 1837 | const importModuleDynamically = vm_dynamic_import_default_internal; |
| 1838 | if (patched) { |
| 1839 | const wrapped = Module.wrap(content); |
| 1840 | const script = makeContextifyScript( |
| 1841 | wrapped, // code |
| 1842 | filename, // filename |
| 1843 | 0, // lineOffset |
| 1844 | 0, // columnOffset |
| 1845 | undefined, // cachedData |
| 1846 | false, // produceCachedData |
| 1847 | undefined, // parsingContext |
| 1848 | hostDefinedOptionId, // hostDefinedOptionId |
| 1849 | importModuleDynamically, // importModuleDynamically |
| 1850 | ); |
| 1851 | |
| 1852 | // Cache the source map for the module if present. |
| 1853 | const { sourceMapURL, sourceURL } = script; |
| 1854 | if (sourceMapURL) { |
| 1855 | maybeCacheSourceMap(filename, content, cjsModuleInstance, false, sourceURL, sourceMapURL); |
| 1856 | } |
| 1857 | |
| 1858 | return { |
| 1859 | __proto__: null, |
| 1860 | function: runScriptInThisContext(script, true, false), |
| 1861 | sourceMapURL, |
| 1862 | }; |
| 1863 | } |
| 1864 | |
| 1865 | let shouldDetectModule = false; |
| 1866 | if (format !== 'commonjs') { |
| 1867 | if (cjsModuleInstance?.[kIsMainSymbol]) { |
| 1868 | // For entry points, format detection is used unless explicitly disabled. |
| 1869 | shouldDetectModule = getOptionValue('--experimental-detect-module'); |
| 1870 | } else { |
| 1871 | // For modules being loaded by `require()`, if require(esm) is disabled, |
| 1872 | // don't try to reparse to detect format and just throw for ESM syntax. |
| 1873 | shouldDetectModule = getOptionValue('--require-module'); |
| 1874 | } |
| 1875 | } |
| 1876 | const result = compileFunctionForCJSLoader(content, filename, false /* is_sea_main */, shouldDetectModule); |
| 1877 | |
| 1878 | // Cache the source map for the module if present. |
| 1879 | if (result.sourceMapURL) { |
| 1880 | maybeCacheSourceMap(filename, content, cjsModuleInstance, false, result.sourceURL, result.sourceMapURL); |
| 1881 | } |
| 1882 | |
| 1883 | return result; |
| 1884 | } |
| 1885 | |
| 1886 | /** |
| 1887 | * Run the file contents in the correct scope or sandbox. Expose the correct helper variables (`require`, `module`, |
no test coverage detected
searching dependent graphs…