我正在尝试在next js中fork一个子进程,以进行耗时的操作。下面是我的webpack配置(next.config.js
):
const { merge } = require('webpack-merge');
module.exports = {
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
if (isServer) {
return merge(config, {
entry () {
return config.entry().then((entry) => {
return Object.assign({}, entry, { 'collection.worker': path.resolve(process.cwd(), 'workers/collection.worker.ts') })
})
}
});
} else {
return config;
}
}
};
字符串
我的collection.worker.ts文件:
const sleep = require('await-sleep');
const paymentId = process.argv[2];
(async () => {
// DO WORK HERE
console.log("doing work ", paymentId);
await sleep(10000);
console.log("Done!");
})();
// tslint:disable-next-line: no-empty
const noop = () => {};
export default noop;
型
以下是我的src/pages/api/hello.ts
端点:
import type { NextApiRequest, NextApiResponse } from 'next'
import {getServerSession} from "next-auth";
import authOptions from "@/pages/api/auth/[...nextauth]";
import {fork} from "child_process";
type Data = {
name: string
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
const session = await getServerSession(req, res, authOptions)
fork("./.next/server/collection.worker.js", ["150"], { cwd: process.cwd() });
res.status(200).json({ name: 'John Doe' })
}
型
请注意,workers/collection.worker.ts
不在src/
目录中(与next.config.js
的级别相同:
的数据
当我访问/api/hello
端点时,我在控制台中得到以下错误:
at Object.<anonymous> (/Users/...projectdir/.next/server/collection.worker.js:9:1)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
型
下面是生成的.next/server/collection.woker.js
/*
* ATTENTION: An "eval-source-map" devtool has been used.
* This devtool is neither made for production nor for readable output files.
* It uses "eval()" calls to create a separate source file with attached SourceMaps in the browser devtools.
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
* or disable the default devtool with "devtool: false".
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
*/
(self["webpackChunk_N_E"] = self["webpackChunk_N_E"] || []).push([["collection.worker"],{
/***/ "./node_modules/await-sleep/index.js":
/*!*******************************************!*\
!*** ./node_modules/await-sleep/index.js ***!
\*******************************************/
/***/ ((module) => {
eval("module.exports = ms => new Promise(resolve => setTimeout(resolve, ms));\n//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9ub2RlX21vZHVsZXMvYXdhaXQtc2xlZXAvaW5kZXguanMuanMiLCJtYXBwaW5ncyI6IkFBQUEiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly9fTl9FLy4vbm9kZV9tb2R1bGVzL2F3YWl0LXNsZWVwL2luZGV4LmpzPzVmMmUiXSwic291cmNlc0NvbnRlbnQiOlsibW9kdWxlLmV4cG9ydHMgPSBtcyA9PiBuZXcgUHJvbWlzZShyZXNvbHZlID0+IHNldFRpbWVvdXQocmVzb2x2ZSwgbXMpKTtcbiJdLCJuYW1lcyI6W10sInNvdXJjZVJvb3QiOiIifQ==\n//# sourceURL=webpack-internal:///./node_modules/await-sleep/index.js\n");
/***/ }),
/***/ "./workers/collection.worker.ts":
/*!**************************************!*\
!*** ./workers/collection.worker.ts ***!
\**************************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (__WEBPACK_DEFAULT_EXPORT__)\n/* harmony export */ });\nconst sleep = __webpack_require__(/*! await-sleep */ \"./node_modules/await-sleep/index.js\");\nconst paymentId = process.argv[2];\n(async ()=>{\n // DO WORK HERE\n console.log(\"doing work \", paymentId);\n await sleep(10000);\n console.log(\"Done!\");\n})();\n// tslint:disable-next-line: no-empty\nconst noop = ()=>{};\n/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (noop);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2VSb290IjoiIn0=\n//# sourceURL=webpack-internal:///./workers/collection.worker.ts\n");
/***/ })
},
/******/ __webpack_require__ => { // webpackRuntimeModules
/******/ var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
/******/ var __webpack_exports__ = (__webpack_exec__("./workers/collection.worker.ts"));
/******/ (_ENTRIES = typeof _ENTRIES === "undefined" ? {} : _ENTRIES)["middleware_collection.worker"] = __webpack_exports__;
/******/ }
]);
型
1条答案
按热度按时间zlwx9yxi1#
也许你的webpack配置一定是这样的
字符串