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

can't dynamic require images #1668

Closed
BeliefRC opened this issue Jul 5, 2018 · 13 comments
Closed

can't dynamic require images #1668

BeliefRC opened this issue Jul 5, 2018 · 13 comments

Comments

@BeliefRC
Copy link

BeliefRC commented Jul 5, 2018

🐛 bug report

I use parcel to package the react project,but can't dynamic require images

File Directory
image

🎛 Configuration (.babelrc, package.json, cli command)

.babelrc

{
  "presets": [
    "env",
    "react"
  ],
  "plugins": [
    [
      "import",
      {
        "libraryName": "antd",
        "style": true
      }
    ],
    [
      "transform-runtime",
      {
        "polyfill": false,
        "regenerator": true
      }
    ]
  ]
}

package.json

{
  "name": "parcelReactDemo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "parcel ./src/index.html",
    "build": "parcel build ./src/index.html -d build"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "antd": "^3.1.4",
    "react": "^16.2.0",
    "react-dom": "^16.2.0"
  },
  "devDependencies": {
    "babel-plugin-import": "^1.6.3",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "less": "^2.7.3",
    "parcel-bundler": "^1.9.4"
  }
}

🤔 Expected Behavior

Each picture should be properly introduced

😯 Current Behavior

image

💁 Possible Solution

🔦 Context

💻 Code Sample

app.js

import React, { Component } from 'react'

export default class App extends Component {

    render () {
        return (
            <div>
                {[1, 2, 3].map(
                    item => <img key={item} src={require(`./${item}.png`)}
                                 alt=""/>)}
            </div>
        )
    }
}

index.js

import React from 'react'
import ReactDOM from 'react-dom'
import App from './containers/app.js'

ReactDOM.render(<App/>, document.getElementById('root'))

🌍 Your Environment

Software Version(s)
Parcel 1.9.4
Node 8.9.3
npm/Yarn npm 5.5.1
Operating System win10
@DeMoorJasper
Copy link
Member

Probably better to use something like this:

const images = require('./images/*.png');

// images returns an object with all the paths of the images matching the glob with content hashing this also helps with improving your caching
// You can loop through images :)

@BeliefRC
Copy link
Author

BeliefRC commented Jul 5, 2018

@DeMoorJasper
It still doesn't work properly
error message: Uncaught Error: Cannot find module './1.png'

import React, { Component } from 'react'

export default class App extends Component {

    render () {
        return (
            <div>
                {[1, 2, 3].map(
                    item => {
                        const images = require(`./${item}.png`)
                        return <img key={item} src={images}
                                    alt=""/>
                    })}
            </div>
        )
    }
}

@DeMoorJasper
Copy link
Member

DeMoorJasper commented Jul 5, 2018

Well you didn't apply my code snippet at all?

I'll write an example :)

import React, { Component } from 'react';

/*
Images should resolve to the following:
{
  '1': '/1-hash.png',
  '2': '/2-hash.png',
  '3': '/3-hash.png'
}
*/
import images from './*.png';

export default class App extends Component {

    render () {
        return (
            <div>
                {Object.keys(images).map(
                    key => {
                        return <img key={key} src={images[key]}
                                    alt=""/>
                    })}
            </div>
        )
    }
}

@QAQLiuCong
Copy link

@DeMoorJasper Hello, excuse me, if there is a Vue file, is there a way to achieve this? Introduce static resources in the data.

@DeMoorJasper
Copy link
Member

@QAQLiuCong parcel works the same in any asset type, so as long as you can pass a link to the vue component it should work

Sent with GitHawk

@QAQLiuCong
Copy link

@DeMoorJasper OK,Thank you for your reply

@DeMoorJasper
Copy link
Member

Closing this, as I'm pretty sure it's resolved by my response

@mkucaj1986
Copy link

How can I force application to refresh folder to look images like in your example
'''
import images from './*.png';
'''
eg. after I completed upload file to folder ?

@bologer
Copy link

bologer commented Sep 8, 2018

@DeMoorJasper but this solution is very limited, as you are not having the hash. Is it somehow possible to get the full name of the file?

@davidysoards
Copy link

Thanks you @DeMoorJasper. Goddamn I spent like an hour trying to figure this out, and @bologer if you know the file name you can just call images[FILENAME]

brianzelip added a commit to brianzelip/mattanderin.us that referenced this issue Oct 1, 2019
Import paths matching a glob via a hint in a Parcel issue,
parcel-bundler/parcel#1668
@simonwiles
Copy link

simonwiles commented May 3, 2020

Is there any way to make this work with Parcel 2 (currently trying with 2.0.0-alpha.3.2)? I've attempted variations of require('./images/*.png'), require('url:./images/*.png'), require('url:~/src/images/*.png') etc. all to no avail, but I can't see anything that looks like it's related to this in the issues or milestones. Is there a trick I'm missing?

@cmditch
Copy link

cmditch commented Apr 9, 2021

Also curious about what @simonwiles is asking about. It seems the only solution for now is to manually import every asset individually.

@mischnic
Copy link
Member

mischnic commented Apr 9, 2021

#5933 should enable this

doub1ejack added a commit to codeforbtv/expunge-vt that referenced this issue Jun 18, 2021
Problem: The `addDocketCounts` function specifies the name 'payload.js'
as the file that should be executed in the chrome tab when scraping
the page. However, when parcel builds the source files, the dist/ files
all have a unique hash added to them so the file becomes something
like 'payload.89274893.js'. I haven't figured out how to tell parcel
to update this filename though.

Solution: No idea. The problem is simiar to this issue, and
this might be an option, but it feels a little smelly to me:
parcel-bundler/parcel#1668

For now: I'm copying the file over manually so that the file is
available as expected. Seems hacky, but I think it's currently working.
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

9 participants