Webpack 在构建时不呈现任何标记逻辑

如何解决Webpack 在构建时不呈现任何标记逻辑

我有一个带有自定义 webpack(v5) 配置的微前端应用程序,带有 react、typescript 和模块联合插件。 在开发模式下一切正常。此外,构建的项目在导入并呈现到微前端容器应用中时运行良好(容器应用将所有微前端应用连接在一起)。

但是,当执行构建并且我尝试在本地运行生产构建时,使用 serve -s dist -p 9997,我只会得到一个没有标记呈现的空白页面。 (为长帖提前道歉):

webpack 构建配置文件

module.exports = {
    resolve: {
        extensions: [".ts",".tsx",".js","jsx"],// file extensions we allow webpack to evaluate
    },mode: "production",output: {
        filename: "[name].[contenthash].js",clean: true,},module: {
        rules: [
            {
                test: /\.tsx?$/,exclude: /node_modules/,use: {
                    loader: "babel-loader",options: {
                        presets: [
                            // "@babel/preset-env","@babel/preset-react","@babel/preset-typescript",],plugins: ["@babel/plugin-transform-runtime"],plugins: [
        new ModuleFederationPlugin({
            name: "microfrontend_app",// name of the app used by the container to get the app code
            filename: "remoteEntry.js",// name of the file this app sends to the container - DO NOT CHANGE.
            exposes: {
                "./MicrofrontendApp": "./src/bootstrap",// alias,lets the container app import this app as a module with name PortalNavigationApp
            },shared: packageJson.dependencies,}),new HtmlWebpackPlugin({
            template: "./public/index.html",};

webpack 开发配置文件

module.exports = {
    mode: "development",resolve: {
        extensions: [".ts",devServer: {
        port: 9998,historyApiFallback: {
            index: "/index.html",};

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <title>Title1</title>
</head>
<body>
    <div id="_microfrontend-app"></div>
</body>
</html>

App.tsx

export default ({ history }: IApp): JSX.Element => {
    console.log('loading App.tsx');
    return (
        <MainWrapper>
            <Router history={history as unkNown as H.History}>
                <h1>hello from wellcom template app</h1>
            </Router>
        </MainWrapper>
    );
};

const MainWrapper = styled.main``;

bootstrap.tsx

const mount = (
    el: Element,{
        onNavigate,defaultHistory,initialPath,}: {
        onNavigate: (() => void) | null;
        defaultHistory: any;
        initialPath: string | null;
    }
): unkNown => {
    // if in development and isolation,use browser history. If not,use memory history
    const history =
        defaultHistory ||
        createMemoryHistory({
            initialEntries: [initialPath || ""],});
    if (onNavigate) {
        history.listen(onNavigate);
    }

    ReactDOM.render(<App history={history} />,el);

    return {
        onParentNavigate({ pathname: nextPathName }: { pathname: string }) {
            const { pathname } = history.location;

            //prevent infinite routing loop
            if (pathname !== nextPathName) {
                history.push(nextPathName);
            }
        },};
};

// If we are in development and in isolation call mount immediately
if (process.env.NODE_ENV === "development") {
    const devRoot = document.querySelector("#_microfrontend-app");

    if (devRoot) {
        mount(devRoot,{
            defaultHistory: createbrowserHistory(),initialPath: null,onNavigate: null,});
    }
}

export { mount };

index.ts

import("./bootstrap");

这是我运行 webpack.dev.js 时得到的输出

Running in dev with webpack.dev

这是我使用 serve -s dist -p 9997 运行 webpack 构建和服务后得到的输出

Running in prod with webpack.prod

我就是不明白为什么会发生这种情况。这是我的 dist 文件夹的输出

enter image description here

index.html 正确附加了 main.975.... js 文件。如果我在所有 js 文件中执行 CTRL+F 查找“来自 App.tsx 的你好”,我会在 499.63...js 中找到它。所以标记逻辑存在于构建中。

enter image description here

此外,main.975 js 文件似乎聚合了所有其他 js 文件

enter image description here

我只是不明白为什么标记没有在构建版本中呈现?我怀疑 webpack prod 文件中缺少某些内容。我试图搜索 create-react-app webpack 文件进行比较,但那里的东西太多了,我完全迷失了。

解决方法

想通了。非常愚蠢,但在 bootstrap.tsx 中,如果 development = 'production',则永远不会调用 mount 函数。

所以只需删除

if (process.env.NODE_ENV === "development") {
...
}

如果检查使它工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?