fix(js): fix swc compilation output path handling (#18588)
Co-authored-by: FrozenPandaz <jasonjean1993@gmail.com>
This commit is contained in:
parent
ccd6e04d3f
commit
b152f20125
@ -51,6 +51,7 @@
|
|||||||
"@babel/preset-env",
|
"@babel/preset-env",
|
||||||
"@babel/preset-typescript",
|
"@babel/preset-typescript",
|
||||||
"@babel/runtime",
|
"@babel/runtime",
|
||||||
|
"@swc/cli",
|
||||||
"babel-plugin-const-enum",
|
"babel-plugin-const-enum",
|
||||||
"babel-plugin-macros",
|
"babel-plugin-macros",
|
||||||
"babel-plugin-transform-typescript-metadata"
|
"babel-plugin-transform-typescript-metadata"
|
||||||
|
|||||||
@ -59,12 +59,19 @@ function normalizeOptions(
|
|||||||
outputPath
|
outputPath
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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();
|
||||||
|
// default to current directory if projectRootParts is [].
|
||||||
|
// Eg: when a project is at the root level, outside of layout dir
|
||||||
|
const swcCwd = projectRootParts.join('/') || '.';
|
||||||
const swcrcPath = getSwcrcPath(options, root, projectRoot);
|
const swcrcPath = getSwcrcPath(options, root, projectRoot);
|
||||||
// TODO(meeroslav): Check why this is needed in order for swc to properly nest folders
|
|
||||||
const distParent = outputPath.split('/').slice(0, -1).join('/');
|
|
||||||
const swcCliOptions = {
|
const swcCliOptions = {
|
||||||
srcPath: projectRoot,
|
srcPath: projectDir,
|
||||||
destPath: relative(root, distParent),
|
destPath: relative(join(root, swcCwd), outputPath),
|
||||||
|
swcCwd,
|
||||||
swcrcPath,
|
swcrcPath,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -121,11 +128,13 @@ export async function* swcExecutor(
|
|||||||
if (!isInlineGraphEmpty(inlineProjectGraph)) {
|
if (!isInlineGraphEmpty(inlineProjectGraph)) {
|
||||||
options.projectRoot = '.'; // set to root of workspace to include other libs for type check
|
options.projectRoot = '.'; // set to root of workspace to include other libs for type check
|
||||||
|
|
||||||
options.swcCliOptions.srcPath = root.split('/').slice(0, -1).join('/'); // set to root of libraries to include other libs
|
// remap paths for SWC compilation
|
||||||
options.swcCliOptions.destPath = join(
|
options.swcCliOptions.srcPath = options.swcCliOptions.swcCwd;
|
||||||
_options.outputPath,
|
options.swcCliOptions.swcCwd = '.';
|
||||||
options.swcCliOptions.srcPath
|
options.swcCliOptions.destPath = options.swcCliOptions.destPath
|
||||||
); // new destPath is dist/{libs}/{parentLib}/{libs}
|
.split('../')
|
||||||
|
.at(-1)
|
||||||
|
.concat('/', options.swcCliOptions.srcPath);
|
||||||
|
|
||||||
// tmp swcrc with dependencies to exclude
|
// tmp swcrc with dependencies to exclude
|
||||||
// - buildable libraries
|
// - buildable libraries
|
||||||
|
|||||||
@ -1,9 +1,4 @@
|
|||||||
import {
|
import { cacheDir, ExecutorContext, logger } from '@nx/devkit';
|
||||||
cacheDir,
|
|
||||||
ExecutorContext,
|
|
||||||
getPackageManagerCommand,
|
|
||||||
logger,
|
|
||||||
} from '@nx/devkit';
|
|
||||||
import { exec, execSync } from 'child_process';
|
import { exec, execSync } from 'child_process';
|
||||||
import { removeSync } from 'fs-extra';
|
import { removeSync } from 'fs-extra';
|
||||||
import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable';
|
import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable';
|
||||||
@ -15,8 +10,8 @@ function getSwcCmd(
|
|||||||
{ swcrcPath, srcPath, destPath }: SwcCliOptions,
|
{ swcrcPath, srcPath, destPath }: SwcCliOptions,
|
||||||
watch = false
|
watch = false
|
||||||
) {
|
) {
|
||||||
const packageManager = getPackageManagerCommand();
|
const swcCLI = require.resolve('@swc/cli/bin/swc.js');
|
||||||
let swcCmd = `${packageManager.exec} swc ${
|
let swcCmd = `${swcCLI} ${
|
||||||
// TODO(jack): clean this up when we remove inline module support
|
// TODO(jack): clean this up when we remove inline module support
|
||||||
// Handle root project
|
// Handle root project
|
||||||
srcPath === '.' ? 'src' : srcPath
|
srcPath === '.' ? 'src' : srcPath
|
||||||
@ -58,6 +53,7 @@ export async function compileSwc(
|
|||||||
|
|
||||||
const swcCmdLog = execSync(getSwcCmd(normalizedOptions.swcCliOptions), {
|
const swcCmdLog = execSync(getSwcCmd(normalizedOptions.swcCliOptions), {
|
||||||
encoding: 'utf8',
|
encoding: 'utf8',
|
||||||
|
cwd: normalizedOptions.swcCliOptions.swcCwd,
|
||||||
});
|
});
|
||||||
logger.log(swcCmdLog.replace(/\n/, ''));
|
logger.log(swcCmdLog.replace(/\n/, ''));
|
||||||
const isCompileSuccess = swcCmdLog.includes('Successfully compiled');
|
const isCompileSuccess = swcCmdLog.includes('Successfully compiled');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user