Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scss styles background image parse error #141

Closed
upteran opened this issue Apr 2, 2020 · 16 comments
Closed

scss styles background image parse error #141

upteran opened this issue Apr 2, 2020 · 16 comments

Comments

@upteran
Copy link

upteran commented Apr 2, 2020

Hi, thanks for your work at first

Describe the bug

After upgrading to 2.5.6, next build errors with:

Failed to compile.

./app/components/FileLoader/styles.scss
ModuleParseError: Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)


> Build error occurred
Error: > Build failed because of webpack errors

System information

  • System macOS Catalina 10.15.3
  • node 12.8.0
  • npm 6.10.3
  • next 9.3.3
  • next-optimized-images 2.5.6

Additional context

with next-optimized-images 2.5.5 everything ok, an error occurs in styles files with background images

@zobzn
Copy link

zobzn commented Apr 2, 2020

I confirm error after updating to 2.5.6 from 2.5.5.

system:

  • windows 10
  • node 12.16.1
  • npm 6.13.4
  • next 9.3.4
  • next-compose-plugins 2.2.0
  • @zeit/next-sass 1.0.1
  • next-optimized-images 2.5.6

next.config.js

const withPlugins = require("next-compose-plugins");
const withSass = require("@zeit/next-sass");
const optimizedImages = require("next-optimized-images");

const nextConfig = {
  // distDir: "build",
  webpack: (config, options) => {
    const { isServer } = options;

    // Fixes npm packages that depend on `fs` module
    if (!isServer) {
      config.node = {
        fs: "empty",
      };
    }

    return config;
  },
};

module.exports = withPlugins(
  [
    [
      withSass,
      {
        cssLoaderOptions: {
          localIdentName: "[local]___[hash:base64:5]",
        },
      },
    ],
    [optimizedImages, { optimizeImages: false }],
  ],
  nextConfig
);

part of app.scss

.section {
    background-image: url("./bg-section-01-desktop.svg");
}

command

rm .next/ -rf && next build

error

Failed to compile.

./src/styles/app.scss
ModuleParseError: Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <svg viewBox="0 0 1920 1080" fill="none" preserveAspectRatio="none" xmlns="http:https://www.w3.org/2000/svg">
|     <g clip-path="url(#clip0)">

Downgrade to 2.5.5 fixes problem

@cyrilwanner
Copy link
Owner

I'm very sorry that this version broke your builds. I just published v2.5.7 which should hopefully fix them again. Can you please verify that?

Also, if you are already on nextjs 9.3, you might want to consider switching to the builtin sass solution which got introduced in 9.3 so you wouldn't need the @zeit/next-sass plugin anymore.

@zobzn
Copy link

zobzn commented Apr 2, 2020

With 2.5.7 build went smoothly. Thank you

@zobzn
Copy link

zobzn commented Apr 2, 2020

But I'm not sure that your fix using isModuleInstalled with require.resolve is a bulletproof method.
In theory the packages '@zeit/next-sass' and '@zeit/next-css' might be installed but not used in next.config.js.

@EvHaus
Copy link

EvHaus commented Apr 4, 2020

I'm having the same issue after upgrading to 2.5.6. 2.5.7 is still broken for me. Getting a ton of errors like this:

Error occurred prerendering page "/contact". Read more: https://err.sh/next.js/prerender-error:
Error: Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)

2.5.5 is the last version that works for me.

@zobzn
Copy link

zobzn commented Apr 5, 2020

@EvHaus It would be nice if you could provide more details. What nextjs version do you use? With what packages? Any of '@zeit/next-sass' and '@zeit/next-css' ?. It would be even more useful if you could make a simple minimal repository with reproducible error.

@EvHaus
Copy link

EvHaus commented Apr 7, 2020

@zobzn See below:

  • System: macOS Catalina 10.15.4
  • node: 10.17.0
  • npm: 6.14.4
  • next: 9.3.3
  • next-compose-plugins: 2.2.0
  • next-optimized-images: 2.5.7

My next.config.js:

const optimizedImages = require('next-optimized-images');
const withPlugins = require('next-compose-plugins');

const __DEV__ = process.env.NODE_ENV !== 'production';

module.exports = withPlugins([
	[optimizedImages, {
		handleImages: ['jpeg', 'png'],
		imagesFolder: 'images',
		mozjpeg: {
			quality: 80,
		},
		optimizeImages: !__DEV__,
		optimizeImagesInDev: false,
		optipng: {
			optimizationLevel: 3,
		},
	}],
]);

No custom webpack configuration. Using CSS module pattern via file.module.css for imports.

@zobzn
Copy link

zobzn commented Apr 7, 2020

@EvHaus
I've created the simplest repository with next.js 9.3.4, next-compose-plugins 2.2.0, next-optimized-images 2.5.7 and your next.config.js.
Tried to use images both in index.js and in index.module.css, and didn't have any issues.
Tried to downgrade next.js from 9.3.4 to 9.3.3 - no issues again.

https://github.com/zobzn/next-with-images-test/
https://next-with-images-test.now.sh/ - successfully deployed to now.sh platform.

Maybe you haven't mention something else specific to your case?
Maybe you have '@zeit/next-sass' or '@zeit/next-css' or whatever else installed?
Try to remove .next and node_modules folders and do npm install && next build from scratch.

@EvHaus
Copy link

EvHaus commented Apr 9, 2020

@zobzn Thanks for creating that repo to try and repro the problem. After some investigation I was able to narrow it down to this line in my code:

const resizedImage = require(`../../images/${src}?resize&sizes[]=480&sizes[]=960&sizes[]=1920`);

If I change that require statement to import a specific image (ie. test.png) then it works fine, but if the require statement is dynamic (ie /${src}) then [email protected] throws a bunch of errors.

I created a branch in your repo to reproduce this: https://github.com/zobzn/next-with-images-test/pull/1/files

Once you make this change, next-optimized-images fails with:

Error: Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
    at Object../src/styles/image.jpg (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/index.js:192:7)
    at __webpack_require__ (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/index.js:23:31)
    at webpackContext (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/index.js:166:9)
    at IndexPage.__jsx.map.src (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/index.js:138:71)
    at Array.map (<anonymous>)
    at IndexPage (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/index.js:137:33)
    at processChild (/Users/evhaus/Git/next-with-images-test/node_modules/react-dom/cjs/react-dom-server.node.development.js:3043:14)
    at resolve (/Users/evhaus/Git/next-with-images-test/node_modules/react-dom/cjs/react-dom-server.node.development.js:2960:5)
    at ReactDOMServerRenderer.render (/Users/evhaus/Git/next-with-images-test/node_modules/react-dom/cjs/react-dom-server.node.development.js:3435:22)
    at ReactDOMServerRenderer.read (/Users/evhaus/Git/next-with-images-test/node_modules/react-dom/cjs/react-dom-server.node.development.js:3373:29)
    at renderToString (/Users/evhaus/Git/next-with-images-test/node_modules/react-dom/cjs/react-dom-server.node.development.js:3988:27)
    at render (/Users/evhaus/Git/next-with-images-test/node_modules/next/dist/next-server/server/render.js:83:16)
    at Object.renderPage (/Users/evhaus/Git/next-with-images-test/node_modules/next/dist/next-server/server/render.js:406:16)
    at Function.getInitialProps (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/_document.js:506:19)
    at Object.loadGetInitialProps (/Users/evhaus/Git/next-with-images-test/node_modules/next/dist/next-server/lib/utils.js:59:29)
    at Object.renderToHTML (/Users/evhaus/Git/next-with-images-test/node_modules/next/dist/next-server/server/render.js:410:36)
Error: Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
    at Object../src/styles/image.jpg (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/index.js:192:7)
    at __webpack_require__ (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/index.js:23:31)
    at webpackContext (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/index.js:166:9)
    at IndexPage.__jsx.map.src (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/index.js:138:71)
    at Array.map (<anonymous>)
    at IndexPage (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/index.js:137:33)
    at processChild (/Users/evhaus/Git/next-with-images-test/node_modules/react-dom/cjs/react-dom-server.node.development.js:3043:14)
    at resolve (/Users/evhaus/Git/next-with-images-test/node_modules/react-dom/cjs/react-dom-server.node.development.js:2960:5)
    at ReactDOMServerRenderer.render (/Users/evhaus/Git/next-with-images-test/node_modules/react-dom/cjs/react-dom-server.node.development.js:3435:22)
    at ReactDOMServerRenderer.read (/Users/evhaus/Git/next-with-images-test/node_modules/react-dom/cjs/react-dom-server.node.development.js:3373:29)
    at renderToString (/Users/evhaus/Git/next-with-images-test/node_modules/react-dom/cjs/react-dom-server.node.development.js:3988:27)
    at render (/Users/evhaus/Git/next-with-images-test/node_modules/next/dist/next-server/server/render.js:83:16)
    at Object.renderPage (/Users/evhaus/Git/next-with-images-test/node_modules/next/dist/next-server/server/render.js:406:16)
    at Function.getInitialProps (/Users/evhaus/Git/next-with-images-test/.next/server/static/development/pages/_document.js:506:19)
    at Object.loadGetInitialProps (/Users/evhaus/Git/next-with-images-test/node_modules/next/dist/next-server/lib/utils.js:59:29)
    at Object.renderToHTML (/Users/evhaus/Git/next-with-images-test/node_modules/next/dist/next-server/server/render.js:410:36)

This only happens in version 2.5.6 and higher. 2.5.5 works fine.

@zobzn
Copy link

zobzn commented Apr 9, 2020

Yes, an interesting situation.
I spent quite some time trying different combinations, but none of them worked completely.
I asked a question in the next.js. repository
vercel/next.js#11164 (comment)

@ghost
Copy link

ghost commented Apr 10, 2020

I found the same error ,I use styled-jsx with styled-jsx-plugin-sass.

@cyrilwanner
Copy link
Owner

cyrilwanner commented Apr 11, 2020

Thank you all for helping to find the cause, that means and helps a lot!

I think that I was now able to overwrite the builtin image handling (only images, not the whole css/sass feature) introduced in nextjs 9.2 which should hopefully solve all those errors and the image optimization should be the same again as in 2.5.5, even when using the builtin css/sass feature.

But to avoid accidentally breaking even more, I published a canary version first.
It would be very helpful if you can test the 2.5.8-canary.1 version in your projects and tell me if everything works again the same way as it did in 2.5.5 (images in css/sass, in js, dynamic imports, etc). If that is the case and we are sure it doesn't break anything else, I will publish this version with the latest tag.

Thanks again for your help 🙌

@EvHaus
Copy link

EvHaus commented Apr 12, 2020

Nice @cyrilwanner. 2.5.8-canary.1 solved my issue.

@xeniajensen
Copy link

Hi. I know this issue is closed, but I'm experiencing the exact same thing the previous guys are mentioning. If I use a dynamic require statement, the loader attempts to apply sizes to all of the SVG's in my SCSS files.

const img = require(`../public/images/${imgName}?resize&sizes[]=210&sizes[]=420&sizes[]=700&sizes[]=1200&sizes[]=2000`)

I get these errors in my console:

./public/images/icon-lock--black.svg?resize&sizes[]=210&sizes[]=420&sizes[]=700&sizes[]=1200&sizes[]=2000
Error: No mime type for file with extension svgsupported

I'm using version 2.6.0.

@cyrilwanner
Copy link
Owner

cyrilwanner commented May 4, 2020

Hi @xeniajensen
This is a slightly different issue and has to do with the dynamic require.

When using a dynamic require, webpack applies the loaders on all files that could match this require. Because at build time, it does not know which images will be used later. So when you use ../public/images/${imgName}, it applies the loaders to all images within that folder, regardless of their extension since webpack does not know which extensions the imgName variable can have.

I think there are two solutions:

  • If all the images in imgName have the same extension, you could move the extension out of the variable: require(`../public/images/${imgName}.jpg?...`)
  • You can create multiple directories and move the images into subdirectories. E.g. move all svg files into a icons directory, background images into backgrounds, thumbnails, etc...
    This also has the advantage, that your query will only get applied on the images you need it to be applied and not just on all you have (regardless if they are really used in this size or not).

You can read more about it here: https://webpack.js.org/guides/dependency-management/#require-with-expression

@xeniajensen
Copy link

Hi @cyrilwanner
That makes a lot of sense! Thanks for taking the time to give a thorough explanation. I went with option number 1 and my console warnings are gone.
Thanks - and apologies for re-opening the ticket when it wasn't actually related after all :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants