question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

revert back to webpack 3 for 1.0

See original GitHub issue

Current Behavior

For our plugins, presets, and their respective configurations, the webpack 4 upgrade causes a lot of problems. Some plugins refer to things outside of the @webpack, error messages are generally less readable/actionable, etc.

Expected Behavior

Our existing presets, plugin, and configuration should only require minor tweaks and the impact on bundle size should not be negative.

Haul Configuration (webpack.haul.js)

import DefinePlugin from 'webpack/lib/DefinePlugin';
import NormalModuleReplacementPlugin from 'webpack/lib/NormalModuleReplacementPlugin';
import buildConfigChain from 'babel-core/lib/transformation/file/options/build-config-chain';
import load from 'babel-load-config';
import { resolve } from 'path';

function replaceReactNativePreset({ dev, platform }, dir = process.cwd()) {
  const { options: { presets, ...options } } = load(dir, buildConfigChain);
  const platforms = ['windows'];
  const providesModules = ['react-native-windows', 'react-native'];
  return {
    ...options,
    plugins: [
      require.resolve('haul/src/utils/fixRequireIssues'),
      ...(dev
        ? [
          require.resolve('react-hot-loader/babel'),
          require.resolve('haul/src/hot/babelPlugin')
        ]
        : []),
      ...(Array.isArray(options.plugins) ? options.plugins : [])
    ],
    presets: presets.map(preset => {
      if (
        !(
          preset === 'react-native' ||
          (Array.isArray(preset) && preset[0] === 'react-native')
        )
      ) { return preset; }
      return ['@rdy/babel-preset-react-native-windows', { platform, platforms, providesModules, treeShaking: true }];
    })
  };
}

function replaceBabelLoader({ dev }, rules, babelOptions) {
  const babelLoader = require.resolve('babel-loader');
  const r = rules.map(rule => {
    if (
      !Array.isArray(rule.use) ||
      !rule.use.some(use => use.loader === babelLoader)
    ) { return rule; }
    return {
      test: /\.js$/,
      exclude: /node_modules\/(?!react|@expo|pretty-format|haul)/,
      use: [
        {
          loader: require.resolve('thread-loader')
        },
        {
          loader: require.resolve('babel-loader'),
          options: {
            babelrc: false,
            cacheDirectory: dev,
            ...babelOptions
          }
        }
      ]
    };
  });
  return r;
}

function nodeModules(m) {
  return resolve(__dirname, 'node_modules', m);
}

module.exports = (options, defaults) => {
  const { dev } = options;
  const babelOptions = replaceReactNativePreset(options);
  const plugins = [
    new NormalModuleReplacementPlugin(
      new RegExp(
        require.resolve(
          'react-native/Libraries/react-native/react-native-implementation'
        )
      ),
      require.resolve(
        './src/vendor/react-native/Libraries/react-native/react-native-implementation.js'
      )
    ),
    new NormalModuleReplacementPlugin(
      new RegExp(require.resolve('react-native/Libraries/Network/fetch')),
      require.resolve('./src/vendor/react-native/Libraries/Network/fetch.js')
    ),
    new DefinePlugin({
      __BUNDLE_START_TIME__: JSON.stringify(1)
    })
  ];

  return {
    ...defaults,
    entry: './index.windows.js',
    module: {
      ...defaults.module,
      rules: [
        ...replaceBabelLoader(options, defaults.module.rules, babelOptions),
        {
          test: /\.js$/,
          include: [
            ...['@rdy', '@react-native-windows'].map(nodeModules)
          ],
          use: {
            loader: 'babel-loader',
            options: {
              babelrc: false,
              cacheDirectory: dev,
              ...babelOptions
            }
          }
        },
        {
          test: /\.js$/,
          include: [...['@react-native-windows/js-modules-navigation-redux', '@react-native-windows/js-modules-navigation', '@react-native-windows/native-modules-database', 'react-native', 'react-native-windows', '@react-native-windows'].map(nodeModules)],
          use: {
            loader: 'babel-loader',
            options: {
              babelrc: false,
              cacheDirectory: dev,
              ...babelOptions,
              plugins: [
                ...babelOptions.plugins,
                [
                  'transform-es2015-modules-commonjs',
                  { strict: false, allowTopLevelThis: true }
                ]
              ]
            }
          }
        }
      ]
    },
    plugins: [...defaults.plugins, ...plugins]
  };
};

Your Environment

We are aggressively pursuing smaller bundle sizes by employing tree-shaking via haul using webpack plugins and rewriters. We need a slightly broader ecosystem of webpack plugins to be functional than 4.0 currently allows. While we think webpack4 will provide even better bundle size performance in the future, it seems to not be ready for production (in this React Native/haul context) just yet.

software version
Haul master
react-native 0.49
node 8.9.4
npm or yarn npm 5.6.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thymikeecommented, May 4, 2018

We’re eventually going to use webpack 4, so I’d like to keep it for RC. We still have beta channel with webpack 3

0reactions
thymikeecommented, May 9, 2018

@FredyC if there’s a critical fix we can always cherry-pick to beta channel

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to downgrade version of Webpack? - Stack Overflow
Try npm uninstall webpack npm install webpack@2.1.0-beta.25. or npm uninstall webpack --save npm install webpack@2.1.0-beta.25 --save.
Read more >
To v2 or v3 from v1 - webpack
import() creates a separate chunk for each possible module. function route(path, query) { return import ...
Read more >
webpack - npm
Start using webpack in your project by running `npm i webpack`. There are 26632 other projects in the npm registry using webpack.
Read more >
How I solved and debugged my Webpack issue through trial ...
I knew that Webpack was not easy to configure: there are many parts ... Component { render() { return ( <div> <Hello hello={'Hello,...
Read more >
Changelog - Cypress Documentation
This change aligns Cypress' cookie rules with the browser cookie rules. This may affect what cookies are returned by cy.getCookie() , what cookies...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found