-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Module with Top-level await transformed incorrectly during server build #7238
Comments
Related to #6520 We can investigate this—my hypothesis is that it has something to do with the static site generation plugin, which probably doesn't wait for the top-level promise to resolve. No guarantee that it will be fixed, though, since it's not our own code. |
For some reason, TLA results in the default export being a promise, instead of the entire module being a promise. In order to investigate, I logged the output of what got loaded (the first prop passed to But in SSR: {
__comp: {
default: Promise {
[Object [Module]],
[Symbol(webpack exports)]: [Object [Module]],
[Symbol(webpack then)]: [Function (anonymous)]
}
},
'__context.plugin': {
name: 'docusaurus-plugin-content-pages',
id: 'default',
default: { name: 'docusaurus-plugin-content-pages', id: 'default' }
},
config: Object [Module] { default: [Getter] }
}
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. I will spin up an isolated Webpack config to see who that is to blame (Babel? Webpack? React-loadable?) |
I strongly suspect this has something to do with Edit. Seems that's indeed the case: playground The reason why it puts it inside a |
While trying to remove $ git diff main packages/docusaurus/src/babel/preset.ts
diff --git a/packages/docusaurus/src/babel/preset.ts b/packages/docusaurus/src/babel/preset.ts
index 3dd351ef9a..1fc764d7da 100644
--- a/packages/docusaurus/src/babel/preset.ts
+++ b/packages/docusaurus/src/babel/preset.ts
@@ -62,10 +62,6 @@ function getTransformOptions(isServer: boolean): TransformOptions {
absoluteRuntime: absoluteRuntimePath,
},
],
- // Adds syntax support for import()
- isServer
- ? require.resolve('babel-plugin-dynamic-import-node')
- : require.resolve('@babel/plugin-syntax-dynamic-import'),
],
};
} And I attempted to build a site with nothing but export const text = await Promise.resolve("Hello");
export default function Home() {
return text;
} Surprisingly,
The actual chunk for the 404 page is I've seen the same errors in the past when I'm setting up SWC, but I don't have the patience to look into it since it looks super obscure. My hypothesis is that it's because the same file is compiled differently, leading to different hashes (? I decided to not touch it at this point. If anyone wants to look into it, help appreciated. |
I actually dug into the server bundle output (before SSG plugin kicks in), and the "module not found" is thrown from here: /* webpack/runtime/get javascript chunk filename */
(() => {
// This function allow to reference async chunks
__webpack_require__.u = (chunkId) => {
// return url for filenames based on template
return "assets/js/" + ({"115":"4825d8ed","237":"1df93b7f"}[chunkId] || chunkId) + "." + {"115":"17431192","237":"9726a329","763":"3e8c76c2"}[chunkId] + ".js";
};
})(); Note that Edit. The problem is that the SSG webpack plugin kicks in too early: before the dynamically imported code gets emitted. Here's a minimum repro: // entry.js
export default function render() {
return import("./comp.js").then(({ val }) => val);
} // comp.js
export const val = "Hello"; // webpack.config.js
const path = require("path");
const StaticSiteGeneratorPlugin = require("@slorber/static-site-generator-webpack-plugin");
module.exports = {
target: "node18.0",
mode: "development",
name: "test",
entry: {
main: path.resolve(__dirname, "./entry.js"),
},
output: {
clean: true,
pathinfo: false,
libraryTarget: "commonjs2",
globalObject: "this",
path: path.resolve("build"),
filename: "server.bundle.js",
chunkFilename: "assets/js/[name].[contenthash:8].js",
publicPath: "/",
hashFunction: "xxhash64",
libraryTarget: "commonjs2",
},
plugins: [
new StaticSiteGeneratorPlugin({
entry: "main",
paths: ["/"],
}),
],
optimization: {
splitChunks: false,
},
}; When you comment out the SSG plugin, everything gets emitted, including the dynamic chunks; however, the above code throws: Maybe we can fix it in the SSG plugin? |
Have you read the Contributing Guidelines on issues?
Prerequisites
npm run clear
oryarn clear
command.rm -rf node_modules yarn.lock package-lock.json
and re-installing packages.Description
Hi, I have enabled top-level await support in webpack config, and things work fine during
docusaurus run
, but fails duringdocusaurus build
This is my mdx content:
Reproducible demo
https://stackblitz.com/edit/github-7mt6ad?file=src/pages/test.mdx
Steps to reproduce
experiments.topLevelAwait
totrue
using configureWebpack (see plugins at docusaurus.config.js )docusaurus run
, things work fine.docusaurus build
Expected behavior
top-level await should also work during build process
Actual behavior
top-level await doesn't work during build process
Your environment
Self-service
The text was updated successfully, but these errors were encountered: