You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.6 KiB
53 lines
1.6 KiB
'use strict';
|
|
|
|
const babelJest = require('babel-jest');
|
|
const makeCacheKey = require('@jest/create-cache-key-function').default();
|
|
const peggy = require("peggy");
|
|
|
|
const hasJsxRuntime = (() => {
|
|
if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
require.resolve('react/jsx-runtime');
|
|
return true;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
})();
|
|
|
|
const babelJestInstance = babelJest.createTransformer({
|
|
presets: [
|
|
[
|
|
require.resolve('babel-preset-react-app'),
|
|
{
|
|
runtime: hasJsxRuntime ? 'automatic' : 'classic',
|
|
},
|
|
],
|
|
],
|
|
babelrc: false,
|
|
configFile: false,
|
|
});
|
|
|
|
const startRuleFinder = /\s*\/\/\s*peggy-loader:startRule\s*(\S+)\s*\n/g
|
|
|
|
module.exports = {
|
|
canInstrument: babelJestInstance.canInstrument,
|
|
createTransformer() {
|
|
return this;
|
|
},
|
|
getCacheKey(sourceText, sourcePath, configString, options) {
|
|
return makeCacheKey(sourceText, sourcePath, configString, options) + babelJestInstance.getCacheKey(sourceText, sourcePath, configString, options)
|
|
},
|
|
process(sourceText, sourcePath, configString, options) {
|
|
const start = Array.from({ [Symbol.iterator]() { return sourceText.matchAll(startRuleFinder) } }, ([_, rule]) => rule)
|
|
const resultSource = peggy.generate(sourceText, {
|
|
grammarSource: this.resourcePath,
|
|
allowedStartRules: start,
|
|
output: "source",
|
|
format: "es",
|
|
})
|
|
return babelJest.process(resultSource, sourcePath, configString, options)
|
|
},
|
|
} |