Skip to content

Commit

Permalink
feat(react): add swc transpile option rather than tsc+babel
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored and Jack Hsu committed Sep 2, 2021
1 parent 44db568 commit 6680bcb
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 51 deletions.
8 changes: 8 additions & 0 deletions docs/angular/api-react/generators/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ Possible values: `css`, `scss`, `styl`, `less`, `styled-components`, `@emotion/s

The file extension to be used for style files.

### swc

Default: `false`

Type: `boolean`

Use SWC to transpile code instead of Babel and TSC. Note that this will skip type-checking.

### tags

Alias(es): t
Expand Down
8 changes: 8 additions & 0 deletions docs/angular/api-web/executors/package.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ Type: `array[] | string `

Path to a function which takes a rollup config and returns an updated rollup config

### swc

Default: `false`

Type: `boolean`

Use SWC to transpile code instead of Babel and TSC. Note that this will skip type-checking.

### umdName

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/node/api-react/generators/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ Possible values: `css`, `scss`, `styl`, `less`, `styled-components`, `@emotion/s

The file extension to be used for style files.

### swc

Default: `false`

Type: `boolean`

Use SWC to transpile code instead of Babel and TSC. Note that this will skip type-checking.

### tags

Alias(es): t
Expand Down
8 changes: 8 additions & 0 deletions docs/node/api-web/executors/package.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ Type: `array[] | string `

Path to a function which takes a rollup config and returns an updated rollup config

### swc

Default: `false`

Type: `boolean`

Use SWC to transpile code instead of Babel and TSC. Note that this will skip type-checking.

### umdName

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/react/api-react/generators/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ Possible values: `css`, `scss`, `styl`, `less`, `styled-components`, `@emotion/s

The file extension to be used for style files.

### swc

Default: `false`

Type: `boolean`

Use SWC to transpile code instead of Babel and TSC. Note that this will skip type-checking.

### tags

Alias(es): t
Expand Down
8 changes: 8 additions & 0 deletions docs/react/api-web/executors/package.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ Type: `array[] | string `

Path to a function which takes a rollup config and returns an updated rollup config

### swc

Default: `false`

Type: `boolean`

Use SWC to transpile code instead of Babel and TSC. Note that this will skip type-checking.

### umdName

Type: `string`
Expand Down
2 changes: 1 addition & 1 deletion e2e/react/src/react-package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('Build React libraries and apps', () => {
`generate @nrwl/react:library ${buildableChildLib} --buildable --no-interactive`
);
runCLI(
`generate @nrwl/react:library ${buildableChildLib2} --buildable --no-interactive`
`generate @nrwl/react:library ${buildableChildLib2} --buildable --no-interactive --swc`
);

createDep(buildableParentLib, [buildableChildLib, buildableChildLib2]);
Expand Down
36 changes: 35 additions & 1 deletion packages/react/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getProjects, readJson, Tree, updateJson } from '@nrwl/devkit';
import {
getProjects,
readJson,
Tree,
updateJson,
readProjectConfiguration,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import libraryGenerator from './library';
import { Linter } from '@nrwl/linter';
Expand Down Expand Up @@ -615,6 +621,34 @@ describe('lib', () => {
});
});

describe('--swc', () => {
it('should use SWC to build the library when --swc=true', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
buildable: true,
experimentalSwc: true,
});

const packageJson = readJson(appTree, '/package.json');
const config = readProjectConfiguration(appTree, 'my-lib');
expect(packageJson.devDependencies['@swc/core']).toMatch(
/\^?\d+\.\d+\.\d+$/
);
expect(config.targets.build.options.swc).toBe(true);
});

it('should not use SWC to build the library when --swc=false', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
buildable: true,
experimentalSwc: false,
});

const config = readProjectConfiguration(appTree, 'my-lib');
expect(config.targets.build.options.swc).not.toBeDefined();
});
});

it.each`
style
${'styled-components'}
Expand Down
45 changes: 24 additions & 21 deletions packages/react/src/generators/library/library.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
import { CSS_IN_JS_DEPENDENCIES } from '../../utils/styled';

import * as ts from 'typescript';
import { assertValidStyle } from '../../utils/assertion';
import {
addBrowserRouter,
addInitialRoutes,
addRoute,
findComponentImportPath,
} from '../../utils/ast-utils';
import {
extraEslintDependencies,
createReactEslintJson,
} from '../../utils/lint';
import {
reactDomVersion,
reactRouterDomVersion,
reactVersion,
typesReactRouterDomVersion,
} from '../../utils/versions';
import { Schema } from './schema';
import {
addDependenciesToPackageJson,
addProjectConfiguration,
Expand All @@ -37,6 +17,25 @@ import {
Tree,
updateJson,
} from '@nrwl/devkit';
import { assertValidStyle } from '../../utils/assertion';
import {
addBrowserRouter,
addInitialRoutes,
addRoute,
findComponentImportPath,
} from '../../utils/ast-utils';
import {
createReactEslintJson,
extraEslintDependencies,
} from '../../utils/lint';
import {
reactDomVersion,
reactRouterDomVersion,
reactVersion,
swcCoreVersion,
typesReactRouterDomVersion,
} from '../../utils/versions';
import { Schema } from './schema';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
import init from '../init/init';
import { Linter, lintProjectGenerator } from '@nrwl/linter';
Expand Down Expand Up @@ -121,7 +120,7 @@ export async function libraryGenerator(host: Tree, schema: Schema) {
react: reactVersion,
'react-dom': reactDomVersion,
},
{}
options.experimentalSwc ? { '@swc/core': swcCoreVersion } : {}
);
tasks.push(installTask);

Expand Down Expand Up @@ -200,6 +199,10 @@ function addProject(host: Tree, options: NormalizedSchema) {
],
},
};

if (options.experimentalSwc) {
targets.build.options.swc = true;
}
}

addProjectConfiguration(
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/generators/library/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export interface Schema {
strict?: boolean;
setParserOptionsProject?: boolean;
standaloneConfig?: boolean;
experimentalSwc?: boolean;
}
5 changes: 5 additions & 0 deletions packages/react/src/generators/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@
"description": "Split the project configuration into <projectRoot>/project.json rather than including it inside workspace.json",
"type": "boolean",
"default": false
},
"experimentalSwc": {
"description": "Use SWC to transpile code instead of Babel and TSC. Note that this will skip type-checking.",
"type": "boolean",
"default": false
}
},
"required": ["name"]
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ export const eslintPluginReactVersion = '7.23.1';
export const eslintPluginReactHooksVersion = '4.2.0';

export const babelPluginStyledComponentsVersion = '1.10.7';

export const swcCoreVersion = '^1.2.78';
20 changes: 20 additions & 0 deletions packages/web/src/executors/package/lib/swc-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Plugin } from 'rollup';

export function swc(options = {}): Plugin {
try {
const { transform } = require('@swc/core');
return {
name: 'nx-swc',
transform(code, filename) {
return transform(code, {
filename,
...options,
});
},
};
} catch {
throw new Error(
'"@swc/core" not installed in the workspace. Try `npm install --save-dev @swc/core` or `yarn add -D @swc/core`'
);
}
}
62 changes: 35 additions & 27 deletions packages/web/src/executors/package/package.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
NormalizedWebPackageOptions,
normalizePackageOptions,
} from './lib/normalize';
import { swc } from './lib/swc-plugin';

// These use require because the ES import isn't correct.
const commonjs = require('@rollup/plugin-commonjs');
Expand Down Expand Up @@ -171,13 +172,6 @@ export function createRollupOptions(
),
}),
image(),
typescript({
check: true,
tsconfig: options.tsConfig,
tsconfigOverride: {
compilerOptions: createCompilerOptions(format, options, dependencies),
},
}),
peerDepsExternal({
packageJsonPath: options.project,
}),
Expand All @@ -191,29 +185,43 @@ export function createRollupOptions(
preferBuiltins: true,
extensions: fileExtensions,
}),
getBabelInputPlugin({
// Let's `@nrwl/web/babel` preset know that we are packaging.
caller: {
// Ignored since this is for our custom babel-loader + babel preset
// @ts-ignore
isNxPackage: true,
},
cwd: join(context.root, sourceRoot),
rootMode: 'upward',
babelrc: true,
extensions: fileExtensions,
babelHelpers: 'bundled',
exclude: /node_modules/,
plugins: [
format === 'esm'
? undefined
: require.resolve('babel-plugin-transform-async-to-promises'),
].filter(Boolean),
}),
options.experimentalSwc && swc(),
!options.experimentalSwc &&
typescript({
check: true,
tsconfig: options.tsConfig,
tsconfigOverride: {
compilerOptions: createCompilerOptions(
format,
options,
dependencies
),
},
}),
!options.experimentalSwc &&
getBabelInputPlugin({
// Let's `@nrwl/web/babel` preset know that we are packaging.
caller: {
// Ignored since this is for our custom babel-loader + babel preset
// @ts-ignore
isNxPackage: true,
},
cwd: join(context.root, sourceRoot),
rootMode: 'upward',
babelrc: true,
extensions: fileExtensions,
babelHelpers: 'bundled',
exclude: /node_modules/,
plugins: [
format === 'esm'
? undefined
: require.resolve('babel-plugin-transform-async-to-promises'),
].filter(Boolean),
}),
commonjs(),
filesize(),
json(),
];
].filter(Boolean);

const globals = options.globals
? options.globals.reduce(
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/executors/package/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export interface WebPackageOptions {
umdName?: string;
deleteOutputPath?: boolean;
format: string[];
experimentalSwc?: boolean;
}
5 changes: 5 additions & 0 deletions packages/web/src/executors/package/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@
"items": {
"$ref": "#/definitions/assetPattern"
}
},
"experimentalSwc": {
"type": "boolean",
"description": "Use SWC to transpile code instead of Babel and TSC. Note that this will skip type-checking.",
"default": false
}
},
"required": ["tsConfig", "project", "entryFile", "outputPath"],
Expand Down
1 change: 1 addition & 0 deletions scripts/check-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const scoped = [
'babel',
'emotion',
'reduxjs',
'swc',
'testing-library',
'types',
'zeit',
Expand Down
6 changes: 5 additions & 1 deletion scripts/depcheck/missing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ const IGNORE_MATCHES = {
'@angular-devkit/core',
'@angular-devkit/architect',
],
web: ['fibers', 'node-sass'],
web: [
'@swc/core', // we don't want to bloat the install of @nrwl/web by including @swc/core as a dependency.
'fibers',
'node-sass',
],
workspace: [
'tslint',
'@angular-devkit/architect',
Expand Down

0 comments on commit 6680bcb

Please sign in to comment.