alibaba / rax

🐰 Rax is a progressive framework for building universal application. https://rax.js.org
Other
8k stars 627 forks source link

[Question]静态构建提示`ReferenceError: window is not defined` #2278

Closed xmsz closed 2 years ago

xmsz commented 2 years ago
{
  "targets": ["web"],
  "web": {
    "mpa": true,
    "pha": true,
    "staticExport": true
  },
  "plugins": [],
  "sourceMap": false,
  "hash": "contenthash"
}

(node:3284) UnhandledPromiseRejectionWarning: ReferenceError: window is not defined
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at exec (node_modules/build-plugin-rax-web/lib/Plugins/DocumentPlugin.js:205:12)
    at node_modules/build-plugin-rax-web/lib/Plugins/DocumentPlugin.js:124:39
    at Array.forEach (<anonymous>)
    at emitAssets (node_modules/build-plugin-rax-web/lib/Plugins/DocumentPlugin.js:75:23)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3284) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3284) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
SoloJiang commented 2 years ago

静态构建本质是 html 会在 node 端先构建出来,所以和 ssr 的限制条件一样,无法使用 DOM/BOM API

xmsz commented 2 years ago

静态构建本质是 html 会在 node 端先构建出来,所以和 ssr 的限制条件一样,无法使用 DOM/BOM API

ok

xmsz commented 2 years ago

那有没有什么好的方法解决

我的场景是

xmsz commented 2 years ago

或者说我有个统计插件里面有用到window对象 但其实构建的时候并不需要