@nx/nuxt
Version:
183 lines (182 loc) • 7.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNodesV2 = exports.createNodes = void 0;
const devkit_1 = require("@nx/devkit");
const config_utils_1 = require("@nx/devkit/src/utils/config-utils");
const get_named_inputs_1 = require("@nx/devkit/src/utils/get-named-inputs");
const calculate_hash_for_create_nodes_1 = require("@nx/devkit/src/utils/calculate-hash-for-create-nodes");
const cache_directory_1 = require("nx/src/utils/cache-directory");
const js_1 = require("@nx/js");
const path_1 = require("path");
const fs_1 = require("fs");
const executor_utils_1 = require("../utils/executor-utils");
const util_1 = require("@nx/js/src/plugins/typescript/util");
const cachePath = (0, path_1.join)(cache_directory_1.workspaceDataDirectory, 'nuxt.hash');
const targetsCache = readTargetsCache();
const pmc = (0, devkit_1.getPackageManagerCommand)();
function readTargetsCache() {
return (0, fs_1.existsSync)(cachePath) ? (0, devkit_1.readJsonFile)(cachePath) : {};
}
function writeTargetsToCache() {
const oldCache = readTargetsCache();
(0, devkit_1.writeJsonFile)(cachePath, {
...oldCache,
...targetsCache,
});
}
exports.createNodes = [
'**/nuxt.config.{js,ts,mjs,mts,cjs,cts}',
async (files, options, context) => {
//TODO(@nrwl/nx-vue-reviewers): This should batch hashing like our other plugins.
const result = await (0, devkit_1.createNodesFromFiles)(createNodesInternal, files, options, context);
writeTargetsToCache();
return result;
},
];
exports.createNodesV2 = exports.createNodes;
async function createNodesInternal(configFilePath, options, context) {
const projectRoot = (0, path_1.dirname)(configFilePath);
// Do not create a project if package.json and project.json isn't there.
const siblingFiles = (0, fs_1.readdirSync)((0, path_1.join)(context.workspaceRoot, projectRoot));
if (!siblingFiles.includes('package.json') &&
!siblingFiles.includes('project.json')) {
return {};
}
options = normalizeOptions(options);
const hash = await (0, calculate_hash_for_create_nodes_1.calculateHashForCreateNodes)(projectRoot, options, context, [(0, js_1.getLockFileName)((0, devkit_1.detectPackageManager)(context.workspaceRoot))]);
targetsCache[hash] ??= await buildNuxtTargets(configFilePath, projectRoot, options, context);
return {
projects: {
[projectRoot]: {
root: projectRoot,
targets: targetsCache[hash],
},
},
};
}
async function buildNuxtTargets(configFilePath, projectRoot, options, context) {
const nuxtConfig = await getInfoFromNuxtConfig(configFilePath, context, projectRoot);
const { buildOutputs } = getOutputs(nuxtConfig, projectRoot);
const namedInputs = (0, get_named_inputs_1.getNamedInputs)(projectRoot, context);
const targets = {};
targets[options.buildTargetName] = buildTarget(options.buildTargetName, namedInputs, buildOutputs, projectRoot);
targets[options.serveTargetName] = serveTarget(projectRoot);
targets[options.serveStaticTargetName] = serveStaticTarget(options);
targets[options.buildStaticTargetName] = buildStaticTarget(options.buildStaticTargetName, namedInputs, buildOutputs, projectRoot);
(0, util_1.addBuildAndWatchDepsTargets)(context.workspaceRoot, projectRoot, targets, options, pmc);
return targets;
}
function buildTarget(buildTargetName, namedInputs, buildOutputs, projectRoot) {
return {
command: `nuxt build`,
options: { cwd: projectRoot },
cache: true,
dependsOn: [`^${buildTargetName}`],
inputs: [
...('production' in namedInputs
? ['production', '^production']
: ['default', '^default']),
{
externalDependencies: ['nuxt'],
},
],
outputs: buildOutputs,
};
}
function serveTarget(projectRoot) {
const targetConfig = {
command: `nuxt dev`,
options: {
cwd: projectRoot,
},
continuous: true,
};
return targetConfig;
}
function serveStaticTarget(options) {
const targetConfig = {
dependsOn: [`${options.buildStaticTargetName}`],
continuous: true,
executor: '@nx/web:file-server',
options: {
buildTarget: `${options.buildStaticTargetName}`,
staticFilePath: '{projectRoot}/dist',
port: 4200,
// Routes are found correctly with serve-static
spa: false,
},
};
return targetConfig;
}
function buildStaticTarget(buildStaticTargetName, namedInputs, buildOutputs, projectRoot) {
const targetConfig = {
command: `nuxt build --prerender`,
options: { cwd: projectRoot },
cache: true,
dependsOn: [`^${buildStaticTargetName}`],
inputs: [
...('production' in namedInputs
? ['production', '^production']
: ['default', '^default']),
{
externalDependencies: ['nuxt'],
},
],
outputs: buildOutputs,
};
return targetConfig;
}
async function getInfoFromNuxtConfig(configFilePath, context, projectRoot) {
let config;
if (process.env.NX_ISOLATE_PLUGINS !== 'false') {
config = await (await (0, executor_utils_1.loadNuxtKitDynamicImport)()).loadNuxtConfig({
configFile: configFilePath,
});
}
else {
config = await (0, config_utils_1.loadConfigFile)((0, path_1.join)(context.workspaceRoot, configFilePath));
}
return {
buildDir: config?.buildDir ??
// Match .nuxt default build dir from '@nuxt/schema'
// See: https://github.com/nuxt/nuxt/blob/871404ae5673425aeedde82f123ea58aa7c6facf/packages/schema/src/config/common.ts#L117-L119
'.nuxt',
};
}
function getOutputs(nuxtConfig, projectRoot) {
const buildOutputPath = normalizeOutputPath(nuxtConfig?.buildDir, projectRoot);
return {
buildOutputs: [buildOutputPath, '{projectRoot}/.output'],
};
}
function normalizeOutputPath(outputPath, projectRoot) {
if (!outputPath) {
if (projectRoot === '.') {
return `{projectRoot}`;
}
else {
return `{workspaceRoot}/{projectRoot}`;
}
}
else {
if ((0, path_1.isAbsolute)(outputPath)) {
return `{workspaceRoot}/${(0, path_1.relative)(devkit_1.workspaceRoot, outputPath)}`;
}
else {
if (outputPath.startsWith('..')) {
return (0, path_1.join)('{workspaceRoot}', (0, path_1.join)(projectRoot, outputPath));
}
else {
return (0, path_1.join)('{projectRoot}', outputPath);
}
}
}
}
function normalizeOptions(options) {
options ??= {};
options.buildTargetName ??= 'build';
options.serveTargetName ??= 'serve';
options.serveStaticTargetName ??= 'serve-static';
options.buildStaticTargetName ??= 'build-static';
return options;
}