feat(v2): add custom output directory to build (#2417)

This commit is contained in:
ZachJW34 2020-03-19 10:52:26 -05:00 committed by GitHub
parent 2fd50f9c33
commit ce45413804
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 18 deletions

View file

@ -63,6 +63,7 @@ export interface StartCLIOptions {
export interface BuildCLIOptions { export interface BuildCLIOptions {
bundleAnalyzer: boolean; bundleAnalyzer: boolean;
outDir: string;
} }
export interface LoadContext { export interface LoadContext {

View file

@ -41,9 +41,14 @@ cli
'--bundle-analyzer', '--bundle-analyzer',
'Visualize size of webpack output files with an interactive zoomable treemap (default = false)', 'Visualize size of webpack output files with an interactive zoomable treemap (default = false)',
) )
.action((siteDir = '.', {bundleAnalyzer}) => { .option(
'--out-dir <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), { wrapCommand(build)(path.resolve(siteDir), {
bundleAnalyzer, bundleAnalyzer,
outDir,
}); });
}); });
@ -57,8 +62,12 @@ cli
cli cli
.command('deploy [siteDir]') .command('deploy [siteDir]')
.description('Deploy website to GitHub pages') .description('Deploy website to GitHub pages')
.action((siteDir = '.') => { .option(
wrapCommand(deploy)(path.resolve(siteDir)); '--out-dir <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 cli

View file

@ -47,12 +47,12 @@ function compile(config: Configuration[]): Promise<any> {
export async function build( export async function build(
siteDir: string, siteDir: string,
cliOptions: Partial<BuildCLIOptions> = {}, cliOptions: Partial<BuildCLIOptions> = {},
): Promise<void> { ): Promise<string> {
process.env.BABEL_ENV = 'production'; process.env.BABEL_ENV = 'production';
process.env.NODE_ENV = 'production'; process.env.NODE_ENV = 'production';
console.log(chalk.blue('Creating an optimized production build...')); 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. // Apply user webpack config.
const {outDir, generatedFilesDir, plugins} = props; const {outDir, generatedFilesDir, plugins} = props;
@ -147,4 +147,5 @@ export async function build(
relativeDir, relativeDir,
)}.\n`, )}.\n`,
); );
return outDir;
} }

View file

@ -8,15 +8,15 @@
import fs from 'fs-extra'; import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import shell from 'shelljs'; import shell from 'shelljs';
import { import {CONFIG_FILE_NAME, GENERATED_FILES_DIR_NAME} from '../constants';
BUILD_DIR_NAME,
CONFIG_FILE_NAME,
GENERATED_FILES_DIR_NAME,
} from '../constants';
import {loadConfig} from '../server/config'; import {loadConfig} from '../server/config';
import {build} from './build'; import {build} from './build';
import {BuildCLIOptions} from '@docusaurus/types';
export async function deploy(siteDir: string): Promise<void> { export async function deploy(
siteDir: string,
cliOptions: Partial<BuildCLIOptions> = {},
): Promise<void> {
console.log('Deploy command invoked ...'); console.log('Deploy command invoked ...');
if (!shell.which('git')) { if (!shell.which('git')) {
throw new Error('Sorry, this script requires git'); throw new Error('Sorry, this script requires git');
@ -98,8 +98,8 @@ export async function deploy(siteDir: string): Promise<void> {
fs.removeSync(tempDir); fs.removeSync(tempDir);
// Build static html files, then push to deploymentBranch branch of specified repo. // Build static html files, then push to deploymentBranch branch of specified repo.
build(siteDir) build(siteDir, cliOptions)
.then(() => { .then(outDir => {
shell.cd(tempDir); shell.cd(tempDir);
if ( if (
@ -140,7 +140,7 @@ export async function deploy(siteDir: string): Promise<void> {
shell.cd('../..'); shell.cd('../..');
const fromPath = path.join(BUILD_DIR_NAME); const fromPath = outDir;
const toPath = path.join( const toPath = path.join(
GENERATED_FILES_DIR_NAME, GENERATED_FILES_DIR_NAME,
`${projectName}-${deploymentBranch}`, `${projectName}-${deploymentBranch}`,

View file

@ -28,13 +28,18 @@ import {
} from '@docusaurus/types'; } from '@docusaurus/types';
import {loadHtmlTags} from './html-tags'; import {loadHtmlTags} from './html-tags';
export function loadContext(siteDir: string): LoadContext { export function loadContext(
siteDir: string,
customOutDir?: string,
): LoadContext {
const generatedFilesDir: string = path.resolve( const generatedFilesDir: string = path.resolve(
siteDir, siteDir,
GENERATED_FILES_DIR_NAME, GENERATED_FILES_DIR_NAME,
); );
const siteConfig: DocusaurusConfig = loadConfig(siteDir); 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; const {baseUrl} = siteConfig;
return { return {
@ -58,9 +63,12 @@ export function loadPluginConfigs(context: LoadContext): PluginConfig[] {
]; ];
} }
export async function load(siteDir: string): Promise<Props> { export async function load(
siteDir: string,
customOutDir?: string,
): Promise<Props> {
// Context. // Context.
const context: LoadContext = loadContext(siteDir); const context: LoadContext = loadContext(siteDir, customOutDir);
const {generatedFilesDir, siteConfig, outDir, baseUrl} = context; const {generatedFilesDir, siteConfig, outDir, baseUrl} = context;
const genSiteConfig = generate( const genSiteConfig = generate(
generatedFilesDir, generatedFilesDir,

View file

@ -59,6 +59,7 @@ Compiles your site for production.
| Options | Default | Description | | Options | Default | Description |
| --- | --- | --- | | --- | --- | --- |
| `--bundle-analyzer` | | Analyze your bundle with [bundle analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer) | | `--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` ### `docusaurus swizzle`
@ -90,3 +91,9 @@ To learn more about swizzling, check [here](#).
### `docusaurus deploy` ### `docusaurus deploy`
Deploys your site with [GitHub Pages](https://pages.github.com/). 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. |