diff --git a/.changeset/bright-cherries-juggle.md b/.changeset/bright-cherries-juggle.md new file mode 100644 index 0000000000..bc9fc61340 --- /dev/null +++ b/.changeset/bright-cherries-juggle.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix for using the snowpack polyfillNode option diff --git a/package.json b/package.json index b6a6cec00d..b68d1596bd 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "www", "docs-www", "packages/astro/test/fixtures/builtins/packages/*", + "packages/astro/test/fixtures/builtins-polyfillnode", "packages/astro/test/fixtures/custom-elements/my-component-lib" ], "volta": { diff --git a/packages/astro/src/runtime.ts b/packages/astro/src/runtime.ts index 53a5fa8bb0..a12f5d4fed 100644 --- a/packages/astro/src/runtime.ts +++ b/packages/astro/src/runtime.ts @@ -418,12 +418,14 @@ async function createSnowpack(astroConfig: AstroConfig, options: CreateSnowpackO packageOptions: { knownEntrypoints, external, - }, - alias: { - ...Object.fromEntries(nodeBuiltinsMap), - }, + } }); + const polyfillNode = (snowpackConfig.packageOptions as any).polyfillNode as boolean; + if(!polyfillNode) { + snowpackConfig.alias = Object.fromEntries(nodeBuiltinsMap); + } + snowpack = await startSnowpackServer( { config: snowpackConfig, diff --git a/packages/astro/test/builtins-polyfillnode.test.js b/packages/astro/test/builtins-polyfillnode.test.js new file mode 100644 index 0000000000..8fef5e8e9f --- /dev/null +++ b/packages/astro/test/builtins-polyfillnode.test.js @@ -0,0 +1,19 @@ +import { suite } from 'uvu'; +import * as assert from 'uvu/assert'; +import { doc } from './test-utils.js'; +import { setup } from './helpers.js'; + +const Builtins = suite('Node builtins with polyfillNode option'); + +setup(Builtins, './fixtures/builtins-polyfillnode'); + +Builtins('Doesnt alias to node: prefix', async ({ runtime }) => { + const result = await runtime.load('/'); + if (result.error) throw new Error(result.error); + + const $ = doc(result.contents); + + assert.match($('#url').text(), new RegExp('unicorn.jpg')); +}); + +Builtins.run(); diff --git a/packages/astro/test/fixtures/builtins-polyfillnode/package.json b/packages/astro/test/fixtures/builtins-polyfillnode/package.json new file mode 100644 index 0000000000..1c13826191 --- /dev/null +++ b/packages/astro/test/fixtures/builtins-polyfillnode/package.json @@ -0,0 +1,7 @@ +{ + "name": "@astrojs/astro-test-builtins-polyfillnode", + "version": "1.2.0", + "dependencies": { + "file-url": "4.0.0" + } +} \ No newline at end of file diff --git a/packages/astro/test/fixtures/builtins-polyfillnode/snowpack.config.json b/packages/astro/test/fixtures/builtins-polyfillnode/snowpack.config.json new file mode 100644 index 0000000000..3c503c831e --- /dev/null +++ b/packages/astro/test/fixtures/builtins-polyfillnode/snowpack.config.json @@ -0,0 +1,6 @@ +{ + "workspaceRoot": "../../../../../", + "packageOptions": { + "polyfillNode": false + } +} diff --git a/packages/astro/test/fixtures/builtins-polyfillnode/src/pages/index.astro b/packages/astro/test/fixtures/builtins-polyfillnode/src/pages/index.astro new file mode 100644 index 0000000000..8286f1ed16 --- /dev/null +++ b/packages/astro/test/fixtures/builtins-polyfillnode/src/pages/index.astro @@ -0,0 +1,12 @@ +--- +import fileUrl from 'file-url'; + +const r = fileUrl('unicorn.jpg'); +--- + + +Testing + +
{r}
+ + \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 5cd35649d7..e29b9886de 100644 --- a/yarn.lock +++ b/yarn.lock @@ -62,6 +62,9 @@ time-require "^0.1.2" valid-url "^1.0.9" +"@astrojs/astro-test-builtins-dep2@file:./packages/astro/test/fixtures/builtins-polyfillnode/packages/dep": + version "0.0.1" + "@babel/code-frame@7.12.11": version "7.12.11" resolved "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.12.11.tgz" @@ -4143,6 +4146,11 @@ file-entry-cache@^6.0.1: dependencies: flat-cache "^3.0.4" +file-url@4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/file-url/-/file-url-4.0.0.tgz#6fe05262d3187da70bc69889091932b6bc7df270" + integrity sha512-vRCdScQ6j3Ku6Kd7W1kZk9c++5SqD6Xz5Jotrjr/nkY714M14RFHy/AAVA2WQvpsqVAVgTbDrYyBpU205F0cLw== + fill-range@^7.0.1: version "7.0.1" resolved "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz"