-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(faster,bundler,core): improve js loader DX (#10655)
- Loading branch information
Showing
7 changed files
with
218 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...us/src/server/__tests__/__fixtures__/siteMessages/siteWithBabelConfigFile/babel.config.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
packages/docusaurus/src/server/__tests__/siteMessages.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import path from 'path'; | ||
import {fromPartial} from '@total-typescript/shoehorn'; | ||
import {collectAllSiteMessages} from '../siteMessages'; | ||
|
||
function siteDirFixture(name: string) { | ||
return path.resolve(__dirname, '__fixtures__', 'siteMessages', name); | ||
} | ||
|
||
describe('collectAllSiteMessages', () => { | ||
describe('uselessBabelConfigMessages', () => { | ||
async function getMessagesFor({ | ||
siteDir, | ||
swcJsLoader, | ||
}: { | ||
siteDir: string; | ||
swcJsLoader: boolean; | ||
}) { | ||
return collectAllSiteMessages( | ||
fromPartial({ | ||
site: { | ||
props: { | ||
siteDir, | ||
siteConfig: { | ||
future: { | ||
experimental_faster: { | ||
swcJsLoader, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
it('warns for useless babel config file when SWC enabled', async () => { | ||
const messages = await getMessagesFor({ | ||
siteDir: siteDirFixture('siteWithBabelConfigFile'), | ||
swcJsLoader: true, | ||
}); | ||
expect(messages).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"message": "Your site is using the SWC js loader. You can safely remove the Babel config file at \`packages/docusaurus/src/server/__tests__/__fixtures__/siteMessages/siteWithBabelConfigFile/babel.config.js\`.", | ||
"type": "warning", | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
it('does not warn for babel config file when SWC disabled', async () => { | ||
const messages = await getMessagesFor({ | ||
siteDir: siteDirFixture('siteWithBabelConfigFile'), | ||
swcJsLoader: false, | ||
}); | ||
expect(messages).toMatchInlineSnapshot(`[]`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import path from 'path'; | ||
import _ from 'lodash'; | ||
import {getCustomBabelConfigFilePath} from '@docusaurus/babel'; | ||
import logger from '@docusaurus/logger'; | ||
import type {Site} from './site'; | ||
|
||
type Params = {site: Site}; | ||
|
||
type SiteMessage = {type: 'warning' | 'error'; message: string}; | ||
|
||
type SiteMessageCreator = (params: Params) => Promise<SiteMessage[]>; | ||
|
||
const uselessBabelConfigMessages: SiteMessageCreator = async ({site}) => { | ||
const { | ||
props: {siteDir, siteConfig}, | ||
} = site; | ||
if (siteConfig.future.experimental_faster.swcJsLoader) { | ||
const babelConfigFilePath = await getCustomBabelConfigFilePath(siteDir); | ||
if (babelConfigFilePath) { | ||
return [ | ||
{ | ||
type: 'warning', | ||
message: `Your site is using the SWC js loader. You can safely remove the Babel config file at ${logger.code( | ||
path.relative(process.cwd(), babelConfigFilePath), | ||
)}.`, | ||
}, | ||
]; | ||
} | ||
} | ||
return []; | ||
}; | ||
|
||
export async function collectAllSiteMessages( | ||
params: Params, | ||
): Promise<SiteMessage[]> { | ||
const messageCreators: SiteMessageCreator[] = [uselessBabelConfigMessages]; | ||
return ( | ||
await Promise.all( | ||
messageCreators.map((createMessages) => createMessages(params)), | ||
) | ||
).flat(); | ||
} | ||
|
||
function printSiteMessages(siteMessages: SiteMessage[]): void { | ||
const [errors, warnings] = _.partition( | ||
siteMessages, | ||
(sm) => sm.type === 'error', | ||
); | ||
if (errors.length > 0) { | ||
logger.error(`Docusaurus site errors: | ||
- ${errors.map((sm) => sm.message).join('\n- ')}`); | ||
} | ||
if (warnings.length > 0) { | ||
logger.warn(`Docusaurus site warnings: | ||
- ${warnings.map((sm) => sm.message).join('\n- ')}`); | ||
} | ||
} | ||
|
||
export async function emitSiteMessages(params: Params): Promise<void> { | ||
const siteMessages = await collectAllSiteMessages(params); | ||
printSiteMessages(siteMessages); | ||
} |