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

React Component and static resources (images) #195

Closed
vpezeshkian opened this issue Nov 30, 2016 · 6 comments
Closed

React Component and static resources (images) #195

vpezeshkian opened this issue Nov 30, 2016 · 6 comments

Comments

@vpezeshkian
Copy link

Hi,

Using nwb I'm creating react components and consume them in other projects, so far it's been smooth. However static resources causing the other projects to break.

I have configured nwb config to load images from src/images, and this works fine since webpack is used.

nwb.config.js
var path = require('path');
aliases: {
      'img': path.resolve('src/images')
    }
}

myComponent.js
import myImage from 'img/ic_minimize_question.png'
console.log(myImage) // /ic_add_question_mark.751ec2c3.png
// image is loading just fine

When I build and try to consume the component in other project which is using gulp, I receive Error: Cannot find module 'img/ic_add_question_mark.png'.

How do I handle this as I'm not very good at webpack? I also notice that src/images is not being copied to neither lib nor es...

Thanks.

@insin
Copy link
Owner

insin commented Dec 1, 2016

Webpack isn't used when building a component to lib/ and es/ for publishing, we just transpile from src/ using Babel.

I would suggest using a top-level directory for static resources, adding it to package.json files config so it gets published and using relative paths from src/ to it so the same relative paths work from lib/ and es/. Doing it this way will also require users of your component to use webpack and have appropriate file-loader configuration in place.

The best way to do this is still an open question, this is just what's worked for me when publishing components which needed to import static resources, which I've done very little of.

@vpezeshkian
Copy link
Author

IMO delivered components must be in complete form and users must not do any extra work to use them, nor they have to have "appropriate" environment to use them.

I was thinking to copy statics to es and lib... I'd like to be able to use Copy Webpack Plugin but it seems like it's being discarded during build process...

extra: {
      devtool: 'source-map',
      plugins: [
        new CopyWebpackPlugin([
          { from: 'src/images', to: 'es/images' },
          { from: 'src/images', to: 'lib/images' }
        ])
      ]
    }

can you help me set up this plugin and use it in build ?

Thanks.

@vpezeshkian
Copy link
Author

After copying static images to es and lib dist folders, I am receiving new error from gulp and/or browserify.

events.js:154
      throw er; // Unhandled 'error' event
      ^
SyntaxError: Unexpected character '�'

It seems like webpack and browserify really can't be used in conjunction.

@insin insin closed this as completed Jun 1, 2017
@chadoh
Copy link

chadoh commented Oct 13, 2017

Why was this closed? It still seems to be an issue. @insin

chadoh added a commit to FormulaMonks/thicket that referenced this issue Oct 13, 2017
The images are not currently included in the build, or, possibly, are
merely not found by thicket-webapp when it uses NavLinks.

I'm not currently certain the best way to get around this issue. Trying
to follow along with some suggestions here, but so far not working:

insin/nwb#195
@zomars
Copy link

zomars commented Aug 16, 2018

@insin

Webpack isn't used when building a component to lib/ and es/ for publishing, we just transpile from src/ using Babel.

Is there a way to let the build process copy the assets to the the lib folder, and let the parent (which uses it's own file-loader) handle it?

@zomars
Copy link

zomars commented Aug 16, 2018

Nevemind, I've just found out about the --copy-files flag. I'm such an idiot.

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

4 participants