Skip to content

Commit

Permalink
feat(jest-preset): introduce experimental esbuild transpilation
Browse files Browse the repository at this point in the history
  • Loading branch information
jhiode committed Mar 19, 2021
1 parent 87ce4cd commit da09122
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
28 changes: 20 additions & 8 deletions packages/jest-preset/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ if (Number(jestMajorVersion) < 26) {
);
}

const useEsbuild = process.env.USE_EXPERIMENTAL_ESBUILD === 'true';
const jsTransform = useEsbuild
? require.resolve('./transforms/esbuild.js')
: require.resolve('./transforms/babel.js');
const tsTransform = useEsbuild
? require.resolve('./transforms/esbuild.js')
: require.resolve('ts-jest');

module.exports = {
globals: {
'ts-jest': {
babelConfig: require('./transforms/babel.js').babelConfig,
},
},
globals: useEsbuild
? {}
: {
'ts-jest': {
babelConfig: require('./transforms/babel.js').babelConfig,
},
},
moduleNameMapper: {
'^.+\\.(png|gif|jpe?g|webp|html|svg|((o|t)tf)|woff2?|ico)$': require.resolve(
'./mocks/file.js'
Expand All @@ -37,12 +47,14 @@ module.exports = {
'**/?(*.)+(spec|test).ts?(x)',
],
transform: {
'^.+\\.(js|jsx|mjs)$': require.resolve('./transforms/babel.js'),
'^.+\\.(ts|tsx)$': require.resolve('ts-jest'),
'^.+\\.(js|jsx|mjs)$': jsTransform,
'^.+\\.(ts|tsx)$': tsTransform,
'^.+\\.(gql|graphql)$': require.resolve('./transforms/graphql.js'),
},
transformIgnorePatterns: [],
setupFiles: [require.resolve('regenerator-runtime/runtime')],
setupFiles: useEsbuild
? []
: [require.resolve('regenerator-runtime/runtime')],
// fixes: https://github.com/facebook/jest/issues/6766
testURL: 'http://localhost',
};
17 changes: 17 additions & 0 deletions packages/jest-preset/transforms/esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line node/no-missing-require
const { createTransformer } = require('esbuild-jest');

const transformer = createTransformer({
sourcemap: true,
});

module.exports = {
process(content, filename, config, opts) {
content = content.replace(
/importComponent\s*\(\s*\(\)\s+=>\s+import\(\s*'([^']+)'\s*\)\s*\)/g,
"importComponent({ component: require('$1') })"
);

return transformer.process(content, filename, config, opts);
},
};

0 comments on commit da09122

Please sign in to comment.