diff --git a/.gitignore b/.gitignore index 8ccb285c..26656813 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ node_modules/ .docs/ .DS_Store tmp_* -coverage/ \ No newline at end of file +coverage/ +vite-inspect/ +.vite-inspect/ +demo/dist/ \ No newline at end of file diff --git a/deno.json b/deno.json index f37fcdbe..ceb3b9d6 100644 --- a/deno.json +++ b/deno.json @@ -64,7 +64,7 @@ "@std/streams": "jsr:@std/streams@1", "@astral/astral": "jsr:@astral/astral@^0.5.3", - "@marvinh-test/fresh-island": "jsr:@marvinh-test/fresh-island@^0.0.1", + "@marvinh-test/fresh-island": "jsr:@marvinh-test/fresh-island@^0.0.2", "linkedom": "npm:linkedom@^0.18.10", "@std/async": "jsr:@std/async@^1.0.13", "@std/expect": "jsr:@std/expect@^1.0.16", diff --git a/deno.lock b/deno.lock index 1a276367..ac896b46 100644 --- a/deno.lock +++ b/deno.lock @@ -3,47 +3,74 @@ "specifiers": { "jsr:@astral/astral@~0.5.3": "0.5.3", "jsr:@deno-library/progress@^1.5.1": "1.5.1", + "jsr:@deno/cache-dir@0.14": "0.14.0", + "jsr:@deno/doc@0.172": "0.172.0", "jsr:@deno/esbuild-plugin@^1.2.0": "1.2.0", + "jsr:@deno/graph@0.86": "0.86.9", + "jsr:@deno/graph@~0.82.3": "0.82.3", + "jsr:@deno/loader@~0.3.2": "0.3.4", "jsr:@deno/loader@~0.3.3": "0.3.4", - "jsr:@std/assert@^1.0.13": "1.0.13", + "jsr:@fresh/build-id@1": "1.0.0", + "jsr:@fresh/core@^2.0.0-alpha.37": "2.0.0-alpha.58", + "jsr:@marvinh-test/fresh-island@^0.0.2": "0.0.2", + "jsr:@std/assert@^1.0.13": "1.0.14", "jsr:@std/async@1": "1.0.14", + "jsr:@std/async@^1.0.13": "1.0.14", + "jsr:@std/bytes@^1.0.5": "1.0.6", "jsr:@std/bytes@^1.0.6": "1.0.6", + "jsr:@std/cli@^1.0.19": "1.0.21", + "jsr:@std/cli@^1.0.21": "1.0.21", "jsr:@std/collections@^1.0.11": "1.1.1", "jsr:@std/collections@^1.1.1": "1.1.2", "jsr:@std/collections@^1.1.2": "1.1.2", + "jsr:@std/crypto@^1.0.5": "1.0.5", + "jsr:@std/data-structures@^1.0.9": "1.0.9", "jsr:@std/encoding@1": "1.0.10", "jsr:@std/encoding@^1.0.10": "1.0.10", "jsr:@std/expect@^1.0.16": "1.0.16", "jsr:@std/fmt@1.0.3": "1.0.3", + "jsr:@std/fmt@^1.0.3": "1.0.8", "jsr:@std/fmt@^1.0.7": "1.0.8", "jsr:@std/fmt@^1.0.8": "1.0.8", "jsr:@std/front-matter@^1.0.5": "1.0.9", "jsr:@std/fs@1": "1.0.19", + "jsr:@std/fs@^1.0.19": "1.0.19", + "jsr:@std/fs@^1.0.6": "1.0.19", "jsr:@std/html@1": "1.0.4", + "jsr:@std/html@^1.0.4": "1.0.4", "jsr:@std/http@^1.0.15": "1.0.20", - "jsr:@std/internal@^1.0.6": "1.0.10", + "jsr:@std/internal@^1.0.10": "1.0.10", "jsr:@std/internal@^1.0.7": "1.0.10", "jsr:@std/internal@^1.0.9": "1.0.10", + "jsr:@std/io@0.225": "0.225.2", "jsr:@std/io@0.225.0": "0.225.0", "jsr:@std/json@^1.0.2": "1.0.2", "jsr:@std/jsonc@1": "1.0.2", "jsr:@std/media-types@1": "1.1.0", + "jsr:@std/media-types@^1.1.0": "1.1.0", + "jsr:@std/net@^1.0.4": "1.0.4", "jsr:@std/path@1": "1.1.1", + "jsr:@std/path@^1.0.8": "1.1.1", "jsr:@std/path@^1.1.1": "1.1.1", "jsr:@std/semver@1": "1.0.5", "jsr:@std/streams@1": "1.0.10", + "jsr:@std/streams@^1.0.10": "1.0.10", + "jsr:@std/testing@^1.0.12": "1.0.15", "jsr:@std/toml@^1.0.3": "1.0.8", "jsr:@std/uuid@^1.0.7": "1.0.9", "jsr:@std/yaml@^1.0.5": "1.0.9", "jsr:@zip-js/zip-js@^2.7.52": "2.7.72", "npm:@babel/core@^7.28.0": "7.28.0", + "npm:@babel/preset-react@^7.27.1": "7.27.1_@babel+core@7.28.0", "npm:@opentelemetry/api@^1.9.0": "1.9.0", + "npm:@preact/signals@2": "2.2.1_preact@10.27.0", "npm:@preact/signals@^2.2.1": "2.2.1_preact@10.27.0", - "npm:@prefresh/vite@^2.4.8": "2.4.8_preact@10.27.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@22.15.15", + "npm:@prefresh/vite@^2.4.8": "2.4.8_preact@10.27.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@24.2.0", "npm:@tailwindcss/postcss@^4.1.10": "4.1.11", "npm:@types/babel__core@^7.20.5": "7.20.5", - "npm:@types/node@*": "22.15.15", + "npm:@types/node@*": "24.2.0", "npm:@types/node@^24.1.0": "24.2.1", + "npm:@types/node@^24.2.1": "24.2.1", "npm:autoprefixer@^10.4.21": "10.4.21_postcss@8.5.6", "npm:cssnano@^6.1.2": "6.1.2_postcss@8.5.6", "npm:esbuild-wasm@0.25.7": "0.25.7", @@ -58,22 +85,26 @@ "npm:postcss@8.5.6": "8.5.6", "npm:postcss@^8.5.6": "8.5.6", "npm:preact-render-to-string@^6.5.11": "6.5.13_preact@10.27.0", + "npm:preact@^10.22.0": "10.27.0", "npm:preact@^10.26.9": "10.27.0", "npm:preact@^10.27.0": "10.27.0", "npm:prismjs@^1.29.0": "1.30.0", + "npm:rollup-plugin-visualizer@^6.0.3": "6.0.3", "npm:tailwindcss@^3.4.17": "3.4.17_postcss@8.5.6", "npm:tailwindcss@^4.1.10": "4.1.11", "npm:ts-morph@26": "26.0.0", - "npm:vite-plugin-inspect@^11.3.2": "11.3.2_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@22.15.15", - "npm:vite@^7.0.6": "7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@22.15.15" + "npm:vite-plugin-inspect@^11.3.2": "11.3.2_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@24.2.0", + "npm:vite@*": "7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@24.2.0", + "npm:vite@7.1.2": "7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@24.2.0", + "npm:vite@^7.1.2": "7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@24.2.0" }, "jsr": { "@astral/astral@0.5.3": { "integrity": "d6a4628313d8be99aac0f51005c1dc090fa3b4c6b5c8335c26a52d4842aa1276", "dependencies": [ "jsr:@deno-library/progress", - "jsr:@std/async", - "jsr:@std/fs", + "jsr:@std/async@1", + "jsr:@std/fs@1", "jsr:@std/path@1", "jsr:@zip-js/zip-js" ] @@ -82,24 +113,82 @@ "integrity": "966611826b8bb27baae73ab1c4fa4317cd4edd2abb99750cd6f8488d22d5b121", "dependencies": [ "jsr:@std/fmt@1.0.3", - "jsr:@std/io" + "jsr:@std/io@0.225.0" + ] + }, + "@deno/cache-dir@0.14.0": { + "integrity": "729f0b68e7fc96443c09c2c544b830ca70897bdd5168598446d752f7a4c731ad", + "dependencies": [ + "jsr:@deno/graph@0.86", + "jsr:@std/fmt@^1.0.3", + "jsr:@std/fs@^1.0.6", + "jsr:@std/io@0.225", + "jsr:@std/path@^1.0.8" + ] + }, + "@deno/doc@0.172.0": { + "integrity": "72a68ed533576a06feb930a84784ad9ba6d83ca9d581fc734d498c58e32b7cf5", + "dependencies": [ + "jsr:@deno/cache-dir", + "jsr:@deno/graph@~0.82.3" ] }, "@deno/esbuild-plugin@1.2.0": { "integrity": "04ddd0fca9416d8a2866263928a53b9d5ed08dfca064d64504a0aaf9800c709e", "dependencies": [ - "jsr:@deno/loader", + "jsr:@deno/loader@~0.3.3", "jsr:@std/path@^1.1.1", "npm:esbuild@~0.25.5" ] }, + "@deno/graph@0.82.3": { + "integrity": "5c1fe944368172a9c87588ac81b82eb027ca78002a57521567e6264be322637e" + }, + "@deno/graph@0.86.9": { + "integrity": "c4f353a695bcc5246c099602977dabc6534eacea9999a35a8cb24e807192e6a1" + }, "@deno/loader@0.3.4": { "integrity": "c56003bc7027606301c3fe62704723b207a9e508c9fb154cf5131abb9d4d2673" }, - "@std/assert@1.0.13": { - "integrity": "ae0d31e41919b12c656c742b22522c32fb26ed0cba32975cb0de2a273cb68b29", + "@fresh/build-id@1.0.0": { + "integrity": "c42a1dab0b0b8bcddf93657edf03f404d537ba8e1d44144b3c749727c5671d5a", "dependencies": [ - "jsr:@std/internal@^1.0.6" + "jsr:@std/encoding@^1.0.10" + ] + }, + "@fresh/core@2.0.0-alpha.58": { + "integrity": "368b579ff0da5badd232916f021a458c9c9ccb35ffe4247df6ff5757cffedc82", + "dependencies": [ + "jsr:@deno/esbuild-plugin", + "jsr:@std/encoding@1", + "jsr:@std/fmt@^1.0.8", + "jsr:@std/fs@1", + "jsr:@std/html@1", + "jsr:@std/http", + "jsr:@std/jsonc", + "jsr:@std/media-types@1", + "jsr:@std/path@1", + "jsr:@std/semver", + "jsr:@std/uuid", + "npm:@opentelemetry/api", + "npm:@preact/signals@^2.2.1", + "npm:esbuild-wasm", + "npm:esbuild@0.25.7", + "npm:preact-render-to-string", + "npm:preact@^10.27.0" + ] + }, + "@marvinh-test/fresh-island@0.0.2": { + "integrity": "77fc811a677de22d7149e326aa99e03530e4ce06fb8ce3ea296c16729ef65a54", + "dependencies": [ + "npm:@preact/signals@2", + "npm:preact@^10.22.0" + ] + }, + "@std/assert@1.0.14": { + "integrity": "68d0d4a43b365abc927f45a9b85c639ea18a9fab96ad92281e493e4ed84abaa4", + "dependencies": [ + "jsr:@std/internal@^1.0.10" ] }, "@std/async@1.0.13": { @@ -111,12 +200,21 @@ "@std/bytes@1.0.6": { "integrity": "f6ac6adbd8ccd99314045f5703e23af0a68d7f7e58364b47d2c7f408aeb5820a" }, + "@std/cli@1.0.21": { + "integrity": "cd25b050bdf6282e321854e3822bee624f07aca7636a3a76d95f77a3a919ca2a" + }, "@std/collections@1.1.1": { "integrity": "eff6443fbd9d5a6697018fb39c5d13d5f662f0045f21392d640693d0008ab2af" }, "@std/collections@1.1.2": { "integrity": "f1685dd45c3ec27c39d0e8a642ccf810f391ec8a6cb5e7355926e6dacc64c43e" }, + "@std/crypto@1.0.5": { + "integrity": "0dcfbb319fe0bba1bd3af904ceb4f948cde1b92979ec1614528380ed308a3b40" + }, + "@std/data-structures@1.0.9": { + "integrity": "033d6e17e64bf1f84a614e647c1b015fa2576ae3312305821e1a4cb20674bb4d" + }, "@std/encoding@1.0.10": { "integrity": "8783c6384a2d13abd5e9e87a7ae0520a30e9f56aeeaa3bdf910a3eaaf5c811a1" }, @@ -143,6 +241,7 @@ "@std/fs@1.0.19": { "integrity": "051968c2b1eae4d2ea9f79a08a3845740ef6af10356aff43d3e2ef11ed09fb06", "dependencies": [ + "jsr:@std/internal@^1.0.9", "jsr:@std/path@^1.1.1" ] }, @@ -152,7 +251,15 @@ "@std/http@1.0.20": { "integrity": "b5cc33fc001bccce65ed4c51815668c9891c69ccd908295997e983d8f56070a1", "dependencies": [ - "jsr:@std/encoding@^1.0.10" + "jsr:@std/cli@^1.0.21", + "jsr:@std/encoding@^1.0.10", + "jsr:@std/fmt@^1.0.8", + "jsr:@std/fs@^1.0.19", + "jsr:@std/html@^1.0.4", + "jsr:@std/media-types@^1.1.0", + "jsr:@std/net", + "jsr:@std/path@^1.1.1", + "jsr:@std/streams@^1.0.10" ] }, "@std/internal@1.0.10": { @@ -161,6 +268,12 @@ "@std/io@0.225.0": { "integrity": "c1db7c5e5a231629b32d64b9a53139445b2ca640d828c26bf23e1c55f8c079b3" }, + "@std/io@0.225.2": { + "integrity": "3c740cd4ee4c082e6cfc86458f47e2ab7cb353dc6234d5e9b1f91a2de5f4d6c7", + "dependencies": [ + "jsr:@std/bytes@^1.0.5" + ] + }, "@std/json@1.0.2": { "integrity": "d9e5497801c15fb679f55a2c01c7794ad7a5dfda4dd1bebab5e409cb5e0d34d4" }, @@ -173,6 +286,9 @@ "@std/media-types@1.1.0": { "integrity": "c9d093f0c05c3512932b330e3cc1fe1d627b301db33a4c2c2185c02471d6eaa4" }, + "@std/net@1.0.4": { + "integrity": "2f403b455ebbccf83d8a027d29c5a9e3a2452fea39bb2da7f2c04af09c8bc852" + }, "@std/path@1.1.0": { "integrity": "ddc94f8e3c275627281cbc23341df6b8bcc874d70374f75fec2533521e3d6886" }, @@ -188,7 +304,18 @@ "@std/streams@1.0.10": { "integrity": "75c0b1431873cd0d8b3d679015220204d36d3c7420d93b60acfc379eb0dc30af", "dependencies": [ - "jsr:@std/bytes" + "jsr:@std/bytes@^1.0.6" + ] + }, + "@std/testing@1.0.15": { + "integrity": "a490169f5ccb0f3ae9c94fbc69d2cd43603f2cffb41713a85f99bbb0e3087cbc", + "dependencies": [ + "jsr:@std/assert", + "jsr:@std/async@^1.0.13", + "jsr:@std/data-structures", + "jsr:@std/fs@^1.0.19", + "jsr:@std/internal@^1.0.10", + "jsr:@std/path@^1.1.1" ] }, "@std/toml@1.0.8": { @@ -200,7 +327,8 @@ "@std/uuid@1.0.9": { "integrity": "44b627bf2d372fe1bd099e2ad41b2be41a777fc94e62a3151006895a037f1642", "dependencies": [ - "jsr:@std/bytes" + "jsr:@std/bytes@^1.0.6", + "jsr:@std/crypto" ] }, "@std/yaml@1.0.8": { @@ -268,6 +396,12 @@ "jsesc" ] }, + "@babel/helper-annotate-as-pure@7.27.3": { + "integrity": "sha512-fXSwMQqitTGeHLBC08Eq5yXz2m37E4pJX1qAU1+2cNedz/ifv/bVXft90VeSav5nFO61EcNgwr0aJxbyPaWBPg==", + "dependencies": [ + "@babel/types" + ] + }, "@babel/helper-compilation-targets@7.27.2": { "integrity": "sha512-2+1thGUUWWjLTYTHZWK1n8Yga0ijBz1XAhUXcKy81rd5g6yh7hGqMp45v7cadSbEHc9G3OTv45SyneRN3ps4DQ==", "dependencies": [ @@ -297,6 +431,9 @@ "@babel/traverse" ] }, + "@babel/helper-plugin-utils@7.27.1": { + "integrity": "sha512-1gn1Up5YXka3YYAHGKpbideQ5Yjf1tDa9qYcgysz+cNCXukyLl6DjPXhD3VRwSb8c0J9tA4b2+rHEZtc6R0tlw==" + }, "@babel/helper-string-parser@7.27.1": { "integrity": "sha512-qMlSxKbpRlAridDExk92nSobyDdpPijUq2DW6oDnUqd0iOGxmQjyqhMIihI9+zv4LPyZdRje2cavWPbCbWm3eA==" }, @@ -320,6 +457,58 @@ ], "bin": true }, + "@babel/plugin-syntax-jsx@7.27.1_@babel+core@7.28.0": { + "integrity": "sha512-y8YTNIeKoyhGd9O0Jiyzyyqk8gdjnumGTQPsz0xOZOQ2RmkVJeZ1vmmfIvFEKqucBG6axJGBZDE/7iI5suUI/w==", + "dependencies": [ + "@babel/core", + "@babel/helper-plugin-utils" + ] + }, + "@babel/plugin-transform-react-display-name@7.28.0_@babel+core@7.28.0": { + "integrity": "sha512-D6Eujc2zMxKjfa4Zxl4GHMsmhKKZ9VpcqIchJLvwTxad9zWIYulwYItBovpDOoNLISpcZSXoDJ5gaGbQUDqViA==", + "dependencies": [ + "@babel/core", + "@babel/helper-plugin-utils" + ] + }, + "@babel/plugin-transform-react-jsx-development@7.27.1_@babel+core@7.28.0": { + "integrity": "sha512-ykDdF5yI4f1WrAolLqeF3hmYU12j9ntLQl/AOG1HAS21jxyg1Q0/J/tpREuYLfatGdGmXp/3yS0ZA76kOlVq9Q==", + "dependencies": [ + "@babel/core", + "@babel/plugin-transform-react-jsx" + ] + }, + "@babel/plugin-transform-react-jsx@7.27.1_@babel+core@7.28.0": { + "integrity": "sha512-2KH4LWGSrJIkVf5tSiBFYuXDAoWRq2MMwgivCf+93dd0GQi8RXLjKA/0EvRnVV5G0hrHczsquXuD01L8s6dmBw==", + "dependencies": [ + "@babel/core", + "@babel/helper-annotate-as-pure", + "@babel/helper-module-imports", + "@babel/helper-plugin-utils", + "@babel/plugin-syntax-jsx", + "@babel/types" + ] + }, + "@babel/plugin-transform-react-pure-annotations@7.27.1_@babel+core@7.28.0": { + "integrity": "sha512-JfuinvDOsD9FVMTHpzA/pBLisxpv1aSf+OIV8lgH3MuWrks19R27e6a6DipIg4aX1Zm9Wpb04p8wljfKrVSnPA==", + "dependencies": [ + "@babel/core", + "@babel/helper-annotate-as-pure", + "@babel/helper-plugin-utils" + ] + }, + "@babel/preset-react@7.27.1_@babel+core@7.28.0": { + "integrity": "sha512-oJHWh2gLhU9dW9HHr42q0cI0/iHHXTLGe39qvpAZZzagHy0MzYLCnCVV0symeRvzmjHyVU7mw2K06E6u/JwbhA==", + "dependencies": [ + "@babel/core", + "@babel/helper-plugin-utils", + "@babel/helper-validator-option", + "@babel/plugin-transform-react-display-name", + "@babel/plugin-transform-react-jsx", + "@babel/plugin-transform-react-jsx-development", + "@babel/plugin-transform-react-pure-annotations" + ] + }, "@babel/template@7.27.2": { "integrity": "sha512-LPDZ85aEJyYSd18/DkjNh4/y1ntkE5KwUHWTiqgRxruuZL2F1yuHligVHLvcHY2vMHXttKFpJn6LwfI7cw7ODw==", "dependencies": [ @@ -990,7 +1179,7 @@ "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3" ] }, - "@prefresh/vite@2.4.8_preact@10.27.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@22.15.15": { + "@prefresh/vite@2.4.8_preact@10.27.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@24.2.0": { "integrity": "sha512-H7vlo9UbJInuRbZhRQrdgVqLP7qKjDoX7TgYWWwIVhEHeHO0hZ4zyicvwBrV1wX5A3EPOmArgRkUaN7cPI2VXQ==", "dependencies": [ "@babel/core", @@ -999,7 +1188,7 @@ "@prefresh/utils", "@rollup/pluginutils", "preact", - "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@22.15.15" + "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@24.2.0" ] }, "@rollup/pluginutils@4.2.1": { @@ -1275,16 +1464,16 @@ "@types/estree@1.0.8": { "integrity": "sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==" }, - "@types/node@22.15.15": { - "integrity": "sha512-R5muMcZob3/Jjchn5LcO8jdKwSCbzqmPB6ruBxMcf9kbxtniZHP327s6C37iOfuw8mbKK3cAQa7sEl7afLrQ8A==", + "@types/node@24.2.0": { + "integrity": "sha512-3xyG3pMCq3oYCNg7/ZP+E1ooTaGB4cG8JWRsqqOYQdbWNY4zbaV0Ennrd7stjiJEFZCaybcIgpTjJWHRfBSIDw==", "dependencies": [ - "undici-types@6.21.0" + "undici-types" ] }, "@types/node@24.2.1": { "integrity": "sha512-DRh5K+ka5eJic8CjH7td8QpYEV6Zo10gfRkjHCO3weqZHWDtAaSTFtl4+VMqOJ4N5jcuhZ9/l+yy8rVgw7BQeQ==", "dependencies": [ - "undici-types@7.10.0" + "undici-types" ] }, "ansi-regex@5.0.1": { @@ -1404,6 +1593,14 @@ "chownr@3.0.0": { "integrity": "sha512-+IxzY9BZOQd/XuYPRmrvEVjF/nqj5kgT4kEq7VofrDoM1MxoRjEWkrCC3EtLi59TVawxTAn+orJwFQcrqEN1+g==" }, + "cliui@8.0.1": { + "integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==", + "dependencies": [ + "string-width@4.2.3", + "strip-ansi@6.0.1", + "wrap-ansi@7.0.0" + ] + }, "code-block-writer@13.0.3": { "integrity": "sha512-Oofo0pq3IKnsFtuHqSF7TqBfr71aeyZDVJ0HpmqB7FBM2qEigL0iPONSCZSO9pE9dZTAxANe5XHG9Uy0YMv8cg==" }, @@ -1548,6 +1745,9 @@ "default-browser-id" ] }, + "define-lazy-prop@2.0.0": { + "integrity": "sha512-Ds09qNh8yw3khSjiJjiUInaGX9xlqZDY7JVryGxdxV7NPeuqQfplOpQ66yJFZut3jLa5zOwkXw1g9EI2uKh4Og==" + }, "define-lazy-prop@3.0.0": { "integrity": "sha512-N+MeXYoqr3pOgn8xfyRPREN7gHakLYjhsHhWGT3fWAiL4IkAt0iDw14QiiEm2bE30c5XX5q0FtAA3CK5f9/BUg==" }, @@ -1711,6 +1911,9 @@ "gensync@1.0.0-beta.2": { "integrity": "sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==" }, + "get-caller-file@2.0.5": { + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==" + }, "github-slugger@2.0.0": { "integrity": "sha512-IaOQ9puYtjrkq7Y0Ygl9KDZnrf/aiUJYUpVf89y8kyaxbRG7Y1SrX/jaumrv81vc61+kiMempujsM3Yw7w5qcw==" }, @@ -1771,6 +1974,10 @@ "hasown" ] }, + "is-docker@2.2.1": { + "integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==", + "bin": true + }, "is-docker@3.0.0": { "integrity": "sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ==", "bin": true @@ -1790,13 +1997,19 @@ "is-inside-container@1.0.0": { "integrity": "sha512-KIYLCCJghfHZxqjYBE7rEy0OBuTd5xCHS7tHVgvCLkx7StIoaxwNW3hCALgEUjFfeRk+MG/Qxmp/vtETEF3tRA==", "dependencies": [ - "is-docker" + "is-docker@3.0.0" ], "bin": true }, "is-number@7.0.0": { "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==" }, + "is-wsl@2.2.0": { + "integrity": "sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==", + "dependencies": [ + "is-docker@2.2.1" + ] + }, "is-wsl@3.1.0": { "integrity": "sha512-UcVfVfaK4Sc4m7X3dUSoHoozQGBEFeDC+zVo06t98xe8CzHSZZBekNXH+tu0NalHolcJ/QAGqS46Hef7QXBIMw==", "dependencies": [ @@ -2039,11 +2252,19 @@ "integrity": "sha512-YgBpdJHPyQ2UE5x+hlSXcnejzAvD0b22U2OuAP+8OnlJT+PjWPxtgmGqKKc+RgTM63U9gN0YzrYc71R2WT/hTA==", "dependencies": [ "default-browser", - "define-lazy-prop", + "define-lazy-prop@3.0.0", "is-inside-container", "wsl-utils" ] }, + "open@8.4.2": { + "integrity": "sha512-7x81NCL719oNbsq/3mh+hVrAWmFuEYUqrq/Iw3kUzH8ReypT9QQ0BLoJS7/G9k6N81XjW4qHWtjWwe/9eLy1EQ==", + "dependencies": [ + "define-lazy-prop@2.0.0", + "is-docker@2.2.1", + "is-wsl@2.2.0" + ] + }, "package-json-from-dist@1.0.1": { "integrity": "sha512-UEZIS3/by4OC8vL3P2dTXRETpebLI2NiI5vIrjaD/5UtrkFX/tNbwjTSRAGC/+7CAo2pIcBaRgWmcBBHcsaCIw==" }, @@ -2364,6 +2585,9 @@ "picomatch@2.3.1" ] }, + "require-directory@2.1.1": { + "integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==" + }, "resolve@1.22.10": { "integrity": "sha512-NPRy+/ncIMeDlTAsuqwKIiferiawhefFJtkNSW0qZJEqMEb+qBt/77B/jGeeek+F0uOeN05CDa6HXbbIgtVX4w==", "dependencies": [ @@ -2376,6 +2600,16 @@ "reusify@1.1.0": { "integrity": "sha512-g6QUff04oZpHs0eG5p83rFLhHeV00ug/Yf9nZM6fLeUrPguBTkTQOdpAWWspMh55TZfVQDPaN3NQJfbVRAxdIw==" }, + "rollup-plugin-visualizer@6.0.3": { + "integrity": "sha512-ZU41GwrkDcCpVoffviuM9Clwjy5fcUxlz0oMoTXTYsK+tcIFzbdacnrr2n8TXcHxbGKKXtOdjxM2HUS4HjkwIw==", + "dependencies": [ + "open@8.4.2", + "picomatch@4.0.3", + "source-map", + "yargs" + ], + "bin": true + }, "rollup@4.46.2": { "integrity": "sha512-WMmLFI+Boh6xbop+OAGo9cQ3OgX9MIg7xOQjn+pTCwOkk+FNDAeAemXkJ3HzDJrVXleLOFVa1ipuc1AmEx1Dwg==", "dependencies": [ @@ -2442,6 +2676,9 @@ "source-map-js@1.2.1": { "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==" }, + "source-map@0.7.6": { + "integrity": "sha512-i5uvt8C3ikiWeNZSVZNWcfZPItFQOsYTUAOkcUPGd8DqDy1uOUikjt5dG+uRlwyvR108Fb9DOd4GvXfT0N2/uQ==" + }, "string-width@4.2.3": { "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", "dependencies": [ @@ -2596,9 +2833,6 @@ "uhyphen@0.2.0": { "integrity": "sha512-qz3o9CHXmJJPGBdqzab7qAYuW8kQGKNEuoHFYrBwV6hWIMcpAmxDLXojcHfFr9US1Pe6zUswEIJIbLI610fuqA==" }, - "undici-types@6.21.0": { - "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==" - }, "undici-types@7.10.0": { "integrity": "sha512-t5Fy/nfn+14LuOc2KNYg75vZqClpAiqscVvMygNnlsHBFpSXdJaYtXMcdNLpl/Qvc3P2cB3s6lOV51nqsFq4ag==" }, @@ -2629,12 +2863,12 @@ "vite-hot-client@2.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1" ] }, - "vite-dev-rpc@1.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@22.15.15": { + "vite-dev-rpc@1.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@24.2.0": { "integrity": "sha512-pKXZlgoXGoE8sEKiKJSng4hI1sQ4wi5YT24FCrwrLt6opmkjlqPPVmiPWWJn8M8byMxRGzp1CrFuqQs4M/Z39A==", "dependencies": [ "birpc", - "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@22.15.15", - "vite-hot-client@2.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@22.15.15" + "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@24.2.0", + "vite-hot-client@2.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@24.2.0" ] }, "vite-hot-client@2.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1": { @@ -2643,10 +2877,10 @@ "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3" ] }, - "vite-hot-client@2.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@22.15.15": { + "vite-hot-client@2.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@24.2.0": { "integrity": "sha512-7SpgZmU7R+dDnSmvXE1mfDtnHLHQSisdySVR7lO8ceAXvM0otZeuQQ6C8LrS5d/aYyP/QZ0hI0L+dIPrm4YlFQ==", "dependencies": [ - "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@22.15.15" + "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@24.2.0" ] }, "vite-plugin-inspect@11.3.2_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1": { @@ -2656,7 +2890,7 @@ "debug", "error-stack-parser-es", "ohash", - "open", + "open@10.2.0", "perfect-debounce", "sirv", "unplugin-utils", @@ -2664,19 +2898,19 @@ "vite-dev-rpc@1.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1" ] }, - "vite-plugin-inspect@11.3.2_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@22.15.15": { + "vite-plugin-inspect@11.3.2_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@24.2.0": { "integrity": "sha512-nzwvyFQg58XSMAmKVLr2uekAxNYvAbz1lyPmCAFVIBncCgN9S/HPM+2UM9Q9cvc4JEbC5ZBgwLAdaE2onmQuKg==", "dependencies": [ "ansis", "debug", "error-stack-parser-es", "ohash", - "open", + "open@10.2.0", "perfect-debounce", "sirv", "unplugin-utils", - "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@22.15.15", - "vite-dev-rpc@1.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@22.15.15" + "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@24.2.0", + "vite-dev-rpc@1.1.0_vite@7.1.2__@types+node@24.2.1__picomatch@4.0.3_@types+node@24.2.1_@types+node@24.2.0" ] }, "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3": { @@ -2698,10 +2932,10 @@ ], "bin": true }, - "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@22.15.15": { + "vite@7.1.2_@types+node@24.2.1_picomatch@4.0.3_@types+node@24.2.0": { "integrity": "sha512-J0SQBPlQiEXAF7tajiH+rUooJPo0l8KQgyg4/aMunNtrOa7bwuZJsJbDWzeljqQpgftxuq5yNJxQ91O9ts29UQ==", "dependencies": [ - "@types/node@22.15.15", + "@types/node@24.2.0", "esbuild", "fdir", "picomatch@4.0.3", @@ -2713,7 +2947,7 @@ "fsevents" ], "optionalPeers": [ - "@types/node@22.15.15" + "@types/node@24.2.0" ], "bin": true }, @@ -2743,9 +2977,12 @@ "wsl-utils@0.1.0": { "integrity": "sha512-h3Fbisa2nKGPxCpm89Hk33lBLsnaGBvctQopaBSOW/uIs6FTe1ATyAnKFJrzVs9vpGdsTe73WF3V4lIsk4Gacw==", "dependencies": [ - "is-wsl" + "is-wsl@3.1.0" ] }, + "y18n@5.0.8": { + "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==" + }, "yallist@3.1.1": { "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==" }, @@ -2755,6 +2992,21 @@ "yaml@2.8.1": { "integrity": "sha512-lcYcMxX2PO9XMGvAJkJ3OsNMw+/7FKes7/hgerGUYWIoWu5j/+YQqcZr5JnPZWzOsEBgMbSbiSTn/dv/69Mkpw==", "bin": true + }, + "yargs-parser@21.1.1": { + "integrity": "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==" + }, + "yargs@17.7.2": { + "integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==", + "dependencies": [ + "cliui", + "escalade", + "get-caller-file", + "require-directory", + "string-width@4.2.3", + "y18n", + "yargs-parser" + ] } }, "redirects": { @@ -2825,7 +3077,7 @@ "jsr:@deno/esbuild-plugin@^1.2.0", "jsr:@fresh/build-id@1", "jsr:@fresh/core@^2.0.0-alpha.37", - "jsr:@marvinh-test/fresh-island@^0.0.1", + "jsr:@marvinh-test/fresh-island@^0.0.2", "jsr:@std/async@^1.0.13", "jsr:@std/cli@^1.0.19", "jsr:@std/collections@^1.1.2", @@ -2884,13 +3136,15 @@ "dependencies": [ "jsr:@deno/loader@~0.3.2", "npm:@babel/core@^7.28.0", + "npm:@babel/preset-react@^7.27.1", "npm:@prefresh/vite@^2.4.8", "npm:@types/babel__core@^7.20.5", "npm:@types/node@^24.1.0", "npm:mime-db@^1.54.0", "npm:preact@^10.26.9", + "npm:rollup-plugin-visualizer@^6.0.3", "npm:vite-plugin-inspect@^11.3.2", - "npm:vite@^7.0.6" + "npm:vite@^7.1.2" ] } } diff --git a/packages/plugin-vite/demo/islands/tests/JsrIsland.tsx b/packages/plugin-vite/demo/islands/tests/JsrIsland.tsx new file mode 100644 index 00000000..dbecc138 --- /dev/null +++ b/packages/plugin-vite/demo/islands/tests/JsrIsland.tsx @@ -0,0 +1 @@ +export { RemoteIsland } from "@marvinh-test/fresh-island"; diff --git a/packages/plugin-vite/demo/islands/tests/Mime.tsx b/packages/plugin-vite/demo/islands/tests/Mime.tsx index 2ef3ec81..d55e5984 100644 --- a/packages/plugin-vite/demo/islands/tests/Mime.tsx +++ b/packages/plugin-vite/demo/islands/tests/Mime.tsx @@ -1,12 +1,11 @@ -import * as mime from "mime-db"; +import mime from "mime-db"; import { useEffect, useState } from "preact/hooks"; export function MimeIsland() { - // deno-lint-ignore no-console - console.log(mime); - const [ready, setReady] = useState(false); useEffect(() => { + // deno-lint-ignore no-console + console.log(mime); setReady(true); }, []); diff --git a/packages/plugin-vite/demo/routes/tests/remote_island.tsx b/packages/plugin-vite/demo/routes/tests/remote_island.tsx new file mode 100644 index 00000000..e6f766b7 --- /dev/null +++ b/packages/plugin-vite/demo/routes/tests/remote_island.tsx @@ -0,0 +1,5 @@ +import { RemoteIsland } from "@marvinh-test/fresh-island"; + +export default function Page() { + return ; +} diff --git a/packages/plugin-vite/deno.json b/packages/plugin-vite/deno.json index c2fb0761..810b0ad9 100644 --- a/packages/plugin-vite/deno.json +++ b/packages/plugin-vite/deno.json @@ -20,13 +20,15 @@ }, "imports": { "@babel/core": "npm:@babel/core@^7.28.0", + "@babel/preset-react": "npm:@babel/preset-react@^7.27.1", "@deno/loader": "jsr:@deno/loader@^0.3.2", "@prefresh/vite": "npm:@prefresh/vite@^2.4.8", "@types/babel__core": "npm:@types/babel__core@^7.20.5", "@types/node": "npm:@types/node@^24.1.0", "mime-db": "npm:mime-db@^1.54.0", "preact": "npm:preact@^10.26.9", - "vite": "npm:vite@^7.0.6", + "rollup-plugin-visualizer": "npm:rollup-plugin-visualizer@^6.0.3", + "vite": "npm:vite@^7.1.2", "vite-plugin-inspect": "npm:vite-plugin-inspect@^11.3.2" } } diff --git a/packages/plugin-vite/src/mod.ts b/packages/plugin-vite/src/mod.ts index b9936d96..5161579d 100644 --- a/packages/plugin-vite/src/mod.ts +++ b/packages/plugin-vite/src/mod.ts @@ -38,18 +38,15 @@ export function fresh(config?: FreshViteConfig): Plugin[] { "react-dom": "preact/compat", react: "preact/compat", }, + // Disallow externals, because it leads to duplicate + // modules with `preact` vs `npm:preact@*` in the server + // environment. + noExternal: true, }, optimizeDeps: { - include: [ - "preact", - "preact/compat", - "preact/debug", - "preact/devtools", - "preact/hooks", - "preact/jsx-runtime", - "preact/jsx-dev-runtime", - "@preact/signals", - ], + // Optimize deps somehow leads to duplicate modules or them + // being placed in the wrong chunks... + noDiscovery: true, }, publicDir: pathWithRoot("static", config.root), @@ -88,7 +85,7 @@ export function fresh(config?: FreshViteConfig): Plugin[] { }, ssr: { build: { - manifest: false, + manifest: true, copyPublicDir: false, outDir: config.environments?.ssr?.build?.outDir ?? @@ -108,8 +105,8 @@ export function fresh(config?: FreshViteConfig): Plugin[] { fConfig.routeDir = pathWithRoot(fConfig.routeDir, config.root); }, }, - patches(), serverEntryPlugin(fConfig), + patches(), ...serverSnapshot(fConfig), clientEntryPlugin(), clientSnapshot(fConfig), diff --git a/packages/plugin-vite/src/plugins/commonjs.ts b/packages/plugin-vite/src/plugins/commonjs.ts deleted file mode 100644 index 9a703b7b..00000000 --- a/packages/plugin-vite/src/plugins/commonjs.ts +++ /dev/null @@ -1,268 +0,0 @@ -import type { types } from "@babel/core"; - -export function cjsPlugin( - { types: t }: { types: typeof types }, -): babel.PluginObj { - const HAS_ES_MODULE = "esModule"; - const REQUIRE_CALLS = "requireCalls"; - const ROOT_SCOPE = "rootScope"; - - return { - name: "fresh-cjs-esm", - visitor: { - Program: { - enter(path, state) { - state.set(ROOT_SCOPE, path.scope); - }, - exit(path, state) { - const body = path.get("body"); - - const requires = state.get(REQUIRE_CALLS); - if (requires !== undefined) { - for (let i = 0; i < requires.length; i++) { - const { specifier, id } = requires[i]; - path.unshiftContainer( - "body", - t.importDeclaration( - [t.importNamespaceSpecifier(id)], - specifier, - ), - ); - } - } - - if (body.length === 0 && state.get(HAS_ES_MODULE)) { - path.pushContainer("body", t.exportNamedDeclaration(null)); - } else { - const seen = new Set(); - - const children = path.get("body"); - for (let i = children.length - 1; i >= 0; i--) { - const child = children[i]; - if (child.isExportNamedDeclaration()) { - if ( - t.isVariableDeclaration(child.node.declaration) && - child.node.declaration.declarations.length > 0 && - t.isIdentifier(child.node.declaration.declarations[0].id) - ) { - const name = child.node.declaration.declarations[0].id.name; - - if (seen.has(name)) { - child.remove(); - } else { - seen.add(name); - } - } - } - } - } - }, - }, - CallExpression(path, state) { - if (isObjEsModuleFlag(t, path.node)) { - state.set(HAS_ES_MODULE, true); - path.remove(); - return; - } - - if ( - t.isIdentifier(path.node.callee) && - path.node.callee.name === "require" - ) { - const root = state.get(ROOT_SCOPE); - const id = root.generateUidIdentifier("mod"); - - const mods = state.get(REQUIRE_CALLS) ?? []; - state.set(REQUIRE_CALLS, mods); - - mods.push({ - id, - specifier: t.cloneNode(path.node.arguments[0], true), - }); - - if ( - path.parentPath?.isVariableDeclarator() && - path.parentPath?.get("id").isIdentifier() || - path.parentPath?.isCallExpression() - ) { - path.replaceWith( - t.logicalExpression( - "??", - t.memberExpression( - t.cloneNode(id, true), - t.identifier("default"), - ), - t.cloneNode(id, true), - ), - ); - return; - } - - path.replaceWith(t.cloneNode(id, true)); - } - }, - ExpressionStatement(path, state) { - const expr = path.get("expression"); - if (expr.isAssignmentExpression()) { - const left = expr.get("left"); - - if (isEsModuleFlag(t, expr.node)) { - state.set(HAS_ES_MODULE, true); - path.remove(); - } else if (left.isMemberExpression()) { - if (isModuleExports(t, left.node)) { - const right = t.cloneNode(expr.node.right, true); - - path.replaceWith(t.exportDefaultDeclaration(right)); - } else { - const named = getExportsAssignName(t, left.node); - if (named === null) return; - - const right = t.cloneNode(expr.node.right, true); - - if (named === "default") { - path.replaceWith(t.exportDefaultDeclaration(right)); - } else { - path.scope.rename(named); - path.replaceWith( - t.exportNamedDeclaration( - t.variableDeclaration("let", [ - t.variableDeclarator(t.identifier(named), right), - ]), - ), - ); - } - } - } - } else if (expr.isCallExpression()) { - if ( - t.isMemberExpression(expr.node.callee) && - t.isIdentifier(expr.node.callee.object) && - expr.node.callee.object.name === "Object" && - t.isIdentifier(expr.node.callee.property) && - expr.node.callee.property.name === "defineProperty" && - expr.node.arguments.length === 3 && - t.isIdentifier(expr.node.arguments[0]) && - expr.node.arguments[0].name === "exports" && - t.isStringLiteral(expr.node.arguments[1]) && - expr.node.arguments[1].value !== "__esModule" && - t.isObjectExpression(expr.node.arguments[2]) - ) { - const named = expr.node.arguments[1].value; - const obj = expr.node.arguments[2]; - - let right: types.Expression = t.nullLiteral(); - - for (let i = 0; i < obj.properties.length; i++) { - const prop = obj.properties[i]; - - if (t.isObjectProperty(prop)) { - if (t.isIdentifier(prop.key)) { - if (prop.key.name === "get") { - if ( - t.isFunctionExpression(prop.value) || - t.isArrowFunctionExpression(prop.value) - ) { - right = t.callExpression( - t.parenthesizedExpression( - t.cloneNode(prop.value, true), - ), - [], - ); - } - } - } - } else if (t.isObjectMethod(prop)) { - if (t.isIdentifier(prop.key)) { - if (prop.key.name === "get") { - right = t.callExpression( - t.parenthesizedExpression( - t.functionExpression( - null, - [], - t.cloneNode(prop.body, true), - ), - ), - [], - ); - } - } - } - } - - path.replaceWith( - t.exportNamedDeclaration( - t.variableDeclaration("let", [ - t.variableDeclarator(t.identifier(named), right), - ]), - ), - ); - } - } - }, - }, - }; -} - -function isModuleExports( - t: typeof types, - node: types.MemberExpression, -): boolean { - return t.isIdentifier(node.object) && node.object.name === "module" && - t.isIdentifier(node.property) && node.property.name === "exports"; -} - -function getExportsAssignName( - t: typeof types, - node: types.MemberExpression, -): string | null { - if ( - (t.isMemberExpression(node.object) && - isModuleExports(t, node.object) || - t.isIdentifier(node.object) && node.object.name === "exports") && - t.isIdentifier(node.property) - ) { - return node.property.name; - } - - return null; -} - -/** - * Detect `exports.__esModule = true;` - */ -function isEsModuleFlag( - t: typeof types, - node: types.AssignmentExpression, -): boolean { - if (!t.isMemberExpression(node.left)) return false; - - const { left, right } = node; - return (t.isMemberExpression(left.object) && - isModuleExports(t, left.object) || - t.isIdentifier(left.object) && left.object.name === "exports") && - t.isIdentifier(left.property) && left.property.name === "__esModule" && - t.isBooleanLiteral(right); -} - -/** - * Check for `Object.defineProperty(exports, '__esModule', { value: true })` - */ -function isObjEsModuleFlag( - t: typeof types, - node: types.CallExpression, -): boolean { - return t.isMemberExpression(node.callee) && - t.isIdentifier(node.callee.object) && - node.callee.object.name === "Object" && - t.isIdentifier(node.callee.property) && - node.callee.property.name === "defineProperty" && - node.arguments.length === 3 && - (t.isMemberExpression(node.arguments[0]) && - isModuleExports(t, node.arguments[0]) || - t.isIdentifier(node.arguments[0]) && - node.arguments[0].name === "exports") && - t.isStringLiteral(node.arguments[1]) && - node.arguments[1].value === "__esModule" && - t.isObjectExpression(node.arguments[2]); -} diff --git a/packages/plugin-vite/src/plugins/deno.ts b/packages/plugin-vite/src/plugins/deno.ts index 871f2650..1c0476ec 100644 --- a/packages/plugin-vite/src/plugins/deno.ts +++ b/packages/plugin-vite/src/plugins/deno.ts @@ -6,34 +6,71 @@ import { Workspace, } from "@deno/loader"; import * as path from "@std/path"; +import * as babel from "@babel/core"; +import babelReact from "@babel/preset-react"; interface DenoState { type: RequestedModuleType; } export function deno(): Plugin { - let loader: Loader; + let ssrLoader: Loader; + let browserLoader: Loader; + + let isDev = false; return { name: "deno", + // We must be first to be able to resolve before the + // Vite's own`vite:resolve` plugin. It always treats bare + // specifiers as external during SSR. + enforce: "pre", + config(_, env) { + isDev = env.command === "serve"; + }, async configResolved() { // TODO: Pass conditions - loader = await new Workspace({}).createLoader(); + ssrLoader = await new Workspace({}).createLoader(); + browserLoader = await new Workspace({ + platform: "browser", + preserveJsx: true, + }) + .createLoader(); }, applyToEnvironment() { return true; }, async resolveId(id, importer, options) { - // Workaround until upstream PR is merged and released, - // see: https://github.com/vitejs/vite/pull/20558 - if (id.startsWith("deno-npm:")) { - id = id.slice("deno-".length); - } + const loader = options?.ssr ? ssrLoader : browserLoader; importer = isDenoSpecifier(importer) ? parseDenoSpecifier(importer).specifier : importer; + // We still want to allow other plugins to participate in + // resolution, with us being in front due to `enforce: "pre"`. + // But we still want to ignore everything `vite:resolve` does + // because we're kinda replacing that plugin here. + const tmp = await this.resolve(id, importer, options); + if (tmp && tmp.resolvedBy !== "vite:resolve") { + if (tmp.external) { + return tmp; + } + + // A plugin namespaced it, we should not attempt to resolve it. + if (tmp.id.startsWith("\0")) { + return tmp; + } + + id = tmp.id; + } + + // Plugins may return lower cased drive letters on windows + if (path.isAbsolute(id)) { + id = path.toFileUrl(path.normalize(id)) + .href; + } + try { // Ensure we're passing a valid importer that Deno understands const denoImporter = importer && !importer.startsWith("\0") @@ -47,15 +84,13 @@ export function deno(): Plugin { ); if (id === resolved) { - return id; + return null; } const type = getDenoType(id, options.attributes.type ?? "default"); if ( type !== RequestedModuleType.Default || - /^(https?|jsr|npm):/.test(resolved) || - // Vite does weird things to windows file paths - Deno.build.os === "windows" + /^(https?|jsr|npm):/.test(resolved) ) { return toDenoSpecifier(resolved, type); } @@ -76,7 +111,9 @@ export function deno(): Plugin { // ignore } }, - async load(id) { + async load(id, options) { + const loader = options?.ssr ? ssrLoader : browserLoader; + if (isDenoSpecifier(id)) { const { type, specifier } = parseDenoSpecifier(id); @@ -85,8 +122,20 @@ export function deno(): Plugin { return null; } + const code = new TextDecoder().decode(result.code); + + const maybeJsx = maybeTransformJsxBrowser({ + ssr: !!options?.ssr, + code, + id: specifier, + isDev, + }); + if (maybeJsx !== null) { + return maybeJsx; + } + return { - code: new TextDecoder().decode(result.code), + code, }; } @@ -108,8 +157,20 @@ export function deno(): Plugin { return null; } + const code = new TextDecoder().decode(result.code); + + const maybeJsx = maybeTransformJsxBrowser({ + ssr: !!options?.ssr, + id, + code, + isDev, + }); + if (maybeJsx) { + return maybeJsx; + } + return { - code: new TextDecoder().decode(result.code), + code, }; }, async transform(_, id, options) { @@ -128,12 +189,15 @@ export function deno(): Plugin { actualId = path.toFileUrl(actualId).href; } - const resolved = await loader.resolve( + const resolved = await ssrLoader.resolve( actualId, undefined, ResolutionMode.Import, ); - const result = await loader.load(resolved, RequestedModuleType.Default); + const result = await ssrLoader.load( + resolved, + RequestedModuleType.Default, + ); if (result.kind === "external") { return; } @@ -176,6 +240,10 @@ function parseDenoSpecifier( specifier = `${protocol}://${rest}`; } + if (path.isAbsolute(specifier)) { + specifier = path.toFileUrl(specifier).href; + } + return { type: +match[1], specifier }; } @@ -194,3 +262,33 @@ function getDenoType(id: string, type: string): RequestedModuleType { return RequestedModuleType.Default; } } + +function maybeTransformJsxBrowser( + options: { ssr: boolean; code: string; id: string; isDev: boolean }, +) { + const { ssr, code, id, isDev } = options; + + if ( + !ssr && id.endsWith(".tsx") || id.endsWith(".jsx") + ) { + const babelResult = babel.transformSync(code, { + filename: id, + babelrc: false, + sourceMaps: "inline", + presets: [[babelReact, { + runtime: "automatic", + importSource: "preact", + development: isDev, + }]], + }); + + if (babelResult !== null && babelResult.code) { + return { + code: babelResult.code, + map: babelResult.map, + }; + } + } + + return null; +} diff --git a/packages/plugin-vite/src/plugins/npm_workaround.ts b/packages/plugin-vite/src/plugins/npm_workaround.ts deleted file mode 100644 index d24f21dc..00000000 --- a/packages/plugin-vite/src/plugins/npm_workaround.ts +++ /dev/null @@ -1,39 +0,0 @@ -import type { types } from "@babel/core"; - -// Workaround until upstream PR is merged and released, -// see: https://github.com/vitejs/vite/pull/20558 -export function npmWorkaround( - { types: t }: { types: typeof types }, -): babel.PluginObj { - return { - name: "fresh-npm-workaround", - visitor: { - ImportDeclaration(path) { - const source = path.node.source; - if (source.value.startsWith("npm:")) { - source.value = `deno-${source.value}`; - } - }, - ExportNamedDeclaration(path) { - const source = path.node.source; - if (source === null || source === undefined) return; - - if (source.value.startsWith("npm:")) { - source.value = `deno-${source.value}`; - } - }, - CallExpression(path) { - if (!t.isImport(path.node.callee)) return; - if (path.node.arguments.length < 1) return; - - const source = path.node.arguments[0]; - if (t.isStringLiteral(source)) { - if (source.value.startsWith("npm:")) { - source.value = `deno-${source.value}`; - return; - } - } - }, - }, - }; -} diff --git a/packages/plugin-vite/src/plugins/npm_workaround_test.ts b/packages/plugin-vite/src/plugins/npm_workaround_test.ts deleted file mode 100644 index 0eda7f73..00000000 --- a/packages/plugin-vite/src/plugins/npm_workaround_test.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { expect } from "@std/expect/expect"; -import * as babel from "@babel/core"; -import { npmWorkaround } from "./npm_workaround.ts"; - -function runTest(options: { input: string; expected: string }) { - const res = babel.transformSync(options.input, { - filename: "foo.js", - babelrc: false, - plugins: [npmWorkaround], - }); - - const output = res?.code ?? ""; - expect(output).toEqual(options.expected); -} - -Deno.test("npm workaround - import declaration", () => { - runTest({ - input: `import * as foo from "npm:foo"; -import foo2 from "npm:foo"; -import { bar } from "npm:foo"; -import baz, { bar2 } from "npm:foo";`, - expected: `import * as foo from "deno-npm:foo"; -import foo2 from "deno-npm:foo"; -import { bar } from "deno-npm:foo"; -import baz, { bar2 } from "deno-npm:foo";`, - }); -}); - -Deno.test("npm workaround - export declaration", () => { - runTest({ - input: `export * as foo from "npm:foo"; -export { bar } from "npm:foo";`, - expected: `export * as foo from "deno-npm:foo"; -export { bar } from "deno-npm:foo";`, - }); -}); - -Deno.test("npm workaround - import()", () => { - runTest({ - input: `import("npm:foo"); -import("npm:foo", { type: "json" })`, - expected: `import("deno-npm:foo"); -import("deno-npm:foo", { - type: "json" -});`, - }); -}); diff --git a/packages/plugin-vite/src/plugins/patches.ts b/packages/plugin-vite/src/plugins/patches.ts index 9212bbec..734cccc8 100644 --- a/packages/plugin-vite/src/plugins/patches.ts +++ b/packages/plugin-vite/src/plugins/patches.ts @@ -1,18 +1,39 @@ import type { Plugin } from "vite"; import * as babel from "@babel/core"; -import { npmWorkaround } from "./npm_workaround.ts"; +import { cjsPlugin } from "./patches/commonjs.ts"; +import { jsxComments } from "./patches/jsx_comment.ts"; +import babelReact from "@babel/preset-react"; export function patches(): Plugin { + let isDev = false; + return { name: "fresh:patches", + config(_, env) { + isDev = env.command === "serve"; + }, applyToEnvironment() { return true; }, - transform(code, id) { + transform(code, id, options) { + if (code.includes("__commonJS") || /\.(tsx?|m[jt]s)$/.test(id)) { + return; + } + + const presets = []; + if (!options?.ssr) { + presets.push([babelReact, { + runtime: "automatic", + importSource: "preact", + development: isDev, + }]); + } + const res = babel.transformSync(code, { filename: id, babelrc: false, - plugins: [npmWorkaround], + plugins: [cjsPlugin, jsxComments], + presets, }); if (res?.code) { diff --git a/packages/plugin-vite/src/plugins/patches/commonjs.ts b/packages/plugin-vite/src/plugins/patches/commonjs.ts new file mode 100644 index 00000000..d76e4b39 --- /dev/null +++ b/packages/plugin-vite/src/plugins/patches/commonjs.ts @@ -0,0 +1,340 @@ +import type { types } from "@babel/core"; + +export function cjsPlugin( + { types: t }: { types: typeof types }, +): babel.PluginObj { + const HAS_ES_MODULE = "esModule"; + const REQUIRE_CALLS = "requireCalls"; + const ROOT_SCOPE = "rootScope"; + const EXPORTED = "exported"; + + return { + name: "fresh-cjs-esm", + visitor: { + Program: { + enter(path, state) { + state.set(ROOT_SCOPE, path.scope); + state.set(EXPORTED, new Set()); + }, + exit(path, state) { + const body = path.get("body"); + const requires = state.get(REQUIRE_CALLS); + if (requires !== undefined) { + for (let i = 0; i < requires.length; i++) { + const { specifier, id } = requires[i]; + path.unshiftContainer( + "body", + t.importDeclaration( + [t.importNamespaceSpecifier(id)], + specifier, + ), + ); + } + } + + const exported = state.get(EXPORTED); + if (exported.size > 0) { + path.unshiftContainer( + "body", + t.variableDeclaration("var", [ + t.variableDeclarator( + t.identifier("exports"), + t.objectExpression([]), + ), + t.variableDeclarator( + t.identifier("module"), + t.objectExpression([ + t.objectProperty( + t.identifier("exports"), + t.identifier("exports"), + false, + true, + ), + ]), + ), + ]), + ); + } + + const exportNamed = new Map(); + + const idExports: types.ExportSpecifier[] = []; + for (const name of exported) { + if (name === "default") { + exportNamed.set(name, name); + continue; + } + + const id = path.scope.generateUidIdentifier(name); + + exportNamed.set(id.name, name); + + path.pushContainer( + "body", + t.variableDeclaration( + "var", + [t.variableDeclarator( + id, + t.memberExpression( + t.identifier("exports"), + t.identifier(name), + ), + )], + ), + ); + idExports.push( + t.exportSpecifier(id, t.identifier(name)), + ); + } + + if (idExports.length > 0) { + path.pushContainer( + "body", + t.exportNamedDeclaration(null, idExports), + ); + } + + if (exportNamed.size > 0) { + const id = path.scope.generateUidIdentifier("__default"); + + path.pushContainer( + "body", + t.variableDeclaration("const", [ + t.variableDeclarator( + id, + t.logicalExpression( + "??", + t.logicalExpression( + "??", + t.memberExpression( + t.memberExpression( + t.identifier("module"), + t.identifier("exports"), + ), + t.identifier("default"), + ), + t.memberExpression( + t.identifier("exports"), + t.identifier("default"), + ), + ), + t.memberExpression( + t.identifier("module"), + t.identifier("exports"), + ), + ), + ), + ]), + ); + + for (const [local, exported] of exportNamed.entries()) { + if (exported === "default") continue; + + path.pushContainer( + "body", + t.expressionStatement( + t.assignmentExpression( + "=", + t.memberExpression(id, t.identifier(exported)), + t.identifier(local), + ), + ), + ); + } + + path.pushContainer("body", t.exportDefaultDeclaration(id)); + } + + if (body.length === 0 && state.get(HAS_ES_MODULE)) { + path.pushContainer("body", t.exportNamedDeclaration(null)); + } + }, + }, + CallExpression(path, state) { + const exported = state.get(EXPORTED); + + if (isObjEsModuleFlag(t, path.node)) { + state.set(HAS_ES_MODULE, true); + path.remove(); + return; + } + if ( + t.isIdentifier(path.node.callee) && + path.node.callee.name === "require" + ) { + const root = state.get(ROOT_SCOPE); + const id = root.generateUidIdentifier("mod"); + + const mods = state.get(REQUIRE_CALLS) ?? []; + state.set(REQUIRE_CALLS, mods); + + mods.push({ + id, + specifier: t.cloneNode(path.node.arguments[0], true), + }); + + if ( + path.parentPath?.isVariableDeclarator() && + path.parentPath?.get("id").isIdentifier() || + path.parentPath?.isCallExpression() + ) { + path.replaceWith( + t.logicalExpression( + "??", + t.memberExpression( + t.cloneNode(id, true), + t.identifier("default"), + ), + t.cloneNode(id, true), + ), + ); + return; + } + + path.replaceWith(t.cloneNode(id, true)); + } else if ( + t.isMemberExpression(path.node.callee) && + t.isIdentifier(path.node.callee.object) && + path.node.callee.object.name === "Object" && + t.isIdentifier(path.node.callee.property) && + path.node.callee.property.name === "defineProperty" && + path.node.arguments.length > 0 && + t.isIdentifier(path.node.arguments[0]) && + path.node.arguments[0].name === "exports" && + t.isStringLiteral(path.node.arguments[1]) + ) { + const name = path.node.arguments[1].value; + exported.add(name); + } + }, + EmptyStatement(path) { + path.remove(); + }, + MemberExpression: { + exit(path, state) { + if ( + t.isIdentifier(path.node.object) && + path.node.object.name === "exports" && + t.isIdentifier(path.node.property) + ) { + const name = t.cloneNode(path.node.property); + + if (name.name === "__esModule") return; + + state.get(EXPORTED).add(name.name); + } + }, + }, + ExpressionStatement: { + enter(path) { + if ( + t.isExpressionStatement(path.node) && + t.isCallExpression(path.node.expression) && + t.isIdentifier(path.node.expression.callee) && + path.node.expression.callee.name === "__exportStar" && + path.node.expression.arguments.length > 0 && + t.isCallExpression(path.node.expression.arguments[0]) && + t.isIdentifier(path.node.expression.arguments[0].callee) && + path.node.expression.arguments[0].callee.name === "require" && + t.isStringLiteral(path.node.expression.arguments[0].arguments[0]) + ) { + const spec = t.cloneNode( + path.node.expression.arguments[0].arguments[0], + true, + ); + path.replaceWith(t.exportAllDeclaration(spec)); + } + }, + exit(path, state) { + const exported = state.get(EXPORTED); + const expr = path.get("expression"); + + if (expr.isAssignmentExpression()) { + const left = expr.get("left"); + + if (isEsModuleFlag(t, expr.node)) { + state.set(HAS_ES_MODULE, true); + path.remove(); + } else if (left.isMemberExpression()) { + if (isModuleExports(t, left.node)) { + exported.add("default"); + } else { + const named = getExportsAssignName(t, left.node); + if (named === null) return; + exported.add(named); + } + } + } else if (expr.isCallExpression()) { + if (isObjEsModuleFlag(t, expr.node)) { + state.set(HAS_ES_MODULE, true); + path.remove(); + } + } + }, + }, + }, + }; +} + +function isModuleExports( + t: typeof types, + node: types.MemberExpression, +): boolean { + return t.isIdentifier(node.object) && node.object.name === "module" && + t.isIdentifier(node.property) && node.property.name === "exports"; +} + +function getExportsAssignName( + t: typeof types, + node: types.MemberExpression, +): string | null { + if ( + (t.isMemberExpression(node.object) && + isModuleExports(t, node.object) || + t.isIdentifier(node.object) && node.object.name === "exports") && + t.isIdentifier(node.property) + ) { + return node.property.name; + } + + return null; +} + +/** + * Detect `exports.__esModule = true;` + */ +function isEsModuleFlag( + t: typeof types, + node: types.AssignmentExpression, +): boolean { + if (!t.isMemberExpression(node.left)) return false; + + const { left, right } = node; + return (t.isMemberExpression(left.object) && + isModuleExports(t, left.object) || + t.isIdentifier(left.object) && left.object.name === "exports") && + t.isIdentifier(left.property) && left.property.name === "__esModule" && + t.isBooleanLiteral(right); +} + +/** + * Check for `Object.defineProperty(exports, '__esModule', { value: true })` + */ +function isObjEsModuleFlag( + t: typeof types, + node: types.CallExpression, +): boolean { + return t.isMemberExpression(node.callee) && + t.isIdentifier(node.callee.object) && + node.callee.object.name === "Object" && + t.isIdentifier(node.callee.property) && + node.callee.property.name === "defineProperty" && + node.arguments.length === 3 && + (t.isMemberExpression(node.arguments[0]) && + isModuleExports(t, node.arguments[0]) || + t.isIdentifier(node.arguments[0]) && + node.arguments[0].name === "exports") && + t.isStringLiteral(node.arguments[1]) && + node.arguments[1].value === "__esModule" && + t.isObjectExpression(node.arguments[2]); +} diff --git a/packages/plugin-vite/src/plugins/commonjs_test.ts b/packages/plugin-vite/src/plugins/patches/commonjs_test.ts similarity index 50% rename from packages/plugin-vite/src/plugins/commonjs_test.ts rename to packages/plugin-vite/src/plugins/patches/commonjs_test.ts index 62e1d345..2cd0d057 100644 --- a/packages/plugin-vite/src/plugins/commonjs_test.ts +++ b/packages/plugin-vite/src/plugins/patches/commonjs_test.ts @@ -1,6 +1,6 @@ import { expect } from "@std/expect/expect"; import * as babel from "@babel/core"; -import { cjsPlugin } from "./commonjs.ts"; +import { cjsPlugin } from "../patches/commonjs.ts"; function runTest(options: { input: string; expected: string }) { const res = babel.transformSync(options.input, { @@ -13,17 +13,32 @@ function runTest(options: { input: string; expected: string }) { expect(output).toEqual(options.expected); } +const INIT = `var exports = {}, + module = { + exports + };`; + +const DEFAULT_EXPORT = + `const _default = module.exports.default ?? exports.default ?? module.exports;`; +const DEFAULT_EXPORT_END = `export default _default;`; + Deno.test("commonjs - module.exports default", () => { runTest({ input: `module.exports = async function () {};`, - expected: "export default (async function () {});", + expected: `${INIT} +module.exports = async function () {}; +${DEFAULT_EXPORT} +${DEFAULT_EXPORT_END}`, }); }); Deno.test("commonjs - module.exports default primitive", () => { runTest({ input: `module.exports = 42;`, - expected: "export default 42;", + expected: `${INIT} +module.exports = 42; +${DEFAULT_EXPORT} +${DEFAULT_EXPORT_END}`, }); }); @@ -32,8 +47,14 @@ Deno.test("commonjs - exports with default + named", () => { input: `exports.__esModule = true; exports.default = 'x'; exports.foo = 'foo';`, - expected: `export default 'x'; -export let foo = 'foo';`, + expected: `${INIT} +exports.default = 'x'; +exports.foo = 'foo'; +var _foo = exports.foo; +export { _foo as foo }; +${DEFAULT_EXPORT} +_default.foo = _foo; +${DEFAULT_EXPORT_END}`, }); }); @@ -42,8 +63,14 @@ Deno.test("commonjs - module.exports with default + named", () => { input: `module.exports.__esModule = true; module.exports.default = 'x'; module.exports.foo = 'foo';`, - expected: `export default 'x'; -export let foo = 'foo';`, + expected: `${INIT} +module.exports.default = 'x'; +module.exports.foo = 'foo'; +var _foo = exports.foo; +export { _foo as foo }; +${DEFAULT_EXPORT} +_default.foo = _foo; +${DEFAULT_EXPORT_END}`, }); }); @@ -53,8 +80,14 @@ Deno.test("commonjs - Object es module flag with named clash", () => { exports.foo = 'bar'; const foo = 'also bar'; `, - expected: `export let foo = 'bar'; -const _foo = 'also bar';`, + expected: `${INIT} +exports.foo = 'bar'; +const foo = 'also bar'; +var _foo = exports.foo; +export { _foo as foo }; +${DEFAULT_EXPORT} +_default.foo = _foo; +${DEFAULT_EXPORT_END}`, }); }); @@ -64,8 +97,14 @@ Deno.test("commonjs - Object es module flag with named + default", () => { exports.default = 'foo'; exports.foo = 'bar'; `, - expected: `export default 'foo'; -export let foo = 'bar';`, + expected: `${INIT} +exports.default = 'foo'; +exports.foo = 'bar'; +var _foo = exports.foo; +export { _foo as foo }; +${DEFAULT_EXPORT} +_default.foo = _foo; +${DEFAULT_EXPORT_END}`, }); }); @@ -97,8 +136,16 @@ Deno.test("commonjs - exports only named", () => { exports.foo = 'bar'; exports.bar = 'foo'; `, - expected: `export let foo = 'bar'; -export let bar = 'foo';`, + expected: `${INIT} +exports.foo = 'bar'; +exports.bar = 'foo'; +var _foo = exports.foo; +var _bar = exports.bar; +export { _foo as foo, _bar as bar }; +${DEFAULT_EXPORT} +_default.foo = _foo; +_default.bar = _bar; +${DEFAULT_EXPORT_END}`, }); }); @@ -170,7 +217,14 @@ Deno.test("commonjs - duplicate exports", () => { input: `Object.defineProperty(exports, "__esModule", { value: true }); exports.trace = void 0; exports.trace = 'foo'`, - expected: `export let trace = 'foo';`, + expected: `${INIT} +exports.trace = void 0; +exports.trace = 'foo'; +var _trace = exports.trace; +export { _trace as trace }; +${DEFAULT_EXPORT} +_default.trace = _trace; +${DEFAULT_EXPORT_END}`, }); }); @@ -179,7 +233,16 @@ Deno.test("commonjs - cleared exports", () => { input: `Object.defineProperty(exports, "__esModule", { value: true }); exports.foo = exports.bar = void 0; exports.foo = 'foo'`, - expected: `export let foo = 'foo';`, + expected: `${INIT} +exports.foo = exports.bar = void 0; +exports.foo = 'foo'; +var _foo = exports.foo; +var _bar = exports.bar; +export { _foo as foo, _bar as bar }; +${DEFAULT_EXPORT} +_default.foo = _foo; +_default.bar = _bar; +${DEFAULT_EXPORT_END}`, }); }); @@ -187,11 +250,20 @@ Deno.test("commonjs - define exports", () => { runTest({ input: `var utils_1 = require("./bar"); Object.defineProperty(exports, "foo", { enumerable: true, get: function () { return utils_1.foo; } });`, - expected: `import * as _mod from "./bar"; + expected: `${INIT} +import * as _mod from "./bar"; var utils_1 = _mod.default ?? _mod; -export let foo = (function () { - return utils_1.foo; -})();`, +Object.defineProperty(exports, "foo", { + enumerable: true, + get: function () { + return utils_1.foo; + } +}); +var _foo = exports.foo; +export { _foo as foo }; +${DEFAULT_EXPORT} +_default.foo = _foo; +${DEFAULT_EXPORT_END}`, }); }); @@ -199,11 +271,20 @@ Deno.test("commonjs - define exports #2", () => { runTest({ input: `var utils_1 = require("./bar"); Object.defineProperty(exports, "foo", { enumerable: true, get() { return utils_1.foo; } });`, - expected: `import * as _mod from "./bar"; + expected: `${INIT} +import * as _mod from "./bar"; var utils_1 = _mod.default ?? _mod; -export let foo = (function () { - return utils_1.foo; -})();`, +Object.defineProperty(exports, "foo", { + enumerable: true, + get() { + return utils_1.foo; + } +}); +var _foo = exports.foo; +export { _foo as foo }; +${DEFAULT_EXPORT} +_default.foo = _foo; +${DEFAULT_EXPORT_END}`, }); }); @@ -212,17 +293,99 @@ Deno.test("commonjs - define exports #3", () => { input: `Object.defineProperty(exports, "__esModule", { value: true }); exports._globalThis = void 0; exports._globalThis = typeof globalThis === 'object' ? globalThis : global;`, - expected: - `export let _globalThis = typeof globalThis === 'object' ? globalThis : global;`, + expected: `${INIT} +exports._globalThis = void 0; +exports._globalThis = typeof globalThis === 'object' ? globalThis : global; +var _globalThis = exports._globalThis; +export { _globalThis }; +${DEFAULT_EXPORT} +_default._globalThis = _globalThis; +${DEFAULT_EXPORT_END}`, }); }); -// I've never seen this, seems rare. Skipping for now. -Deno.test.ignore("commonjs - require", () => { +Deno.test("commonjs - named function", () => { + runTest({ + input: `Object.defineProperty(exports, "__esModule", { value: true }); +function foo() {}; +exports.foo = foo;`, + expected: `${INIT} +function foo() {} +exports.foo = foo; +var _foo = exports.foo; +export { _foo as foo }; +${DEFAULT_EXPORT} +_default.foo = _foo; +${DEFAULT_EXPORT_END}`, + }); +}); + +Deno.test("commonjs - detect esbuild shims", () => { + runTest({ + input: `__exportStar(require("./globalThis"), exports);`, + expected: `export * from "./globalThis";`, + }); +}); + +Deno.test("commonjs - exports.default", () => { + runTest({ + input: `exports.default = {}`, + expected: `${INIT} +exports.default = {}; +${DEFAULT_EXPORT} +${DEFAULT_EXPORT_END}`, + }); +}); + +Deno.test("commonjs - multiple same name", () => { + runTest({ + input: `exports.VERSION = void 0; +exports.VERSION = '1.9.0';`, + expected: `${INIT} +exports.VERSION = void 0; +exports.VERSION = '1.9.0'; +var _VERSION = exports.VERSION; +export { _VERSION as VERSION }; +${DEFAULT_EXPORT} +_default.VERSION = _VERSION; +${DEFAULT_EXPORT_END}`, + }); +}); + +Deno.test("commonjs - export enum", () => { + runTest({ + input: `Object.defineProperty(exports, "__esModule", { value: true }); +exports.DiagLogLevel = void 0; +var DiagLogLevel; +(function (DiagLogLevel) { + DiagLogLevel[DiagLogLevel["ALL"] = 9999] = "ALL"; +})(DiagLogLevel = exports.DiagLogLevel || (exports.DiagLogLevel = {}));`, + expected: `${INIT} +exports.DiagLogLevel = void 0; +var DiagLogLevel; +(function (DiagLogLevel) { + DiagLogLevel[DiagLogLevel["ALL"] = 9999] = "ALL"; +})(DiagLogLevel = exports.DiagLogLevel || (exports.DiagLogLevel = {})); +var _DiagLogLevel = exports.DiagLogLevel; +export { _DiagLogLevel as DiagLogLevel }; +${DEFAULT_EXPORT} +_default.DiagLogLevel = _DiagLogLevel; +${DEFAULT_EXPORT_END}`, + }); +}); + +Deno.test("commonjs - require", () => { runTest({ input: `module.exports = { __esModule: true, default: { foo: 'bar' }}`, - expected: `import * as foo from "tape"; -console.log(foo)`, + expected: `${INIT} +module.exports = { + __esModule: true, + default: { + foo: 'bar' + } +}; +${DEFAULT_EXPORT} +${DEFAULT_EXPORT_END}`, }); }); @@ -232,7 +395,14 @@ Deno.test.ignore("commonjs - export default object", () => { input: `Object.defineProperty(exports, '__esModule', { value: true }); module.exports = { foo: 'bar' }; `, - expected: `export let foo = 'bar'; -export let bar = 'foo';`, + expected: `${INIT} +module.exports = { + foo: 'bar' +}; +var _foo = module.exports.foo; +export let foo = _foo; +${DEFAULT_EXPORT} +_default.foo = _foo; +${DEFAULT_EXPORT_END}`, }); }); diff --git a/packages/plugin-vite/src/plugins/patches/jsx_comment.ts b/packages/plugin-vite/src/plugins/patches/jsx_comment.ts new file mode 100644 index 00000000..5034246d --- /dev/null +++ b/packages/plugin-vite/src/plugins/patches/jsx_comment.ts @@ -0,0 +1,19 @@ +import type { types } from "@babel/core"; + +export function jsxComments(): babel.PluginObj { + return { + name: "fresh-jsx-comment", + visitor: { + Program(path) { + const comments = (path.parent as types.File).comments; + if (comments) { + for (let i = 0; i < comments.length; i++) { + const comment = comments[i]; + + comment.value = comment.value.replaceAll(/@jsx\w+\s+[^\s*]+/g, ""); + } + } + }, + }, + }; +} diff --git a/packages/plugin-vite/src/plugins/patches/jsx_comment_test.ts b/packages/plugin-vite/src/plugins/patches/jsx_comment_test.ts new file mode 100644 index 00000000..1d99483e --- /dev/null +++ b/packages/plugin-vite/src/plugins/patches/jsx_comment_test.ts @@ -0,0 +1,22 @@ +import { expect } from "@std/expect/expect"; +import * as babel from "@babel/core"; +import { jsxComments } from "./jsx_comment.ts"; + +function runTest(options: { input: string; expected: string }) { + const res = babel.transformSync(options.input, { + filename: "foo.js", + babelrc: false, + plugins: [jsxComments], + }); + + const output = res?.code ?? ""; + expect(output).toEqual(options.expected); +} + +Deno.test("jsx comments - import declaration", () => { + runTest({ + input: + `/** @jsxRuntime classic */ /** @jsxImportSource npm:preact@^10.27.0 */ /** @jsxImportSourceTypes npm:preact@^10.27.0 */ /** @jsxFactory React.createElement */ /** @jsxFragmentFactory React.Fragment */ foo;`, + expected: `/** */ /** */ /** */ /** */ /** */foo;`, + }); +}); diff --git a/packages/plugin-vite/src/plugins/server_snapshot.ts b/packages/plugin-vite/src/plugins/server_snapshot.ts index 970410a2..90c95db0 100644 --- a/packages/plugin-vite/src/plugins/server_snapshot.ts +++ b/packages/plugin-vite/src/plugins/server_snapshot.ts @@ -172,6 +172,10 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] { fsRoutesFiles: result.routes, islands: islandMods, writeSpecifier: (file) => { + const def = islands.get(file); + if (def) { + return `fresh-island::${def.name}`; + } return path.toFileUrl(file).href; }, });