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.

Webpack 4 HMR not rebuilding node_modules

See original GitHub issue

If you are reporting a bug or requesting support, start here:

Bug or support request summary

I just upgraded @storybook/react to 4.1.11 and webpack to 4.29.0

Using start-storybook with custom webpack config.

When modifying a file in node_modules/, webpack should rebuild the files so the changes should be seen thanks to HMR.

Problem : Webpack isn’t even rebuilding.

Steps to reproduce

With a custom webpack config, start-storybook and modify a used node_modules/ file.

Please specify which version of Storybook and optionally any affected addons that you’re running

Screenshots / Screencast / Code Snippets (Optional)

The custom Webpack config

export default (storybookBaseConfig, configType, defaultConfig) => {
  const __DEV__ = configType === 'DEVELOPMENT'

  storybookBaseConfig.resolve.modules = [
    paths.fromSrc(),
    ...(__DEV__ ? [paths.fromBase('node_modules')] : []), // Necessary for linked modules
    'node_modules'
  ]
  storybookBaseConfig.resolve.extensions = webpackConfigClient.resolve.extensions
  storybookBaseConfig.resolve.alias = {
    ...storybookBaseConfig.resolve.alias,
    ...webpackConfigClient.resolve.alias
  }
  storybookBaseConfig.resolve.symlinks = !__DEV__ // for linked modules (no-babel) https://github.com/webpack/webpack/issues/1866#issuecomment-284571531
  storybookBaseConfig.plugins.push(
    new webpack.DefinePlugin({
      ...config.clientGlobals,
      __PROJECT_VERSION__: JSON.stringify(packageJson.version)
    })
  )
  // JS / JSX
  storybookBaseConfig.module.rules.push({
    test: /\.(js|jsx)$/,
    include: [
      paths.fromSrc(),
      paths.fromConfig(),
      /node_modules[\\/]aw-.+/,
      /node_modules[\\/]wa-.+/,
    ],
    use: [{
      loader: 'babel-loader',
      options: BABEL_CONFIG_BROWSER
    }]
  })

  // STYLES
  const sassLoader = {
    loader: 'sass-loader',
    options: {
      includePaths: [
        paths.fromBase('node_modules', 'aw-ui-styles', 'styles')
      ]
    }
  }

  // Add any packge names here whose styles need to be treated as CSS modules.
  // These paths will be combined into a single regex.
  const PATHS_TO_TREAT_AS_CSS_MODULES = [
    'aw-ui',
    'aw-ui-styles'
  ]

  // If config has CSS modules enabled, treat this project's styles as CSS modules.
  if (config.compiler_css_modules) {
    PATHS_TO_TREAT_AS_CSS_MODULES.push(paths.fromBase(PATHS_CONFIG.DIR_CLIENT))
  }

  const isUsingCSSModules = !!PATHS_TO_TREAT_AS_CSS_MODULES.length
  const cssModulesRegex = new RegExp(
    `(${PATHS_TO_TREAT_AS_CSS_MODULES
      .map((item) => escapeRegexString(item + path.sep))
      .join('|')})`
  )

  // Loaders for styles that need to be treated as CSS modules.
  if (isUsingCSSModules) {
    const cssModulesLoader = {
      loader: 'css-loader',
      options: {
        modules: true,
        importLoaders: 1,
        localIdentName: '[name]__[local]___[hash:base64:5]'
      }
    }

    storybookBaseConfig.module.rules.push({
      test: /\.scss$/,
      include: cssModulesRegex,
      use: [
        'style-loader',
        cssModulesLoader,
        sassLoader
      ],
    })

    storybookBaseConfig.module.rules.push({
      test: /\.css$/,
      include: cssModulesRegex,
      use: [
        'style-loader',
        cssModulesLoader,
      ],
    })
  }

  // Loaders for files that should not be treated as CSS modules.
  const excludeCSSModules = isUsingCSSModules
    ? cssModulesRegex
    : []

  storybookBaseConfig.module.rules.push({
    test: /\.scss$/,
    exclude: excludeCSSModules,
    use: [
      'style-loader',
      'css-loader',
      sassLoader,
    ],
  })

  storybookBaseConfig.module.rules.push({
    test: /\.css$/,
    exclude: excludeCSSModules,
    use: [
      'style-loader',
      'css-loader',
    ],
  })

  // ------------------------------------
  // Fonts
  // ------------------------------------
  ;[
    ['woff', 'application/font-woff'],
    ['woff2', 'application/font-woff2'],
    ['otf', 'font/opentype'],
    ['ttf', 'application/octet-stream'],
    ['eot', 'application/vnd.ms-fontobject'],
  ].forEach((font) => {
    const extension = font[0]
    const mimetype = font[1]

    storybookBaseConfig.module.rules.push({
      test: new RegExp(`\\.${extension}$`),
      use: {
        loader: 'url-loader',
        options: {
          name: 'fonts/[name].[ext]',
          limit: 10000,
          mimetype
        }
      }
    })
  })

  // ------------------------------------
  // SVG Sprite loader
  // ------------------------------------
  storybookBaseConfig.module.rules.push({
    test: /\.svg/,
    use: {
      loader: 'svg-sprite-loader'
    }
  })

  return storybookBaseConfig
}

The webpackConfigClient is the webpack config used to compile the client application. I can paste it here if you think it is relevant.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
petermikitshcommented, Mar 24, 2020

It appears #6265 got reverted in https://github.com/storybookjs/storybook/pull/8657, and the previous workarounds suggested (https://github.com/storybookjs/storybook/issues/5572#issuecomment-465506042, https://github.com/storybookjs/storybook/issues/5572#issuecomment-465441095) no longer work on 6.0.0-alpha.28.

This is an issue for users with monorepo (lerna) setups who have their components in one subpackage, and storybook in another. You want to reload the components in the other package for a quality development experience.

5reactions
elliosevencommented, Feb 20, 2019

@rgranger @mrmckeb

I managed to get HMR working by implementing the following into my webpack configuration:

module.exports = {
  watchOptions: {
    ignored: [
      /node_modules([\\]+|\/)+(?!my-package-name)/
    ]
  }
}

This essentially ignores everything except anything inside my-package-name.

That said, this did use to work out of the box.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack 4 HMR not rebuilding node_modules #5572 - GitHub
This is an issue for users with monorepo (lerna) setups who have their components in one subpackage, and storybook in another. You want...
Read more >
Webpack-dev-server does not rebuild bundle - Stack Overflow
Run it: node server-hmr.js. In console: -[HMR] Waiting for update signal from WDS... -[WDS] Hot Module Replacement enabled. I change some file, ...
Read more >
Hot Module Replacement - webpack
Angular HMR: No loader necessary! A small change to your main NgModule file is all that's required to have full control over the...
Read more >
Webpack's HMR & React-Hot-Loader — The Missing Manual
Further, Webpack itself is very flexible and provides various ways to enable HMR and some of them may not be good for YOUR...
Read more >
webpack-dev-middleware - npm
An express-style development middleware for use with webpack bundles and allows for ... We do not recommend installing this module globally.
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