summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2021-11-17 14:25:45 -0600
committerGravatar GitHub <noreply@github.com> 2021-11-17 14:25:45 -0600
commit06446d14a3a48e4c4bd3525949faf264102d1531 (patch)
tree87e96f847239e8bcc02cb821185ffe824bb15f21
parentae553a145d9bbd0d24074dcf56451770e0ad2899 (diff)
downloadastro-06446d14a3a48e4c4bd3525949faf264102d1531.tar.gz
astro-06446d14a3a48e4c4bd3525949faf264102d1531.tar.zst
astro-06446d14a3a48e4c4bd3525949faf264102d1531.zip
fix: enable node builtins (#1771)
* fix: enable node builtins * fix: update bare node builtin test * test: update fixture to support node@12
-rw-r--r--packages/astro/src/core/create-vite.ts2
-rw-r--r--packages/astro/test/builtins.test.js15
-rw-r--r--packages/astro/test/fixtures/builtins/src/components/Version.astro4
-rw-r--r--packages/astro/test/fixtures/builtins/src/pages/bare.astro6
4 files changed, 12 insertions, 15 deletions
diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts
index c250e8ca5..613403e8d 100644
--- a/packages/astro/src/core/create-vite.ts
+++ b/packages/astro/src/core/create-vite.ts
@@ -2,6 +2,7 @@ import type { AstroConfig } from '../@types/astro';
import type { AstroDevServer } from './dev';
import type { LogOptions } from './logger';
+import { builtinModules } from 'module';
import { fileURLToPath } from 'url';
import vite from './vite.js';
import astroVitePlugin from '../vite-plugin-astro/index.js';
@@ -14,6 +15,7 @@ import { resolveDependency } from './util.js';
// Some packages are just external, and that’s the way it goes.
const ALWAYS_EXTERNAL = new Set([
+ ...builtinModules.map(name => `node:${name}`),
'@sveltejs/vite-plugin-svelte',
'estree-util-value-to-estree',
'micromark-util-events-to-acorn',
diff --git a/packages/astro/test/builtins.test.js b/packages/astro/test/builtins.test.js
index 299c688b9..5c9a43193 100644
--- a/packages/astro/test/builtins.test.js
+++ b/packages/astro/test/builtins.test.js
@@ -1,5 +1,3 @@
-/**
- * UNCOMMENT: Fix "Unexpected "\x00" bug
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
@@ -11,7 +9,6 @@ before(async () => {
await fixture.build();
});
-// TODO: find a way to build one file at-a-time (different fixtures?)
describe('Node builtins', () => {
it('Can be used with the node: prefix', async () => {
// node:fs/promise is not supported in Node v12. Test currently throws.
@@ -25,12 +22,10 @@ describe('Node builtins', () => {
expect($('#dep-version').text()).to.equal('0.0.1');
});
- it('Throw if using the non-prefixed version', async () => {
- const result = await fixture.readFile('/bare/index.html');
- expect(result.status).to.equal(500);
- expect(result.body).to.include('Use node:fs instead');
+ it('Can also be used with the non-prefixed version', async () => {
+ const html = await fixture.readFile('/bare/index.html');
+ const $ = cheerio.load(html);
+
+ expect($('h1').text()).to.equal('true');
});
});
-*/
-
-it.skip('is skipped', () => {});
diff --git a/packages/astro/test/fixtures/builtins/src/components/Version.astro b/packages/astro/test/fixtures/builtins/src/components/Version.astro
index 2f37ba69f..0a27496ab 100644
--- a/packages/astro/test/fixtures/builtins/src/components/Version.astro
+++ b/packages/astro/test/fixtures/builtins/src/components/Version.astro
@@ -1,9 +1,9 @@
---
-import fs from 'node:fs/promises';
+import { promises as fs } from 'node:fs';
const url = new URL('../../package.json', import.meta.url);
const json = await fs.readFile(url, 'utf-8');
const data = JSON.parse(json);
---
-<span id="version">{data.version}</span> \ No newline at end of file
+<span id="version">{data.version}</span>
diff --git a/packages/astro/test/fixtures/builtins/src/pages/bare.astro b/packages/astro/test/fixtures/builtins/src/pages/bare.astro
index 2c298ce21..a8e197c4b 100644
--- a/packages/astro/test/fixtures/builtins/src/pages/bare.astro
+++ b/packages/astro/test/fixtures/builtins/src/pages/bare.astro
@@ -1,12 +1,12 @@
---
-import fs from 'fs';
+import { builtinModules } from 'module';
---
<html>
<head>
-<title>This should throw</title>
+<title>Bare node import</title>
</head>
<body>
-<h1>Test</h1>
+<h1>{Array.isArray(builtinModules)}</h1>
</body>
</html>