Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
"@storybook/manager-webpack5": "6.5.15",
"@storybook/react": "6.5.15",
"@storybook/theming": "6.5.15",
"@swc/cli": "0.1.59",
"@swc/core": "1.3.30",
"@swc/helpers": "0.4.14",
"@testing-library/dom": "8.11.3",
Expand Down
77 changes: 55 additions & 22 deletions scripts/tasks/src/swc.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,60 @@
import { exec } from 'child_process';
import { promisify } from 'util';
import fs from 'fs';
import path from 'path';

import { transform } from '@swc/core';
import type { Options as SwcOptions } from '@swc/core';
import { logger } from 'just-scripts';
import glob from 'glob';
import * as match from 'micromatch';

const execAsync = promisify(exec);
type Options = SwcOptions & { module: { type: 'es6' | 'commonjs' | 'amd' }; outputPath: string };

type Options = SwcOptions & { module: { type: 'es6' | 'commonjs' | 'amd' } };

function swcCli(options: Options) {
async function swcTransform(options: Options) {
const { outputPath, module } = options;
const swcCliBin = 'npx swc';
const sourceDirMap = {
es6: 'src',
commonjs: 'lib',
amd: 'lib',
};
const sourceDir = sourceDirMap[options.module.type];

const cmd = `${swcCliBin} ${sourceDir} --out-dir ${outputPath} --config module.type=${module?.type}`;
logger.info(`Running swc CLI: ${cmd}`);

return execAsync(cmd);
const packageRoot = process.cwd();
const sourceRootDirName = module.type === 'es6' ? 'src' : 'lib';

let sourceFiles: string[] = [];

if (module.type === 'es6') {
sourceFiles = glob.sync(`${sourceRootDirName}/**/*.{ts,tsx}`);
}

if (module.type === 'commonjs' || module.type === 'amd') {
sourceFiles = glob.sync(`${sourceRootDirName}/**/*.js`);
}

const swcConfig = JSON.parse(fs.readFileSync(path.resolve(packageRoot, '.swcrc'), 'utf-8'));
const tsFileExtensionRegex = /\.(tsx|ts)$/;

for (const fileName of sourceFiles) {
const srcFilePath = path.resolve(packageRoot, fileName);
const isFileExcluded = match.isMatch(srcFilePath, swcConfig.exclude, { contains: true });

if (isFileExcluded) {
continue;
}

const sourceCode = fs.readFileSync(srcFilePath, 'utf-8');

const result = await transform(sourceCode, {
filename: fileName,
module: { type: module.type },
sourceFileName: path.basename(fileName),
outputPath,
});

const compiledFilePath = path.resolve(packageRoot, fileName.replace(`${sourceRootDirName}`, outputPath));

//Create directory folder for new compiled file(s) to live in.
await fs.promises.mkdir(compiledFilePath.replace(path.basename(compiledFilePath), ''), { recursive: true });

const compiledFilePathJS = `${compiledFilePath.replace(tsFileExtensionRegex, '.js')}`;

await fs.promises.writeFile(compiledFilePathJS, result.code);
if (result.map) {
await fs.promises.writeFile(`${compiledFilePathJS}.map`, result.map);
}
}
}

export const swc = {
Expand All @@ -32,7 +65,7 @@ export const swc = {
module: { type: 'commonjs' },
};

return swcCli(options);
return swcTransform(options);
},
esm: () => {
const options: Options = {
Expand All @@ -41,7 +74,7 @@ export const swc = {
module: { type: 'es6' },
};

return swcCli(options);
return swcTransform(options);
},
amd: () => {
const options: Options = {
Expand All @@ -50,6 +83,6 @@ export const swc = {
module: { type: 'amd' },
};

return swcCli(options);
return swcTransform(options);
},
};
Loading