cleanup(web): use json utils (#5944)

This commit is contained in:
Phillip Barta 2021-06-16 23:09:30 +02:00 committed by GitHub
parent d5c3a79f5f
commit 9d78629ec0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 163 additions and 204 deletions

View File

@ -1,4 +1,4 @@
import { ExecutorContext } from '@nrwl/devkit';
import type { ExecutorContext } from '@nrwl/devkit';
import * as webpack from 'webpack';
import { Stats } from 'webpack';

View File

@ -1,4 +1,5 @@
import { ExecutorContext, logger, names } from '@nrwl/devkit';
import { readJsonFile, writeJsonFile, logger, names } from '@nrwl/devkit';
import type { ExecutorContext, ProjectGraphNode } from '@nrwl/devkit';
import * as rollup from 'rollup';
import * as peerDepsExternal from 'rollup-plugin-peer-deps-external';
@ -21,14 +22,7 @@ import { catchError, concatMap, last, tap } from 'rxjs/operators';
import { eachValueFrom } from 'rxjs-for-await';
import * as autoprefixer from 'autoprefixer';
import {
readJsonFile,
writeJsonFile,
} from '@nrwl/workspace/src/utilities/fileutils';
import {
createProjectGraph,
ProjectGraphNode,
} from '@nrwl/workspace/src/core/project-graph';
import { createProjectGraph } from '@nrwl/workspace/src/core/project-graph';
import {
calculateProjectDependencies,
checkDependentProjectsHaveBeenBuilt,

View File

@ -1,5 +1,5 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { readJson, DependencyType } from '@nrwl/devkit';
import { readJson, writeJson, DependencyType } from '@nrwl/devkit';
import { createBabelrcForWorkspaceLibs } from './create-babelrc-for-workspace-libs';
import type { ProjectGraph, Tree } from '@nrwl/devkit';
@ -17,47 +17,41 @@ describe('Create missing .babelrc files', () => {
});
it(`should create .babelrc files for libs that are used in '@nrwl/web:build'`, async () => {
tree.write(
'workspace.json',
JSON.stringify({
projects: {
webapp: {
root: 'apps/webapp',
projectType: 'application',
targets: {
build: { executor: '@nrwl/web:build' },
},
},
nodeapp: {
root: 'apps/nodeapp',
projectType: 'application',
targets: {
build: { executor: '@nrwl/node:build' },
},
},
weblib: {
root: 'libs/weblib',
projectType: 'library',
},
nodelib: {
root: 'libs/nodelib',
projectType: 'library',
writeJson(tree, 'workspace.json', {
projects: {
webapp: {
root: 'apps/webapp',
projectType: 'application',
targets: {
build: { executor: '@nrwl/web:build' },
},
},
})
);
tree.write(
'nx.json',
JSON.stringify({
npmScope: 'proj',
projects: {
webapp: {},
nodeapp: {},
weblib: {},
nodelib: {},
nodeapp: {
root: 'apps/nodeapp',
projectType: 'application',
targets: {
build: { executor: '@nrwl/node:build' },
},
},
})
);
weblib: {
root: 'libs/weblib',
projectType: 'library',
},
nodelib: {
root: 'libs/nodelib',
projectType: 'library',
},
},
});
writeJson(tree, 'nx.json', {
npmScope: 'proj',
projects: {
webapp: {},
nodeapp: {},
weblib: {},
nodelib: {},
},
});
tree.write('apps/webapp/index.ts', `import '@proj/weblib';`);
projectGraph = {
@ -116,47 +110,41 @@ describe('Create missing .babelrc files', () => {
});
it('should not error if there are circular dependencies', async () => {
tree.write(
'workspace.json',
JSON.stringify({
projects: {
webapp: {
root: 'apps/webapp',
projectType: 'application',
targets: {
build: { executor: '@nrwl/web:build' },
},
},
nodeapp: {
root: 'apps/nodeapp',
projectType: 'application',
targets: {
build: { executor: '@nrwl/node:build' },
},
},
weblib: {
root: 'libs/weblib',
projectType: 'library',
},
nodelib: {
root: 'libs/nodelib',
projectType: 'library',
writeJson(tree, 'workspace.json', {
projects: {
webapp: {
root: 'apps/webapp',
projectType: 'application',
targets: {
build: { executor: '@nrwl/web:build' },
},
},
})
);
tree.write(
'nx.json',
JSON.stringify({
npmScope: 'proj',
projects: {
webapp: {},
nodeapp: {},
weblib: {},
nodelib: {},
nodeapp: {
root: 'apps/nodeapp',
projectType: 'application',
targets: {
build: { executor: '@nrwl/node:build' },
},
},
})
);
weblib: {
root: 'libs/weblib',
projectType: 'library',
},
nodelib: {
root: 'libs/nodelib',
projectType: 'library',
},
},
});
writeJson(tree, 'nx.json', {
npmScope: 'proj',
projects: {
webapp: {},
nodeapp: {},
weblib: {},
nodelib: {},
},
});
tree.write('apps/webapp/index.ts', `import '@proj/weblib';`);
projectGraph = {

View File

@ -1,4 +1,4 @@
import { formatFiles, getProjects, Tree } from '@nrwl/devkit';
import { formatFiles, getProjects, Tree, writeJson } from '@nrwl/devkit';
import {
createProjectGraph,
reverse,
@ -18,10 +18,7 @@ export async function createBabelrcForWorkspaceLibs(host: Tree) {
if (p.projectType === 'library' && !host.exists(babelrcPath)) {
// Library is included in applications that require .babelrc to
// exist and contain '@nrwl/web/babel' preset.
host.write(
babelrcPath,
JSON.stringify({ presets: ['@nrwl/web/babel'] }, null, 2)
);
writeJson(host, babelrcPath, { presets: ['@nrwl/web/babel'] });
}
}

View File

@ -1,5 +1,5 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { readJson, Tree } from '@nrwl/devkit';
import { readJson, writeJson, Tree } from '@nrwl/devkit';
import { updateExistingBabelrcFiles } from './update-existing-babelrc-files';
describe('Create missing .babelrc files', () => {
@ -10,73 +10,58 @@ describe('Create missing .babelrc files', () => {
});
it(`should add web babel preset if it does not exist`, async () => {
tree.write(
'workspace.json',
JSON.stringify({
projects: {
'missing-babel-presets': {
root: 'apps/missing-babel-presets',
projectType: 'application',
},
'web-app': {
root: 'apps/web-app',
projectType: 'application',
},
'react-app': {
root: 'apps/react-app',
projectType: 'application',
},
'gatsby-app': {
root: 'apps/gatsby-app',
projectType: 'application',
},
'not-using-babel': {
root: 'apps/not-using-babel',
projectType: 'application',
},
'next-app': {
root: 'apps/next-app',
projectType: 'application',
},
writeJson(tree, 'workspace.json', {
projects: {
'missing-babel-presets': {
root: 'apps/missing-babel-presets',
projectType: 'application',
},
})
);
tree.write(
'nx.json',
JSON.stringify({
projects: {
'missing-babel-presets': {},
'web-app': {},
'react-app': {},
'gatsby-app': {},
'not-using-babel': {},
'next-app': {},
'web-app': {
root: 'apps/web-app',
projectType: 'application',
},
})
);
tree.write(
'babel.config.json',
JSON.stringify({
presets: ['@nrwl/web/babel'],
})
);
tree.write('apps/missing-babel-presets/.babelrc', JSON.stringify({}));
tree.write(
'apps/web-app/.babelrc',
JSON.stringify({ presets: ['@nrwl/web/babel'] })
);
tree.write(
'apps/react-app/.babelrc',
JSON.stringify({ presets: ['@nrwl/react/babel'] })
);
tree.write(
'apps/gatsby-app/.babelrc',
JSON.stringify({ presets: ['@nrwl/gatsby/babel'] })
);
tree.write(
'apps/next-app/.babelrc',
JSON.stringify({ presets: ['@nrwl/next/babel'] })
);
'react-app': {
root: 'apps/react-app',
projectType: 'application',
},
'gatsby-app': {
root: 'apps/gatsby-app',
projectType: 'application',
},
'not-using-babel': {
root: 'apps/not-using-babel',
projectType: 'application',
},
'next-app': {
root: 'apps/next-app',
projectType: 'application',
},
},
});
writeJson(tree, 'nx.json', {
projects: {
'missing-babel-presets': {},
'web-app': {},
'react-app': {},
'gatsby-app': {},
'not-using-babel': {},
'next-app': {},
},
});
writeJson(tree, 'babel.config.json', {
presets: ['@nrwl/web/babel'],
});
writeJson(tree, 'apps/missing-babel-presets/.babelrc', {});
writeJson(tree, 'apps/web-app/.babelrc', { presets: ['@nrwl/web/babel'] });
writeJson(tree, 'apps/react-app/.babelrc', {
presets: ['@nrwl/react/babel'],
});
writeJson(tree, 'apps/gatsby-app/.babelrc', {
presets: ['@nrwl/gatsby/babel'],
});
writeJson(tree, 'apps/next-app/.babelrc', {
presets: ['@nrwl/next/babel'],
});
await updateExistingBabelrcFiles(tree);

View File

@ -10,7 +10,7 @@ export async function updateExistingBabelrcFiles(host: Tree) {
// This is needed because we removed it from the root.
if (host.exists(babelrcPath)) {
updateJson(host, babelrcPath, (json) => {
json.presets = json.presets || [];
json.presets ||= [];
if (
-1 ===
json.presets.findIndex(

View File

@ -1,5 +1,5 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { readJson, Tree } from '@nrwl/devkit';
import { readJson, writeJson, Tree } from '@nrwl/devkit';
import { updateRootBabelConfig } from './update-root-babel-config';
describe('Update root babel config', () => {
@ -10,12 +10,9 @@ describe('Update root babel config', () => {
});
it(`should add web babel preset if it does not exist`, async () => {
tree.write(
'babel.config.json',
JSON.stringify({
presets: ['@nrwl/web/babel'],
})
);
writeJson(tree, 'babel.config.json', {
presets: ['@nrwl/web/babel'],
});
await updateRootBabelConfig(tree);

View File

@ -1,12 +1,6 @@
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import {
updateJsonInTree,
readJsonInTree,
updateWorkspaceInTree,
readWorkspace,
getWorkspacePath,
} from '@nrwl/workspace';
import { readWorkspace, updateJsonInTree } from '@nrwl/workspace';
import * as path from 'path';
@ -23,25 +17,27 @@ describe('Update 8-5-0', () => {
});
it(`should remove differentialLoading as an option for build builder`, async () => {
tree.create(
'workspace.json',
JSON.stringify({
projects: {
demo: {
root: 'apps/demo',
sourceRoot: 'apps/demo/src',
architect: {
build: {
builder: '@nrwl/web:build',
options: {
differentialLoading: true,
tree = await schematicRunner
.callRule(
updateJsonInTree('workspace.json', () => ({
projects: {
demo: {
root: 'apps/demo',
sourceRoot: 'apps/demo/src',
architect: {
build: {
builder: '@nrwl/web:build',
options: {
differentialLoading: true,
},
},
},
},
},
},
})
);
})),
tree
)
.toPromise();
tree = await schematicRunner
.runSchematicAsync('update-builder-8.5.0', {}, tree)

View File

@ -1,6 +1,6 @@
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { readWorkspace } from '@nrwl/workspace';
import { readWorkspace, updateJsonInTree } from '@nrwl/workspace';
import * as path from 'path';
@ -17,23 +17,25 @@ describe('set buildLibsFromSource to true', () => {
});
it(`should set buildLibsFromSource to true`, async () => {
tree.create(
'workspace.json',
JSON.stringify({
projects: {
demo: {
root: 'apps/demo',
sourceRoot: 'apps/demo/src',
architect: {
build: {
builder: '@nrwl/web:build',
options: {},
tree = await schematicRunner
.callRule(
updateJsonInTree('workspace.json', () => ({
projects: {
demo: {
root: 'apps/demo',
sourceRoot: 'apps/demo/src',
architect: {
build: {
builder: '@nrwl/web:build',
options: {},
},
},
},
},
},
})
);
})),
tree
)
.toPromise();
tree = await schematicRunner
.runSchematicAsync('set-build-libs-from-source', {}, tree)

View File

@ -1,4 +1,4 @@
module.exports = require('babel-loader').custom((babel) => {
module.exports = require('babel-loader').custom(() => {
return {
customOptions({ isModern, emitDecoratorMetadata, ...loader }) {
return {