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 5: raw-loader deprecated, require.context approach no longer working

See original GitHub issue

After the upgrade to builder-webpack5 and 6.3.12 on storybook, I noticed that the approach suggested in the readme is no longer working. Attempting to load all scss files in a folder no longer provides the sourcecode:

const svgIconsReq = require.context('!!raw-loader!../../../assets/icons', true, /\.svg/);
const svgIconTokenFiles = svgIconsReq
  .keys()
  .map(filename => ({ filename, content: svgIconsReq(filename).default }));

...

> svgIconTokenFiles
[... { filename: './add.svg', content: undefined }, { filename: './delete.svg', content: undefined } ...]

It looks like the raw-loader approach is no longer going to be supported moving forward (see: https://webpack.js.org/guides/asset-modules/#replacing-inline-loader-syntax), and using the new method isn’t going to allow inlining without modifications to the existing rules: (see: https://github.com/webpack/webpack/issues/12900). The only files that weren’t dropped from importing were ones that were marked as entrypoints.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14

github_iconTop GitHub Comments

4reactions
Sqrrlcommented, Mar 4, 2022

Hey. Just published v2.0.0-beta.0 with some “experimental” changes. It implements a different way of loading source files and simplifies the addon configuration. I hope it gets rid of the issues with webpack 5.

Would be great if you could check it out.

Migration:

  • Bump addon version to v2.0.0-beta.0
  • Get rid of the token file loading in your .storybook/preview.js

Before:

const tokenContext = require.context(
  '!!raw-loader!../src',
  true,
  /.\.(css|less|scss|svg)$/
);

const tokenFiles = tokenContext.keys().map(function (filename) {
  return { filename: filename, content: tokenContext(filename).default };
});

export const parameters = {
  designToken: {
    defaultTab: 'Colors',
    files: tokenFiles
  }
};

After:

export const parameters = {
  designToken: {
    defaultTab: 'Colors'
  }
};
2reactions
thayna-oliveiracommented, Mar 4, 2022

Hi @Sqrrl, I checked here and it works like a charm!! Thank you, you saved my day!!! ❤️ 😍 ✨

Read more comments on GitHub >

github_iconTop Results From Across the Web

To v5 from v4 - webpack
Webpack 5 requires at least Node.js 10.13.0 (LTS), so make sure you upgrade ... As webpack 5 removes all deprecated features, make sure...
Read more >
raw-loader - webpack - JS.ORG
A loader for webpack that allows importing files as a String. Getting Started. To begin, you'll need to install raw-loader : $ npm...
Read more >
Resolve | webpack
Create aliases to import or require certain modules more easily. ... null-loader is deprecated in webpack 5. use alias: { xyz$: false }...
Read more >
Module Methods - webpack
This section covers all methods available in code compiled with webpack. When using webpack to bundle your application, you can pick from a...
Read more >
Loader Interface - webpack
The this context of the function is filled-in by webpack and the loader ... The this.callback method is more flexible as it allows...
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