@nx/web
Version:
84 lines (83 loc) • 3.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.webStaticServeGenerator = webStaticServeGenerator;
const devkit_1 = require("@nx/devkit");
async function webStaticServeGenerator(tree, options) {
const opts = await normalizeOptions(tree, options);
addStaticConfig(tree, opts);
}
async function normalizeOptions(tree, options) {
let projectGraph;
try {
projectGraph = (0, devkit_1.readCachedProjectGraph)();
}
catch (e) {
projectGraph = await (0, devkit_1.createProjectGraphAsync)();
}
const target = (0, devkit_1.parseTargetString)(options.buildTarget, projectGraph);
const opts = {
...options,
targetName: options.targetName || 'serve-static',
projectName: target.project,
spa: options.spa ?? true,
parsedBuildTarget: target.target,
};
const projectConfig = (0, devkit_1.readProjectConfiguration)(tree, target.project);
const buildTargetConfig = projectConfig?.targets?.[target.target];
if (!buildTargetConfig) {
throw new Error((0, devkit_1.stripIndents) `Unable to read the target configuration for the provided build target, ${opts.buildTarget}
Are you sure this target exists?`);
}
if (projectConfig.targets[opts.targetName]) {
throw new Error((0, devkit_1.stripIndents) `Project ${target.project} already has a '${opts.targetName}' target configured.
Either rename or remove the existing '${opts.targetName}' target and try again.
Optionally, you can provide a different name with the --target-name option other than '${opts.targetName}'`);
}
// NOTE: @nx/web:file-server only looks for the outputPath option
if (!buildTargetConfig.options?.outputPath && !opts.outputPath) {
// attempt to find the suitable path from the outputs
let maybeOutputValue;
for (const o of buildTargetConfig?.outputs || []) {
const isInterpolatedOutput = o.trim().startsWith('{options.');
if (!isInterpolatedOutput) {
continue;
}
const noBracketParts = o.replace(/[{}]/g, '').split('.');
if (noBracketParts.length === 2 && noBracketParts?.[1]) {
const key = noBracketParts[1].trim();
const value = buildTargetConfig.options?.[key];
if (value) {
maybeOutputValue = value;
break;
}
}
}
// NOTE: outputDir is the storybook option.
opts.outputPath = buildTargetConfig.options?.outputDir || maybeOutputValue;
if (opts.outputPath) {
devkit_1.logger.warn(`Automatically detected the output path to be ${opts.outputPath}.
If this is incorrect, the update the staticFilePath option in the ${target.project}:${opts.targetName} target configuration`);
}
else {
devkit_1.logger.warn((0, devkit_1.stripIndents) `${opts.buildTarget} did not have an outputPath property set and --output-path was not provided.
Without either options, the static serve will most likely be unable to serve your project.
It's recommend to provide a --output-path option in this case.`);
}
}
return opts;
}
function addStaticConfig(tree, opts) {
const projectConfig = (0, devkit_1.readProjectConfiguration)(tree, opts.projectName);
const staticServeOptions = {
executor: '@nx/web:file-server',
dependsOn: [opts.parsedBuildTarget],
options: {
buildTarget: opts.buildTarget,
staticFilePath: opts.outputPath,
spa: opts.spa,
},
};
projectConfig.targets[opts.targetName] = staticServeOptions;
(0, devkit_1.updateProjectConfiguration)(tree, opts.projectName, projectConfig);
}
exports.default = webStaticServeGenerator;
;