Skip to content

Commit

Permalink
fix(js): handle swcrc path mappings if specified
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Feb 28, 2023
1 parent d21eb1d commit f2d2594
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
49 changes: 49 additions & 0 deletions e2e/js/src/js-swc.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { readJsonFile, writeJsonFile } from '@nrwl/devkit';
import { execSync } from 'child_process';
import { writeFileSync } from 'fs-extra';
import {
checkFilesDoNotExist,
checkFilesExist,
Expand Down Expand Up @@ -116,4 +119,50 @@ describe('js e2e', () => {
'peerDependencies.@swc/helpers'
);
}, 240_000);

it('should handle swcrc path mappings', async () => {
const lib = uniq('lib');
runCLI(`generate @nrwl/js:lib ${lib} --compiler=swc --no-interactive`);

// add a dummy x.ts file for path mappings
writeFileSync(
`libs/${lib}/src/x.ts`,
`
export function x() {
console.log('x');
}
`
);

// update .swcrc to use path mappings
updateJson(`libs/${lib}/.swcrc`, (json) => {
json.jsc.paths = {
'src/*': ['src/*'],
};
return json;
});

// update lib.ts to use x
updateFile(`libs/${lib}/src/lib/${lib}.ts`, () => {
return `
// @ts-ignore
import { x } from 'src/x';
export function myLib() {
console.log(x());
}
myLib();
`;
});

// now run build
runCLI(`build ${lib}`);

// invoke the lib with node
const result = execSync(`node dist/libs/${lib}/src/lib/${lib}.js`, {
cwd: tmpProjPath(),
}).toString();
expect(result).toContain('x');
}, 240_000);
});
21 changes: 18 additions & 3 deletions packages/js/src/executors/swc/swc.impl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExecutorContext } from '@nrwl/devkit';
import { ExecutorContext, readJsonFile, writeJsonFile } from '@nrwl/devkit';
import {
assetGlobsToFiles,
FileInputOutput,
Expand All @@ -24,6 +24,7 @@ import {
import { compileSwc, compileSwcWatch } from '../../utils/swc/compile-swc';
import { getSwcrcPath } from '../../utils/swc/get-swcrc-path';
import { generateTmpSwcrc } from '../../utils/swc/inline';
import type { Options } from '@swc/core';

export function normalizeOptions(
options: SwcExecutorOptions,
Expand Down Expand Up @@ -68,7 +69,21 @@ export function normalizeOptions(
// 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, contextRoot, projectRoot);
let swcrcPath = getSwcrcPath(options, contextRoot, projectRoot);

try {
const swcrcContent = readJsonFile(swcrcPath) as Options;
// if we have path mappings setup but baseUrl isn't specified, then we're proceeding with the following logic
if (
swcrcContent.jsc &&
swcrcContent.jsc.paths &&
!swcrcContent.jsc.baseUrl
) {
swcrcContent.jsc.baseUrl = `./${projectDir}`;
swcrcPath = getSwcrcPath(options, contextRoot, projectRoot, true);
writeJsonFile(swcrcPath, swcrcContent);
}
} catch (e) {}

const swcCliOptions = {
srcPath: projectDir,
Expand Down Expand Up @@ -190,7 +205,7 @@ export async function* swcExecutor(
}

function removeTmpSwcrc(swcrcPath: string) {
if (swcrcPath.startsWith('tmp/')) {
if (swcrcPath.includes('tmp/') && swcrcPath.includes('.generated.swcrc')) {
removeSync(dirname(swcrcPath));
}
}
Expand Down
13 changes: 9 additions & 4 deletions packages/js/src/utils/swc/get-swcrc-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { SwcExecutorOptions } from '../schema';
export function getSwcrcPath(
options: SwcExecutorOptions,
contextRoot: string,
projectRoot: string
projectRoot: string,
temp = false
) {
return options.swcrc
? join(contextRoot, options.swcrc)
: join(contextRoot, projectRoot, '.swcrc');
let swcrcPath = options.swcrc ?? join(projectRoot, '.swcrc');

if (temp) {
swcrcPath = join('tmp', swcrcPath.replace('.swcrc', '.generated.swcrc'));
}

return join(contextRoot, swcrcPath);
}

0 comments on commit f2d2594

Please sign in to comment.