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

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.

主站蜘蛛池模板: 恭城| 遂昌县| 抚顺县| 普安县| 古丈县| 绥宁县| 文成县| 任丘市| 鲁甸县| 灵台县| 锦屏县| 南京市| 塔城市| 凤冈县| 江永县| 赣榆县| 房产| 辉县市| 清水河县| 曲水县| 游戏| 莱芜市| 腾冲县| 灯塔市| 平塘县| 泽普县| 寻甸| 绥滨县| 万州区| 塘沽区| 奉贤区| 秦皇岛市| 江安县| 易门县| 聂拉木县| 吉水县| 平江县| 江油市| 葵青区| 噶尔县| 平泉县|