我看到一些开发人员想知道如何在没有 CLI 的情况下运行 jscodeshift,存在一些问题,但它们非常简单,所以如果有人想使用选项有效地运行它,这里有一个示例。
const path = require('path');
const Runner = require('jscodeshift/src/Runner');
// just the paths of the files to apply the transformation
const paths = ['path/to/example1.js', 'path/to/example2.js'];
/**
* taken from
* @link https://github.com/facebook/jscodeshift/blob/48f5d6d6e5e769639b958f1a955c83c68157a5fa/bin/jscodeshift.js#L18
*/
const options = {
transform: 'path/to/transformerFile.js',
verbose: 0,
dry: false,
print: true,
babel: true,
extensions: 'js',
ignorePattern: [],
ignoreConfig: [],
runInBand: false,
silent: false,
parser: 'babel',
stdin: false
}
/**
* taken from
* @link https://github.com/facebook/jscodeshift/blob/48f5d6d6e5e769639b958f1a955c83c68157a5fa/bin/jscodeshift.js#L135
*/
Runner.run(
/^https?/.test(options.transform) ? options.transform : path.resolve(options.transform),
paths, …Run Code Online (Sandbox Code Playgroud) 好吧,我有一个Webpack和Jest的安装程序,用于测试反应,也使用Webpack的别名,并在项目目录“ src / app”中使用“ @”符号,我也知道它必须使用以下命令映射到jest.config中“ moduleNameMapper”属性以与该别名正确对齐。
问题是,有一个<Icon />组件可以使用ES动态导入来动态导入.svg图标。
这是jest.config.js映射的正则表达式
'^ @ /':'/ src / app / $ 1'
但仅在测试时会破坏
一切正常,除了测试时
我的文件夹结构如下:
config/
webpack-config/
src/
app/
assets/
img/
some images
svg/
icons/
cart.svg
components/
base/
core/
shared/
...
client/
...
server/
...
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的Webpack别名:
module.exports = {
extensions: ['.js', '.mjs', '.json', '.jsx', '.css'],
modules: paths.resolveModules,
alias: {
'@': paths.srcApp, // that just point
}
};
Run Code Online (Sandbox Code Playgroud)
这是我的jest.config文件:
const paths = require('./config/paths');
module.exports = {
verbose: true,
collectCoverageFrom: ['src/**/*.{js,jsx,mjs}'],
setupFiles: [
'<rootDir>/node_modules/regenerator-runtime/runtime', …Run Code Online (Sandbox Code Playgroud)