Webpack.config how to just copy the index.html to the dist folder

Webpack.config how to just copy the index.html to the dist folder



I am trying to automate assets going into /dist. I have the following config.js:


module.exports =
context: __dirname + "/lib",
entry:
main: [
"./baa.ts"
]
,
output:
path: __dirname + "/dist",
filename: "foo.js"
,
devtool: "source-map",
module:
loaders: [

test: /.ts$/,
loader: 'awesome-typescript-loader'
,
test: /.css$/, loader: "style-loader!css-loader"
]
,
resolve:
// you can now require('file') instead of require('file.js')
extensions: ['', '.js', '.json']




I also want to include main.html from the directory that sits next to /lib, into the /dist folder when running webpack. How can I do this?



UPDATE 1 2017_____________



My favourite way to do this now is to use the html-webpack-plugin with a template file. Thanks to the accepted answer too! The advantage of this way is that the index file will also have the cachbusted js link added out of the box!


html-webpack-plugin




7 Answers
7



Option 1



In your index.js file (i.e. webpack entry) add a require to your index.html via file-loader plugin, e.g.:


index.js


index.html



require('file-loader?name=[name].[ext]!../index.html');


require('file-loader?name=[name].[ext]!../index.html');



Once you build your project with webpack, index.html will be in the output folder.


index.html



Option 2



Use html-webpack-plugin to avoid having an index.html at all. Simply have webpack generate the file for you.






I use the second way and it works, thank you !

– soulmachine
Dec 11 '15 at 2:25






now moved this to the accepted answer

– SuperUberDuper
Jan 18 '16 at 11:41






Seems wrong to have the javascript include a dependency on the HTML file...

– MonkeyWrench
Mar 30 '16 at 20:13






can i somehow load it writing something in config file itself ?

– codeVerine
May 10 '16 at 10:29






In webpack v2 you apparently can't omit the -loader suffix. e.g. require('file-loader?name=[name].[ext]!../index.html');

– overthink
Feb 4 '17 at 18:40


-loader


require('file-loader?name=[name].[ext]!../index.html');



I will add an option to VitalyB's answer:



Option 3



Via npm. If you run your commands via npm, then you could add this setup to your package.json (check out also the webpack.config.js there too). For developing run npm start, no need to copy index.html in this case because the web server will be run from the source files directory, and the bundle.js will be available from the same place (the bundle.js will live in memory only but will available as if it was located together with index.html). For production run npm run build and a dist folder will contain your bundle.js and index.html gets copied with good old cp-command, as you can see below:


npm start


npm run build


"scripts":
"test": "NODE_ENV=test karma start",
"start": "node node_modules/.bin/webpack-dev-server --content-base app",
"build": "NODE_ENV=production node node_modules/.bin/webpack && cp app/index.html dist/index.html"



Update: Option 4



There is a copy-webpack-plugin, as described in this Stackoverflow answer



But generally, except for the very "first" file (like index.html) and larger assets (like large images or video), include the css, html, images and so on directly in your app via require and webpack will include it for you (well, after you set it up correctly with loaders and possibly plugins).


require






wow this is cool

– SuperUberDuper
Jan 22 '16 at 12:52






Option 3 should be Option 1

– Gil Epshtain
Mar 9 '16 at 19:51






I tried option 3, but hot reloading of index.html didn't work. Do you not edit your index.html very often? Serious question.

– stone
Mar 21 '16 at 6:46






Option 4 worked great for me!!!!

– Vaccano
Jul 27 '16 at 18:11






Use ncp instead of of cp if you'd like to support cross-OS dev environments

– Vivek Maharajh
Dec 29 '16 at 4:29



You could use the CopyWebpackPlugin. It's working just like this:


module.exports =
plugins: [
new CopyWebpackPlugin([
from: './*.html'
])
]



I would say the answer is: you can't. (or at least: you shouldn't). This is not what Webpack is supposed to do. Webpack is a bundler, and it should not be used for other tasks (in this case: copying static files is another task). You should use a tool like Grunt or Gulp to do such tasks. It is very common to integrate Webpack as a Grunt task or as a Gulp task. They both have other tasks useful for copying files like you described, for example, grunt-contrib-copy or gulp-copy.



For other assets (not the index.html), you can just bundle them in with Webpack (that is exactly what Webpack is for). For example, var image = require('assets/my_image.png');. But I assume your index.html needs to not be a part of the bundle, and therefore it is not a job for the bundler.


index.html


var image = require('assets/my_image.png');


index.html






I precisely went to webpack so I wouldn't need to use grunt or gulp. Is there any other alternative? If I need to use gulp why should I bother with webpack?

– SuperUberDuper
Aug 22 '15 at 10:49







The question is upside down. Why should you use webpack if you can use grunt or gulp? They are very good task/build systems. Webpack (or browserify or r.js) are tools you can use for bundling lots of JS-files (and other resources) into one big (or multiple) javascript bundles. You should use the correct tool for the job. And again, it is very common to run webpack, browserify or other bundlers from grunt or gulp.

– Brodie Garnet
Aug 22 '15 at 10:55






There are many ways webpack can do that. You could use file-loader which basically just copies the file/image to the output directory and gives you the url when you requires it: var url = require('myFile');. As I said, a bundle can be one or multiple files.

– Brodie Garnet
Aug 22 '15 at 11:08


file-loader


var url = require('myFile');






I might use brocolli as the parent build process

– SuperUberDuper
Aug 22 '15 at 11:18






This is the right answer to me. In large/complex projects, webpack build performance is an important consideration. Various file copy plugins adds unnecessary cost to webpack, letting webpack focus on JS bundling is a better idea.

– Evi Song
Aug 28 '16 at 13:31



You can add the index directly to your entry config and using a file-loader to load it


module.exports =

entry: [
__dirname + "/index.html",
.. other js files here
],

module:
rules: [

test: /.html/,
loader: 'file-loader?name=[name].[ext]',
,
.. other loaders
]








awesome solution! Isn't the html really the entry point.

– vidstige
Dec 30 '17 at 21:48



This work well on Windows:


npm install --save-dev copyfiles


package.json


"copy": "copyfiles -u 1 ./app/index.html ./deploy"



This move my index.html from the app folder into the deploy folder.






I get it works using answer there: stackoverflow.com/questions/38858718/…

– IsraGab
Jul 16 '18 at 22:48



I also found it easy and generic enough to put my index.html file in dist/ directory and add <script src='main.js'></script> to index.html to include my bundled webpack files. main.js seems to be default output name of our bundle if no other specified in webpack's conf file. I guess it's not good and long-term solution, but I hope it can help to understand how webpack works.


index.html


dist/


<script src='main.js'></script>


index.html


main.js




Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



Would you like to answer one of these unanswered questions instead?

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)