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 4, 2021
1 parent bda565e commit de1933c
Show file tree
Hide file tree
Showing 3 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',
};
1 change: 1 addition & 0 deletions packages/jest-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@babel/preset-react": "^7.9.4",
"colors": "^1.4.0",
"core-js": "^3.2.1",
"esbuild-jest": "^0.5.0",
"hops": "^14.0.0-nightly.7",
"identity-obj-proxy": "^3.0.0",
"import-from": "^3.0.0",
Expand Down
16 changes: 16 additions & 0 deletions packages/jest-preset/transforms/esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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 de1933c

Please sign in to comment.