From ce4541380415faaa673fa55411f0ac33750127ce Mon Sep 17 00:00:00 2001 From: ZachJW34 Date: Thu, 19 Mar 2020 10:52:26 -0500 Subject: [PATCH] feat(v2): add custom output directory to build (#2417) --- packages/docusaurus-types/src/index.d.ts | 1 + packages/docusaurus/bin/docusaurus.js | 15 ++++++++++++--- packages/docusaurus/src/commands/build.ts | 5 +++-- packages/docusaurus/src/commands/deploy.ts | 18 +++++++++--------- packages/docusaurus/src/server/index.ts | 16 ++++++++++++---- website/docs/cli.md | 7 +++++++ 6 files changed, 44 insertions(+), 18 deletions(-) diff --git a/packages/docusaurus-types/src/index.d.ts b/packages/docusaurus-types/src/index.d.ts index 2ee518bf34..9dccb8a39e 100644 --- a/packages/docusaurus-types/src/index.d.ts +++ b/packages/docusaurus-types/src/index.d.ts @@ -63,6 +63,7 @@ export interface StartCLIOptions { export interface BuildCLIOptions { bundleAnalyzer: boolean; + outDir: string; } export interface LoadContext { diff --git a/packages/docusaurus/bin/docusaurus.js b/packages/docusaurus/bin/docusaurus.js index 271760993b..4d40b7ff3d 100755 --- a/packages/docusaurus/bin/docusaurus.js +++ b/packages/docusaurus/bin/docusaurus.js @@ -41,9 +41,14 @@ cli '--bundle-analyzer', 'Visualize size of webpack output files with an interactive zoomable treemap (default = false)', ) - .action((siteDir = '.', {bundleAnalyzer}) => { + .option( + '--out-dir ', + 'The full path for the new output directory, relative to the current workspace (default = build).', + ) + .action((siteDir = '.', {bundleAnalyzer, outDir}) => { wrapCommand(build)(path.resolve(siteDir), { bundleAnalyzer, + outDir, }); }); @@ -57,8 +62,12 @@ cli cli .command('deploy [siteDir]') .description('Deploy website to GitHub pages') - .action((siteDir = '.') => { - wrapCommand(deploy)(path.resolve(siteDir)); + .option( + '--out-dir ', + 'The full path for the new output directory, relative to the current workspace (default = build).', + ) + .action((siteDir = '.', {outDir}) => { + wrapCommand(deploy)(path.resolve(siteDir), {outDir}); }); cli diff --git a/packages/docusaurus/src/commands/build.ts b/packages/docusaurus/src/commands/build.ts index 6b9d574517..b34095cf53 100644 --- a/packages/docusaurus/src/commands/build.ts +++ b/packages/docusaurus/src/commands/build.ts @@ -47,12 +47,12 @@ function compile(config: Configuration[]): Promise { export async function build( siteDir: string, cliOptions: Partial = {}, -): Promise { +): Promise { process.env.BABEL_ENV = 'production'; process.env.NODE_ENV = 'production'; console.log(chalk.blue('Creating an optimized production build...')); - const props: Props = await load(siteDir); + const props: Props = await load(siteDir, cliOptions.outDir); // Apply user webpack config. const {outDir, generatedFilesDir, plugins} = props; @@ -147,4 +147,5 @@ export async function build( relativeDir, )}.\n`, ); + return outDir; } diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 0bfb23ebe7..949faf8066 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -8,15 +8,15 @@ import fs from 'fs-extra'; import path from 'path'; import shell from 'shelljs'; -import { - BUILD_DIR_NAME, - CONFIG_FILE_NAME, - GENERATED_FILES_DIR_NAME, -} from '../constants'; +import {CONFIG_FILE_NAME, GENERATED_FILES_DIR_NAME} from '../constants'; import {loadConfig} from '../server/config'; import {build} from './build'; +import {BuildCLIOptions} from '@docusaurus/types'; -export async function deploy(siteDir: string): Promise { +export async function deploy( + siteDir: string, + cliOptions: Partial = {}, +): Promise { console.log('Deploy command invoked ...'); if (!shell.which('git')) { throw new Error('Sorry, this script requires git'); @@ -98,8 +98,8 @@ export async function deploy(siteDir: string): Promise { fs.removeSync(tempDir); // Build static html files, then push to deploymentBranch branch of specified repo. - build(siteDir) - .then(() => { + build(siteDir, cliOptions) + .then(outDir => { shell.cd(tempDir); if ( @@ -140,7 +140,7 @@ export async function deploy(siteDir: string): Promise { shell.cd('../..'); - const fromPath = path.join(BUILD_DIR_NAME); + const fromPath = outDir; const toPath = path.join( GENERATED_FILES_DIR_NAME, `${projectName}-${deploymentBranch}`, diff --git a/packages/docusaurus/src/server/index.ts b/packages/docusaurus/src/server/index.ts index f9787c9612..9734f4b1db 100644 --- a/packages/docusaurus/src/server/index.ts +++ b/packages/docusaurus/src/server/index.ts @@ -28,13 +28,18 @@ import { } from '@docusaurus/types'; import {loadHtmlTags} from './html-tags'; -export function loadContext(siteDir: string): LoadContext { +export function loadContext( + siteDir: string, + customOutDir?: string, +): LoadContext { const generatedFilesDir: string = path.resolve( siteDir, GENERATED_FILES_DIR_NAME, ); const siteConfig: DocusaurusConfig = loadConfig(siteDir); - const outDir = path.resolve(siteDir, BUILD_DIR_NAME); + const outDir = customOutDir + ? path.resolve(customOutDir) + : path.resolve(siteDir, BUILD_DIR_NAME); const {baseUrl} = siteConfig; return { @@ -58,9 +63,12 @@ export function loadPluginConfigs(context: LoadContext): PluginConfig[] { ]; } -export async function load(siteDir: string): Promise { +export async function load( + siteDir: string, + customOutDir?: string, +): Promise { // Context. - const context: LoadContext = loadContext(siteDir); + const context: LoadContext = loadContext(siteDir, customOutDir); const {generatedFilesDir, siteConfig, outDir, baseUrl} = context; const genSiteConfig = generate( generatedFilesDir, diff --git a/website/docs/cli.md b/website/docs/cli.md index f696b83877..f1710e1704 100644 --- a/website/docs/cli.md +++ b/website/docs/cli.md @@ -59,6 +59,7 @@ Compiles your site for production. | Options | Default | Description | | --- | --- | --- | | `--bundle-analyzer` | | Analyze your bundle with [bundle analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) | +| `--out-dir` | `build` | The full path for the new output directory, relative to the current workspace. | ### `docusaurus swizzle` @@ -90,3 +91,9 @@ To learn more about swizzling, check [here](#). ### `docusaurus deploy` Deploys your site with [GitHub Pages](https://pages.github.com/). + +**options** + +| Options | Default | Description | +| --- | --- | --- | +| `--out-dir` | `build` | The full path for the new output directory, relative to the current workspace. |