Skip to content

Commit

Permalink
Make babel loader resolve and require config file
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 committed Jun 10, 2020
1 parent 614f0f9 commit aba33b4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
20 changes: 10 additions & 10 deletions packages/docusaurus/src/webpack/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,10 @@ export function createBaseConfig(
const totalPages = routesPaths.length;
const isProd = process.env.NODE_ENV === 'production';

let customBabelConfiguration;
try {
customBabelConfiguration = require(path.join(
siteDir,
BABEL_CONFIG_FILE_NAME,
));
} catch {
customBabelConfiguration = undefined;
}
const customBabelConfigurationPath = path.join(
siteDir,
BABEL_CONFIG_FILE_NAME,
);

return {
mode: isProd ? 'production' : 'development',
Expand Down Expand Up @@ -168,7 +163,12 @@ export function createBaseConfig(
exclude: excludeJS,
use: [
getCacheLoader(isServer),
getBabelLoader(isServer, customBabelConfiguration),
getBabelLoader(
isServer,
fs.existsSync(customBabelConfigurationPath)
? customBabelConfigurationPath
: undefined,
),
].filter(Boolean) as Loader[],
},
{
Expand Down
28 changes: 19 additions & 9 deletions packages/docusaurus/src/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,28 @@ export function getCacheLoader(

export function getBabelLoader(
isServer: boolean,
babelOptions?: TransformOptions,
babelOptions?: TransformOptions | string,
): Loader {
const babelOptionsWithDocusaurusPreset = babelOptions ?? {
presets: [require.resolve('../babel/preset')],
};
return {
loader: require.resolve('babel-loader'),
options: Object.assign(babelOptionsWithDocusaurusPreset, {
let options: TransformOptions;
if (typeof babelOptions === 'string') {
options = {
babelrc: false,
configFile: false,
configFile: babelOptions,
caller: {name: isServer ? 'server' : 'client'},
}),
};
} else {
options = Object.assign(
babelOptions ?? {presets: [require.resolve('../babel/preset')]},
{
babelrc: false,
configFile: false,
caller: {name: isServer ? 'server' : 'client'},
},
);
}
return {
loader: require.resolve('babel-loader'),
options,
};
}

Expand Down

0 comments on commit aba33b4

Please sign in to comment.