Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tools): scripts and generator updates to enable SWC transpilation for v9 packages #26527

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
ec31070
chore: add swc/cli as dev dep
TristanWatanabe Jan 26, 2023
05be727
chore: add just-scripts-utils dev dep
TristanWatanabe Jan 26, 2023
e33940c
feat(migrate-converged-pkg): implement setup to add swcrc config file
TristanWatanabe Jan 26, 2023
61a12fc
feat(scripts): add swc.ts file which runs SWC CLI
TristanWatanabe Jan 27, 2023
a15384f
feat(scripts): add just-script preset build:react-components to be us…
TristanWatanabe Jan 27, 2023
f93cc81
feat(migrate-converged-pkg): setup just config file to map just-scrip…
TristanWatanabe Jan 27, 2023
fa7434d
test(migrate-converged-pkg): add test cases for added behaviors
TristanWatanabe Jan 27, 2023
316a71b
fix: deduplicate graceful-fs
TristanWatanabe Jan 27, 2023
ed944bd
fix: deduplicate just-scripts-utils and just-task-logger
TristanWatanabe Jan 27, 2023
f4146bc
fix: deduplicate semver
TristanWatanabe Jan 27, 2023
df9852e
fix(migrate-converged-pkg): add justConfig to projectConfig paths
TristanWatanabe Jan 27, 2023
82f4a67
feat: simplify v9 compilation flow with swc
Hotell Feb 6, 2023
f85bc97
fixup! feat: simplify v9 compilation flow with swc
Hotell Feb 8, 2023
e8d5e73
fixup! fixup! feat: simplify v9 compilation flow with swc
Hotell Feb 8, 2023
7d71a9a
Merge branch 'master' into use-swc-transpilation-tooling-updates
TristanWatanabe Feb 9, 2023
40ab558
chore: update yarn.lock
TristanWatanabe Feb 9, 2023
108daed
Merge remote-tracking branch 'hotell/swc-transpilation-simplification…
TristanWatanabe Feb 9, 2023
cdc90d6
nit: remove unintended prettier changes
TristanWatanabe Feb 9, 2023
1afe76a
fix: use babel-plugin-module-resolver version 4.1.0 which works with …
TristanWatanabe Feb 9, 2023
24989d6
chore: manually dedupe resolve package
TristanWatanabe Feb 9, 2023
57303e5
test(migrate-converged-pkg): update babelrc setup test to be correct
TristanWatanabe Feb 9, 2023
8b887a9
chore: add .swcrc to files v-build owns
TristanWatanabe Feb 9, 2023
8656f48
chore: cleanup just preset.ts of unused code
TristanWatanabe Feb 9, 2023
5473564
nit: remove unintended prettier change
TristanWatanabe Feb 9, 2023
234cd93
nit: remove unintended prettier change
TristanWatanabe Feb 9, 2023
b43b595
nit: remove unintended prettier change
TristanWatanabe Feb 9, 2023
ad54b8a
chore: cleanup commented out code
TristanWatanabe Feb 9, 2023
848e0c6
cleanup: remove unused just-scripts-utils dep
TristanWatanabe Feb 10, 2023
c40842f
Merge branch 'master' into use-swc-transpilation-tooling-updates
TristanWatanabe Feb 21, 2023
77d4f9a
Merge branch 'master' into use-swc-transpilation-tooling-updates
TristanWatanabe Feb 23, 2023
6572a66
chore: update yarn.lock after master merge
TristanWatanabe Feb 23, 2023
3616186
feat: add monorepo root .babelrc-v9.json file and update migrate-conv…
TristanWatanabe Feb 23, 2023
4dcbfc7
Merge branch 'master' into use-swc-transpilation-tooling-updates
TristanWatanabe Mar 4, 2023
21b5a18
chore: update yarn.lock
TristanWatanabe Mar 4, 2023
bf390c0
fix(migrate-converged-generator): add react useBuiltIns config to swc…
TristanWatanabe Mar 4, 2023
80f2432
test(migrate-converged): update test
TristanWatanabe Mar 4, 2023
95f2686
chore: add bugfixes: true to swcrc file to suppress bundle size incre…
TristanWatanabe Mar 4, 2023
b8f6483
nit: remove ecma target from swcrc since it does nothing, browserlist…
TristanWatanabe Mar 4, 2023
91bd611
Merge branch 'master' into use-swc-transpilation-tooling-updates
TristanWatanabe Mar 9, 2023
6617e3f
nit: use optional chaining instead of bang operator for just config task
TristanWatanabe Mar 9, 2023
90079dc
chore: add runtime:classic to swcrc
TristanWatanabe Mar 10, 2023
6d3b05d
test: update test to reflect swcrc addition
TristanWatanabe Mar 10, 2023
6913c18
Merge branch 'master' into use-swc-transpilation-tooling-updates
TristanWatanabe Mar 10, 2023
ca8f5dd
fix: replace useBuiltIns with useSpread to spread react props instead…
TristanWatanabe Mar 15, 2023
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
23 changes: 23 additions & 0 deletions .babelrc-v9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"presets": [
[
"@griffel",
{
"babelOptions": {
"plugins": [
[
"babel-plugin-module-resolver",
{
"root": ["../../../"],
"alias": {
"@fluentui/tokens": "packages/tokens/lib/index.js",
"^@fluentui/(?!react-icons)(.+)": "packages/react-components/\\1/lib/index.js"
}
}
]
]
}
}
]
]
}
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,4 @@ packages/react-experiments/src/components/TileList @ThomasMichon
**/cypress.config.ts @microsoft/fluentui-react-build
**/api-extractor.json @microsoft/fluentui-react-build
**/api-extractor.unstable.json @microsoft/fluentui-react-build
**/.swcrc @microsoft/fluentui-react-build
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"@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 Expand Up @@ -196,6 +197,7 @@
"babel-plugin-annotate-pure-imports": "1.0.0-1",
"babel-plugin-iife-wrap-react-components": "1.0.0-5",
"babel-plugin-lodash": "3.3.4",
"babel-plugin-module-resolver": "4.1.0",
"babel-plugin-tester": "10.1.0",
"beachball": "2.31.0",
"chalk": "4.1.0",
Expand Down
9 changes: 7 additions & 2 deletions scripts/tasks/src/babel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fs from 'fs';
import path from 'path';

import { BabelFileResult, transformAsync } from '@babel/core';
import * as glob from 'glob';
import fs from 'fs';
import { logger } from 'just-scripts';
import path from 'path';

const EOL_REGEX = /\r?\n/g;

Expand All @@ -11,6 +12,10 @@ function addSourceMappingUrl(code: string, loc: string): string {
return code + '\n//# sourceMappingURL=' + path.basename(loc);
}

export function hasBabel() {
return fs.existsSync(path.join(process.cwd(), '.babelrc.json'));
}

export async function babel() {
const files = glob.sync('{lib,lib-commonjs}/**/*.js');

Expand Down
17 changes: 17 additions & 0 deletions scripts/tasks/src/generate-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { exec } from 'child_process';
import { promisify } from 'util';

import { series } from 'just-scripts';

import { apiExtractor } from './api-extractor';

const execAsync = promisify(exec);

export function generateApi() {
return series(generateTypeDeclarations, apiExtractor);
}

function generateTypeDeclarations() {
const cmd = 'tsc -p ./tsconfig.lib.json --emitDeclarationOnly';
return execAsync(cmd);
}
48 changes: 44 additions & 4 deletions scripts/tasks/src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import { isConvergedPackage, shipsAMD } from '@fluentui/scripts-monorepo';
import { addResolvePath, condition, option, parallel, series, task } from 'just-scripts';

import { apiExtractor } from './api-extractor';
import { getJustArgv } from './argv';
import { babel } from './babel';
import { JustArgs, getJustArgv } from './argv';
import { babel, hasBabel } from './babel';
import { clean } from './clean';
import { copy, copyCompiled } from './copy';
import { eslint } from './eslint';
import { generateApi } from './generate-api';
import { jest as jestTask, jestWatch } from './jest';
import { lintImportTaskAll, lintImportTaskAmdOnly } from './lint-imports';
import { postprocessTask } from './postprocess';
import { postprocessAmdTask } from './postprocess-amd';
import { prettier } from './prettier';
import { sass } from './sass';
import { hasSass, sass } from './sass';
import { buildStorybookTask, startStorybookTask } from './storybook';
import { swc } from './swc';
import { ts } from './ts';
import { webpack, webpackDevServer } from './webpack';

Expand Down Expand Up @@ -69,6 +71,7 @@ export function preset() {
task('storybook:start', startStorybookTask());
task('storybook:build', buildStorybookTask());
task('babel:postprocess', babel);
task('generate-api', generateApi);

task('ts:compile', () => {
const moduleFlag = args.module;
Expand All @@ -93,7 +96,7 @@ export function preset() {
'ts:compile',
'copy-compiled',
'ts:postprocess',
condition('babel:postprocess', () => fs.existsSync(path.join(process.cwd(), '.babelrc.json'))),
condition('babel:postprocess', () => hasBabel()),
);
});

Expand All @@ -104,6 +107,19 @@ export function preset() {

task('lint', 'eslint');

task('swc:commonjs', swc.commonjs);
task('swc:esm', swc.esm);
task('swc:amd', swc.amd);

task('swc:compile', () => {
const moduleFlag = args.module;
return series(
'swc:esm',
condition('babel:postprocess', () => hasBabel()),
resolveModuleCompilation(moduleFlag),
);
});

task('code-style', series('prettier', 'lint'));

task('dev:storybook', series('storybook:start'));
Expand All @@ -124,10 +140,34 @@ export function preset() {
),
).cached!();

task('build:react-components', () => {
return series(
'clean',
'copy',
condition('sass', () => hasSass()),
parallel('swc:compile', 'generate-api'),
);
}).cached!();

task(
'bundle',
condition('webpack', () => fs.existsSync(path.join(process.cwd(), 'webpack.config.js'))),
);

function resolveModuleCompilation(moduleFlag?: JustArgs['module']) {
// default behaviour
if (!moduleFlag) {
return parallel(
'swc:commonjs',
condition('swc:amd', () => !!args.production && !isConvergedPackage()),
);
}

return parallel(
condition('swc:commonjs', () => moduleFlag.cjs),
condition('swc:amd', () => moduleFlag.amd),
);
}
}

if (process.cwd() === __dirname) {
Expand Down
7 changes: 6 additions & 1 deletion scripts/tasks/src/sass.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from 'path';

import * as glob from 'glob';
import { sassTask } from 'just-scripts';
import postcssModules from 'postcss-modules';
Expand Down Expand Up @@ -37,10 +38,14 @@ function getJSON(cssFileName: string, json: Record<string, string>) {
_fileNameToClassMap[path.resolve(cssFileName)] = json;
}

export function hasSass() {
return glob.sync(path.join(process.cwd(), 'src/**/*.scss')).length > 0;
}

export function sass() {
// small optimization: if there are no sass files, the task does nothing
// (skip actually calling sassTask which must parse several extra dependencies)
if (!glob.sync(path.join(process.cwd(), 'src/**/*.scss')).length) {
if (!hasSass()) {
return () => undefined;
}

Expand Down
55 changes: 55 additions & 0 deletions scripts/tasks/src/swc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { exec } from 'child_process';
import { promisify } from 'util';

import type { Options as SwcOptions } from '@swc/core';
import { logger } from 'just-scripts';

const execAsync = promisify(exec);

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

function swcCli(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);
}

export const swc = {
commonjs: () => {
const options: Options = {
configFile: true,
outputPath: 'lib-commonjs',
module: { type: 'commonjs' },
};

return swcCli(options);
},
esm: () => {
const options: Options = {
configFile: true,
outputPath: 'lib',
module: { type: 'es6' },
TristanWatanabe marked this conversation as resolved.
Show resolved Hide resolved
};

return swcCli(options);
},
amd: () => {
const options: Options = {
configFile: true,
outputPath: 'lib-amd',
module: { type: 'amd' },
};

return swcCli(options);
},
};
Loading