Comparing version 0.1.65 to 0.2.0
@@ -172,2 +172,26 @@ "use strict"; | ||
}); | ||
it("--workers exits on non-numeric values", async ()=>{ | ||
const args = [ | ||
"node", | ||
"/path/to/node_modules/swc-cli/bin/swc.js", | ||
"--workers", | ||
"not-a-number", | ||
"src" | ||
]; | ||
await (0, _options.default)(args); | ||
expect(mockExit).toHaveBeenCalledWith(2); | ||
expect(mockConsoleError).toHaveBeenCalledTimes(2); | ||
}); | ||
it("--workers exits on non-integer values", async ()=>{ | ||
const args = [ | ||
"node", | ||
"/path/to/node_modules/swc-cli/bin/swc.js", | ||
"--workers", | ||
"1.5", | ||
"src" | ||
]; | ||
await (0, _options.default)(args); | ||
expect(mockExit).toHaveBeenCalledWith(2); | ||
expect(mockConsoleError).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
@@ -174,0 +198,0 @@ describe("--source-maps", ()=>{ |
@@ -9,8 +9,8 @@ "use strict"; | ||
}); | ||
const _slash = /*#__PURE__*/ _interopRequireDefault(require("slash")); | ||
const _fs = require("fs"); | ||
const _path = require("path"); | ||
const _piscina = /*#__PURE__*/ _interopRequireDefault(require("piscina")); | ||
const _constants = require("./constants"); | ||
const _util = require("./util"); | ||
const _compile = require("./compile"); | ||
const _dirWorker = /*#__PURE__*/ _interopRequireDefault(require("./dirWorker")); | ||
const _sources = require("./sources"); | ||
@@ -23,43 +23,7 @@ function _interopRequireDefault(obj) { | ||
const { mkdir , rmdir , rm , copyFile , unlink } = _fs.promises; | ||
const cwd = process.cwd(); | ||
const recursive = { | ||
recursive: true | ||
}; | ||
/** | ||
* Removes the leading directory, including all parent relative paths | ||
*/ function stripComponents(filename) { | ||
const components = filename.split("/").slice(1); | ||
if (!components.length) { | ||
return filename; | ||
} | ||
while(components[0] === ".."){ | ||
components.shift(); | ||
} | ||
return components.join("/"); | ||
} | ||
function getDest(filename, outDir, ext) { | ||
const relativePath = (0, _slash.default)((0, _path.relative)(cwd, filename)); | ||
let base = stripComponents(relativePath); | ||
if (ext) { | ||
base = base.replace(/\.\w*$/, ext); | ||
} | ||
return (0, _path.join)(outDir, base); | ||
} | ||
async function handleCompile(filename, outDir, sync, swcOptions) { | ||
const dest = getDest(filename, outDir, ".js"); | ||
const sourceFileName = (0, _slash.default)((0, _path.relative)((0, _path.dirname)(dest), filename)); | ||
const options = { | ||
...swcOptions, | ||
sourceFileName | ||
}; | ||
const result = await (0, _util.compile)(filename, options, sync, dest); | ||
if (result) { | ||
await (0, _compile.outputResult)(result, filename, dest, options); | ||
return _constants.CompileStatus.Compiled; | ||
} else { | ||
return _constants.CompileStatus.Omitted; | ||
} | ||
} | ||
async function handleCopy(filename, outDir) { | ||
const dest = getDest(filename, outDir); | ||
const dest = (0, _util.getDest)(filename, outDir); | ||
const dir = (0, _path.dirname)(dest); | ||
@@ -88,3 +52,8 @@ await mkdir(dir, recursive); | ||
try { | ||
const result = await handleCompile(filename, outDir, sync, swcOptions); | ||
const result = await (0, _dirWorker.default)({ | ||
filename, | ||
outDir, | ||
sync, | ||
swcOptions | ||
}); | ||
results.set(filename, result); | ||
@@ -106,4 +75,14 @@ } catch (err) { | ||
} else { | ||
const workers = new _piscina.default({ | ||
filename: (0, _path.resolve)(__dirname, "./dirWorker.js"), | ||
maxThreads: cliOptions.workers, | ||
concurrentTasksPerWorker: 2 | ||
}); | ||
await Promise.all([ | ||
Promise.allSettled(compilable.map((file)=>handleCompile(file, outDir, sync, swcOptions).catch((err)=>{ | ||
Promise.allSettled(compilable.map((filename)=>workers.run({ | ||
filename, | ||
outDir, | ||
sync, | ||
swcOptions | ||
}).catch((err)=>{ | ||
console.error(err.message); | ||
@@ -182,4 +161,4 @@ throw err; | ||
if ((0, _sources.isCompilableExtension)(filename, extensions)) { | ||
await unlink(getDest(filename, outDir, ".js")); | ||
const sourcemapPath = getDest(filename, outDir, ".js.map"); | ||
await unlink((0, _util.getDest)(filename, outDir, ".js")); | ||
const sourcemapPath = (0, _util.getDest)(filename, outDir, ".js.map"); | ||
const sourcemapExists = await (0, _util.exists)(sourcemapPath); | ||
@@ -190,3 +169,3 @@ if (sourcemapExists) { | ||
} else if (copyFiles) { | ||
await unlink(getDest(filename, outDir)); | ||
await unlink((0, _util.getDest)(filename, outDir)); | ||
} | ||
@@ -207,3 +186,8 @@ } catch (err) { | ||
const start = process.hrtime(); | ||
const result = await handleCompile(filename, outDir, sync, swcOptions); | ||
const result = await (0, _dirWorker.default)({ | ||
filename, | ||
outDir, | ||
sync, | ||
swcOptions | ||
}); | ||
if (!quiet && result === _constants.CompileStatus.Compiled) { | ||
@@ -210,0 +194,0 @@ const end = process.hrtime(start); |
@@ -48,2 +48,3 @@ "use strict"; | ||
program.option("--sync", "Invoke swc synchronously. Useful for debugging.", collect); | ||
program.option("--workers [number]", "The number of workers to use for parallel processing"); | ||
program.option("--log-watch-compilation", "Log a message when a watched file is successfully compiled", true); | ||
@@ -100,2 +101,9 @@ program.option("--extensions [list]", "Use specific extensions", collect); | ||
} | ||
let workers; | ||
if (opts.workers != null) { | ||
workers = parseFloat(opts.workers); | ||
if (!Number.isInteger(workers) || workers < 0) { | ||
errors.push("--workers must be a positive integer (found " + opts.workers + ")"); | ||
} | ||
} | ||
if (errors.length) { | ||
@@ -155,2 +163,3 @@ console.error("swc:"); | ||
sync: !!opts.sync, | ||
workers, | ||
sourceMapTarget: opts.sourceMapTarget, | ||
@@ -157,0 +166,0 @@ extensions: opts.extensions || _core.DEFAULT_EXTENSIONS, |
@@ -16,3 +16,4 @@ "use strict"; | ||
outputFile: ()=>outputFile, | ||
assertCompilationResult: ()=>assertCompilationResult | ||
assertCompilationResult: ()=>assertCompilationResult, | ||
getDest: ()=>getDest | ||
}); | ||
@@ -153,3 +154,24 @@ const _core = /*#__PURE__*/ _interopRequireWildcard(require("@swc/core")); | ||
} | ||
/** | ||
* Removes the leading directory, including all parent relative paths | ||
*/ function stripComponents(filename) { | ||
const components = filename.split("/").slice(1); | ||
if (!components.length) { | ||
return filename; | ||
} | ||
while(components[0] === ".."){ | ||
components.shift(); | ||
} | ||
return components.join("/"); | ||
} | ||
const cwd = process.cwd(); | ||
function getDest(filename, outDir, ext) { | ||
const relativePath = (0, _slash.default)((0, _path.relative)(cwd, filename)); | ||
let base = stripComponents(relativePath); | ||
if (ext) { | ||
base = base.replace(/\.\w*$/, ext); | ||
} | ||
return (0, _path.join)(outDir, base); | ||
} | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "@swc/cli", | ||
"version": "0.1.65", | ||
"version": "0.2.0", | ||
"description": "CLI for the swc project", | ||
@@ -41,3 +41,3 @@ "main": "lib/swc/index.js", | ||
"engines": { | ||
"node": ">= 12.13" | ||
"node": ">= 16.14.0" | ||
}, | ||
@@ -54,2 +54,3 @@ "bin": { | ||
"minimatch": "^9.0.3", | ||
"piscina": "^4.3.0", | ||
"semver": "^7.3.8", | ||
@@ -64,3 +65,3 @@ "slash": "3.0.0", | ||
"@types/jest": "^29.5.0", | ||
"@types/node": "^12.19.16", | ||
"@types/node": "^20.11.5", | ||
"@types/semver": "^7.3.13", | ||
@@ -67,0 +68,0 @@ "chokidar": "^3.5.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
193448
37
2135
10
+ Addedpiscina@^4.3.0
+ Added@napi-rs/nice@1.0.1(transitive)
+ Added@napi-rs/nice-android-arm-eabi@1.0.1(transitive)
+ Added@napi-rs/nice-android-arm64@1.0.1(transitive)
+ Added@napi-rs/nice-darwin-arm64@1.0.1(transitive)
+ Added@napi-rs/nice-darwin-x64@1.0.1(transitive)
+ Added@napi-rs/nice-freebsd-x64@1.0.1(transitive)
+ Added@napi-rs/nice-linux-arm-gnueabihf@1.0.1(transitive)
+ Added@napi-rs/nice-linux-arm64-gnu@1.0.1(transitive)
+ Added@napi-rs/nice-linux-arm64-musl@1.0.1(transitive)
+ Added@napi-rs/nice-linux-ppc64-gnu@1.0.1(transitive)
+ Added@napi-rs/nice-linux-riscv64-gnu@1.0.1(transitive)
+ Added@napi-rs/nice-linux-s390x-gnu@1.0.1(transitive)
+ Added@napi-rs/nice-linux-x64-gnu@1.0.1(transitive)
+ Added@napi-rs/nice-linux-x64-musl@1.0.1(transitive)
+ Added@napi-rs/nice-win32-arm64-msvc@1.0.1(transitive)
+ Added@napi-rs/nice-win32-ia32-msvc@1.0.1(transitive)
+ Added@napi-rs/nice-win32-x64-msvc@1.0.1(transitive)
+ Addedpiscina@4.7.0(transitive)