jlengstorf / learn-rollup

This is an example project to accompany a tutorial on using Rollup.
https://code.lengstorf.com/learn-rollup-js/
ISC License
191 stars 61 forks source link

How to require asset file? #15

Closed gaterking closed 7 years ago

gaterking commented 7 years ago

Using webpack, we can use file-loader to require a file(json/png/jpg) in js file, then webpack will publish the file to dist. let zhCNJson = require('file?name=i18n/zh-CN.[hash:8].[ext]!../../i18n/zh-CN.json'); Can rollup do it?

jlengstorf commented 7 years ago

I haven't tried this specific use of Rollup, so this is probably a better question for the official Rollup repo. I don't think it's supported, though.

I've worked around this using npm scripts and a package like copyfiles:

  "scripts": {
    "build": "npm run copy && npm run bundle",
    "bundle": "NODE_ENV=production rollup -c",
    "copy": "copyfiles -f static/**/* dist"
  },

That's a simplified excerpt from the package.json of one of my projects. It basically copies the contents of the static dir to the dist dir.

So the short answer (I think) is, "No."

But the longer answer is, "That's not really what Rollup is for, and it's really easy to get that functionality without putting it into Rollup."

gaterking commented 7 years ago

@jlengstorf With webpack, we packs any anything from one entry. Maybe Rollup is another pack method. Somethimes we will organization our code as separate components. Every component has it own css、js、html and resource file. The component will be used by many another page html or js. But they not need to pack component's every thing manually. Do you think this is the best usage for this feature?

jlengstorf commented 7 years ago

I think an advanced build like that probably merits webpack 2. They're using Rollup under the hood, so you get the benefits of smaller bundles plus the extra stuff you're looking for.

Good luck!

gaterking commented 7 years ago

OK, Thanks.