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.

Simple Example with raw-loader

See original GitHub issue

Can you please provide the most simple example to use raw-loader for .frag files using craco ?

Thanks

here is my non working test:

/* craco.config.js */
module.exports = {
  webpack: {
    alias: {},
    plugins: [],
    mode: 'extends',
    configure: { 
      module: {          
        rules: [
          {
            test: /\.frag$/,
            use: 'raw-loader'
          }
        ]
      }, 
    }
  },
}

Issue Analytics

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

github_iconTop GitHub Comments

7reactions
melMasscommented, Jan 29, 2019

It was easier than expected!!

Raw Loader for Craco:

/* craco-plugin-raw-loader.js */

const { loaderByName, addBeforeLoader } = require('@craco/craco')

module.exports = {
  overrideWebpackConfig: ({
    webpackConfig, cracoConfig, pluginOptions, context: { env, paths } 
  }) => {

    const rawLoader = {
      test: pluginOptions.test,
      use: [ 'raw-loader' ]
    }

    addBeforeLoader(webpackConfig, loaderByName('file-loader'), rawLoader)

    return webpackConfig
  }
}

Use example

here to detect .frag files:

/* craco.config.js */

module.exports = {
  webpack: {
    plugins: [
     { plugin: rawWebpackConfigPlugin, options: { test: /\.frag$/ } }
  ]
}
7reactions
patricklafrancecommented, Jan 29, 2019

Hi @melMass

The problem is that your rule is added after the file-loader which is CRA fallback for every resources that are not handled by any other specific loader.

Here’s a comment from CRA webpack config:

// ** STOP ** Are you adding a new loader?
// Make sure to add the new loader(s) before the "file" loader.

The solution is to add your loader before the file-loader.

You can do this 2 ways with the help of craco utility function addBeforeLoader:

1- By using the function override of configure:

module.exports = {
  webpack: {
    configure: function(webpackConfig) {
         const fragLoader = {
            test: /\.frag$/,
            use: ['raw-loader']
         };

         addBeforeLoader(webpackConfig, loaderByName("file-loader"), fragLoader );

         return webpackConfig;
    }
  },
}

2- Or by creating a craco plugin for raw-loader (which I think is the best way 😃). Something like craco-plugin-add-raw-loader with plugin options:

{
    test: "regex of file to handle"
}

How to a craco develop-a-plugin

Hope it helps!

Patrick

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
raw-loader examples - CodeSandbox
Learn how to use raw-loader by viewing and forking example apps that make use of raw-loader on CodeSandbox. ... p5.js-web-editorThe web editor for...
Read more >
webpack-contrib/raw-loader - GitHub
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 >
raw-loader - webpack
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 >
raw-loader - npm
A loader for webpack that allows importing files as a String. Latest version: 4.0.2, last published: 2 years ago. Start using raw-loader in ......
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