fix(node-swc/cli): Correctly expose swcx entrypoint (#3784)

This commit is contained in:
OJ Kwon 2022-03-01 00:41:45 -08:00 committed by GitHub
parent 0c22af3f02
commit 95ecc013fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 111 additions and 45 deletions

View File

@ -12,6 +12,7 @@ yarn.lock
package-lock.json
*.log
.idea/
docs/
wasm/

82
node-swc/src/swcx.ts Normal file
View File

@ -0,0 +1,82 @@
#!/usr/bin/env node
/**
* Lightweight entrypoint to native swc cli binary.
*
* This is to locate corresponding per-platform executables correctly, as well as
* let npm links binary to `node_modules/.bin` allows npm-related ecosystem (`npx swcx`, etcs)
* works correctly. However, it means spawning native binary still requires warmup from node.js
* process.
*
* NOTE: THIS IS NOT A PERMANENT APPROACH.
* Distribution of native cli binary is not fully concluded yet. This allows easier
* opt-in while implementation is in progress to collect feedback.
*/
import { spawn, StdioOptions } from "child_process";
import path from "path";
import { readFileSync } from "fs";
const { platform, arch } = process;
const isMusl = () => (() => {
function isMusl() {
if (!process.report || typeof process.report.getReport !== "function") {
try {
return readFileSync("/usr/bin/ldd", "utf8").includes("musl");
} catch (e) {
return true;
}
} else {
const { glibcVersionRuntime } = (process.report.getReport() as any).header;
return !glibcVersionRuntime;
}
}
return isMusl();
})();
const platformPackagesMap: Record<string, Partial<Record<string, string>>> = {
"android": {
"arm64": "@swc/core-android-arm64",
"arm": "@swc/core-android-arm-eabi",
},
"win32": {
"x64": "@swc/core-win32-x64-msvc",
"ia32": "@swc/core-win32-ia32-msvc",
"arm64": "@swc/core-win32-arm64-msvc"
},
"darwin": {
"x64": "@swc/core-darwin-x64",
"arm64": "@swc/core-darwin-arm64",
},
"freebsd": {
"x64": "@swc/core-freebsd-x64",
},
"linux": {
"x64": `@swc/core-linux-x64-${isMusl() ? 'musl' : 'gnu'}`,
"arm64": `@swc/core-linux-arm64-${isMusl() ? 'musl' : 'gnu'}`,
"arm": "@swc/core-linux-arm64-gnu"
},
};
const inferBinaryName = () => {
const packageName = platformPackagesMap[platform][arch];
if (!packageName) {
throw new Error(`Unsupported platform: binary for '${platform} ${arch}' is not available`);
}
return path.join(path.dirname(require.resolve(packageName)), platform === 'win32' ? 'swc.exe' : 'swc');
}
const executeBinary = async () => {
const binary = inferBinaryName();
const [, , ...args] = process.argv;
const options = { cwd: process.cwd(), stdio: "inherit" as StdioOptions };
return spawn(binary, args, options);
};
executeBinary().catch((e) => console.error(e));

View File

@ -4,6 +4,9 @@
"description": "Super-fast alternative for babel",
"homepage": "https://swc.rs",
"main": "./index.js",
"bin": {
"swcx": "swcx.js"
},
"author": "강동윤 <kdy1997.dev@gmail.com>",
"license": "Apache-2.0",
"keywords": [
@ -102,5 +105,20 @@
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/swc"
},
"optionalDependencies": {
"@swc/core-win32-x64-msvc": "1.2.146",
"@swc/core-darwin-x64": "1.2.146",
"@swc/core-linux-x64-gnu": "1.2.146",
"@swc/core-linux-x64-musl": "1.2.146",
"@swc/core-freebsd-x64": "1.2.146",
"@swc/core-win32-ia32-msvc": "1.2.146",
"@swc/core-linux-arm64-gnu": "1.2.146",
"@swc/core-linux-arm-gnueabihf": "1.2.146",
"@swc/core-darwin-arm64": "1.2.146",
"@swc/core-android-arm64": "1.2.146",
"@swc/core-linux-arm64-musl": "1.2.146",
"@swc/core-win32-arm64-msvc": "1.2.146",
"@swc/core-android-arm-eabi": "1.2.146"
}
}
}

View File

@ -12,9 +12,6 @@
"swc.android-arm-eabi.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.android-arm64.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.darwin-arm64.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.darwin-x64.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.freebsd-x64.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.linux-arm-gnueabihf.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.linux-arm64-gnu.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.linux-arm64-musl.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.linux-x64-gnu.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.linux-x64-musl.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.win32-arm64-msvc.node",
"swc.exe"
],
"bin": {
"swcx": "swc.exe"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.win32-ia32-msvc.node",
"swc.exe"
],
"bin": {
"swcx": "swc.exe"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -12,9 +12,6 @@
"swc.win32-x64-msvc.node",
"swc.exe"
],
"bin": {
"swcx": "swc.exe"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",

View File

@ -4,7 +4,7 @@
"target": "es2015" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
// "lib": [], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
"allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true /* Generates corresponding '.d.ts' file. */,
@ -14,7 +14,7 @@
"outDir": "./" /* Redirect output structure to the directory. */,
"rootDir": "./node-swc/src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
"removeComments": false, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
@ -52,6 +52,10 @@
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"include": ["node-swc/src/"],
"exclude": ["*.js"]
}
"include": [
"node-swc/src/"
],
"exclude": [
"*.js"
]
}