Skip to content

Commit

Permalink
revert --generated-files-dir option + some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Mar 2, 2021
1 parent 82b0a26 commit 9d67ddc
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 86 deletions.
14 changes: 3 additions & 11 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,12 @@ export type StartCLIOptions = HostPortCLIOptions &
export type ServeCLIOptions = HostPortCLIOptions &
ConfigOptions & {
dir: string;
} & (
| {
build: true;
// We only need generatedFilesDir when `build` is true
generatedFilesDir: string;
}
| {
build: false;
}
);
build: boolean;
};

export type BuildOptions = ConfigOptions & {
bundleAnalyzer: boolean;
outDir: string;
generatedFilesDir: string;
minify: boolean;
skipBuild: boolean;
};
Expand All @@ -173,6 +164,7 @@ export interface LoadContext {
siteDir: string;
generatedFilesDir: string;
siteConfig: DocusaurusConfig;
siteConfigPath: string;
outDir: string;
baseUrl: string;
i18n: I18n;
Expand Down
34 changes: 9 additions & 25 deletions packages/docusaurus/bin/docusaurus.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ cli
'--config <config>',
'Path to docusaurus config file, default to `[siteDir]/docusaurus.config.js`',
)
.option(
'--generated-files-dir <dir>',
'Path to temporary `.docusaurus` folder, default to `[siteDir]/.docusaurus`.',
)
.option(
'-l, --locale <locale>',
'Build the site in a specified locale. Build all known locales otherwise.',
Expand All @@ -125,21 +121,15 @@ cli
'--no-minify',
'Build website without minimizing JS bundles (default: false)',
)
.action(
(
siteDir = '.',
{bundleAnalyzer, config, generatedFilesDir, outDir, locale, minify},
) => {
wrapCommand(build)(path.resolve(siteDir), {
bundleAnalyzer,
outDir,
config,
generatedFilesDir,
locale,
minify,
});
},
);
.action((siteDir = '.', {bundleAnalyzer, config, outDir, locale, minify}) => {
wrapCommand(build)(path.resolve(siteDir), {
bundleAnalyzer,
outDir,
config,
locale,
minify,
});
});

cli
.command('swizzle [themeName] [componentName] [siteDir]')
Expand Down Expand Up @@ -230,10 +220,6 @@ cli
'--config <config>',
'Path to docusaurus config file, default to `[siteDir]/docusaurus.config.js`',
)
.option(
'--generated-files-dir <dir>',
'Path to temporary `.docusaurus` folder, default to `[siteDir]/.docusaurus`. This is only relevant when `--build` passed in.',
)
.option('-p, --port <port>', 'use specified port (default: 3000)')
.option('--build', 'Build website before serving (default: false)')
.option('-h, --host <host>', 'use specified host (default: localhost')
Expand All @@ -246,15 +232,13 @@ cli
host = 'localhost',
build: buildSite = false,
config,
generatedFilesDir,
},
) => {
wrapCommand(serve)(path.resolve(siteDir), {
dir,
port,
build: buildSite,
config,
generatedFilesDir,
host,
});
},
Expand Down
2 changes: 0 additions & 2 deletions packages/docusaurus/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export default async function build(
}
const context = await loadContext(siteDir, {
customOutDir: cliOptions.outDir,
customGeneratedFilesDir: cliOptions.generatedFilesDir,
customConfigFilePath: cliOptions.config,
locale: cliOptions.locale,
localizePath: cliOptions.locale ? false : undefined,
Expand Down Expand Up @@ -118,7 +117,6 @@ async function buildLocale({
const props: Props = await load(siteDir, {
customOutDir: cliOptions.outDir,
customConfigFilePath: cliOptions.config,
customGeneratedFilesDir: cliOptions.generatedFilesDir,
locale,
localizePath: cliOptions.locale ? false : undefined,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus/src/commands/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import fs from 'fs-extra';
import path from 'path';
import chalk = require('chalk');
import {BUILD_DIR_NAME, GENERATED_FILES_DIR_NAME} from '../constants';
import {DEFAULT_BUILD_DIR_NAME, GENERATED_FILES_DIR_NAME} from '../constants';

function removePath(fsPath: string) {
return fs
Expand All @@ -24,7 +24,7 @@ function removePath(fsPath: string) {
export default async function clear(siteDir: string): Promise<unknown> {
return Promise.all([
removePath(path.join(siteDir, GENERATED_FILES_DIR_NAME)),
removePath(path.join(siteDir, BUILD_DIR_NAME)),
removePath(path.join(siteDir, DEFAULT_BUILD_DIR_NAME)),
removePath(path.join(siteDir, 'node_modules/.cache/cache-loader')),
]);
}
7 changes: 3 additions & 4 deletions packages/docusaurus/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import fs from 'fs-extra';
import shell from 'shelljs';
import chalk from 'chalk';
import {CONFIG_FILE_NAME} from '../constants';
import {loadContext} from '../server';
import build from './build';
import {BuildCLIOptions} from '@docusaurus/types';
Expand Down Expand Up @@ -42,7 +41,7 @@ export default async function deploy(
siteDir: string,
cliOptions: Partial<BuildCLIOptions> = {},
): Promise<void> {
const {outDir, siteConfig} = await loadContext(siteDir, {
const {outDir, siteConfig, siteConfigPath} = await loadContext(siteDir, {
customConfigFilePath: cliOptions.config,
customOutDir: cliOptions.outDir,
});
Expand All @@ -68,7 +67,7 @@ export default async function deploy(
siteConfig.organizationName;
if (!organizationName) {
throw new Error(
`Missing project organization name. Did you forget to define 'organizationName' in ${CONFIG_FILE_NAME}? You may also export it via the ORGANIZATION_NAME environment variable.`,
`Missing project organization name. Did you forget to define 'organizationName' in ${siteConfigPath}? You may also export it via the ORGANIZATION_NAME environment variable.`,
);
}
console.log(`${chalk.cyan('organizationName:')} ${organizationName}`);
Expand All @@ -79,7 +78,7 @@ export default async function deploy(
siteConfig.projectName;
if (!projectName) {
throw new Error(
`Missing project name. Did you forget to define 'projectName' in ${CONFIG_FILE_NAME}? You may also export it via the PROJECT_NAME environment variable.`,
`Missing project name. Did you forget to define 'projectName' in ${siteConfigPath}? You may also export it via the PROJECT_NAME environment variable.`,
);
}
console.log(`${chalk.cyan('projectName:')} ${projectName}`);
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default async function serve(
dir = await build(
siteDir,
{
generatedFilesDir: cliOptions.generatedFilesDir,
config: cliOptions.config,
outDir: dir,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import merge from 'webpack-merge';
import HotModuleReplacementPlugin from 'webpack/lib/HotModuleReplacementPlugin';
import {load} from '../server';
import {StartCLIOptions} from '@docusaurus/types';
import {CONFIG_FILE_NAME, STATIC_DIR_NAME} from '../constants';
import {STATIC_DIR_NAME} from '../constants';
import createClientConfig from '../webpack/client';
import {
applyConfigureWebpack,
Expand Down Expand Up @@ -98,7 +98,7 @@ export default async function start(

const pathsToWatch: string[] = [
...pluginPaths,
CONFIG_FILE_NAME,
props.siteConfigPath,
getTranslationsLocaleDirPath({
siteDir,
locale: props.i18n.currentLocale,
Expand Down
16 changes: 12 additions & 4 deletions packages/docusaurus/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
* LICENSE file in the root directory of this source tree.
*/

export const BABEL_CONFIG_FILE_NAME = 'babel.config.js';
export const BUILD_DIR_NAME = 'build';
export const CONFIG_FILE_NAME = 'docusaurus.config.js';
export const GENERATED_FILES_DIR_NAME = '.docusaurus';
// Can be overridden with cli option --out-dir
export const DEFAULT_BUILD_DIR_NAME = 'build';

// Can be overridden with cli option --config
export const DEFAULT_CONFIG_FILE_NAME = 'docusaurus.config.js';

export const BABEL_CONFIG_FILE_NAME =
process.env.DOCUSAURUS_BABEL_CONFIG_FILE_NAME || 'babel.config.js';

export const GENERATED_FILES_DIR_NAME =
process.env.DOCUSAURUS_GENERATED_FILES_DIR_NAME || '.docusaurus';

export const SRC_DIR_NAME = 'src';
export const STATIC_DIR_NAME = 'static';
export const OUTPUT_STATIC_ASSETS_DIR_NAME = 'assets'; // files handled by webpack, hashed (can be cached aggressively)
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import {DocusaurusConfig, I18nConfig} from '@docusaurus/types';
import {CONFIG_FILE_NAME} from '../constants';
import {DEFAULT_CONFIG_FILE_NAME} from '../constants';
import Joi from 'joi';
import {
logValidationBugReportHint,
Expand Down Expand Up @@ -164,7 +164,7 @@ export function validateConfig(
'',
);
formattedError = unknownFields
? `${formattedError}These field(s) [${unknownFields}] are not recognized in ${CONFIG_FILE_NAME}.\nIf you still want these fields to be in your configuration, put them in the 'customFields' attribute.\nSee https://v2.docusaurus.io/docs/docusaurus.config.js/#customfields`
? `${formattedError}These field(s) [${unknownFields}] are not recognized in ${DEFAULT_CONFIG_FILE_NAME}.\nIf you still want these fields to be in your configuration, put them in the 'customFields' attribute.\nSee https://v2.docusaurus.io/docs/docusaurus.config.js/#customfields`
: formattedError;
throw new Error(formattedError);
} else {
Expand Down
33 changes: 16 additions & 17 deletions packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import path, {join} from 'path';
import chalk from 'chalk';
import ssrDefaultTemplate from '../client/templates/ssr.html.template';
import {
BUILD_DIR_NAME,
CONFIG_FILE_NAME,
DEFAULT_BUILD_DIR_NAME,
DEFAULT_CONFIG_FILE_NAME,
GENERATED_FILES_DIR_NAME,
THEME_PATH,
} from '../constants';
Expand Down Expand Up @@ -40,7 +40,6 @@ import {mapValues} from 'lodash';

type LoadContextOptions = {
customOutDir?: string;
customGeneratedFilesDir?: string;
customConfigFilePath?: string;
locale?: string;
localizePath?: boolean; // undefined = only non-default locales paths are localized
Expand All @@ -50,26 +49,23 @@ export async function loadContext(
siteDir: string,
options: LoadContextOptions = {},
): Promise<LoadContext> {
const {
customOutDir,
locale,
customGeneratedFilesDir,
customConfigFilePath,
} = options;
const generatedFilesDir = customGeneratedFilesDir
? path.resolve(customGeneratedFilesDir)
const {customOutDir, locale, customConfigFilePath} = options;
const generatedFilesDir = path.isAbsolute(GENERATED_FILES_DIR_NAME)
? GENERATED_FILES_DIR_NAME
: path.resolve(siteDir, GENERATED_FILES_DIR_NAME);

const configPath = customConfigFilePath
? path.resolve(customConfigFilePath)
: path.resolve(siteDir, CONFIG_FILE_NAME);
const siteConfigPathUnresolved =
customConfigFilePath ?? DEFAULT_CONFIG_FILE_NAME;
const siteConfigPath = path.isAbsolute(siteConfigPathUnresolved)
? siteConfigPathUnresolved
: path.resolve(siteDir, siteConfigPathUnresolved);

const initialSiteConfig: DocusaurusConfig = loadConfig(configPath);
const initialSiteConfig: DocusaurusConfig = loadConfig(siteConfigPath);
const {ssrTemplate} = initialSiteConfig;

const baseOutDir = customOutDir
? path.resolve(customOutDir)
: path.resolve(siteDir, BUILD_DIR_NAME);
: path.resolve(siteDir, DEFAULT_BUILD_DIR_NAME);

const i18n = await loadI18n(initialSiteConfig, {locale});

Expand Down Expand Up @@ -104,6 +100,7 @@ export async function loadContext(
siteDir,
generatedFilesDir,
siteConfig,
siteConfigPath,
outDir,
baseUrl,
i18n,
Expand Down Expand Up @@ -133,6 +130,7 @@ export async function load(
const {
generatedFilesDir,
siteConfig,
siteConfigPath,
outDir,
baseUrl,
i18n,
Expand Down Expand Up @@ -160,7 +158,7 @@ export async function load(
// We want the generated config to have been normalized by the plugins!
const genSiteConfig = generate(
generatedFilesDir,
CONFIG_FILE_NAME,
DEFAULT_CONFIG_FILE_NAME,
`export default ${JSON.stringify(siteConfig, null, 2)};`,
);

Expand Down Expand Up @@ -326,6 +324,7 @@ ${Object.keys(registry)

const props: Props = {
siteConfig,
siteConfigPath,
siteDir,
outDir,
baseUrl,
Expand Down
5 changes: 2 additions & 3 deletions packages/docusaurus/src/server/plugins/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import Module from 'module';
import {join} from 'path';
import importFresh from 'import-fresh';
import {
DocusaurusPluginVersionInformation,
Expand All @@ -15,7 +14,7 @@ import {
PluginConfig,
PluginOptions,
} from '@docusaurus/types';
import {CONFIG_FILE_NAME, DEFAULT_PLUGIN_ID} from '../../constants';
import {DEFAULT_PLUGIN_ID} from '../../constants';
import {getPluginVersion} from '../versions';
import {ensureUniquePluginInstanceIds} from './pluginIds';
import {
Expand All @@ -40,7 +39,7 @@ export default function initPlugins({
// We need to fallback to createRequireFromPath since createRequire is only available in node v12.
// See: https://nodejs.org/api/modules.html#modules_module_createrequire_filename
const createRequire = Module.createRequire || Module.createRequireFromPath;
const pluginRequire = createRequire(join(context.siteDir, CONFIG_FILE_NAME));
const pluginRequire = createRequire(context.siteConfigPath);

const plugins: InitPlugin[] = pluginConfigs
.map((pluginItem) => {
Expand Down
Loading

0 comments on commit 9d67ddc

Please sign in to comment.