module-from-string
Advanced tools
Comparing version 3.1.3 to 3.1.4
@@ -21,5 +21,5 @@ import path from 'path' | ||
it('should resolve correctly if option `dirPath` is provided', async () => { | ||
it('should resolve correctly if option `dirname` is provided', async () => { | ||
const modulePath = './cjs/fixtures/defaultExport.js' | ||
const res = await importFromString(`export { default } from '${modulePath}'`, { | ||
const res = await importFromString(`import greet from '${modulePath}'; export default greet`, { | ||
dirname: path.dirname(__dirname) | ||
@@ -76,5 +76,5 @@ }) | ||
it('should resolve correctly if option `dirPath` is provided', () => { | ||
it('should resolve correctly if option `dirname` is provided', () => { | ||
const modulePath = './cjs/fixtures/defaultExport.js' | ||
const res = importFromStringSync(`export { default } from '${modulePath}'`, { | ||
const res = importFromStringSync(`import greet from '${modulePath}'; export default greet`, { | ||
dirname: path.dirname(__dirname) | ||
@@ -106,4 +106,4 @@ }) | ||
it('should work if transformOption is provided', async () => { | ||
const res = await importFromStringSync("export const greet: () => string = () => 'hi'", { | ||
it('should work if transformOption is provided', () => { | ||
const res = importFromStringSync("export const greet: () => string = () => 'hi'", { | ||
transformOptions: { loader: 'ts' } | ||
@@ -110,0 +110,0 @@ }) |
@@ -21,3 +21,3 @@ import path from 'path' | ||
it('should resolve correctly if option `dirPath` is provided', () => { | ||
it('should resolve correctly if option `dirname` is provided', () => { | ||
const modulePath = './cjs/fixtures/namedExport.js' | ||
@@ -24,0 +24,0 @@ const res = requireFromString(`exports.greet = require('${modulePath}').greet`, { |
@@ -5,3 +5,3 @@ import path from 'path' | ||
const dirname = path.dirname(fileURLToPath(new URL(import.meta.url))) | ||
const __dirname = path.dirname(fileURLToPath(new URL(import.meta.url))) | ||
@@ -25,6 +25,6 @@ describe('importFromString', () => { | ||
it('should resolve correctly if option `dirPath` is provided', async () => { | ||
it('should resolve correctly if option `dirname` is provided', async () => { | ||
const modulePath = './esm/fixtures/defaultExport.js' | ||
const res = await importFromString(`export { default } from '${modulePath}'`, { | ||
dirname: path.dirname(dirname) | ||
dirname: path.dirname(__dirname) | ||
}) | ||
@@ -35,3 +35,3 @@ expect(res.default).toBe('hi') | ||
it('should work with absolute path import', async () => { | ||
const modulePath = path.join(dirname, 'fixtures/namedExport.js') | ||
const modulePath = path.join(__dirname, 'fixtures/namedExport.js') | ||
const res = await importFromString(`export { greet } from '${modulePath}'`) | ||
@@ -71,3 +71,3 @@ expect(res.greet).toBe('hi') | ||
const res = await importFromString('export const { url } = import.meta') | ||
expect(res.url).toMatch(dirname) | ||
expect(res.url).toMatch(`file://${__dirname}`) | ||
}) | ||
@@ -74,0 +74,0 @@ }) |
@@ -5,3 +5,3 @@ import path from 'path' | ||
const dirname = path.dirname(fileURLToPath(new URL(import.meta.url))) | ||
const __dirname = path.dirname(fileURLToPath(new URL(import.meta.url))) | ||
@@ -25,6 +25,6 @@ it('should work with `module.exports`', () => { | ||
it('should resolve correctly if option `dirPath` is provided', () => { | ||
it('should resolve correctly if option `dirname` is provided', () => { | ||
const modulePath = './cjs/fixtures/namedExport.js' | ||
const res = requireFromString(`exports.greet = require('${modulePath}').greet`, { | ||
dirname: path.dirname(dirname) | ||
dirname: path.dirname(__dirname) | ||
}) | ||
@@ -35,3 +35,3 @@ expect(res.greet).toBe('hi') | ||
it('should work with absolute path import', () => { | ||
const modulePath = path.join(dirname, '../cjs/fixtures/defaultExport.js') | ||
const modulePath = path.join(__dirname, '../cjs/fixtures/defaultExport.js') | ||
const res = requireFromString(`module.exports = require('${modulePath}')`) | ||
@@ -38,0 +38,0 @@ expect(res).toBe('hi') |
# Changelog | ||
## [3.1.4](https://github.com/exuanbo/module-from-string/compare/v3.1.3...v3.1.4) (2021-12-23) | ||
### Bug Fixes | ||
- `import.meta.url` is incorrectly set to path instead of URL string in `importFromString`. | ||
### Chores | ||
- Bump dependencies. Note that [esbuild 0.14.4](https://github.com/evanw/esbuild/releases/tag/v0.14.4) adjusted its handling of `default` exports and the `__esModule` marker to improve compatibility with Webpack and Node, which may cause some changes when transforming the code from ES modules to CommonJS modules. | ||
## [3.1.3](https://github.com/exuanbo/module-from-string/compare/v3.1.2...v3.1.3) (2021-11-27) | ||
@@ -4,0 +14,0 @@ |
@@ -42,3 +42,5 @@ "use strict"; | ||
const isVMModuleAvailable = () => vm__default["default"].Module !== void 0; | ||
const fileURLToPath = (value) => value.startsWith("file://") ? url__default["default"].fileURLToPath(value) : value; | ||
const FILE_URL_SCHEME = "file:"; | ||
const fileURLToPath = (value) => value.startsWith(FILE_URL_SCHEME) ? url__default["default"].fileURLToPath(value) : value; | ||
const pathToFileURL = (value) => (value.startsWith(FILE_URL_SCHEME) ? new URL(value) : url__default["default"].pathToFileURL(value)).href; | ||
const FUNCTION_NAMES = [ | ||
@@ -52,3 +54,3 @@ "getCallerDirname", | ||
const getCallerDirname = () => { | ||
const _prepareStackTrace = Error.prepareStackTrace; | ||
const __prepareStackTrace = Error.prepareStackTrace; | ||
Error.prepareStackTrace = (_err, stackTraces) => stackTraces; | ||
@@ -59,5 +61,5 @@ const callSites = new Error().stack.filter((callSite) => { | ||
}); | ||
Error.prepareStackTrace = _prepareStackTrace; | ||
Error.prepareStackTrace = __prepareStackTrace; | ||
const callerFilename = callSites[0].getFileName(); | ||
return path__default["default"].dirname(callerFilename !== null ? fileURLToPath(callerFilename) : process.argv[1]); | ||
return path__default["default"].dirname(callerFilename === null ? process.argv[1] : fileURLToPath(callerFilename)); | ||
}; | ||
@@ -129,2 +131,3 @@ const resolveModuleSpecifier = (specifier, dirname) => { | ||
const moduleFilename = path__default["default"].join(dirname, `${nanoid.nanoid()}.js`); | ||
const moduleFileURL = pathToFileURL(moduleFilename); | ||
const context = vm__default["default"].createContext(__spreadValues({ | ||
@@ -134,6 +137,6 @@ __IMPORTS__: {} | ||
const vmModule = new vm__default["default"].SourceTextModule(transformedCode != null ? transformedCode : code, { | ||
identifier: moduleFilename, | ||
identifier: moduleFileURL, | ||
context, | ||
initializeImportMeta(meta) { | ||
meta.url = moduleFilename; | ||
meta.url = moduleFileURL; | ||
}, | ||
@@ -140,0 +143,0 @@ async importModuleDynamically(specifier) { |
{ | ||
"name": "module-from-string", | ||
"version": "3.1.3", | ||
"version": "3.1.4", | ||
"description": "Load module from string using require or import.", | ||
@@ -49,3 +49,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"esbuild": "^0.14.0", | ||
"esbuild": "^0.14.8", | ||
"nanoid": "^3.1.30" | ||
@@ -55,8 +55,8 @@ }, | ||
"@types/jest": "27.0.3", | ||
"@types/node": "16.11.10", | ||
"jest": "27.3.1", | ||
"rollup": "2.60.1", | ||
"@types/node": "17.0.4", | ||
"jest": "27.4.5", | ||
"rollup": "2.61.1", | ||
"rollup-plugin-dts": "4.0.1", | ||
"rollup-plugin-esbuild-transform": "1.3.2", | ||
"ts-jest": "27.0.7", | ||
"ts-jest": "27.1.2", | ||
"ts-standardx": "0.8.4", | ||
@@ -63,0 +63,0 @@ "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
34640
622
Updatedesbuild@^0.14.8