Skip to content

Commit

Permalink
feat: custom include paths and esbuild jest config (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: pradel <[email protected]>
  • Loading branch information
mariobm and pradel authored Jan 18, 2021
1 parent 682f8c3 commit 3438f09
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/craco-esbuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module.exports = {
{
plugin: CracoEsbuildPlugin,
options: {
includePaths: ["/external/dir/with/components"], // Optional. If you want to include components which are not in src folder
esbuildLoaderOptions: {
loader: "jsx",
target: "es2015",
Expand Down
71 changes: 59 additions & 12 deletions packages/craco-esbuild/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ module.exports = {
context: { paths },
}) => {
const useTypeScript = fs.existsSync(paths.appTsConfig);
const esbuildLoaderOptions =
pluginOptions && pluginOptions.esbuildLoaderOptions;

// add includePaths custom option, for including files/components in other folders than src
// Used as in addition to paths.appSrc, optional parameter.
const optionalIncludes =
(pluginOptions && pluginOptions.includePaths) || [];

// add esbuild-loader
addAfterLoader(webpackConfig, loaderByName("babel-loader"), {
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: paths.appSrc,
include: [paths.appSrc, ...optionalIncludes],
loader: require.resolve("esbuild-loader"),
options:
pluginOptions && pluginOptions.esbuildLoaderOptions
? pluginOptions.esbuildLoaderOptions
: {
loader: useTypeScript ? "tsx" : "jsx",
target: "es2015",
},
options: esbuildLoaderOptions
? esbuildLoaderOptions
: {
loader: useTypeScript ? "tsx" : "jsx",
target: "es2015",
},
});

// remove the babel loaders
Expand All @@ -48,10 +54,51 @@ module.exports = {
* To process the js/ts files we replace the babel-loader with the esbuild jest loader
*/
overrideJestConfig: ({ jestConfig }) => {
// TODO add back once https://github.com/aelbore/esbuild-jest/issues/7 is fixed
// // Replace babel transform with esbuild
// const key = Object.keys(jestConfig.transform)[0];
// jestConfig.transform[key] = [require.resolve("esbuild-jest")];
const options = {
loaders: {
".js": "jsx",
".test.js": "jsx",
".ts": "tsx",
".test.ts": "tsx",
},
};

// Replace babel transform with esbuild
// babelTransform is first transformer key
/*
transform:
{
'^.+\\.(js|jsx|mjs|cjs|ts|tsx)$': 'node_modules\\react-scripts\\config\\jest\\babelTransform.js',
'^.+\\.css$': 'node_modules\\react-scripts\\config\\jest\\cssTransform.js',
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)': 'node_modules\\react-scripts\\config\\jest\\fileTransform.js'
}
*/
const babelKey = Object.keys(jestConfig.transform)[0];

// We replace babelTransform and add loaders to esbuild-jest
jestConfig.transform[babelKey] = [require.resolve("esbuild-jest"), options];

// Adds loader to all other transform options (2 in this case: cssTransform and fileTransform)
// Reason for this is esbuild-jest plugin. It considers only loaders or other options from the last transformer
// You can see it for yourself in: /node_modules/esbuild-jest/esbuid-jest.js:21 getOptions method
// also in process method line 32 gives empty loaders, because options is already empty object
// Issue reported here: https://github.com/aelbore/esbuild-jest/issues/18
Object.keys(jestConfig.transform).forEach((key) => {
if (babelKey === key) return; // ebuild-jest transform, already has loader

// Checks if value is array, usually it's not
// Our example is above on 70-72 lines. Usually default is: {"\\.[jt]sx?$": "babel-jest"}
// (https://jestjs.io/docs/en/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object)
// But we have to cover all the cases
if (
Array.isArray(jestConfig.transform[key]) &&
jestConfig.transform[key].length === 1
) {
jestConfig.transform[key].push(options);
} else {
jestConfig.transform[key] = [jestConfig.transform[key], options];
}
});

return jestConfig;
},
Expand Down

0 comments on commit 3438f09

Please sign in to comment.