diff options
author | 2022-04-06 06:14:29 -0700 | |
---|---|---|
committer | 2022-04-06 06:14:29 -0700 | |
commit | ac06ce21604a449c7fe9d0cb36d827ba38cede5c (patch) | |
tree | f720eae3b1ee9dc30467e1729c8c878e33f239ac | |
parent | 69653729f0a8e0dd2efcb7ff48e061c9b541c051 (diff) | |
download | bun-ac06ce21604a449c7fe9d0cb36d827ba38cede5c.tar.gz bun-ac06ce21604a449c7fe9d0cb36d827ba38cede5c.tar.zst bun-ac06ce21604a449c7fe9d0cb36d827ba38cede5c.zip |
[bun.js][breaking] `import.meta.url` is now a file:// url
-rw-r--r-- | integration/bunjs-only-snippets/resolve.test.js | 32 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/ZigGlobalObject.cpp | 5 |
2 files changed, 18 insertions, 19 deletions
diff --git a/integration/bunjs-only-snippets/resolve.test.js b/integration/bunjs-only-snippets/resolve.test.js index 0a4ac314a..f86d31980 100644 --- a/integration/bunjs-only-snippets/resolve.test.js +++ b/integration/bunjs-only-snippets/resolve.test.js @@ -3,46 +3,46 @@ import { mkdirSync, writeFileSync } from "fs"; import { join } from "path"; it("import.meta.resolve", async () => { - expect(await import.meta.resolve("./resolve.test.js")).toBe(import.meta.url); + expect(await import.meta.resolve("./resolve.test.js")).toBe(import.meta.path); - expect(await import.meta.resolve("./resolve.test.js", import.meta.url)).toBe( - import.meta.url + expect(await import.meta.resolve("./resolve.test.js", import.meta.path)).toBe( + import.meta.path ); expect( // optional second param can be any path, including a dir await import.meta.resolve( "./bunjs-only-snippets/resolve.test.js", - join(import.meta.url, "../") + join(import.meta.path, "../") ) - ).toBe(import.meta.url); + ).toBe(import.meta.path); // can be a package path - expect((await import.meta.resolve("react", import.meta.url)).length > 0).toBe( - true - ); + expect( + (await import.meta.resolve("react", import.meta.path)).length > 0 + ).toBe(true); // file extensions are optional - expect(await import.meta.resolve("./resolve.test")).toBe(import.meta.url); + expect(await import.meta.resolve("./resolve.test")).toBe(import.meta.path); // works with tsconfig.json "paths" expect(await import.meta.resolve("foo/bar")).toBe( - join(import.meta.url, "../baz.js") + join(import.meta.path, "../baz.js") ); // works with package.json "exports" writePackageJSONExportsFixture(); expect(await import.meta.resolve("package-json-exports/baz")).toBe( - join(import.meta.url, "../node_modules/package-json-exports/foo/bar.js") + join(import.meta.path, "../node_modules/package-json-exports/foo/bar.js") ); // works with TypeScript compiler edgecases like: // - If the file ends with .js and it doesn't exist, try again with .ts and .tsx expect(await import.meta.resolve("./resolve-typescript-file.js")).toBe( - join(import.meta.url, "../resolve-typescript-file.tsx") + join(import.meta.path, "../resolve-typescript-file.tsx") ); expect(await import.meta.resolve("./resolve-typescript-file.tsx")).toBe( - join(import.meta.url, "../resolve-typescript-file.tsx") + join(import.meta.path, "../resolve-typescript-file.tsx") ); // throws a ResolveError on failure @@ -51,7 +51,7 @@ it("import.meta.resolve", async () => { throw new Error("Test failed"); } catch (exception) { expect(exception instanceof ResolveError).toBe(true); - expect(exception.referrer).toBe(import.meta.url); + expect(exception.referrer).toBe(import.meta.path); expect(exception.name).toBe("ResolveError"); } }); @@ -60,14 +60,14 @@ it("import.meta.resolve", async () => { // and expects a directory instead of a filepath it("Bun.resolve", async () => { expect(await Bun.resolve("./resolve.test.js", import.meta.dir)).toBe( - import.meta.url + import.meta.path ); }); // synchronous it("Bun.resolveSync", () => { expect(Bun.resolveSync("./resolve.test.js", import.meta.dir)).toBe( - import.meta.url + import.meta.path ); }); diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp index b41ff0ab5..509f6d2fc 100644 --- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp +++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp @@ -707,7 +707,7 @@ static JSC_DEFINE_HOST_FUNCTION(functionImportMeta__resolve, auto clientData = WebCore::clientData(vm); - from = JSC::JSValue::encode(thisObject->get(globalObject, clientData->builtinNames().urlPublicName())); + from = JSC::JSValue::encode(thisObject->get(globalObject, clientData->builtinNames().pathPublicName())); } return Bun__resolve(globalObject, JSC::JSValue::encode(moduleName), from); @@ -1090,8 +1090,7 @@ JSC::JSObject* GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObje } metaProperties->putDirect(vm, clientData->builtinNames().pathPublicName(), key); - // this is a lie - metaProperties->putDirect(vm, clientData->builtinNames().urlPublicName(), key); + metaProperties->putDirect(vm, clientData->builtinNames().urlPublicName(), JSC::JSValue(JSC::jsString(vm, WTF::URL::fileURLWithFileSystemPath(view).string()))); RETURN_IF_EXCEPTION(scope, nullptr); |