@nxext/stencil
Version:
Nx plugin for stenciljs
90 lines • 4.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeLibBuildableGenerator = makeLibBuildableGenerator;
const tslib_1 = require("tslib");
const devkit_1 = require("@nx/devkit");
const stencil_core_utils_1 = require("../../stencil-core-utils");
const stencil_core_utils_2 = require("../../stencil-core-utils");
const update_tsconfig_1 = require("./lib/update-tsconfig");
const targets_1 = require("../../utils/targets");
const generator_1 = require("../storybook-configuration/generator");
/**
* Always the plain, scope-free project name - even when `options.name` (the
* Nx project identifier) is the full scoped importPath in TS-solution mode
* (Design 1.5). Used for Stencil's own generated `unpkg` bundle path, which
* must never contain the npm scope.
*/
function toSimpleProjectName(name) {
return name.startsWith('@') ? name.split('/').pop() : name;
}
function normalize(host, options) {
options.projectRoot = (0, devkit_1.readProjectConfiguration)(host, options.name).root;
options.importPath =
options.importPath || (0, generator_1.getProjectTsImportPath)(host, options.name);
return Object.assign(Object.assign({}, options), { simpleProjectName: toSimpleProjectName(options.name) });
}
function createFiles(host, options) {
// In TS-solution mode the project's package.json already carries
// `name`/`nx.targets`/`nx.generators` written by the library generator's
// `addProjectPackageJson` call (Design 1.4). The template below
// overwrites `package.json` wholesale with Stencil's dist-output shape,
// which has no `nx` field at all - capture whatever was there beforehand
// and reapply it afterwards so registration/target metadata survives
// becoming buildable.
const packageJsonPath = (0, devkit_1.joinPathFragments)(options.projectRoot, 'package.json');
const priorNx = host.exists(packageJsonPath)
? (0, devkit_1.readJson)(host, packageJsonPath).nx
: undefined;
(0, devkit_1.generateFiles)(host, (0, devkit_1.joinPathFragments)(__dirname, './files/lib'), options.projectRoot, options);
if (priorNx) {
(0, devkit_1.updateJson)(host, packageJsonPath, (json) => {
json.nx = priorNx;
return json;
});
}
}
function updateProjectConfig(host, options) {
const projectConfig = (0, devkit_1.readProjectConfiguration)(host, options.name);
projectConfig.targets = projectConfig.targets || {};
// `build` / `serve` / `e2e` are inferred by `@nxext/stencil/plugin` once the
// new `stencil.config.ts` (written below in createFiles) is on disk.
projectConfig.targets.lint = (0, targets_1.getLintTarget)(options.projectRoot);
(0, devkit_1.updateProjectConfiguration)(host, options.name, projectConfig);
}
function makeLibBuildableGenerator(host, schema) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const options = normalize(host, schema);
updateProjectConfig(host, options);
createFiles(host, options);
(0, stencil_core_utils_1.addStylePluginToConfig)(host, (0, devkit_1.joinPathFragments)(options.projectRoot, 'stencil.config.ts'), options.style);
(0, stencil_core_utils_2.addToOutputTargets)(host, [
`{
type: 'dist',
esmLoaderPath: '../loader'
}`,
`{
type: 'dist-custom-elements',
}`,
`{
type: 'docs-readme',
}`,
`{
type: 'www',
serviceWorker: null // disable service workers
}`,
`{
type: 'dist-hydrate-script',
dir: 'dist/hydrate',
}`,
`{
type: 'dist-custom-elements',
customElementsExportBehavior: 'auto-define-custom-elements',
includeGlobalScripts: false,
}`,
], (0, devkit_1.joinPathFragments)(options.projectRoot, 'stencil.config.ts'));
(0, update_tsconfig_1.updateTsConfig)(host, options);
yield (0, devkit_1.formatFiles)(host);
});
}
exports.default = makeLibBuildableGenerator;
//# sourceMappingURL=make-lib-buildable.js.map