当前位置: 首页 > 知识库问答 >
问题:

带webpack 4的树震动嵌套模块

狄玉书
2023-03-14

不知道为什么摇树没有像我预期的那样有效。。。我的目标是建立一个树摇动库。

  • index.js
    • 标题
    • 按钮

    我有一个使用这个库的webpack应用程序。当我只导入Header组件时,Button模块会按预期删除,但我的webpack包包含ButtonGroup组件。

    有人能解释一下为什么吗?如果可能的话,我如何从webpack构建中摇动嵌套组件?

    谢谢

    汇总

    汇总。配置。js

    import babel from 'rollup-plugin-babel';
    import resolve from 'rollup-plugin-node-resolve';
    import commonjs from 'rollup-plugin-commonjs';
    import replace from 'rollup-plugin-replace';
    
    export default {
      input: './src/index.js',
      output: {
        name: 'ui-component',
        sourcemap: true, 
        sourcemapFile: 'ui-component',
        format: 'es',
        file: 'build/ui-component.module.js',
      },
      plugins: [
        replace({
          'process.env.NODE_ENV': JSON.stringify('production')
        }),
        resolve({
            extensions: [ '.js', '.jsx', '.json' ]  
        }),
        commonjs(),
        babel({
          exclude: 'node_modules/**',
        })
      ],
      external: ['react', 'react-dom']
    };
    

    指数js

    export * from './Button';
    export * from './Header';
    

    标题。jsx-简单反应组件

    import React, { Component } from 'react';
    
    export class Header extends Component {
      render() {
        return (
          <div style={ { padding: '3px 7px' } }>
            <span>{this.props.children}</span>
          </div>
        )
      }
    }
    

    按钮jsx-react组件

    import React, { Component } from 'react';
    import { ButtonGroup } from './ButtonGroup';
    
    class Button extends Component {
    
        constructor(props) {
            super(props);
        }
    
        render() {
            const display = 'button';
            return (
                <ButtonGroup>
                    <a> {display} </a>
                </ButtonGroup>
            );
        }
    }
    
    export { Button } 
    

    以及我的网页包配置和构建:

    网页包。配置

    const path    = require('path');
    const express = require('express');
    
    module.exports = {
      mode: 'production',
      devtool: false,
      optimization: {
        sideEffects: false
      },
      entry: {
        index: './src/App.jsx'
      },
      output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, './bundle/')
      },
      resolve: {
        extensions: ['.js', '.jsx'],
        alias: {
          'ui-component': path.resolve(__dirname, '../') 
        }
      },
      module: {
        rules: [
          {
            test: /\.(jsx|js)$/,
            include: [
              path.resolve(__dirname, 'src'), 
              path.resolve(__dirname, '../') 
            ],   
            use: {
              loader: 'babel-loader',
              options: {
                presets: [
                  [ "@babel/env", { "modules": false}],
                  [ "@babel/react" ]
                ],
                plugins: [
                  ["@babel/plugin-proposal-object-rest-spread"],
                  ["@babel/plugin-proposal-class-properties"] 
                ]
              }
            }
          },
          {
            test: /\.css$/,
            use: [ 'style-loader', 'css-loader' ]
          },
          {
            test: /\.(eot|svg|ttf|woff|woff2)$/,
            use: [ 'url-loader' ]
          }
        ]
      },
      externals: {
        react: 'React',
        'react-dom': 'ReactDOM'
      },
      devServer: {
        contentBase: './',
        publicPath: '/build',
        port: 8080,
        before(app) {
          app.use('/build', express.static(path.resolve(__dirname, '../build/')))
        }
      }
    };
    

    *应用程序。jsx*

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Header } from 'ui-component';
    
    class App extends React.Component {
      render() {
        return (
          <div>
            <Header >
              header only
            </Header>
          </div>
        );
      }
    }
    
    
    ReactDOM.render(
      <App />, document.getElementById('app')
    )
    

    *构建状态*

    single entry ./src/App.jsx  index
         | ./src/App.jsx 751 bytes [depth 0] [built]
         |     [no exports]
         |     ModuleConcatenation bailout: Module is an entry point
         |     single entry ./src/App.jsx  index
         | ../build/ui-component.module.js 1.43 KiB [depth 1] [built]
         |     [exports: Button, Header]
         |     [only some exports used: Header]
         |     harmony side effect evaluation ui-component  ./src/App.jsx 5:0-38
         |     harmony import specifier ui-component  ./src/App.jsx 19:64-70
    

    *构建输出*

    e.prototype.render=function(){return o.a.createElement("div",null,"Groups")},e}
    

共有2个答案

寿元白
2023-03-14

我在嵌套模块和重新导出(两层重新导出用于更简单的代码维护-更少接触主入口点文件)方面也遇到了这个问题。

Webpack目前不能很好地处理这种情况,但看起来Webpack v5将:https://github.com/webpack/webpack/pull/9203

章远航
2023-03-14

如果您不需要按钮,请像从“ui组件/标题”导入标题一样导入它 或在包中添加“副作用”:false。“ui组件”的json;https://github.com/webpack/webpack/tree/master/examples/side-effects

 类似资料:
  • 问题内容: 这是我在elasticsearch中存储在索引上的数据类型。我必须找到包含主要成分牛肉(且重量小于1000)和成分-(辣椒粉且重量小于250),(橄榄油和重量小于300)以及所有其他成分类似的食谱。 索引的映射是 我的查询是 但这给了Null。有人可以帮我吗?我认为我没有正确使用嵌套查询 问题答案: 试试这个:

  • Since 8.2 vibrate 调用震动 使用方法 AlipayJSBridge.call('vibrate'); 代码演示 基本使用 <h1>点击按钮手机会震动</h1> <a href="#" class="btn read">手机震动</a> <script> function ready(callback) { // 如果jsbridge已经注入则直接调用 if (win

  • 我只从语义ui中导入一个组件 但是在运行之后,我看到了捆绑包中语义UI的所有组件,其大小超过300KB(~30KB,没有语义UI)。我安装了所有最新版本:webpack@4.14.0; 语义用户界面-react@0.81.3 这是我的webpack.config.js 我做错什么了吗?我如何使树摇动工作与语义ui-反应?我不想看到捆绑包中未使用的组件。

  • 我一直试图学习如何编写树摇动友好的代码,但遇到了一个不可避免的副作用的问题,我不知道如何处理。 在我的一个模块中,我访问全局构造函数,并使用它来确定浏览器可以播放哪些音频文件(类似于现代的方式)。每当我尝试树形摇动我的代码时,元素和对它的所有引用都不会被删除,即使我没有在我的文件中导入模块。 我知道包含副作用的代码无法消除,但我找不到的是如何处理不可避免的副作用。我不能不访问全局对象来创建检测功能

  • 问题内容: 我目前正在研究应用程序的聊天功能。并且我在StreamBuilder中设置了AnimatedList,以使消息反向显示。这是我的代码 我的问题是该构建器从未被点击,因此AnimatedList从未被调用。我不确定设置是否正确,因此对此表示感谢。 编辑:我正在尝试使其像FirebaseAnimatedList小部件一样工作。我不知道这是否有助于了解我的目标。 谢谢 问题答案: 更新:我通

  • 根据谷歌的文档: 现在可以在片段中嵌入片段。这对于各种情况都很有用,在这些情况下,您需要将动态和可重用的UI组件放置到本身是动态和可重用的UI组件中。例如,如果使用ViewPager创建左右滑动并占用大部分屏幕空间的片段,现在可以将片段插入每个片段页面。要嵌套片段,只需对要添加片段的片段调用getChildFragmentManager()。这将返回一个FragmentManager,您可以像通常