带有webpack 4的树状嵌套模块

时间:2018-09-03 13:23:27

标签: javascript reactjs webpack rollupjs tree-shaking

无法找出为什么摇树没有按我预期的那样... 我的目标是建立一个tree-shak-able库。

  • index.js
    • 标题
    • Button => ButtonGroups

我有一个使用此lib的webpack应用程序。当我仅导入Header组件时,Button模块将按预期方式删除,但是我的webpack包中包含ButtonGroup组件。

有人可以解释为什么吗? 如何在webpack构建中摇晃嵌套的组件-如果可能的话?

谢谢

卷和库配置

rollup.config.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']
};

index.js

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

Header.jsx-简单的React组件

import React, { Component } from 'react';

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

Button.jsx-反应组件

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 } 

我的Webpack配置和构建:

webpack.config

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/')))
    }
  }
};

* App.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 个答案:

答案 0 :(得分:2)

如果您不想要按钮,请像导入一样 import Header from 'ui-component/Header'; 或添加 "sideEffects": false 在“ ui-component”的package.json中; https://github.com/webpack/webpack/tree/master/examples/side-effects

答案 1 :(得分:0)

嵌套模块和重新导出(两层重新导出以简化代码维护-减少对主要入口点文件的访问)也遇到了这个问题。

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