module-from-string
Advanced tools
Comparing version 3.1.1 to 3.1.2
@@ -25,4 +25,4 @@ 'use strict'; | ||
}; | ||
const isVMModuleAvailable = () => vm__default['default'].Module !== void 0; | ||
const fileURLToPath = (value) => value.startsWith("file://") ? url__default['default'].fileURLToPath(new URL(value)) : value; | ||
const isVMModuleAvailable = () => vm__default["default"].Module !== void 0; | ||
const fileURLToPath = (value) => value.startsWith("file://") ? url__default["default"].fileURLToPath(value) : value; | ||
const FUNCTION_NAMES = [ | ||
@@ -44,7 +44,8 @@ "getCallerDirname", | ||
const callerFilename = callSites[0].getFileName(); | ||
return path__default['default'].dirname(callerFilename !== null ? fileURLToPath(callerFilename) : process.argv[1]); | ||
return path__default["default"].dirname(callerFilename !== null ? fileURLToPath(callerFilename) : process.argv[1]); | ||
}; | ||
const VALID_PATH_REGEXP = /^[./\\]/; | ||
const resolveModuleSpecifier = (specifier, dirname) => { | ||
const specifierPath = fileURLToPath(specifier); | ||
return /^[./\\]/.test(specifierPath) ? path__default['default'].resolve(dirname, specifierPath) : specifier; | ||
return VALID_PATH_REGEXP.test(specifierPath) ? path__default["default"].resolve(dirname, specifierPath) : specifier; | ||
}; | ||
@@ -55,3 +56,3 @@ | ||
const moduleDirname = dirname != null ? dirname : getCallerDirname(); | ||
const moduleFilename = path__default['default'].join(moduleDirname, `${nanoid.nanoid()}.js`); | ||
const moduleFilename = path__default["default"].join(moduleDirname, `${nanoid.nanoid()}.js`); | ||
const mainModule = isInESModuleScope() ? void 0 : require.main; | ||
@@ -63,3 +64,3 @@ const contextModule = new module$1.Module(moduleFilename, mainModule); | ||
contextModule.require = module$1.createRequire(moduleFilename); | ||
vm__default['default'].runInNewContext(code, { | ||
vm__default["default"].runInNewContext(code, { | ||
__dirname: contextModule.path, | ||
@@ -80,3 +81,2 @@ __filename: contextModule.filename, | ||
const ERR_REQUIRE_ESM = "ERR_REQUIRE_ESM"; | ||
const IMPORT_META_URL_SHIM = "var import_meta_url = require('url').pathToFileURL(__filename).toString();"; | ||
@@ -90,3 +90,3 @@ const IMPORT_META_RESOLVE_SHIM = `var import_meta_resolve = () => { | ||
};`; | ||
const getCJS = (transformOptions) => { | ||
const getCommonJS = (transformOptions) => { | ||
return { | ||
@@ -105,5 +105,6 @@ ...transformOptions, | ||
}; | ||
const ERR_REQUIRE_ESM = "ERR_REQUIRE_ESM"; | ||
const importFromString = async (code, { dirname, globals, transformOptions } = {}) => { | ||
if (!isVMModuleAvailable()) { | ||
const { code: transformedCode2 } = await esbuild.transform(code, getCJS(transformOptions)); | ||
const { code: transformedCode2 } = await esbuild.transform(code, getCommonJS(transformOptions)); | ||
try { | ||
@@ -120,4 +121,4 @@ return requireFromString(transformedCode2, { dirname, globals }); | ||
const moduleDirname = dirname != null ? dirname : getCallerDirname(); | ||
const moduleFilename = path__default['default'].join(moduleDirname, `${nanoid.nanoid()}.js`); | ||
const context = vm__default['default'].createContext({ | ||
const moduleFilename = path__default["default"].join(moduleDirname, `${nanoid.nanoid()}.js`); | ||
const context = vm__default["default"].createContext({ | ||
__IMPORTS__: {}, | ||
@@ -130,3 +131,3 @@ ...globals | ||
}) : {}; | ||
const vmModule = new vm__default['default'].SourceTextModule(transformedCode != null ? transformedCode : code, { | ||
const vmModule = new vm__default["default"].SourceTextModule(transformedCode != null ? transformedCode : code, { | ||
identifier: moduleFilename, | ||
@@ -145,6 +146,6 @@ context, | ||
context.__IMPORTS__[specifier] = targetModule; | ||
const exportedNames = new Set(Object.getOwnPropertyNames(targetModule)); | ||
const targetModuleContent = `${exportedNames.delete("default") ? `export default __IMPORTS__['${specifier}'].default; | ||
` : ""}export const { ${[...exportedNames].join(", ")} } = __IMPORTS__['${specifier}'];`; | ||
return new vm__default['default'].SourceTextModule(targetModuleContent, { | ||
const exportedNames = Object.getOwnPropertyNames(targetModule); | ||
const targetModuleContent = `${exportedNames.includes("default") ? `export default __IMPORTS__['${specifier}'].default; | ||
` : ""}export const { ${exportedNames.filter((exportedName) => exportedName !== "default").join(", ")} } = __IMPORTS__['${specifier}'];`; | ||
return new vm__default["default"].SourceTextModule(targetModuleContent, { | ||
identifier: resolvedSpecifier, | ||
@@ -159,3 +160,3 @@ context | ||
const importFromStringSync = (code, { dirname, globals, transformOptions } = {}) => { | ||
const { code: transformedCode } = esbuild.transformSync(code, getCJS(transformOptions)); | ||
const { code: transformedCode } = esbuild.transformSync(code, getCommonJS(transformOptions)); | ||
try { | ||
@@ -162,0 +163,0 @@ return requireFromString(transformedCode, { dirname, globals }); |
{ | ||
"name": "module-from-string", | ||
"version": "3.1.1", | ||
"version": "3.1.2", | ||
"description": "Load module from string using require or import.", | ||
@@ -49,16 +49,16 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"esbuild": "0.12.24", | ||
"nanoid": "3.1.25" | ||
"esbuild": "^0.13.12", | ||
"nanoid": "^3.1.30" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "27.0.1", | ||
"@types/node": "16.7.4", | ||
"jest": "27.1.0", | ||
"rollup": "2.56.3", | ||
"rollup-plugin-dts": "3.0.2", | ||
"rollup-plugin-esbuild-transform": "1.1.1", | ||
"ts-jest": "27.0.5", | ||
"ts-standardx": "0.7.9", | ||
"typescript": "4.4.2" | ||
"@types/jest": "27.0.2", | ||
"@types/node": "16.11.6", | ||
"jest": "27.3.1", | ||
"rollup": "2.59.0", | ||
"rollup-plugin-dts": "4.0.1", | ||
"rollup-plugin-esbuild-transform": "1.2.0", | ||
"ts-jest": "27.0.7", | ||
"ts-standardx": "0.8.4", | ||
"typescript": "4.4.4" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30741
587
+ Addedesbuild@0.13.15(transitive)
+ Addedesbuild-android-arm64@0.13.15(transitive)
+ Addedesbuild-darwin-64@0.13.15(transitive)
+ Addedesbuild-darwin-arm64@0.13.15(transitive)
+ Addedesbuild-freebsd-64@0.13.15(transitive)
+ Addedesbuild-freebsd-arm64@0.13.15(transitive)
+ Addedesbuild-linux-32@0.13.15(transitive)
+ Addedesbuild-linux-64@0.13.15(transitive)
+ Addedesbuild-linux-arm@0.13.15(transitive)
+ Addedesbuild-linux-arm64@0.13.15(transitive)
+ Addedesbuild-linux-mips64le@0.13.15(transitive)
+ Addedesbuild-linux-ppc64le@0.13.15(transitive)
+ Addedesbuild-netbsd-64@0.13.15(transitive)
+ Addedesbuild-openbsd-64@0.13.15(transitive)
+ Addedesbuild-sunos-64@0.13.15(transitive)
+ Addedesbuild-windows-32@0.13.15(transitive)
+ Addedesbuild-windows-64@0.13.15(transitive)
+ Addedesbuild-windows-arm64@0.13.15(transitive)
+ Addednanoid@3.3.7(transitive)
- Removedesbuild@0.12.24(transitive)
- Removednanoid@3.1.25(transitive)
Updatedesbuild@^0.13.12
Updatednanoid@^3.1.30