Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Commit

Permalink
feat: Add strip-leading-paths option (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda authored Jan 26, 2024
1 parent f9c8a9b commit 0d8f59a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 10 deletions.
11 changes: 11 additions & 0 deletions src/swc/__tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const createDefaultResult = (): ParserArgsReturn => ({
outFile: undefined,
quiet: false,
sourceMapTarget: undefined,
stripLeadingPaths: false,
sync: false,
watch: false,
},
Expand Down Expand Up @@ -206,13 +207,23 @@ describe("parserArgs", () => {

describe("--config", () => {
it("throws with no config", async () => {
let mockConsoleError: jest.SpyInstance;

mockConsoleError = jest
.spyOn(process.stderr, "write")
.mockImplementation(() => {
return true;
});

const args = [
"node",
"/path/to/node_modules/swc-cli/bin/swc.js",
"src",
"-C",
];
expect(() => parserArgs(args)).toThrow();

mockConsoleError.mockRestore();
});

it("react development", async () => {
Expand Down
22 changes: 22 additions & 0 deletions src/swc/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { join } from "path";
import { getDest } from "../util";

describe("getDest", () => {
it("does not modify the filename by default", () => {
expect(
getDest(join(process.cwd(), "src/path/name.ts"), "foo/bar", false)
).toEqual("foo/bar/src/path/name.ts");
});

it("when stripLeadingPaths is true, it removes leading paths", () => {
expect(
getDest(join(process.cwd(), "src/path/name.ts"), "foo/bar", true)
).toEqual("foo/bar/path/name.ts");
});

it("when stripLeadingPaths is true, it also resolves relative paths", () => {
expect(
getDest(join(process.cwd(), "../../path/name.ts"), "foo/bar", true)
).toEqual("foo/bar/path/name.ts");
});
});
31 changes: 23 additions & 8 deletions src/swc/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ const { mkdir, rmdir, rm, copyFile, unlink } = promises;

const recursive = { recursive: true };

async function handleCopy(filename: string, outDir: string) {
const dest = getDest(filename, outDir);
async function handleCopy(
filename: string,
outDir: string,
stripLeadingPaths: boolean
) {
const dest = getDest(filename, outDir, stripLeadingPaths);
const dir = dirname(dest);

await mkdir(dir, recursive);
Expand All @@ -56,6 +60,7 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) {
copyFiles,
extensions,
outDir,
stripLeadingPaths,
sync,
quiet,
watch,
Expand Down Expand Up @@ -85,6 +90,7 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) {
filename,
outDir,
sync,
cliOptions,
swcOptions,
});
results.set(filename, result);
Expand All @@ -95,7 +101,7 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) {
}
for (const filename of copyable) {
try {
const result = await handleCopy(filename, outDir);
const result = await handleCopy(filename, outDir, stripLeadingPaths);
results.set(filename, result);
} catch (err: any) {
console.error(err.message);
Expand All @@ -118,7 +124,9 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) {
})
)
),
Promise.allSettled(copyable.map(file => handleCopy(file, outDir))),
Promise.allSettled(
copyable.map(file => handleCopy(file, outDir, stripLeadingPaths))
),
]).then(([compiled, copied]) => {
compiled.forEach((result, index) => {
const filename = compilable[index];
Expand Down Expand Up @@ -197,6 +205,7 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) {
copyFiles,
extensions,
outDir,
stripLeadingPaths,
quiet,
sync,
} = cliOptions;
Expand All @@ -210,14 +219,19 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) {
watcher.on("unlink", async filename => {
try {
if (isCompilableExtension(filename, extensions)) {
await unlink(getDest(filename, outDir, ".js"));
const sourcemapPath = getDest(filename, outDir, ".js.map");
await unlink(getDest(filename, outDir, stripLeadingPaths, ".js"));
const sourcemapPath = getDest(
filename,
outDir,
stripLeadingPaths,
".js.map"
);
const sourcemapExists = await exists(sourcemapPath);
if (sourcemapExists) {
await unlink(sourcemapPath);
}
} else if (copyFiles) {
await unlink(getDest(filename, outDir));
await unlink(getDest(filename, outDir, stripLeadingPaths));
}
} catch (err: any) {
if (err.code !== "ENOENT") {
Expand All @@ -234,6 +248,7 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) {
filename,
outDir,
sync,
cliOptions,
swcOptions,
});
if (!quiet && result === CompileStatus.Compiled) {
Expand All @@ -249,7 +264,7 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) {
} else if (copyFiles) {
try {
const start = process.hrtime();
const result = await handleCopy(filename, outDir);
const result = await handleCopy(filename, outDir, stripLeadingPaths);
if (!quiet && result === CompileStatus.Copied) {
const end = process.hrtime(start);
console.log(
Expand Down
9 changes: 8 additions & 1 deletion src/swc/dirWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import { compile, getDest } from "./util";
import { outputResult } from "./compile";

import type { Options } from "@swc/core";
import type { CliOptions } from "./options";

export default async function handleCompile(opts: {
filename: string;
outDir: string;
sync: boolean;
cliOptions: CliOptions;
swcOptions: Options;
}) {
const dest = getDest(opts.filename, opts.outDir, ".js");
const dest = getDest(
opts.filename,
opts.outDir,
opts.cliOptions.stripLeadingPaths,
".js"
);
const sourceFileName = slash(relative(dirname(dest), opts.filename));

const options = { ...opts.swcOptions, sourceFileName };
Expand Down
8 changes: 8 additions & 0 deletions src/swc/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export const initProgram = () => {
"-D, --copy-files",
"When compiling a directory copy over non-compilable files"
);

program.option(
"--strip-leading-paths",
"Remove the leading directory (including all parent relative paths) when building the final output path"
);

program.option(
"--include-dotfiles",
"Include dotfiles when compiling and copying non-compilable files"
Expand Down Expand Up @@ -158,6 +164,7 @@ function collect(
export interface CliOptions {
readonly outDir: string;
readonly outFile: string;
readonly stripLeadingPaths: boolean;
/**
* Invoke swc using transformSync. It's useful for debugging.
*/
Expand Down Expand Up @@ -278,6 +285,7 @@ export default function parserArgs(args: string[]) {
const cliOptions: CliOptions = {
outDir: opts.outDir,
outFile: opts.outFile,
stripLeadingPaths: Boolean(opts.stripLeadingPaths),
filename: opts.filename,
filenames,
sync: !!opts.sync,
Expand Down
21 changes: 20 additions & 1 deletion src/swc/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,29 @@ export function assertCompilationResult<T>(
}
}

function stripComponents(filename: string) {
const components = filename.split("/").slice(1);
if (!components.length) {
return filename;
}
while (components[0] === "..") {
components.shift();
}
return components.join("/");
}

const cwd = process.cwd();

export function getDest(filename: string, outDir: string, ext?: string) {
export function getDest(
filename: string,
outDir: string,
stripLeadingPaths: boolean,
ext?: string
) {
let base = slash(relative(cwd, filename));
if (stripLeadingPaths) {
base = stripComponents(base);
}
if (ext) {
base = base.replace(/\.\w*$/, ext);
}
Expand Down

0 comments on commit 0d8f59a

Please sign in to comment.