mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-07 06:07:42 +02:00
test(v2): babel exclude transpilation logic to prevent regression (#2024)
* refactor(v2): add tests to babel exclude transpilation logic to prevent regression * stronger test
This commit is contained in:
parent
b360b450d8
commit
205b367000
2 changed files with 73 additions and 12 deletions
59
packages/docusaurus/src/webpack/__tests__/base.test.ts
Normal file
59
packages/docusaurus/src/webpack/__tests__/base.test.ts
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2017-present, Facebook, Inc.
|
||||||
|
*
|
||||||
|
* 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 {excludeJS, clientDir} from '../base';
|
||||||
|
|
||||||
|
describe('babel transpilation exclude logic', () => {
|
||||||
|
test('always transpile client dir files', () => {
|
||||||
|
const clientFiles = [
|
||||||
|
'App.js',
|
||||||
|
'clientEntry.js',
|
||||||
|
'serverEntry.js',
|
||||||
|
path.join('exports', 'Link.js'),
|
||||||
|
];
|
||||||
|
clientFiles.forEach(file => {
|
||||||
|
expect(excludeJS(path.join(clientDir, file))).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('always transpile non node_module files', () => {
|
||||||
|
const moduleFiles = [
|
||||||
|
'/pages/user/App.jsx',
|
||||||
|
'/website/src/components/foo.js',
|
||||||
|
'/src/theme/SearchBar/index.js',
|
||||||
|
];
|
||||||
|
moduleFiles.forEach(file => {
|
||||||
|
expect(excludeJS(file)).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('transpile docusaurus npm packages even in node_modules', () => {
|
||||||
|
const moduleFiles = [
|
||||||
|
'/website/node_modules/docusaurus-theme-search/theme/Navbar/index.js',
|
||||||
|
'node_modules/@docusaurus/theme-classic/theme/Layout.js',
|
||||||
|
'/docusaurus/website/node_modules/@docusaurus/theme-search-algolia/theme/SearchBar.js',
|
||||||
|
];
|
||||||
|
moduleFiles.forEach(file => {
|
||||||
|
expect(excludeJS(file)).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('does not transpile node_modules', () => {
|
||||||
|
const moduleFiles = [
|
||||||
|
'node_modules/react-toggle.js',
|
||||||
|
'/website/node_modules/react-trend/index.js',
|
||||||
|
'/docusaurus/website/node_modules/react-super.js',
|
||||||
|
'/docusaurus/website/node_modules/@docusaurus/core/node_modules/core-js/modules/_descriptors.js',
|
||||||
|
'node_modules/docusaurus-theme-classic/node_modules/react-daypicker/index.js',
|
||||||
|
];
|
||||||
|
moduleFiles.forEach(file => {
|
||||||
|
expect(excludeJS(file)).toEqual(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -17,6 +17,19 @@ import {getBabelLoader, getCacheLoader, getStyleLoaders} from './utils';
|
||||||
|
|
||||||
const CSS_REGEX = /\.css$/;
|
const CSS_REGEX = /\.css$/;
|
||||||
const CSS_MODULE_REGEX = /\.module\.css$/;
|
const CSS_MODULE_REGEX = /\.module\.css$/;
|
||||||
|
export const clientDir = path.join(__dirname, '..', 'client');
|
||||||
|
|
||||||
|
export function excludeJS(modulePath: string) {
|
||||||
|
// always transpile client dir
|
||||||
|
if (modulePath.startsWith(clientDir)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Don't transpile node_modules except any docusaurus npm package
|
||||||
|
return (
|
||||||
|
/node_modules/.test(modulePath) &&
|
||||||
|
!/(docusaurus)((?!node_modules).)*\.jsx?$/.test(modulePath)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function createBaseConfig(
|
export function createBaseConfig(
|
||||||
props: Props,
|
props: Props,
|
||||||
|
@ -24,7 +37,6 @@ export function createBaseConfig(
|
||||||
): Configuration {
|
): Configuration {
|
||||||
const {outDir, siteDir, baseUrl, generatedFilesDir, routesPaths} = props;
|
const {outDir, siteDir, baseUrl, generatedFilesDir, routesPaths} = props;
|
||||||
|
|
||||||
const clientDir = path.join(__dirname, '..', 'client');
|
|
||||||
const totalPages = routesPaths.length;
|
const totalPages = routesPaths.length;
|
||||||
const isProd = process.env.NODE_ENV === 'production';
|
const isProd = process.env.NODE_ENV === 'production';
|
||||||
return {
|
return {
|
||||||
|
@ -133,17 +145,7 @@ export function createBaseConfig(
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
test: /\.jsx?$/,
|
test: /\.jsx?$/,
|
||||||
exclude: modulePath => {
|
exclude: excludeJS,
|
||||||
// always transpile client dir
|
|
||||||
if (modulePath.startsWith(clientDir)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Don't transpile node_modules except any docusaurus npm package
|
|
||||||
return (
|
|
||||||
/node_modules/.test(modulePath) &&
|
|
||||||
!/(docusaurus)((?!node_modules).)*\.jsx?$/.test(modulePath)
|
|
||||||
);
|
|
||||||
},
|
|
||||||
use: [getCacheLoader(isServer), getBabelLoader(isServer)].filter(
|
use: [getCacheLoader(isServer), getBabelLoader(isServer)].filter(
|
||||||
Boolean,
|
Boolean,
|
||||||
) as Loader[],
|
) as Loader[],
|
||||||
|
|
Loading…
Add table
Reference in a new issue