cleanup(core): create a shared add-babel-input function (#13538)

This commit is contained in:
Katerina Skroumpelou 2022-12-01 22:19:04 +02:00 committed by GitHub
parent 2fc86e23d4
commit 3a39f95a3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 62 additions and 206 deletions

View File

@ -0,0 +1,37 @@
import {
formatFiles,
joinPathFragments,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
writeJson,
} from '@nrwl/devkit';
export async function addBabelInputs(tree: Tree) {
const workspaceConfiguration = readWorkspaceConfiguration(tree);
let globalBabelFile = ['babel.config.js', 'babel.config.json'].find((file) =>
tree.exists(file)
);
if (!globalBabelFile) {
writeJson(tree, '/babel.config.json', {
babelrcRoots: ['*'], // Make sure .babelrc files other than root can be loaded in a monorepo
});
globalBabelFile = 'babel.config.json';
}
if (workspaceConfiguration.namedInputs?.sharedGlobals) {
const sharedGlobalFileset = new Set(
workspaceConfiguration.namedInputs.sharedGlobals
);
sharedGlobalFileset.add(
joinPathFragments('{workspaceRoot}', globalBabelFile)
);
workspaceConfiguration.namedInputs.sharedGlobals =
Array.from(sharedGlobalFileset);
}
updateWorkspaceConfiguration(tree, workspaceConfiguration);
await formatFiles(tree);
}

View File

@ -28,6 +28,7 @@
"@nrwl/detox": "file:../detox",
"@nrwl/devkit": "file:../devkit",
"@nrwl/jest": "file:../jest",
"@nrwl/js": "file:../js",
"@nrwl/linter": "file:../linter",
"@nrwl/react": "file:../react",
"@nrwl/storybook": "file:../storybook",

View File

@ -9,6 +9,8 @@ import {
} from '@nrwl/devkit';
import { Schema } from './schema';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs';
import { jestInitGenerator } from '@nrwl/jest';
import { detoxInitGenerator } from '@nrwl/detox';
import {
@ -37,11 +39,10 @@ import {
} from '../../utils/versions';
import { addGitIgnoreEntry } from './lib/add-git-ignore-entry';
import { initRootBabelConfig } from './lib/init-root-babel-config';
export async function reactNativeInitGenerator(host: Tree, schema: Schema) {
addGitIgnoreEntry(host);
initRootBabelConfig(host);
addBabelInputs(host);
const tasks: GeneratorCallback[] = [];

View File

@ -1,25 +0,0 @@
import {
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
writeJson,
} from '@nrwl/devkit';
export function initRootBabelConfig(tree: Tree) {
if (tree.exists('/babel.config.json') || tree.exists('/babel.config.js')) {
return;
}
writeJson(tree, '/babel.config.json', {
babelrcRoots: ['*'], // Make sure .babelrc files other than root can be loaded in a monorepo
});
const workspaceConfiguration = readWorkspaceConfiguration(tree);
if (workspaceConfiguration.namedInputs?.sharedGlobals) {
workspaceConfiguration.namedInputs.sharedGlobals.push(
'{workspaceRoot}/babel.config.json'
);
}
updateWorkspaceConfiguration(tree, workspaceConfiguration);
}

View File

@ -1,6 +1,5 @@
import { getProjects, Tree } from '@nrwl/devkit';
import { initRootBabelConfig } from '../../generators/init/lib/init-root-babel-config';
import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs';
export default async function update(tree: Tree) {
const projects = getProjects(tree);
@ -17,6 +16,6 @@ export default async function update(tree: Tree) {
);
if (hasReactNaiveProject) {
initRootBabelConfig(tree);
addBabelInputs(tree);
}
}

View File

@ -1,30 +1,6 @@
import {
formatFiles,
joinPathFragments,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import { Tree } from '@nrwl/devkit';
import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs';
export default async function (tree: Tree) {
const workspaceConfiguration = readWorkspaceConfiguration(tree);
const globalBabelFile = ['babel.config.js', 'babel.config.json'].find(
(file) => tree.exists(file)
);
if (globalBabelFile && workspaceConfiguration.namedInputs?.sharedGlobals) {
const sharedGlobalFileset = new Set(
workspaceConfiguration.namedInputs.sharedGlobals
);
sharedGlobalFileset.add(
joinPathFragments('{workspaceRoot}', globalBabelFile)
);
workspaceConfiguration.namedInputs.sharedGlobals =
Array.from(sharedGlobalFileset);
}
updateWorkspaceConfiguration(tree, workspaceConfiguration);
await formatFiles(tree);
addBabelInputs(tree);
}

View File

@ -3,20 +3,18 @@ import {
convertNxGenerator,
formatFiles,
GeneratorCallback,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
writeJson,
} from '@nrwl/devkit';
import { Schema } from './schema';
import { swcCoreVersion, swcHelpersVersion } from '@nrwl/js/src/utils/versions';
import { swcLoaderVersion, tsLibVersion } from '../../utils/versions';
import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs';
export async function rollupInitGenerator(tree: Tree, schema: Schema) {
let task: GeneratorCallback;
if (schema.compiler === 'babel') {
initRootBabelConfig(tree);
addBabelInputs(tree);
}
if (schema.compiler === 'swc') {
@ -42,25 +40,6 @@ export async function rollupInitGenerator(tree: Tree, schema: Schema) {
return task;
}
function initRootBabelConfig(tree: Tree) {
if (tree.exists('/babel.config.json') || tree.exists('/babel.config.js')) {
return;
}
writeJson(tree, '/babel.config.json', {
babelrcRoots: ['*'], // Make sure .babelrc files other than root can be loaded in a monorepo
});
const workspaceConfiguration = readWorkspaceConfiguration(tree);
if (workspaceConfiguration.namedInputs?.sharedGlobals) {
workspaceConfiguration.namedInputs.sharedGlobals.push(
'{workspaceRoot}/babel.config.json'
);
}
updateWorkspaceConfiguration(tree, workspaceConfiguration);
}
export default rollupInitGenerator;
export const rollupInitSchematic = convertNxGenerator(rollupInitGenerator);

View File

@ -1,30 +1,6 @@
import {
formatFiles,
joinPathFragments,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import { Tree } from '@nrwl/devkit';
import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs';
export default async function (tree: Tree) {
const workspaceConfiguration = readWorkspaceConfiguration(tree);
const globalBabelFile = ['babel.config.js', 'babel.config.json'].find(
(file) => tree.exists(file)
);
if (globalBabelFile && workspaceConfiguration.namedInputs?.sharedGlobals) {
const sharedGlobalFileset = new Set(
workspaceConfiguration.namedInputs.sharedGlobals
);
sharedGlobalFileset.add(
joinPathFragments('{workspaceRoot}', globalBabelFile)
);
workspaceConfiguration.namedInputs.sharedGlobals =
Array.from(sharedGlobalFileset);
}
updateWorkspaceConfiguration(tree, workspaceConfiguration);
await formatFiles(tree);
addBabelInputs(tree);
}

View File

@ -18,6 +18,7 @@ import {
typesNodeVersion,
} from '../../utils/versions';
import { Schema } from './schema';
import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs';
function updateDependencies(tree: Tree, schema: Schema) {
removeDependenciesFromPackageJson(tree, ['@nrwl/web'], []);
@ -42,27 +43,6 @@ function updateDependencies(tree: Tree, schema: Schema) {
);
}
function initRootBabelConfig(tree: Tree, schema: Schema) {
if (tree.exists('/babel.config.json') || tree.exists('/babel.config.js')) {
return;
}
if (!schema.skipBabelConfig) {
writeJson(tree, '/babel.config.json', {
babelrcRoots: ['*'], // Make sure .babelrc files other than root can be loaded in a monorepo
});
}
const workspaceConfiguration = readWorkspaceConfiguration(tree);
if (workspaceConfiguration.namedInputs?.sharedGlobals) {
workspaceConfiguration.namedInputs.sharedGlobals.push(
'{workspaceRoot}/babel.config.json'
);
}
updateWorkspaceConfiguration(tree, workspaceConfiguration);
}
export async function webInitGenerator(tree: Tree, schema: Schema) {
let tasks: GeneratorCallback[] = [];
@ -82,7 +62,8 @@ export async function webInitGenerator(tree: Tree, schema: Schema) {
const installTask = updateDependencies(tree, schema);
tasks.push(installTask);
}
initRootBabelConfig(tree, schema);
addBabelInputs(tree);
if (!schema.skipFormat) {
await formatFiles(tree);
}

View File

@ -1,30 +1,6 @@
import {
formatFiles,
joinPathFragments,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import { Tree } from '@nrwl/devkit';
import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs';
export default async function (tree: Tree) {
const workspaceConfiguration = readWorkspaceConfiguration(tree);
const globalBabelFile = ['babel.config.js', 'babel.config.json'].find(
(file) => tree.exists(file)
);
if (globalBabelFile && workspaceConfiguration.namedInputs?.sharedGlobals) {
const sharedGlobalFileset = new Set(
workspaceConfiguration.namedInputs.sharedGlobals
);
sharedGlobalFileset.add(
joinPathFragments('{workspaceRoot}', globalBabelFile)
);
workspaceConfiguration.namedInputs.sharedGlobals =
Array.from(sharedGlobalFileset);
}
updateWorkspaceConfiguration(tree, workspaceConfiguration);
await formatFiles(tree);
addBabelInputs(tree);
}

View File

@ -3,10 +3,7 @@ import {
convertNxGenerator,
formatFiles,
GeneratorCallback,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
writeJson,
} from '@nrwl/devkit';
import { Schema } from './schema';
import { swcCoreVersion } from '@nrwl/js/src/utils/versions';
@ -15,12 +12,13 @@ import {
swcLoaderVersion,
tsLibVersion,
} from '../../utils/versions';
import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs';
export async function webpackInitGenerator(tree: Tree, schema: Schema) {
let task: GeneratorCallback;
if (schema.compiler === 'babel') {
initRootBabelConfig(tree);
addBabelInputs(tree);
}
if (schema.compiler === 'swc') {
@ -46,25 +44,6 @@ export async function webpackInitGenerator(tree: Tree, schema: Schema) {
return task;
}
function initRootBabelConfig(tree: Tree) {
if (tree.exists('/babel.config.json') || tree.exists('/babel.config.js')) {
return;
}
writeJson(tree, '/babel.config.json', {
babelrcRoots: ['*'], // Make sure .babelrc files other than root can be loaded in a monorepo
});
const workspaceConfiguration = readWorkspaceConfiguration(tree);
if (workspaceConfiguration.namedInputs?.sharedGlobals) {
workspaceConfiguration.namedInputs.sharedGlobals.push(
'{workspaceRoot}/babel.config.json'
);
}
updateWorkspaceConfiguration(tree, workspaceConfiguration);
}
export default webpackInitGenerator;
export const webpackInitSchematic = convertNxGenerator(webpackInitGenerator);

View File

@ -1,30 +1,6 @@
import {
formatFiles,
joinPathFragments,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import { Tree } from '@nrwl/devkit';
import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs';
export default async function (tree: Tree) {
const workspaceConfiguration = readWorkspaceConfiguration(tree);
const globalBabelFile = ['babel.config.js', 'babel.config.json'].find(
(file) => tree.exists(file)
);
if (globalBabelFile && workspaceConfiguration.namedInputs?.sharedGlobals) {
const sharedGlobalFileset = new Set(
workspaceConfiguration.namedInputs.sharedGlobals
);
sharedGlobalFileset.add(
joinPathFragments('{workspaceRoot}', globalBabelFile)
);
workspaceConfiguration.namedInputs.sharedGlobals =
Array.from(sharedGlobalFileset);
}
updateWorkspaceConfiguration(tree, workspaceConfiguration);
await formatFiles(tree);
addBabelInputs(tree);
}