官术网_书友最值得收藏!

Creating a custom script

Make a new folder in the root of your directory and name it scripts. Inside, make a file called copy_assets.js.

In here, we will copy everything in public to build, excluding our index.html.

To do this (you guessed it), we need one more package; run yarn add fs-extra.

Then, require it inside copy_assets.js, as illustrated:

var fs = require('fs-extra');

fs-extra is a package used for manipulating files in a Node environment. It has a method called copySync, which we'll use here.

The code is rather straightforward:

fs.copySync('public', 'build', {
dereference: true,
filter: file => file !== 'public/index.html'
});

This says copy everything in the public folder to the build folder, except the index.html file.

If you have a bundle.js in your public folder from our previous Webpack config, you can delete it now.

Now, to run this command whenever we build, add it to the build script in package.json:

 "scripts": {
"build": "node scripts/copy_assets.js && node_modules/.bin/webpack --config
webpack.config.prod.js",
"start": "node_modules/.bin/webpack-dev-server"
},

It's a good idea to put the copy_assets command before our Webpack command, just to ensure that we don't accidentally copy any JavaScript assets in public without transpiling them.

主站蜘蛛池模板: 聂拉木县| 资溪县| 肥城市| 罗江县| 喀什市| 乐至县| 江门市| 平泉县| 贡山| 砚山县| 平潭县| 武强县| 名山县| 双牌县| 嘉峪关市| 北流市| 隆化县| 永泰县| 凭祥市| 贵定县| 克东县| 南溪县| 乃东县| 吐鲁番市| 漯河市| 共和县| 旺苍县| 淮阳县| 安丘市| 微山县| 炉霍县| 白朗县| 商水县| 茶陵县| 遂溪县| 德阳市| 永新县| 娄底市| 绥中县| 沙田区| 临澧县|