Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(swc/cli): correctly expose swcx entrypoint #3784

Merged
merged 2 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ yarn.lock
package-lock.json
*.log
.idea/
docs/

wasm/

Expand Down
82 changes: 82 additions & 0 deletions node-swc/src/swcx.ts
Original file line number Diff line number Diff line change
@@ -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));
20 changes: 19 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"description": "Super-fast alternative for babel",
"homepage": "https://swc.rs",
"main": "./index.js",
"bin": {
"swcx": "swcx.js"
},
"author": "강동윤 <[email protected]>",
"license": "Apache-2.0",
"keywords": [
Expand Down Expand Up @@ -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"
}
}
}
3 changes: 0 additions & 3 deletions scripts/npm/android-arm-eabi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.android-arm-eabi.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/android-arm64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.android-arm64.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.darwin-arm64.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.darwin-x64.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/freebsd-x64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.freebsd-x64.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/linux-arm-gnueabihf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.linux-arm-gnueabihf.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/linux-arm64-gnu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.linux-arm64-gnu.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/linux-arm64-musl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.linux-arm64-musl.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.linux-x64-gnu.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/linux-x64-musl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.linux-x64-musl.node",
"swc"
],
"bin": {
"swcx": "swc"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/win32-arm64-msvc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.win32-arm64-msvc.node",
"swc.exe"
],
"bin": {
"swcx": "swc.exe"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/win32-ia32-msvc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.win32-ia32-msvc.node",
"swc.exe"
],
"bin": {
"swcx": "swc.exe"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
3 changes: 0 additions & 3 deletions scripts/npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"swc.win32-x64-msvc.node",
"swc.exe"
],
"bin": {
"swcx": "swc.exe"
},
"description": "Super-fast alternative for babel",
"keywords": [
"swc",
Expand Down
14 changes: 9 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */,
Expand All @@ -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'. */
Expand Down Expand Up @@ -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"
]
}