Last active
October 16, 2019 21:34
-
-
Save bcabanes/25da82a2f914e9236e55ca463ad1a5a7 to your computer and use it in GitHub Desktop.
Nx schematic adding universal capabilities to an Nx application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
apply, | |
chain, | |
externalSchematic, | |
mergeWith, | |
move, | |
Rule, | |
template, | |
Tree, | |
url | |
} from '@angular-devkit/schematics'; | |
import { names } from '@nrwl/workspace'; | |
interface UniversalAppSchema { | |
name: string; | |
} | |
export default function(schema: UniversalAppSchema): Rule { | |
return chain([ | |
externalSchematic('@nrwl/angular', 'application', { | |
name: schema.name | |
}), | |
externalSchematic('@schematics/angular', 'universal', { | |
clientProject: schema.name | |
}), | |
updateOutputPath(schema.name), | |
externalSchematic('@nrwl/node', 'app', { | |
name: `${schema.name}-ssr`, | |
framework: 'none', | |
directory: '', | |
}), | |
updateServerApp(schema.name), | |
addContactTarget(`${schema.name}-ssr`) | |
]); | |
} | |
function updateOutputPath(name: string): Rule { | |
return (host: Tree) => { | |
const angularJson = JSON.parse(host.read('angular.json').toString()); | |
const serverTargetFixed = angularJson.projects[name].architect.server; | |
serverTargetFixed.options.outputPath = `dist/apps/${name}/server`; | |
angularJson.projects[name].architect.server = serverTargetFixed; | |
host.overwrite('angular.json', JSON.stringify(angularJson)); | |
} | |
} | |
function addConcatTarget(name: string): Rule { | |
return (host: Tree) => { | |
const serveAll = { | |
builder: "@angular-devkit/architect:concat", | |
options: { | |
targets: [ | |
{ target: `${name}:build` }, | |
{ target: `${name}:server` }, | |
{ target: `${name}-ssr:serve` } | |
] | |
} | |
}; | |
const angularJson = JSON.parse(host.read('angular.json').toString()); | |
angularJson.projects[`${name}-ssr`].architect["serve-all"] = serveAll; | |
host.overwrite('angular.json', JSON.stringify(angularJson)); | |
} | |
} | |
function updateServerApp(name: string): Rule { | |
return chain([(host: Tree) => { | |
host.delete(`apps/${name}-ssr/src/main.ts`); | |
}, | |
mergeWith( | |
apply(url('./files'), [ | |
template({ | |
tmpl: '', | |
...names(name) | |
}), | |
move(`apps/${name}-ssr/src`) | |
]) | |
)]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment