Skip to content

Commit

Permalink
fix(js): change logic around swc cli so that outputPath isn't computed
Browse files Browse the repository at this point in the history
  • Loading branch information
Chau Tran authored and Chau Tran committed Feb 23, 2022
1 parent 750a9e8 commit 0aad7a1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
23 changes: 8 additions & 15 deletions packages/js/src/executors/swc/swc.impl.ts
Expand Up @@ -39,23 +39,16 @@ function normalizeOptions(
outputPath
);

/**
* Reduce outputPath to root of layout dir (libsDir/appsDir)
* in desired build output dir (default is dist)
*
* dist/packages/lib-one -> dist/packages
* build/packages/lib-one -> build/packages
* dist/packages/lib-one/nested/nested-lib-one -> dist/packages
* dist/libs/lib-one -> dist/libs
* dist/apps/app-one -> dist/apps
*/
const swcDestPath =
options.outputPath.substring(0, options.outputPath.indexOf(layoutDir)) +
layoutDir;
const projectRootParts = projectRoot.split('/');
// We pop the last part of the `projectRoot` to pass
// the last part (projectDir) and the remainder (projectRootParts) to swc
const projectDir = projectRootParts.pop();
const swcCwd = projectRootParts.join('/');

const swcCliOptions = {
srcPath: projectRoot,
destPath: swcDestPath,
srcPath: projectDir,
destPath: relative(join(contextRoot, swcCwd), outputPath),
swcCwd,
swcrcPath: join(projectRoot, '.swcrc'),
};

Expand Down
3 changes: 2 additions & 1 deletion packages/js/src/utils/schema.d.ts
Expand Up @@ -53,8 +53,9 @@ export interface SwcExecutorOptions extends ExecutorOptions {

export interface SwcCliOptions {
srcPath: string;
swcrcPath: string;
destPath: string;
swcrcPath: string;
swcCwd: string;
}

export interface NormalizedSwcExecutorOptions
Expand Down
13 changes: 8 additions & 5 deletions packages/js/src/utils/swc/compile-swc.ts
Expand Up @@ -10,7 +10,7 @@ function getSwcCmd(
{ swcrcPath, srcPath, destPath }: SwcCliOptions,
watch = false
) {
let swcCmd = `npx swc ${srcPath} -d ${destPath} --source-root=${srcPath} --source-maps --no-swcrc --config-file=${swcrcPath}`;
let swcCmd = `npx swc ${srcPath} -d ${destPath} --source-maps --no-swcrc --config-file=${swcrcPath}`;
return watch ? swcCmd.concat(' --watch') : swcCmd;
}

Expand Down Expand Up @@ -39,9 +39,9 @@ export async function compileSwc(
) {
logger.log(`Compiling with SWC for ${context.projectName}...`);

const swcCmdLog = execSync(
getSwcCmd(normalizedOptions.swcCliOptions)
).toString();
const swcCmdLog = execSync(getSwcCmd(normalizedOptions.swcCliOptions), {
cwd: normalizedOptions.swcCliOptions.swcCwd,
}).toString();
logger.log(swcCmdLog.replace(/\n/, ''));
const isCompileSuccess = swcCmdLog.includes('Successfully compiled');

Expand Down Expand Up @@ -84,7 +84,10 @@ export async function* compileSwcWatch(
let stderrOnData: () => void;
let watcherOnExit: () => void;

const swcWatcher = exec(getSwcCmd(normalizedOptions.swcCliOptions, true));
const swcWatcher = exec(
getSwcCmd(normalizedOptions.swcCliOptions, true),
{ cwd: normalizedOptions.swcCliOptions.swcCwd }
);

processOnExit = () => {
swcWatcher.kill();
Expand Down

0 comments on commit 0aad7a1

Please sign in to comment.