css-blocks icon indicating copy to clipboard operation
css-blocks copied to clipboard

Error "Stray reference to block import (styles)" for nested React components

Open lyosha-v opened this issue 8 years ago • 4 comments

Hi! I'm facing an issue trying to setup css-blocks in playground project.

Repo structure

I've got two components and one index file in my repo:

  1. src/index.js
  2. src/components/A.js
  3. src/components/B.js

Index file has a dependency to css file src/index.block.css. Component B has a dependency src/components/B.block.css.

     index.js → index.block.css
        ↓
       A.js
        ↓
       B.js
        ↓
    B.block.css

Problem

If you run ./node_modules/.bin/webpack, styles from index.block.css will be in result bundle, but styles from B.block.css will not. Webpack will also show warning.

WARNING: Stray reference to block import (styles). Imports are removed during rewrite so this will probably be a runtime error. (/Users/averkholantcev/dev/css-blocks-example/src/components/B.js:4:27)

Link to repo

https://github.com/verkholantsev/css-blocks-example

lyosha-v avatar May 11 '18 03:05 lyosha-v

Ok. This was because your files were named *.js instead of *.jsx. Which is a current limitation that we'd like to be able to relax at some point.

In the meantime, There will now be a better warning message:

WARNING: For performance reasons, only jsx and tsx files are properly analyzed for block dependencies and rewritten. Consider renaming /Users/ceppstei/Work/support/css-blocks-example/src/components/B.js to /Users/ceppstei/Work/support/css-blocks-example/src/components/B.jsx as well as any leading to importing it from the entry point.
WARNING: Stray reference to block import (styles). Imports are removed during rewrite so this will probably be a runtime error. (/Users/ceppstei/Work/support/css-blocks-example/src/components/B.js:4:27)
WARNING: This usually happens when a style reference is incorrectly used outside a jsx expression. But sometimes when an application is improperly configured. Be sure that only files ending in jsx or tsx are involved in the importing of components using css blocks.

chriseppstein avatar May 15 '18 01:05 chriseppstein

I'm experiencing the same issue.

This is how my build looks like:

WARNING: Stray reference to block import (styles). Imports are removed during rewrite so this will probably be a runtime error. (/Users/mtsferreirasilva/Documents/react-app-styles-implementations/app-using-css-blocks/src/components/Card/Card.jsx:1:185)
WARNING: Stray reference to block import (styles). Imports are removed during rewrite so this will probably be a runtime error. (/Users/mtsferreirasilva/Documents/react-app-styles-implementations/app-using-css-blocks/src/components/Card/Card.jsx:1:229)
WARNING: Stray reference to block import (styles). Imports are removed during rewrite so this will probably be a runtime error. (/Users/mtsferreirasilva/Documents/react-app-styles-implementations/app-using-css-blocks/src/components/Card/Card.jsx:1:287)

And in runtime I get:

ReferenceError: styles is not defined

As you can see I'm using .jsx extension.

mateus avatar Jun 25 '18 02:06 mateus

Well, after some time I find out that even an index.js that exports Card.jsx was also a problem. I don't know if I'm missing something but that seems to be a big issue to me not being able to use .js.

My current bug is

[css-blocks] AnalysisError: First argument passed to "objstr" call must be an object literal. 

But I'm not sure what's the issue on the code:

import React from "react";
import objstr from "obj-str";

import styles from "./Badge.block.css";

export default function Badge({ children, success = false, error = false }) {
  const classNames = objstr({
    [styles]: true,
    [styles.success()]: success,
    [styles.error()]: error
  });

  return <span className={classNames}>{children}</span>;
}

mateus avatar Jun 25 '18 03:06 mateus

Hey !

I get the exact same problem and I have tried renaming all files to .jsx but it didn't help and I still get:

WARNING: Stray reference to block import (styles). Imports are removed during rewrite so this will probably be a runtime error. (/Users/saravieira/Projects/css-blocks/src/Components/Button/index.js:14:23)

Here is the repo if it helps: https://github.com/SaraVieira/css-blocks

Thank you

SaraVieira avatar Jul 25 '18 16:07 SaraVieira