Merge pull request #7569 from backstage/rugvip/transformers
cli: rework how we handle ESM transforms for tests
This commit is contained in:
+42
-14
@@ -16,7 +16,25 @@
|
||||
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const glob = require('util').promisify(require('glob'));
|
||||
const { version } = require('../package.json');
|
||||
|
||||
const transformIgnorePattern = [
|
||||
'@material-ui',
|
||||
'@rjsf',
|
||||
'ajv',
|
||||
'core-js',
|
||||
'jest-.*',
|
||||
'jsdom',
|
||||
'knex',
|
||||
'react',
|
||||
'react-dom',
|
||||
'highlight\\.js',
|
||||
'prismjs',
|
||||
'react-use',
|
||||
'typescript',
|
||||
].join('|');
|
||||
|
||||
async function getProjectConfig(targetPath, displayName) {
|
||||
const configJsPath = path.resolve(targetPath, 'jest.config.js');
|
||||
@@ -58,10 +76,7 @@ async function getProjectConfig(targetPath, displayName) {
|
||||
currentPath = newPath;
|
||||
}
|
||||
|
||||
// We add an additional Jest config parameter only known by the Backstage CLI
|
||||
// called `transformModules`. It's a list of modules that we want to apply
|
||||
// our configured jest transformations for.
|
||||
// This is useful when packages are published in untranspiled ESM or TS form.
|
||||
// This is an old deprecated option that is no longer used.
|
||||
const transformModules = pkgJsonConfigs
|
||||
.flatMap(conf => {
|
||||
const modules = conf.transformModules || [];
|
||||
@@ -70,10 +85,14 @@ async function getProjectConfig(targetPath, displayName) {
|
||||
})
|
||||
.map(name => `${name}/`)
|
||||
.join('|');
|
||||
const transformModulePattern = transformModules && `(?!${transformModules})`;
|
||||
if (transformModules.length > 0) {
|
||||
console.warn(
|
||||
'The Backstage CLI jest transformModules option is no longer used and will be ignored. All modules are now always transformed.',
|
||||
);
|
||||
}
|
||||
|
||||
const options = {
|
||||
displayName,
|
||||
...(displayName && { displayName }),
|
||||
rootDir: path.resolve(targetPath, 'src'),
|
||||
coverageDirectory: path.resolve(targetPath, 'coverage'),
|
||||
collectCoverageFrom: ['**/*.{js,jsx,ts,tsx}', '!**/*.d.ts'],
|
||||
@@ -82,8 +101,7 @@ async function getProjectConfig(targetPath, displayName) {
|
||||
},
|
||||
|
||||
transform: {
|
||||
'\\.esm\\.js$': require.resolve('./jestEsmTransform.js'), // See jestEsmTransform.js
|
||||
'\\.(js|jsx|ts|tsx)$': require.resolve('@sucrase/jest-plugin'),
|
||||
'\\.(js|jsx|ts|tsx)$': require.resolve('./jestSucraseTransform.js'),
|
||||
'\\.(bmp|gif|jpg|jpeg|png|frag|xml|svg|eot|woff|woff2|ttf)$':
|
||||
require.resolve('./jestFileTransform.js'),
|
||||
'\\.(yaml)$': require.resolve('jest-transform-yaml'),
|
||||
@@ -92,11 +110,7 @@ async function getProjectConfig(targetPath, displayName) {
|
||||
// A bit more opinionated
|
||||
testMatch: ['**/?(*.)test.{js,jsx,mjs,ts,tsx}'],
|
||||
|
||||
// Default behaviour is to not apply transforms for node_modules, but we still want
|
||||
// to apply the esm-transformer to .esm.js files, since that's what we use in backstage packages.
|
||||
transformIgnorePatterns: [
|
||||
`/node_modules/${transformModulePattern}.*\\.(?:(?<!esm\\.)js|json)$`,
|
||||
],
|
||||
transformIgnorePatterns: [`/node_modules/(?:${transformIgnorePattern})/`],
|
||||
};
|
||||
|
||||
// Use src/setupTests.ts as the default location for configuring test env
|
||||
@@ -104,7 +118,21 @@ async function getProjectConfig(targetPath, displayName) {
|
||||
options.setupFilesAfterEnv = ['<rootDir>/setupTests.ts'];
|
||||
}
|
||||
|
||||
return Object.assign(options, ...pkgJsonConfigs);
|
||||
const config = Object.assign(options, ...pkgJsonConfigs);
|
||||
|
||||
// The config name is a cache key that lets us share the jest cache across projects.
|
||||
// If no explicit name was configured, generated one based on the configuration.
|
||||
if (!config.name) {
|
||||
const configHash = crypto
|
||||
.createHash('md5')
|
||||
.update(version)
|
||||
.update(Buffer.alloc(1))
|
||||
.update(JSON.stringify(config.transform))
|
||||
.digest('hex');
|
||||
config.name = `backstage_cli_${configHash}`;
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
// This loads the root jest config, which in turn will either refer to a single
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
const babel = require('@babel/core');
|
||||
|
||||
// We build .esm.js files with plugin:build, so to be able to load these in tests they need to be transformed
|
||||
// TODO: jest is working on module support, it's possible that we can remove this in the future
|
||||
module.exports = {
|
||||
process(src) {
|
||||
const result = babel.transform(src, {
|
||||
babelrc: false,
|
||||
compact: false,
|
||||
plugins: [
|
||||
// This transforms the regular ESM syntax, import and export statements
|
||||
require.resolve('@babel/plugin-transform-modules-commonjs'),
|
||||
// This transforms dynamic `import()`, which is not supported yet in the Node.js VM API
|
||||
require.resolve('babel-plugin-dynamic-import-node'),
|
||||
],
|
||||
});
|
||||
|
||||
return result.code;
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2021 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
const { createHash } = require('crypto');
|
||||
const { transform } = require('sucrase');
|
||||
const sucrasePkg = require('sucrase/package.json');
|
||||
const sucrasePluginPkg = require('@sucrase/jest-plugin/package.json');
|
||||
|
||||
const ESM_REGEX = /\b(?:import|export)\b/;
|
||||
|
||||
function process(source, filePath) {
|
||||
let transforms;
|
||||
|
||||
if (filePath.endsWith('.esm.js')) {
|
||||
transforms = ['imports'];
|
||||
} else if (filePath.endsWith('.js')) {
|
||||
// This is a very rough filter to avoid transforming things that we quickly
|
||||
// can be sure are definitely not ESM modules.
|
||||
if (ESM_REGEX.test(source)) {
|
||||
transforms = ['imports', 'jsx']; // JSX within .js is currently allowed
|
||||
}
|
||||
} else if (filePath.endsWith('.jsx')) {
|
||||
transforms = ['jsx', 'imports'];
|
||||
} else if (filePath.endsWith('.ts')) {
|
||||
transforms = ['typescript', 'imports'];
|
||||
} else if (filePath.endsWith('.tsx')) {
|
||||
transforms = ['typescript', 'jsx', 'imports'];
|
||||
}
|
||||
|
||||
// Only apply the jest transform to the test files themselves
|
||||
if (transforms && filePath.includes('.test.')) {
|
||||
transforms.push('jest');
|
||||
}
|
||||
|
||||
if (transforms) {
|
||||
return transform(source, { transforms, filePath }).code;
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
// TODO: contribute something like this to @sucrase/jest-plugin
|
||||
function getCacheKey(sourceText) {
|
||||
return createHash('md5')
|
||||
.update(sourceText)
|
||||
.update(Buffer.alloc(1))
|
||||
.update(sucrasePkg.version)
|
||||
.update(Buffer.alloc(1))
|
||||
.update(sucrasePluginPkg.version)
|
||||
.digest('hex');
|
||||
}
|
||||
|
||||
module.exports = { process, getCacheKey };
|
||||
@@ -28,8 +28,6 @@
|
||||
"backstage-cli": "bin/backstage-cli"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.4.4",
|
||||
"@babel/plugin-transform-modules-commonjs": "^7.4.4",
|
||||
"@backstage/cli-common": "^0.1.4",
|
||||
"@backstage/config": "^0.1.10",
|
||||
"@backstage/config-loader": "^0.6.10",
|
||||
@@ -54,7 +52,6 @@
|
||||
"@typescript-eslint/eslint-plugin": "^v4.30.0",
|
||||
"@typescript-eslint/parser": "^v4.28.3",
|
||||
"@yarnpkg/lockfile": "^1.1.0",
|
||||
"babel-plugin-dynamic-import-node": "^2.3.3",
|
||||
"bfj": "^7.0.2",
|
||||
"buffer": "^6.0.3",
|
||||
"chalk": "^4.0.0",
|
||||
|
||||
Reference in New Issue
Block a user