From f4efa8add445bd1135f819321274f833124d194f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 23 Dec 2022 18:02:11 +0100 Subject: [PATCH 1/6] npmResolve plugin --- src/deno.ts | 21 ++++++++++++++++++- src/npm-resolve.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++-- src/types.ts | 9 +++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/deno.ts b/src/deno.ts index fc31cfd..d89a806 100644 --- a/src/deno.ts +++ b/src/deno.ts @@ -1,4 +1,4 @@ -import { Module, ModuleInfo, PluginConfig } from './types.ts'; +import { CacheInfo, Module, ModuleInfo, PluginConfig } from './types.ts'; export function createDeno( { cacheCache, infoCache, moduleCache, tempDirectory }: PluginConfig, @@ -66,9 +66,28 @@ export function createDeno( return moduleInfo; } + async function cacheInfo(): Promise { + const p = Deno.run({ + cmd: [Deno.execPath(), 'info', '--json'], + stdout: 'piped', + stderr: 'piped', + cwd: tempDirectory, + }); + + const status = await p.status(); + if (!status.success) { + throw new Error(`invariant: could not get info on ${name}`); + } + + const output = await p.output(); + const cacheInfo: CacheInfo = JSON.parse(new TextDecoder().decode(output)); + return cacheInfo; + } + return { cache, info, module, + cacheInfo, }; } diff --git a/src/npm-resolve.ts b/src/npm-resolve.ts index ae0d3e1..267ae23 100644 --- a/src/npm-resolve.ts +++ b/src/npm-resolve.ts @@ -1,13 +1,59 @@ +import { createDeno } from './deno.ts'; import { PluginConfig } from './types.ts'; -export default function npmResolve({}: PluginConfig) { +export default function npmResolve(config: PluginConfig) { + // TODO(bartlomieju): both plugins should use the same instance, or maybe + // both plugins should be combined. + const deno = createDeno(config); + return { name: 'vite:deno-npm-resolve', enforce: 'pre' as const, - resolveId() { + async resolveId(importee: string, importer: string | undefined) { + console.log('resolveId npm', importee, importer); + + if (importee.startsWith('npm:')) { + await deno.cache(importee); + return importee; + } + return null; }, + + async load(id: string) { + console.log('load npm', id); + + if (!id.startsWith('npm:')) { + return null; + } + // strip npm: prefix + id = id.substring(4); + + // TODO(bartlomieju): this shouldn't be called on each load, it can + // be cached per run + const cacheInfo = await deno.cacheInfo(); + const npmCache = cacheInfo.npmCache; + + // TODO(bartlomieju): handle subpath + let specifier, version; + + if (id.startsWith('@')) { + const versionIndex = id.lastIndexOf('@'); + specifier = id.substring(0, versionIndex); + version = id.substring(versionIndex + 1); + } else { + const parts = id.split('@'); + specifier = parts[0]; + version = parts[1]; + } + + console.log(specifier, version); + const moduleDirPath = + `${npmCache}/registry.npmjs.org/${specifier}/${version}`; + + return await Deno.readTextFile(`${moduleDirPath}/index.js`); + }, }; } diff --git a/src/types.ts b/src/types.ts index 9fe41f8..eee7f39 100644 --- a/src/types.ts +++ b/src/types.ts @@ -56,6 +56,15 @@ export interface ModuleInfo { roots: string[]; } +export interface CacheInfo { + denoDir: string; + modulesCache: string; + npmCache: string; + typescriptCache: string; + registryCache: string; + originStorage: string; +} + export interface PluginConfig { cacheCache: CacheCache; infoCache: InfoCache; From 5accd657b7e4c2a7567e7166e975c032875cf1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 23 Dec 2022 18:16:47 +0100 Subject: [PATCH 2/6] parse NpmPackageReference --- src/npm-resolve.ts | 95 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 15 deletions(-) diff --git a/src/npm-resolve.ts b/src/npm-resolve.ts index 267ae23..1cce6da 100644 --- a/src/npm-resolve.ts +++ b/src/npm-resolve.ts @@ -28,32 +28,97 @@ export default function npmResolve(config: PluginConfig) { if (!id.startsWith('npm:')) { return null; } - // strip npm: prefix - id = id.substring(4); + + const ref = npmPackageReference(id); // TODO(bartlomieju): this shouldn't be called on each load, it can // be cached per run const cacheInfo = await deno.cacheInfo(); const npmCache = cacheInfo.npmCache; - // TODO(bartlomieju): handle subpath - let specifier, version; - - if (id.startsWith('@')) { - const versionIndex = id.lastIndexOf('@'); - specifier = id.substring(0, versionIndex); - version = id.substring(versionIndex + 1); - } else { - const parts = id.split('@'); - specifier = parts[0]; - version = parts[1]; + const specifier = ref.name; + const version = ref.versionReq; + + if (!version) { + throw new Error(`Version not specified for ${specifier}`); } - console.log(specifier, version); + console.log('NpmPackageReference', ref); const moduleDirPath = `${npmCache}/registry.npmjs.org/${specifier}/${version}`; - return await Deno.readTextFile(`${moduleDirPath}/index.js`); + const packageJson = JSON.parse( + await Deno.readTextFile( + `${moduleDirPath}/package.json`, + ), + ); + + let file = packageJson.main || 'index.js'; + if (ref.subPath) { + // TODO(bartlomieju): handle other extensions + file = `${ref.subPath}.js`; + } + return await Deno.readTextFile(`${moduleDirPath}/${file}`); }, }; } + +interface NpmPackageReference { + name: string; + versionReq: string | null; + subPath: string | null; +} +function npmPackageReference(specifier: string): NpmPackageReference { + if (!specifier.startsWith('npm:')) { + throw new Error(`Invalid npm package reference: ${specifier}`); + } + + specifier = specifier.substring(4); + const parts = specifier.split('/'); + let namePartLen; + if (specifier.startsWith('@')) { + namePartLen = 2; + } else { + namePartLen = 1; + } + if (parts.length < namePartLen) { + throw new Error(`Invalid npm package reference: ${specifier}`); + } + const nameParts = parts.slice(0, namePartLen); + let lastNamePart = nameParts[nameParts.length - 1]; + const atIndex = lastNamePart.lastIndexOf('@'); + let version; + let name; + + if (atIndex !== -1) { + version = lastNamePart.substring(atIndex + 1); + lastNamePart = lastNamePart.substring(0, atIndex); + + if (namePartLen === 1) { + name = lastNamePart; + } else { + name = `${nameParts[0]}/${lastNamePart}`; + } + } else { + name = nameParts.join('/'); + } + + let subPath = null; + if (parts.length !== namePartLen) { + subPath = parts.slice(namePartLen).join('/'); + } + + // TODO(bartlomieju): handle version specified after subpath + + if (!name) { + throw new Error( + `Invalid npm package reference: ${specifier}. Did not contain a package name`, + ); + } + + return { + name, + versionReq: version || null, + subPath, + }; +} From 408b08f57ed922997a564e2709f34c8e33083972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 29 Dec 2022 11:42:48 +0100 Subject: [PATCH 3/6] revert some changes --- deno.lock | 394 ++++++++++++++++++++++++++++++++++++++++++++- src/npm-resolve.ts | 18 ++- vite.config.mts | 6 +- 3 files changed, 408 insertions(+), 10 deletions(-) diff --git a/deno.lock b/deno.lock index a07c9e9..f093be8 100644 --- a/deno.lock +++ b/deno.lock @@ -35,8 +35,216 @@ "https://deno.land/x/case@2.1.1/vendor/nonWordRegexp.ts": "c1a052629a694144b48c66b0175a22a83f4d61cb40f4e45293fc5d6b123f927e" }, "npm": { - "specifiers": { "vite@3.2.4": "vite@3.2.4" }, + "specifiers": { + "@vitejs/plugin-react": "@vitejs/plugin-react@2.2.0_vite@3.2.4_@babel+core@7.20.5", + "react": "react@18.2.0", + "react-dom": "react-dom@18.2.0_react@18.2.0", + "vite@3.2.4": "vite@3.2.4" + }, "packages": { + "@ampproject/remapping@2.2.0": { + "integrity": "sha512-qRmjj8nj9qmLTQXXmaR1cck3UXSRMPrbsLJAasZpF+t3riI71BXed5ebIOYwQntykeZuhjsdweEc9BxH5Jc26w==", + "dependencies": { + "@jridgewell/gen-mapping": "@jridgewell/gen-mapping@0.1.1", + "@jridgewell/trace-mapping": "@jridgewell/trace-mapping@0.3.17" + } + }, + "@babel/code-frame@7.18.6": { + "integrity": "sha512-TDCmlK5eOvH+eH7cdAFlNXeVJqWIQ7gW9tY1GJIpUtFb6CmjVyq2VM3u71bOyR8CRihcCgMUYoDNyLXao3+70Q==", + "dependencies": { "@babel/highlight": "@babel/highlight@7.18.6" } + }, + "@babel/compat-data@7.20.5": { + "integrity": "sha512-KZXo2t10+/jxmkhNXc7pZTqRvSOIvVv/+lJwHS+B2rErwOyjuVRh60yVpb7liQ1U5t7lLJ1bz+t8tSypUZdm0g==", + "dependencies": {} + }, + "@babel/core@7.20.5": { + "integrity": "sha512-UdOWmk4pNWTm/4DlPUl/Pt4Gz4rcEMb7CY0Y3eJl5Yz1vI8ZJGmHWaVE55LoxRjdpx0z259GE9U5STA9atUinQ==", + "dependencies": { + "@ampproject/remapping": "@ampproject/remapping@2.2.0", + "@babel/code-frame": "@babel/code-frame@7.18.6", + "@babel/generator": "@babel/generator@7.20.5", + "@babel/helper-compilation-targets": "@babel/helper-compilation-targets@7.20.0_@babel+core@7.20.5", + "@babel/helper-module-transforms": "@babel/helper-module-transforms@7.20.2", + "@babel/helpers": "@babel/helpers@7.20.6", + "@babel/parser": "@babel/parser@7.20.5", + "@babel/template": "@babel/template@7.18.10", + "@babel/traverse": "@babel/traverse@7.20.5", + "@babel/types": "@babel/types@7.20.5", + "convert-source-map": "convert-source-map@1.9.0", + "debug": "debug@4.3.4", + "gensync": "gensync@1.0.0-beta.2", + "json5": "json5@2.2.1", + "semver": "semver@6.3.0" + } + }, + "@babel/generator@7.20.5": { + "integrity": "sha512-jl7JY2Ykn9S0yj4DQP82sYvPU+T3g0HFcWTqDLqiuA9tGRNIj9VfbtXGAYTTkyNEnQk1jkMGOdYka8aG/lulCA==", + "dependencies": { + "@babel/types": "@babel/types@7.20.5", + "@jridgewell/gen-mapping": "@jridgewell/gen-mapping@0.3.2", + "jsesc": "jsesc@2.5.2" + } + }, + "@babel/helper-annotate-as-pure@7.18.6": { + "integrity": "sha512-duORpUiYrEpzKIop6iNbjnwKLAKnJ47csTyRACyEmWj0QdUrm5aqNJGHSSEQSUAvNW0ojX0dOmK9dZduvkfeXA==", + "dependencies": { "@babel/types": "@babel/types@7.20.5" } + }, + "@babel/helper-compilation-targets@7.20.0_@babel+core@7.20.5": { + "integrity": "sha512-0jp//vDGp9e8hZzBc6N/KwA5ZK3Wsm/pfm4CrY7vzegkVxc65SgSn6wYOnwHe9Js9HRQ1YTCKLGPzDtaS3RoLQ==", + "dependencies": { + "@babel/compat-data": "@babel/compat-data@7.20.5", + "@babel/core": "@babel/core@7.20.5", + "@babel/helper-validator-option": "@babel/helper-validator-option@7.18.6", + "browserslist": "browserslist@4.21.4", + "semver": "semver@6.3.0" + } + }, + "@babel/helper-environment-visitor@7.18.9": { + "integrity": "sha512-3r/aACDJ3fhQ/EVgFy0hpj8oHyHpQc+LPtJoY9SzTThAsStm4Ptegq92vqKoE3vD706ZVFWITnMnxucw+S9Ipg==", + "dependencies": {} + }, + "@babel/helper-function-name@7.19.0": { + "integrity": "sha512-WAwHBINyrpqywkUH0nTnNgI5ina5TFn85HKS0pbPDfxFfhyR/aNQEn4hGi1P1JyT//I0t4OgXUlofzWILRvS5w==", + "dependencies": { + "@babel/template": "@babel/template@7.18.10", + "@babel/types": "@babel/types@7.20.5" + } + }, + "@babel/helper-hoist-variables@7.18.6": { + "integrity": "sha512-UlJQPkFqFULIcyW5sbzgbkxn2FKRgwWiRexcuaR8RNJRy8+LLveqPjwZV/bwrLZCN0eUHD/x8D0heK1ozuoo6Q==", + "dependencies": { "@babel/types": "@babel/types@7.20.5" } + }, + "@babel/helper-module-imports@7.18.6": { + "integrity": "sha512-0NFvs3VkuSYbFi1x2Vd6tKrywq+z/cLeYC/RJNFrIX/30Bf5aiGYbtvGXolEktzJH8o5E5KJ3tT+nkxuuZFVlA==", + "dependencies": { "@babel/types": "@babel/types@7.20.5" } + }, + "@babel/helper-module-transforms@7.20.2": { + "integrity": "sha512-zvBKyJXRbmK07XhMuujYoJ48B5yvvmM6+wcpv6Ivj4Yg6qO7NOZOSnvZN9CRl1zz1Z4cKf8YejmCMh8clOoOeA==", + "dependencies": { + "@babel/helper-environment-visitor": "@babel/helper-environment-visitor@7.18.9", + "@babel/helper-module-imports": "@babel/helper-module-imports@7.18.6", + "@babel/helper-simple-access": "@babel/helper-simple-access@7.20.2", + "@babel/helper-split-export-declaration": "@babel/helper-split-export-declaration@7.18.6", + "@babel/helper-validator-identifier": "@babel/helper-validator-identifier@7.19.1", + "@babel/template": "@babel/template@7.18.10", + "@babel/traverse": "@babel/traverse@7.20.5", + "@babel/types": "@babel/types@7.20.5" + } + }, + "@babel/helper-plugin-utils@7.20.2": { + "integrity": "sha512-8RvlJG2mj4huQ4pZ+rU9lqKi9ZKiRmuvGuM2HlWmkmgOhbs6zEAw6IEiJ5cQqGbDzGZOhwuOQNtZMi/ENLjZoQ==", + "dependencies": {} + }, + "@babel/helper-simple-access@7.20.2": { + "integrity": "sha512-+0woI/WPq59IrqDYbVGfshjT5Dmk/nnbdpcF8SnMhhXObpTq2KNBdLFRFrkVdbDOyUmHBCxzm5FHV1rACIkIbA==", + "dependencies": { "@babel/types": "@babel/types@7.20.5" } + }, + "@babel/helper-split-export-declaration@7.18.6": { + "integrity": "sha512-bde1etTx6ZyTmobl9LLMMQsaizFVZrquTEHOqKeQESMKo4PlObf+8+JA25ZsIpZhT/WEd39+vOdLXAFG/nELpA==", + "dependencies": { "@babel/types": "@babel/types@7.20.5" } + }, + "@babel/helper-string-parser@7.19.4": { + "integrity": "sha512-nHtDoQcuqFmwYNYPz3Rah5ph2p8PFeFCsZk9A/48dPc/rGocJ5J3hAAZ7pb76VWX3fZKu+uEr/FhH5jLx7umrw==", + "dependencies": {} + }, + "@babel/helper-validator-identifier@7.19.1": { + "integrity": "sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==", + "dependencies": {} + }, + "@babel/helper-validator-option@7.18.6": { + "integrity": "sha512-XO7gESt5ouv/LRJdrVjkShckw6STTaB7l9BrpBaAHDeF5YZT+01PCwmR0SJHnkW6i8OwW/EVWRShfi4j2x+KQw==", + "dependencies": {} + }, + "@babel/helpers@7.20.6": { + "integrity": "sha512-Pf/OjgfgFRW5bApskEz5pvidpim7tEDPlFtKcNRXWmfHGn9IEI2W2flqRQXTFb7gIPTyK++N6rVHuwKut4XK6w==", + "dependencies": { + "@babel/template": "@babel/template@7.18.10", + "@babel/traverse": "@babel/traverse@7.20.5", + "@babel/types": "@babel/types@7.20.5" + } + }, + "@babel/highlight@7.18.6": { + "integrity": "sha512-u7stbOuYjaPezCuLj29hNW1v64M2Md2qupEKP1fHc7WdOA3DgLh37suiSrZYY7haUB7iBeQZ9P1uiRF359do3g==", + "dependencies": { + "@babel/helper-validator-identifier": "@babel/helper-validator-identifier@7.19.1", + "chalk": "chalk@2.4.2", + "js-tokens": "js-tokens@4.0.0" + } + }, + "@babel/parser@7.20.5": { + "integrity": "sha512-r27t/cy/m9uKLXQNWWebeCUHgnAZq0CpG1OwKRxzJMP1vpSU4bSIK2hq+/cp0bQxetkXx38n09rNu8jVkcK/zA==", + "dependencies": {} + }, + "@babel/plugin-syntax-jsx@7.18.6_@babel+core@7.20.5": { + "integrity": "sha512-6mmljtAedFGTWu2p/8WIORGwy+61PLgOMPOdazc7YoJ9ZCWUyFy3A6CpPkRKLKD1ToAesxX8KGEViAiLo9N+7Q==", + "dependencies": { + "@babel/core": "@babel/core@7.20.5", + "@babel/helper-plugin-utils": "@babel/helper-plugin-utils@7.20.2" + } + }, + "@babel/plugin-transform-react-jsx-development@7.18.6_@babel+core@7.20.5": { + "integrity": "sha512-SA6HEjwYFKF7WDjWcMcMGUimmw/nhNRDWxr+KaLSCrkD/LMDBvWRmHAYgE1HDeF8KUuI8OAu+RT6EOtKxSW2qA==", + "dependencies": { + "@babel/core": "@babel/core@7.20.5", + "@babel/plugin-transform-react-jsx": "@babel/plugin-transform-react-jsx@7.19.0_@babel+core@7.20.5" + } + }, + "@babel/plugin-transform-react-jsx-self@7.18.6_@babel+core@7.20.5": { + "integrity": "sha512-A0LQGx4+4Jv7u/tWzoJF7alZwnBDQd6cGLh9P+Ttk4dpiL+J5p7NSNv/9tlEFFJDq3kjxOavWmbm6t0Gk+A3Ig==", + "dependencies": { + "@babel/core": "@babel/core@7.20.5", + "@babel/helper-plugin-utils": "@babel/helper-plugin-utils@7.20.2" + } + }, + "@babel/plugin-transform-react-jsx-source@7.19.6_@babel+core@7.20.5": { + "integrity": "sha512-RpAi004QyMNisst/pvSanoRdJ4q+jMCWyk9zdw/CyLB9j8RXEahodR6l2GyttDRyEVWZtbN+TpLiHJ3t34LbsQ==", + "dependencies": { + "@babel/core": "@babel/core@7.20.5", + "@babel/helper-plugin-utils": "@babel/helper-plugin-utils@7.20.2" + } + }, + "@babel/plugin-transform-react-jsx@7.19.0_@babel+core@7.20.5": { + "integrity": "sha512-UVEvX3tXie3Szm3emi1+G63jyw1w5IcMY0FSKM+CRnKRI5Mr1YbCNgsSTwoTwKphQEG9P+QqmuRFneJPZuHNhg==", + "dependencies": { + "@babel/core": "@babel/core@7.20.5", + "@babel/helper-annotate-as-pure": "@babel/helper-annotate-as-pure@7.18.6", + "@babel/helper-module-imports": "@babel/helper-module-imports@7.18.6", + "@babel/helper-plugin-utils": "@babel/helper-plugin-utils@7.20.2", + "@babel/plugin-syntax-jsx": "@babel/plugin-syntax-jsx@7.18.6_@babel+core@7.20.5", + "@babel/types": "@babel/types@7.20.5" + } + }, + "@babel/template@7.18.10": { + "integrity": "sha512-TI+rCtooWHr3QJ27kJxfjutghu44DLnasDMwpDqCXVTal9RLp3RSYNh4NdBrRP2cQAoG9A8juOQl6P6oZG4JxA==", + "dependencies": { + "@babel/code-frame": "@babel/code-frame@7.18.6", + "@babel/parser": "@babel/parser@7.20.5", + "@babel/types": "@babel/types@7.20.5" + } + }, + "@babel/traverse@7.20.5": { + "integrity": "sha512-WM5ZNN3JITQIq9tFZaw1ojLU3WgWdtkxnhM1AegMS+PvHjkM5IXjmYEGY7yukz5XS4sJyEf2VzWjI8uAavhxBQ==", + "dependencies": { + "@babel/code-frame": "@babel/code-frame@7.18.6", + "@babel/generator": "@babel/generator@7.20.5", + "@babel/helper-environment-visitor": "@babel/helper-environment-visitor@7.18.9", + "@babel/helper-function-name": "@babel/helper-function-name@7.19.0", + "@babel/helper-hoist-variables": "@babel/helper-hoist-variables@7.18.6", + "@babel/helper-split-export-declaration": "@babel/helper-split-export-declaration@7.18.6", + "@babel/parser": "@babel/parser@7.20.5", + "@babel/types": "@babel/types@7.20.5", + "debug": "debug@4.3.4", + "globals": "globals@11.12.0" + } + }, + "@babel/types@7.20.5": { + "integrity": "sha512-c9fst/h2/dcF7H+MJKZ2T0KjEQ8hY/BNnDk/H3XY8C4Aw/eWQXWn/lWntHF9ooUBnGmEvbfGrTgLWc+um0YDUg==", + "dependencies": { + "@babel/helper-string-parser": "@babel/helper-string-parser@7.19.4", + "@babel/helper-validator-identifier": "@babel/helper-validator-identifier@7.19.1", + "to-fast-properties": "to-fast-properties@2.0.0" + } + }, "@esbuild/android-arm@0.15.18": { "integrity": "sha512-5GT+kcs2WVGjVs7+boataCkO5Fg0y4kCjzkB5bAip7H4jfnOS3dA6KPiww9W1OEKTKeAcUVhdZGvgI65OXmUnw==", "dependencies": {} @@ -45,6 +253,98 @@ "integrity": "sha512-L4jVKS82XVhw2nvzLg/19ClLWg0y27ulRwuP7lcyL6AbUWB5aPglXY3M21mauDQMDfRLs8cQmeT03r/+X3cZYQ==", "dependencies": {} }, + "@jridgewell/gen-mapping@0.1.1": { + "integrity": "sha512-sQXCasFk+U8lWYEe66WxRDOE9PjVz4vSM51fTu3Hw+ClTpUSQb718772vH3pyS5pShp6lvQM7SxgIDXXXmOX7w==", + "dependencies": { + "@jridgewell/set-array": "@jridgewell/set-array@1.1.2", + "@jridgewell/sourcemap-codec": "@jridgewell/sourcemap-codec@1.4.14" + } + }, + "@jridgewell/gen-mapping@0.3.2": { + "integrity": "sha512-mh65xKQAzI6iBcFzwv28KVWSmCkdRBWoOh+bYQGW3+6OZvbbN3TqMGo5hqYxQniRcH9F2VZIoJCm4pa3BPDK/A==", + "dependencies": { + "@jridgewell/set-array": "@jridgewell/set-array@1.1.2", + "@jridgewell/sourcemap-codec": "@jridgewell/sourcemap-codec@1.4.14", + "@jridgewell/trace-mapping": "@jridgewell/trace-mapping@0.3.17" + } + }, + "@jridgewell/resolve-uri@3.1.0": { + "integrity": "sha512-F2msla3tad+Mfht5cJq7LSXcdudKTWCVYUgw6pLFOOHSTtZlj6SWNYAp+AhuqLmWdBO2X5hPrLcu8cVP8fy28w==", + "dependencies": {} + }, + "@jridgewell/set-array@1.1.2": { + "integrity": "sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==", + "dependencies": {} + }, + "@jridgewell/sourcemap-codec@1.4.14": { + "integrity": "sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw==", + "dependencies": {} + }, + "@jridgewell/trace-mapping@0.3.17": { + "integrity": "sha512-MCNzAp77qzKca9+W/+I0+sEpaUnZoeasnghNeVc41VZCEKaCH73Vq3BZZ/SzWIgrqE4H4ceI+p+b6C0mHf9T4g==", + "dependencies": { + "@jridgewell/resolve-uri": "@jridgewell/resolve-uri@3.1.0", + "@jridgewell/sourcemap-codec": "@jridgewell/sourcemap-codec@1.4.14" + } + }, + "@vitejs/plugin-react@2.2.0_vite@3.2.4_@babel+core@7.20.5": { + "integrity": "sha512-FFpefhvExd1toVRlokZgxgy2JtnBOdp4ZDsq7ldCWaqGSGn9UhWMAVm/1lxPL14JfNS5yGz+s9yFrQY6shoStA==", + "dependencies": { + "@babel/core": "@babel/core@7.20.5", + "@babel/plugin-transform-react-jsx": "@babel/plugin-transform-react-jsx@7.19.0_@babel+core@7.20.5", + "@babel/plugin-transform-react-jsx-development": "@babel/plugin-transform-react-jsx-development@7.18.6_@babel+core@7.20.5", + "@babel/plugin-transform-react-jsx-self": "@babel/plugin-transform-react-jsx-self@7.18.6_@babel+core@7.20.5", + "@babel/plugin-transform-react-jsx-source": "@babel/plugin-transform-react-jsx-source@7.19.6_@babel+core@7.20.5", + "magic-string": "magic-string@0.26.7", + "react-refresh": "react-refresh@0.14.0", + "vite": "vite@3.2.4" + } + }, + "ansi-styles@3.2.1": { + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "dependencies": { "color-convert": "color-convert@1.9.3" } + }, + "browserslist@4.21.4": { + "integrity": "sha512-CBHJJdDmgjl3daYjN5Cp5kbTf1mUhZoS+beLklHIvkOWscs83YAhLlF3Wsh/lciQYAcbBJgTOD44VtG31ZM4Hw==", + "dependencies": { + "caniuse-lite": "caniuse-lite@1.0.30001439", + "electron-to-chromium": "electron-to-chromium@1.4.284", + "node-releases": "node-releases@2.0.6", + "update-browserslist-db": "update-browserslist-db@1.0.10_browserslist@4.21.4" + } + }, + "caniuse-lite@1.0.30001439": { + "integrity": "sha512-1MgUzEkoMO6gKfXflStpYgZDlFM7M/ck/bgfVCACO5vnAf0fXoNVHdWtqGU+MYca+4bL9Z5bpOVmR33cWW9G2A==", + "dependencies": {} + }, + "chalk@2.4.2": { + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dependencies": { + "ansi-styles": "ansi-styles@3.2.1", + "escape-string-regexp": "escape-string-regexp@1.0.5", + "supports-color": "supports-color@5.5.0" + } + }, + "color-convert@1.9.3": { + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "dependencies": { "color-name": "color-name@1.1.3" } + }, + "color-name@1.1.3": { + "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==", + "dependencies": {} + }, + "convert-source-map@1.9.0": { + "integrity": "sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A==", + "dependencies": {} + }, + "debug@4.3.4": { + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "dependencies": { "ms": "ms@2.1.2" } + }, + "electron-to-chromium@1.4.284": { + "integrity": "sha512-M8WEXFuKXMYMVr45fo8mq0wUrrJHheiKZf6BArTKk9ZBYCKJEOU5H8cdWgDT+qCVZf7Na4lVUaZsA+h6uA9+PA==", + "dependencies": {} + }, "esbuild-android-64@0.15.18": { "integrity": "sha512-wnpt3OXRhcjfIDSZu9bnzT4/TNTDsOUvip0foZOUBG7QbSt//w3QV4FInVJxNhKc/ErhUxc5z4QjHtMi7/TbgA==", "dependencies": {} @@ -152,6 +452,14 @@ "esbuild-windows-arm64": "esbuild-windows-arm64@0.15.18" } }, + "escalade@3.1.1": { + "integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==", + "dependencies": {} + }, + "escape-string-regexp@1.0.5": { + "integrity": "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==", + "dependencies": {} + }, "fsevents@2.3.2": { "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", "dependencies": {} @@ -160,6 +468,18 @@ "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", "dependencies": {} }, + "gensync@1.0.0-beta.2": { + "integrity": "sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==", + "dependencies": {} + }, + "globals@11.12.0": { + "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", + "dependencies": {} + }, + "has-flag@3.0.0": { + "integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==", + "dependencies": {} + }, "has@1.0.3": { "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", "dependencies": { "function-bind": "function-bind@1.1.1" } @@ -168,10 +488,38 @@ "integrity": "sha512-RRjxlvLDkD1YJwDbroBHMb+cukurkDWNyHx7D3oNB5x9rb5ogcksMC5wHCadcXoo67gVr/+3GFySh3134zi6rw==", "dependencies": { "has": "has@1.0.3" } }, + "js-tokens@4.0.0": { + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", + "dependencies": {} + }, + "jsesc@2.5.2": { + "integrity": "sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==", + "dependencies": {} + }, + "json5@2.2.1": { + "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "dependencies": {} + }, + "loose-envify@1.4.0": { + "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "dependencies": { "js-tokens": "js-tokens@4.0.0" } + }, + "magic-string@0.26.7": { + "integrity": "sha512-hX9XH3ziStPoPhJxLq1syWuZMxbDvGNbVchfrdCtanC7D13888bMFow61x8axrx+GfHLtVeAx2kxL7tTGRl+Ow==", + "dependencies": { "sourcemap-codec": "sourcemap-codec@1.4.8" } + }, + "ms@2.1.2": { + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dependencies": {} + }, "nanoid@3.3.4": { "integrity": "sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==", "dependencies": {} }, + "node-releases@2.0.6": { + "integrity": "sha512-PiVXnNuFm5+iYkLBNeq5211hvO38y63T0i2KKh2KnUs3RpzJ+JtODFjkD8yjLwnDkTYF1eKXheUwdssR+NRZdg==", + "dependencies": {} + }, "path-parse@1.0.7": { "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", "dependencies": {} @@ -188,6 +536,22 @@ "source-map-js": "source-map-js@1.0.2" } }, + "react-dom@18.2.0_react@18.2.0": { + "integrity": "sha512-6IMTriUmvsjHUjNtEDudZfuDQUoWXVxKHhlEGSk81n4YFS+r/Kl99wXiwlVXtPBtJenozv2P+hxDsw9eA7Xo6g==", + "dependencies": { + "loose-envify": "loose-envify@1.4.0", + "react": "react@18.2.0", + "scheduler": "scheduler@0.23.0" + } + }, + "react-refresh@0.14.0": { + "integrity": "sha512-wViHqhAd8OHeLS/IRMJjTSDHF3U9eWi62F/MledQGPdJGDhodXJ9PBLNGr6WWL7qlH12Mt3TyTpbS+hGXMjCzQ==", + "dependencies": {} + }, + "react@18.2.0": { + "integrity": "sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==", + "dependencies": { "loose-envify": "loose-envify@1.4.0" } + }, "resolve@1.22.1": { "integrity": "sha512-nBpuuYuY5jFsli/JIs1oldw6fOQCBioohqWZg/2hiaOybXOft4lonv85uDOKXdf8rhyK159cxU5cDcK/NKk8zw==", "dependencies": { @@ -200,14 +564,42 @@ "integrity": "sha512-uKxbd0IhMZOhjAiD5oAFp7BqvkA4Dv47qpOCtaNvng4HBwdbWtdOh8f5nZNuk2rp51PMGk3bzfWu5oayNEuYnw==", "dependencies": { "fsevents": "fsevents@2.3.2" } }, + "scheduler@0.23.0": { + "integrity": "sha512-CtuThmgHNg7zIZWAXi3AsyIzA3n4xx7aNyjwC2VJldO2LMVDhFK+63xGqq6CsJH4rTAt6/M+N4GhZiDYPx9eUw==", + "dependencies": { "loose-envify": "loose-envify@1.4.0" } + }, + "semver@6.3.0": { + "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "dependencies": {} + }, "source-map-js@1.0.2": { "integrity": "sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==", "dependencies": {} }, + "sourcemap-codec@1.4.8": { + "integrity": "sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==", + "dependencies": {} + }, + "supports-color@5.5.0": { + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dependencies": { "has-flag": "has-flag@3.0.0" } + }, "supports-preserve-symlinks-flag@1.0.0": { "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", "dependencies": {} }, + "to-fast-properties@2.0.0": { + "integrity": "sha512-/OaKK0xYrs3DmxRYqL/yDc+FxFUVYhDlXMhRmv3z915w2HF1tnN1omB354j8VUGO/hbRzyD6Y3sA7v7GS/ceog==", + "dependencies": {} + }, + "update-browserslist-db@1.0.10_browserslist@4.21.4": { + "integrity": "sha512-OztqDenkfFkbSG+tRxBeAnCVPckDBcvibKd35yDONx6OU8N7sqgwc7rCbkJ/WcYtVRZ4ba68d6byhC21GFh7sQ==", + "dependencies": { + "browserslist": "browserslist@4.21.4", + "escalade": "escalade@3.1.1", + "picocolors": "picocolors@1.0.0" + } + }, "vite@3.2.4": { "integrity": "sha512-Z2X6SRAffOUYTa+sLy3NQ7nlHFU100xwanq1WDwqaiFiCe+25zdxP1TfCS5ojPV2oDDcXudHIoPnI1Z/66B7Yw==", "dependencies": { diff --git a/src/npm-resolve.ts b/src/npm-resolve.ts index 1cce6da..7b176f2 100644 --- a/src/npm-resolve.ts +++ b/src/npm-resolve.ts @@ -11,15 +11,17 @@ export default function npmResolve(config: PluginConfig) { enforce: 'pre' as const, - async resolveId(importee: string, importer: string | undefined) { - console.log('resolveId npm', importee, importer); - - if (importee.startsWith('npm:')) { - await deno.cache(importee); - return importee; + async resolveId(id: string, importer: string | undefined) { + // console.log('resolveId npm', id, importer); + + if (id.startsWith('npm:')) { + await deno.cache(id); + return id; + } else { + const r = await this.resolve(id, importer, { skipSelf: true }) + console.log(`resolve result id: ${id} importer: ${importer} r:`, r); + return r; } - - return null; }, async load(id: string) { diff --git a/vite.config.mts b/vite.config.mts index 58ce04b..2632d38 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -1,6 +1,10 @@ import { defineConfig } from 'npm:vite@3.2.4'; import denoResolve from './mod.ts'; +import react from "npm:@vitejs/plugin-react"; + +import "npm:react"; +import "npm:react-dom"; export default defineConfig({ - plugins: [denoResolve()], + plugins: [react(), denoResolve()], }); From c886276e45e1f43b4dd9cb2bf0e81e87a273a407 Mon Sep 17 00:00:00 2001 From: DOUGES Date: Thu, 29 Dec 2022 22:41:51 +1100 Subject: [PATCH 4/6] chore: update example, delete unneeded code --- deno.jsonc | 4 ++-- deno.lock | 1 + examples/import_map.json | 6 ++++++ examples/vite.config.mts | 9 +++++++++ src/npm-resolve.ts | 11 ++++++++--- vite.config.mts | 10 ---------- 6 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 examples/import_map.json create mode 100644 examples/vite.config.mts delete mode 100644 vite.config.mts diff --git a/deno.jsonc b/deno.jsonc index 99090ac..50a5428 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,8 +1,8 @@ { "tasks": { "check": "deno check mod.ts vite.config.mts", - "https": "deno run -A npm:vite@3.2.4 examples/https --config vite.config.mts", - "npm": "deno run -A npm:vite@3.2.4 examples/npm --config vite.config.mts" + "https": "deno run -A npm:vite@3.2.4 examples/https --config examples/vite.config.mts", + "npm": "deno run -A npm:vite@3.2.4 examples/npm --config examples/vite.config.mts" }, "fmt": { "options": { diff --git a/deno.lock b/deno.lock index f093be8..4f65e27 100644 --- a/deno.lock +++ b/deno.lock @@ -37,6 +37,7 @@ "npm": { "specifiers": { "@vitejs/plugin-react": "@vitejs/plugin-react@2.2.0_vite@3.2.4_@babel+core@7.20.5", + "@vitejs/plugin-react@2.2.0": "@vitejs/plugin-react@2.2.0_vite@3.2.4_@babel+core@7.20.5", "react": "react@18.2.0", "react-dom": "react-dom@18.2.0_react@18.2.0", "vite@3.2.4": "vite@3.2.4" diff --git a/examples/import_map.json b/examples/import_map.json new file mode 100644 index 0000000..08be686 --- /dev/null +++ b/examples/import_map.json @@ -0,0 +1,6 @@ +{ + "imports": { + "react/jsx-dev-runtime": "", + "react/jsx-runtime": "" + } +} diff --git a/examples/vite.config.mts b/examples/vite.config.mts new file mode 100644 index 0000000..13e8085 --- /dev/null +++ b/examples/vite.config.mts @@ -0,0 +1,9 @@ +import reactImport from 'npm:@vitejs/plugin-react@2.2.0'; +import { defineConfig } from 'npm:vite@3.2.4'; +import denoResolve from '../mod.ts'; + +const react = reactImport as never as typeof reactImport.default; + +export default defineConfig({ + plugins: [react(), denoResolve()], +}); diff --git a/src/npm-resolve.ts b/src/npm-resolve.ts index 7b176f2..56c4247 100644 --- a/src/npm-resolve.ts +++ b/src/npm-resolve.ts @@ -1,12 +1,16 @@ import { createDeno } from './deno.ts'; import { PluginConfig } from './types.ts'; +function createPlugin(plugin: import('npm:vite').Plugin) { + return plugin; +} + export default function npmResolve(config: PluginConfig) { // TODO(bartlomieju): both plugins should use the same instance, or maybe // both plugins should be combined. const deno = createDeno(config); - return { + return createPlugin({ name: 'vite:deno-npm-resolve', enforce: 'pre' as const, @@ -18,7 +22,7 @@ export default function npmResolve(config: PluginConfig) { await deno.cache(id); return id; } else { - const r = await this.resolve(id, importer, { skipSelf: true }) + const r = await this.resolve(id, importer, { skipSelf: true }); console.log(`resolve result id: ${id} importer: ${importer} r:`, r); return r; } @@ -62,7 +66,7 @@ export default function npmResolve(config: PluginConfig) { } return await Deno.readTextFile(`${moduleDirPath}/${file}`); }, - }; + }); } interface NpmPackageReference { @@ -70,6 +74,7 @@ interface NpmPackageReference { versionReq: string | null; subPath: string | null; } + function npmPackageReference(specifier: string): NpmPackageReference { if (!specifier.startsWith('npm:')) { throw new Error(`Invalid npm package reference: ${specifier}`); diff --git a/vite.config.mts b/vite.config.mts deleted file mode 100644 index 2632d38..0000000 --- a/vite.config.mts +++ /dev/null @@ -1,10 +0,0 @@ -import { defineConfig } from 'npm:vite@3.2.4'; -import denoResolve from './mod.ts'; -import react from "npm:@vitejs/plugin-react"; - -import "npm:react"; -import "npm:react-dom"; - -export default defineConfig({ - plugins: [react(), denoResolve()], -}); From 0f6ea635ff80c0f3adbcaaed9ea36b81242d3c07 Mon Sep 17 00:00:00 2001 From: DOUGES Date: Thu, 29 Dec 2022 22:46:11 +1100 Subject: [PATCH 5/6] core: fix formatting --- examples/vite.config.mts | 6 +++--- src/deno.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/vite.config.mts b/examples/vite.config.mts index 1c1ac8e..13e8085 100644 --- a/examples/vite.config.mts +++ b/examples/vite.config.mts @@ -1,6 +1,6 @@ -import reactImport from "npm:@vitejs/plugin-react@2.2.0"; -import { defineConfig } from "npm:vite@3.2.4"; -import denoResolve from "../mod.ts"; +import reactImport from 'npm:@vitejs/plugin-react@2.2.0'; +import { defineConfig } from 'npm:vite@3.2.4'; +import denoResolve from '../mod.ts'; const react = reactImport as never as typeof reactImport.default; diff --git a/src/deno.ts b/src/deno.ts index 966d506..897671e 100644 --- a/src/deno.ts +++ b/src/deno.ts @@ -87,7 +87,7 @@ export function createDeno( const status = await p.status(); if (!status.success) { - throw new Error(`invariant: could not get info on ${name}`); + throw new Error(`invariant: could not get cache info`); } const output = await p.output(); From 6f8e7bd03af827e270e0d28b4d600a6f6357fd21 Mon Sep 17 00:00:00 2001 From: DOUGES Date: Thu, 29 Dec 2022 23:02:32 +1100 Subject: [PATCH 6/6] chore: add wrapper resolver func --- src/npm-resolve.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/npm-resolve.ts b/src/npm-resolve.ts index 56c4247..7b3e9df 100644 --- a/src/npm-resolve.ts +++ b/src/npm-resolve.ts @@ -15,6 +15,15 @@ export default function npmResolve(config: PluginConfig) { enforce: 'pre' as const, + configResolved(config) { + const previousCreateResolver = config.createResolver; + const createResolver: typeof config['createResolver'] = (options) => { + return previousCreateResolver(options); + }; + + config.createResolver = createResolver; + }, + async resolveId(id: string, importer: string | undefined) { // console.log('resolveId npm', id, importer);