pnpm 的 shellEmulator 对 package.json 脚本的影响

19 0.5~0.6 分钟 204

pnpm 的 shellEmulator 是一个非常实用的配置项。

pnpm-workspace.yaml 添加一行 shellEmulator: true 之后,你就不需要再使用 cross-env 做环境变量的配置了。

如下示例,你现在可以轻松的设置环境变量 TEST"true" 了:

// package.json
"scripts": {
  "test": "TEST=true node test.js"
}
// test.js
console.log(process.env.TEST)

影响

这个功能由 @yarnpkg/shell 支持,在使用过程中我发现一些脚本的写法要进行修改。

shellEmulator: false(默认值)的情况下,以下写法是在 Linux 和 Windows 上通用的写法,用于运行多个脚本

// package.json
"scripts": {
  "test": "pnpm run \"/:test$/\""
}

设置 shellEmulator: true 后,你需要修改为以下其中一种写法:

第一种(推荐):

// package.json
"scripts": {
  "test": "pnpm run '/:test$/'"
}

第二种

// package.json
"scripts": {
  "test": "pnpm run \"/:test\\$/\""
}

结语

欢迎留言交流。


0