Skip to content

Commit

Permalink
fix: disable --use-loader when not available
Browse files Browse the repository at this point in the history
  • Loading branch information
cyco130 committed Aug 13, 2023
1 parent f546837 commit 779b2da
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions packages/vavite/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import { spawn } from "node:child_process";

const startTime = performance.now();

const [major, minor] = process.version
.slice(1)
.split(".")
.map((x) => Number(x));

const loaderAvailable =
(major > 16 || (major === 16 && minor >= 12)) && major < 20;

interface GlobalCLIOptions {
"--"?: string[];
c?: boolean | string;
Expand Down Expand Up @@ -171,30 +179,36 @@ cli
useLoader?: boolean;
},
) => {
if (options.useLoader && !hasLoader) {
// Rerun the command with the loader options
const options =
(process.env.NODE_OPTIONS ? process.env.NODE_OPTIONS + " " : "") +
"-r vavite/suppress-loader-warnings --loader vavite/node-loader";

const cp = spawn(process.execPath, process.argv.slice(1), {
stdio: "inherit",
env: {
...process.env,
NODE_OPTIONS: options,
},
});
if (options.useLoader) {
if (!loaderAvailable) {
console.warn(
`--use-loader is ignored as it requires a Node.js version between 16.12 and 20`,
);
} else if (!hasLoader) {
// Rerun the command with the loader options
const options =
(process.env.NODE_OPTIONS ? process.env.NODE_OPTIONS + " " : "") +
"-r vavite/suppress-loader-warnings --loader vavite/node-loader";

cp.on("error", (err) => {
console.error(err);
process.exit(1);
});
const cp = spawn(process.execPath, process.argv.slice(1), {
stdio: "inherit",
env: {
...process.env,
NODE_OPTIONS: options,
},
});

cp.on("exit", (code) => {
process.exit(code ?? 0);
});
cp.on("error", (err) => {
console.error(err);
process.exit(1);
});

return;
cp.on("exit", (code) => {
process.exit(code ?? 0);
});

return;
}
}

delete options.useLoader;
Expand Down

0 comments on commit 779b2da

Please sign in to comment.