UNPKG

@nx/next

Version:

The Next.js plugin for Nx contains executors and generators for managing Next.js applications and libraries within an Nx workspace. It provides: - Scaffolding for creating, building, serving, linting, and testing Next.js applications. - Integration wit

118 lines (117 loc) 5.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createApplicationFiles = createApplicationFiles; const path_1 = require("path"); const devkit_1 = require("@nx/devkit"); const js_1 = require("@nx/js"); const create_application_files_helpers_1 = require("./create-application-files.helpers"); const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup"); const version_utils_1 = require("../../../utils/version-utils"); async function createApplicationFiles(host, options) { const offsetFromRoot = (0, devkit_1.offsetFromRoot)(options.appProjectRoot); const layoutTypeSrcPath = (0, devkit_1.joinPathFragments)(offsetFromRoot, options.appProjectRoot, '.next/types/**/*.ts'); const layoutTypeDistPath = (0, devkit_1.joinPathFragments)(offsetFromRoot, options.outputPath, '.next/types/**/*.ts'); const rootPath = options.rootProject || (0, ts_solution_setup_1.isUsingTsSolutionSetup)(host) ? options.src ? 'src/' : options.appDir ? 'app/' : 'pages/' : ''; const templateVariables = { ...(0, devkit_1.names)(options.projectSimpleName), ...options, dot: '.', tmpl: '', offsetFromRoot, appDirType: options.appDir ? 'app' : 'pages', layoutTypeSrcPath, rootPath, layoutTypeDistPath, rootTsConfigPath: (0, js_1.getRelativePathToRootTsConfig)(host, options.appProjectRoot), appContent: (0, create_application_files_helpers_1.createAppJsx)(options.projectName), styleContent: (0, create_application_files_helpers_1.createStyleRules)(), pageStyleContent: `.page {}`, stylesExt: options.style === 'less' ? options.style : 'css', isUsingTsSolutionSetup: (0, ts_solution_setup_1.isUsingTsSolutionSetup)(host), isNext16: await (0, version_utils_1.isNext16)(host), }; const generatedAppFilePath = options.src ? (0, path_1.join)(options.appProjectRoot, 'src') : options.appProjectRoot; (0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, '../files/common'), options.appProjectRoot, templateVariables); if (options.appDir) { (0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, '../files/app'), (0, path_1.join)(generatedAppFilePath, 'app'), templateVariables); if (options.unitTestRunner === 'none') { host.delete((0, devkit_1.joinPathFragments)(options.appProjectRoot, 'specs', `index.spec.${options.js ? 'jsx' : 'tsx'}`)); } if (options.style === 'styled-components') { (0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, '../files/app-styled-components'), (0, path_1.join)(generatedAppFilePath, 'app'), templateVariables); } else if (options.style === 'styled-jsx') { (0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, '../files/app-styled-jsx'), (0, path_1.join)(generatedAppFilePath, 'app'), templateVariables); } else { (0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, '../files/app-default-layout'), (0, path_1.join)(generatedAppFilePath, 'app'), templateVariables); } } else { (0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, '../files/pages'), (0, path_1.join)(generatedAppFilePath, 'pages'), templateVariables); } if (options.rootProject) { (0, devkit_1.updateJson)(host, 'tsconfig.base.json', (json) => { const appJSON = (0, devkit_1.readJson)(host, 'tsconfig.json'); let { extends: _, ...updatedJson } = json; // Don't generate the `paths` object or else workspace libs will not work later. // It'll be generated as needed when a lib is first added. delete json.compilerOptions.paths; updatedJson = { ...updatedJson, compilerOptions: { ...updatedJson.compilerOptions, ...appJSON.compilerOptions, }, include: [ ...new Set([ ...(updatedJson.include || []), ...(appJSON.include || []), ]), ], exclude: [ ...new Set([ ...(updatedJson.exclude || []), ...(appJSON.exclude || []), `dist/${options.projectName}/**/*`, ]), ], }; return updatedJson; }); host.delete('tsconfig.json'); host.rename('tsconfig.base.json', 'tsconfig.json'); } if (options.unitTestRunner === 'none') { host.delete(`${options.appProjectRoot}/specs/${options.fileName}.spec.tsx`); } // SWC will be disabled if custom babelrc is provided. // Check for `!== false` because `create-nx-workspace` is not passing default values. if (options.swc !== false) { host.delete(`${options.appProjectRoot}/.babelrc`); } if (options.styledModule || options.style === 'tailwind') { if (options.appDir) { host.delete(`${generatedAppFilePath}/app/page.module.${options.style}`); } else { host.delete(`${generatedAppFilePath}/pages/${options.fileName}.module.${options.style}`); } } if (options.style !== 'styled-components') { host.delete(`${generatedAppFilePath}/pages/_document.tsx`); } if (options.js) { host.delete(`${options.appProjectRoot}/index.d.ts`); (0, devkit_1.toJS)(host); host.delete(`${options.appProjectRoot}/next-env.d.js`); } }