尝试使用 nextjs npm run dev 运行命令时显示错误 - 无法加载 SWC 二进制文件,请在此处查看更多信息: https: //nextjs.org/docs/messages/failed-loading-swc。
我尝试卸载节点并使用版本 16.13 重新安装它,但没有成功,在 vercel 页面上,但到目前为止不成功。有小费吗?
另外,我注意到这是 NextJS 讨论页面上的一个当前问题,它与新的基于 Rust 的编译器有关,它比 Babel 更快。
Next.JS 使用 babel 来配置 Why Did You Render。
module.exports = function (api) {
const isServer = api.caller((caller) => caller?.isServer)
const isCallerDevelopment = api.caller((caller) => caller?.isDev)
const presets = [
[
'next/babel',
{
'preset-react': {
importSource:
!isServer && isCallerDevelopment
? '@welldone-software/why-did-you-render'
: 'react'
}
}
]
]
return {presets}
}
Run Code Online (Sandbox Code Playgroud)
如何在不禁用 SWC 的情况下更新到 Next.JS 12?
如何翻译这样的节点脚本:
"test": "NODE_ENV=test riteway -r @babel/register 'src/**/*.test.js' | tap-nirvana",
Run Code Online (Sandbox Code Playgroud)
使用SWC而不是 Babel?
我们最近升级了 Next.js 版本。Next.js 现在支持SWC 而不是 Babel。
我们项目中的 React 单元测试是用RITEway编写的。测试命令为:
"test": "NODE_ENV=test riteway -r @babel/register 'src/**/*.test.js' | tap-nirvana",
Run Code Online (Sandbox Code Playgroud)
它首先使用 Babel 转换文件,因为否则import语句和 JSX 会导致错误。
在我们尝试迁移到 SWC 的过程中,我们对 CLI 的使用并不顺利。不过,我们还是找到了这个@swc-node/register包裹。它允许我们像这样改变我们的命令:
"test": "riteway -r @swc-node/register src/**/*.test.js | tap-nirvana"
Run Code Online (Sandbox Code Playgroud)
它修复了像import语句这样的新语法,并且将运行这样的测试:
import { describe } from 'riteway';
describe('my test', async assert => {
assert({
given: 'true',
should: 'be true',
actual: true, …Run Code Online (Sandbox Code Playgroud) 我希望在 Rust 中使用SWC来生成一些 TypeScript 代码。不幸的是,发射器似乎只能打印 JavaScript。这是正确的,还是有办法打印 TypeScript?例如,假设我们正在制作以下 AST。
use swc_atoms::js_word;
use swc_common::{BytePos, Span, SyntaxContext};
use swc_ecmascript;
use swc_ecmascript::ast;
fn main() {
let program = ast::Program::Module(ast::Module {
body: vec![ast::ModuleItem::ModuleDecl(ast::ModuleDecl::ExportDecl(
ast::ExportDecl {
decl: ast::Decl::TsTypeAlias(ast::TsTypeAliasDecl {
span: Span::default(),
declare: false,
id: ast::Ident::new(js_word!(""), Span::default()),
type_params: None,
type_ann: Box::new(ast::TsType::TsKeywordType(ast::TsKeywordType {
span: Span::default(),
kind: ast::TsKeywordTypeKind::TsStringKeyword,
})),
}),
span: Span::default(),
},
))],
shebang: None,
span: Span::new(BytePos(1), BytePos(1), SyntaxContext::empty()),
});
}
Run Code Online (Sandbox Code Playgroud)
如何获取与programAST 对应的 TypeScript?
尝试使用 Next.js 运行命令时npm run dev显示error - failed to load SWC binary see more info here: https://nextjs.org/docs/messages/failed-loading-swc.
PS D:\web-development\new-project\explore> npm run dev
> dev
> next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
Error: The specified module could not be found.
\\?\D:\web-development\new-project\explore\node_modules\@next\swc-win32-x64-msvc\next-swc.win32-x64-msvc.node
at Object.Module._extensions..node (node:internal/modules/cjs/loader:1179:18)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at loadNative (D:\web-development\new-project\explore\node_modules\next\dist\build\swc\index.js:84:28)
at loadBindings (D:\web-development\new-project\explore\node_modules\next\dist\build\swc\index.js:41:32)
at async Object.isWasm (D:\web-development\new-project\explore\node_modules\next\dist\build\swc\index.js:146:20)
at async D:\web-development\new-project\explore\node_modules\next\dist\build\webpack\loaders\next-swc-loader.js:62:178 {
code: 'ERR_DLOPEN_FAILED'
}
error - …Run Code Online (Sandbox Code Playgroud) 当尝试使用 Jest 启动服务器时,我不断收到错误:找不到模块“服务/中间件”。
在本例中,services/middleware位于src/services/middleware。
我已经在 jest 配置中设置了 moduleNameMapper 选项,所以我不确定是什么仍然导致了错误。
webpack.config.js
module.exports = (env) => {
return {
mode: MODE,
externalsPresets: { node: true },
externals: [nodeExternals()],
node: {
global: true,
__filename: false,
__dirname: false,
},
watch: !isProduction,
resolve: {
alias: {
services: path.resolve(__dirname, 'src', 'services'),
},
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'swc-loader',
},
],
},
};
};
Run Code Online (Sandbox Code Playgroud)
和
jest.config.js
module.exports = {
testEnvironment: 'node',
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
rootDir: …Run Code Online (Sandbox Code Playgroud) 我有一个打字稿项目,我正在使用 swc 进行编译。我无法让 swc 解析 或 中定义的路径tsconfig.json别名.swcrc。eslint 可以正确解析模块别名,并且在未将 swc 指定为编译器的情况下运行 ts-node 时,可以正确解析模块别名。
鉴于此拉取请求,似乎 swc 应该能够解析tsconfig.json并支持 中定义的路径别名.swcrc。然而,似乎人们在使用此功能后也遇到了与我类似的问题,尽管那是两年前的事了,我无法想象此后还没有解决方案。
另外,在一个奇怪的旁注中,实现 TSConfigResolver 的 PR 的作者在他的 PR 合并一年后的一个问题中提到了 swc“不考虑 tsconfig”。虽然这不应该是我的问题的根源,因为我在中指定了相同的路径别名.swcrc
swc 文档非常模糊,没有提及任何有关模块别名或解析 tsconfig 的内容,除此之外,jsc.baseUrl它们jsc.paths是编译选项,并且它们与 tsconfig 语法匹配。
任何帮助或指导将不胜感激,谢谢!
tsconfig.json
{
"compilerOptions": {
"target": "es2020",
"module": "ESNext",
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"$lib/*": ["src/lib/*"]
},
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"ts-node": …Run Code Online (Sandbox Code Playgroud) 我是第一次使用SWC,无法将 ts 文件编译为普通的 js 文件。
当我运行命令时:
npx swc ./src -d dist
Run Code Online (Sandbox Code Playgroud)
我收到错误:
failed to process js file
Caused by:
0: failed to read swcrc file (src/index.ts)
1: failed to deserialize .swcrc (json) file: syntax error: 1:1
2: expected value at line 1 column 1
Run Code Online (Sandbox Code Playgroud)
这是我用SWC Playground生成的 my.swcrc
npx swc ./src -d dist
Run Code Online (Sandbox Code Playgroud)
我的目标是:
./src从我的目录中获取我的 ts 文件dist启用源映射的目录中我用来swc在一个副项目上转译我的 Typescript 代码,并且正在努力使用 cli 选项忽略最终输出中的测试文件--ignore。
库版本:
@swc/cli: ^0.1.57
@swc/core: ^1.2.173
Run Code Online (Sandbox Code Playgroud)
命令:
swc ./src --out-dir dist --ignore **/*.test.ts
Run Code Online (Sandbox Code Playgroud)
.swrc 配置
{
"jsc": {
"target": "es5",
"paths": {
"@src/*": ["./src/*"]
},
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
}
},
"minify": true,
}
Run Code Online (Sandbox Code Playgroud)
我仍然在我的 dist 输出文件夹中看到所有测试文件。请注意,像这样在 .swcrc 中使用排除属性 "exclude": [".*\\.spec|test\\.(j|t)s$", "mocks", "types"]是可行的,但是--ignore应该如何使用 arg 呢?
我一直在努力使用 SWC 编译器通过 Next.js 转换导入。
我正在尝试使用swc-plugin-transform-import作为babel-plugin-transform-imports的替代品,以缩短 Material UI 导入的时间。
如记录所示,我尝试过此设置。它显示了实验性警告,但除此之外它完全忽略了插件。
// next.config.js
module.exports = {
experimental: {
swcPlugins: [
[
'swc-plugin-transform-import',
{
"@mui/material": {
transform: "@mui/material/${member}",
preventFullImport: true
},
"@mui/icons-material": {
transform: "@mui/icons-material/${member}",
preventFullImport: true
},
"@mui/styles": {
transform: "@mui/styles/${member}",
preventFullImport: true
},
"@mui/lab": {
transform: "@mui/lab/${member}",
preventFullImport: true
}
}
]
]
}
}
Run Code Online (Sandbox Code Playgroud)
有人知道如何启用和配置swc-plugin-transform-importNext.js 吗?谢谢
swc-compiler ×10
babeljs ×4
next.js ×4
typescript ×4
javascript ×3
node.js ×2
codegen ×1
jestjs ×1
jsx ×1
material-ui ×1
react-native ×1
reactjs ×1
rerender ×1
rust ×1
swc ×1
webpack ×1