D:\xxx\node_modules\.pnpm\@unocss+config@0.60.2\node_modules\@unocss\config\dist\index.mjs:1 import { resolve, dirname } from 'node:path'; ^^^^^^
SyntaxError: Cannot use import statement outside a module at Object.compileFunction (node:vm:360:18) at wrapSafe (node:internal/modules/cjs/loader:1124:15) at Module._compile (node:internal/modules/cjs/loader:1160:27) at Module._extensions..js (node:internal/modules/cjs/loader:1250:10) at Object.newLoader [as .mjs] (D:\projects\xxx\node_modules\.pnpm\pirates@4.0.6\node_modules\pirates\lib\index.js:121:7) at Module.load (node:internal/modules/cjs/loader:1074:32) at Function.Module._load (node:internal/modules/cjs/loader:909:12) at Module.require (node:internal/modules/cjs/loader:1098:19) at require (node:internal/modules/cjs/helpers:108:18) at Object.<anonymous> (D:\projects\xxx\node_modules\.pnpm\@unocss+webpack@0.60.2_webpack@5.91.0\node_modules\@unocss\webpack\dist\index.cjs:10:16) D:\projects\xxx: ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL xxx@0.0.1 dev:weapp: `npm run build:weapp -- --watch --env production` Exit status 1
可以看到它的介绍:One of the ways you can use swc is through the require hook. The require hook will bind itself to node’s require and automatically compile files on the fly.使用swc的一种方法是通过require钩子。require钩子将自己绑定到node的require,并自动动态编译文件。
很直白了,就是我们使用require的时候,使用swc将代码转译一下再运行。
再简单耍一下这个工具
为了尝试一下这个工具是不是真的能编译ts成cjs,我们创建个空白文件夹:
1 2
pnpm init pnpm add @swc/register
创建一个ts文件:
1 2
// lib.ts exportconstadd = (a: number, b: number) => a + b