44
55// ISOMORPHIC FILE: NO TOP-LEVEL IMPORT/REQUIRE() ALLOWED
66// This file has to run as both ESM and CJS on older Node.js versions
7- // Assume ESM to start, and then call `require()` below once CJS is confirmed.
87// Needed for Stackblitz: https://github.com/stackblitz/webcontainer-core/issues/281
98
109const CI_INSTRUCTIONS = {
@@ -14,64 +13,41 @@ const CI_INSTRUCTIONS = {
1413 VERCEL : 'https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-version' ,
1514} ;
1615
16+ // Hardcode supported Node.js version so we don't have to read differently in CJS & ESM.
17+ const engines = '>=16.12.0' ;
18+ const skipSemverCheckIfAbove = 16 ;
19+
1720/** `astro *` */
1821async function main ( ) {
19- // Check for ESM support.
20- // Load the "supports-esm" package in an way that works in both ESM & CJS.
21- let supportsESM =
22- typeof require !== 'undefined'
23- ? require ( 'supports-esm' )
24- : ( await import ( 'supports-esm' ) ) . default ;
25-
26- // Check for CJS->ESM named export support.
27- // "path-to-regexp" is a real-world package that we depend on, that only
28- // works in later versions of Node with advanced CJS->ESM support.
29- // If `import {compile} from 'path-to-regexp'` will fail, we need to know.
30- if ( supportsESM ) {
31- const testNamedExportsModule = await import ( 'path-to-regexp' ) ;
32- supportsESM = ! ! testNamedExportsModule . compile ;
33- }
34-
35- // Preflight check complete. Enjoy! ✨
36- if ( supportsESM ) {
37- return import ( './dist/cli/index.js' )
38- . then ( ( { cli } ) => cli ( process . argv ) )
39- . catch ( ( error ) => {
40- console . error ( error ) ;
41- process . exit ( 1 ) ;
42- } ) ;
43- }
44-
4522 const version = process . versions . node ;
46-
47- // Not supported (incomplete ESM): It's very difficult (impossible?) to load the
48- // dependencies below in an unknown module type. If `require` is undefined, then this file
49- // actually was run as ESM but one of the ESM preflight checks above failed. In that case,
50- // it's okay to hard-code the valid Node versions here since they will not change over time.
51- if ( typeof require === 'undefined' ) {
52- console . error ( `\nNode.js v${ version } is not supported by Astro!
53- Please upgrade to a supported version of Node.js: ">=16.12.0"\n` ) ;
23+ // Fast-path for higher Node.js versions
24+ if ( ( parseInt ( version ) || 0 ) <= skipSemverCheckIfAbove ) {
25+ try {
26+ const semver = await import ( 'semver' ) ;
27+ if ( ! semver . satisfies ( version , engines ) ) {
28+ await errorNodeUnsupported ( ) ;
29+ return ;
30+ }
31+ } catch {
32+ await errorNodeUnsupported ( ) ;
33+ return ;
34+ }
5435 }
5536
56- // Not supported: Report the most helpful error message possible.
57- const pkg = require ( './package.json' ) ;
58- const ci = require ( 'ci-info' ) ;
59- const semver = require ( 'semver' ) ;
60- const engines = pkg . engines . node ;
37+ return import ( './dist/cli/index.js' )
38+ . then ( ( { cli } ) => cli ( process . argv ) )
39+ . catch ( ( error ) => {
40+ console . error ( error ) ;
41+ process . exit ( 1 ) ;
42+ } ) ;
43+ }
6144
62- // TODO: Remove "semver" in Astro v1.0: This is mainly just to check our work. Once run in
63- // the wild for a bit without error, we can assume our engine range is correct and won't
64- // change over time.
65- const isSupported = semver . satisfies ( version , engines ) ;
66- if ( isSupported ) {
67- console . error ( `\nNode.js v${ version } is not supported by Astro!
68- Supported versions: ${ engines } \n
69- Issue Detected! This Node.js version was expected to work, but failed a system check.
70- Please file an issue so that we can take a look: https://github.com/withastro/astro/issues/new\n` ) ;
71- } else {
72- console . error ( `\nNode.js v${ version } is not supported by Astro!
45+ async function errorNodeUnsupported ( ) {
46+ console . error ( `\
47+ Node.js v${ process . versions . node } is not supported by Astro!
7348Please upgrade Node.js to a supported version: "${ engines } "\n` ) ;
74- }
49+
50+ const ci = typeof require !== 'undefined' ? require ( 'ci-info' ) : await import ( 'ci-info' ) ;
7551
7652 // Special instructions for CI environments, which may have special steps needed.
7753 // This is a common issue that we can help users with proactively.
0 commit comments