@farmfe/runtime
Advanced tools
Comparing version 0.9.1 to 0.9.2
# @farmfe/runtime | ||
## 0.9.2 | ||
### Patch Changes | ||
- 0ab4edf9: Fix failed to load external cjs require when output esm | ||
- 0ab4edf9: throw error when dynamic load fail. close #836 | ||
## 0.9.1 | ||
@@ -4,0 +11,0 @@ |
{ | ||
"name": "@farmfe/runtime", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"description": "Runtime of Farm", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -79,3 +79,3 @@ import { Module } from './module'; | ||
// TODO require should be async as we support `top level await`, This feature requires Node 16 and higher | ||
require(moduleId: string): any { | ||
require(moduleId: string, isCJS = false): any { | ||
if (INTERNAL_MODULE_MAP[moduleId]) { | ||
@@ -102,6 +102,11 @@ return INTERNAL_MODULE_MAP[moduleId]; | ||
if (!initializer) { | ||
// TODO: fix `const assert = require('assert');` when assert is external. This leads to `assert is not a function` error caused by default export different from esm and cjs | ||
// TODO: we may need to add `@swc/helpers/_/_interop_require_default` for external modules, replace `const assert = require('assert');` to `const assert = _interop_require_default(require('assert'));` | ||
if (this.externalModules[moduleId]) { | ||
return this.externalModules[moduleId]; | ||
const exports = this.externalModules[moduleId]; | ||
// fix `const assert = require('assert');` when assert is external. This leads to `assert is not a function` error caused by default export different from esm and cjs | ||
if (isCJS) { | ||
return exports.default || exports; | ||
} | ||
return exports; | ||
} | ||
@@ -169,2 +174,8 @@ // try node require if target Env is node | ||
.then(() => { | ||
if (!this.modules[moduleId]) { | ||
throw new Error( | ||
`Dynamic imported module "${moduleId}" is not registered.` | ||
); | ||
} | ||
const result = this.require(moduleId); | ||
@@ -171,0 +182,0 @@ // if the module is async, return the default export, the default export should be a promise |
21915
531