简体   繁体   English

@loadable/server 将整个统计信息 JSON 传递给 eval('require')(modulePath)

[英]@loadable/server pass the whole stats JSON to eval('require')(modulePath)

I'm trying to setup SSR for react app with @loadable/components .我正在尝试使用@loadable/components为反应应用程序设置 SSR。 I setup all based on docs with babel and webpack plugins.我设置了所有基于带有 babel 和 webpack 插件的文档。 When I try to run node server.js it runs ok but when I open a browser and throws the following error (into node console):当我尝试运行node server.js它运行正常但是当我打开浏览器并抛出以下错误(进入节点控制台):

TypeError [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received an instance of Object
    at validateString (internal/validators.js:118:11)
    at Module.require (internal/modules/cjs/loader.js:1033:3)
    at require (internal/modules/cjs/helpers.js:72:18)
    at smartRequire (/Users/max/Documents/repos/app/node_modules/@loadable/server/lib/util.js:44:25)
    at new ChunkExtractor (/Users/max/Documents/repos/app/node_modules/@loadable/server/lib/ChunkExtractor.js:181:50)
    at renderer (webpack://app/./node_modules/@MYSCOPE/elm/dist/elm.esm.js?:3619:19)
    at eval (webpack://app/./src/server.tsx?:64:90)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  code: 'ERR_INVALID_ARG_TYPE'
}

As you can see there is @MYSCOPE in the traceback which holds some of my internal packages (if it matters).如您所见,回溯中有@MYSCOPE,其中包含我的一些内部包(如果重要的话)。

@loadable/server/lib/util.js is the following function: @loadable/server/lib/util.js如下 function:

在此处输入图像描述

And when I try to console.log(modulePath) on line 42 I see a whole stats JSON output which seems wrong and I should get a single module path (as I understand).当我尝试在第42console.log(modulePath)时,我看到了一个完整的统计数据 JSON output 这似乎是错误的,我应该得到一个单一的模块路径(据我所知)。

Any help?有什么帮助吗?

I can share some specific parts of my configuration files if needed.如果需要,我可以共享配置文件的某些特定部分。 Because I see my own package in console output seems like something is wrong with it's build (it works perfectly on the client-side with cjs build), but having full stats object as module path is very confusing.因为我在控制台 output 中看到了我自己的 package,所以它的构建似乎有问题(它在 cjs 构建的客户端上完美运行),但是拥有完整的统计数据 ZA8CFDE6331BD59EB2AC96F891C4 非常令人困惑。

UPD: Demo https://www.dropbox.com/s/9r947cgg4qvqbu4/loadable-test.zip?dl=0 UPD:演示https://www.dropbox.com/s/9r947cgg4qvqbu4/loadable-test.zip?dl=0

Run

yarn
yarn dev:server
# go to localhost:3000 and see the error in console

to rebuild:重建:

yarn
yarn dev:build-client
yarn dev:build-server
yarn dev:server # go to localhost:3000

The statsFile option passed to ChunkExtractor expects a path to the loadable-stats.json file, not the actual JSON content of it.传递给ChunkExtractorstatsFile选项需要一个指向loadable-stats.json文件的路径,而不是它的实际 JSON 内容。 By doing require('../loadable-stats.json') , webpack actually resolve the JSON during build time and assign it to the loadableJson variable.通过执行require('../loadable-stats.json') , webpack 实际上在构建期间解析 JSON 并将其分配给loadableJson变量。

You can change your loadableJson as follow:您可以按如下方式更改您的loadableJson

import path from 'path';

const loadableJson = path.resolve(__dirname, '../bundle_client/loadable-stats.json');

This will solve the problem you had on your question.这将解决您在问题上遇到的问题。 But, if you only do this, you will notice that you have another problem.但是,如果你只这样做,你会发现你还有另一个问题。 Loadable by default assumes that your entry chunk name is main . Loadable 默认情况下假定您的条目块名称是main This is not the case in your demo, as you have set the entry chunk name to be app instead.在您的演示中不是这种情况,因为您已将条目块名称设置为app

entry: {
    app: ['@babel/polyfill', './src/index.tsx']
},

To solve this, simply tell loadable about your entrypoints names by passing an array to the ChunkExtractor contructor as such:要解决这个问题,只需通过将数组传递给ChunkExtractor构造函数来告诉可加载您的entrypoints点名称:

    const extractor = new ChunkExtractor({
        statsFile: loadableJson,
        entrypoints: ["app"], // name of your entry chunk
    });

That's it, everything should now be working properly!就是这样,现在一切都应该正常工作了!

If it helps, I set up the demo on GitHub so you can easily see the changes I made here .如果有帮助,我会在 GitHub 上设置演示,以便您可以轻松查看我在此处所做的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM