Вложенные деревьями вложенные модули с веб-пакетом 4 - PullRequest
0 голосов
/ 03 сентября 2018

Не могу понять, почему тряска деревьев не работает так, как я ожидал ... Моя цель - создать библиотеку tree-shak-able.

  • index.js
    • Заголовок
    • Button => ButtonGroups

У меня есть веб-приложение, которое использует эту библиотеку. Когда я импортирую только компонент Header, модуль Button удаляется, как и ожидалось, но мой пакет webpack содержит компонент ButtonGroup.

Может кто-нибудь объяснить, почему? Как я могу потрясти вложенные компоненты из сборки webpack - если это возможно?

Спасибо

Конфигурация свертки и lib

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 - простой компонент реакции

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.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 голосов
/ 08 июня 2019

У меня также была эта проблема с вложенными модулями и реэкспортом (два уровня реэкспорта для упрощения обслуживания кода - меньше касания файлов основной точки входа).

Webpack не обрабатывает эту ситуацию (ну) как сейчас, но похоже, что Webpack v5 будет: https://github.com/webpack/webpack/pull/9203

0 голосов
/ 03 сентября 2018

Если вы не хотите кнопку, пожалуйста, импортируйте ее как import Header from 'ui-component/Header'; или добавить "sideEffects": false в package.json "ui-component"; https://github.com/webpack/webpack/tree/master/examples/side-effects

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...