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

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.

主站蜘蛛池模板: 南溪县| 岱山县| 辽宁省| 临邑县| 博白县| 玉溪市| 安泽县| 岳池县| 会东县| 永靖县| 庆阳市| 富源县| 共和县| 堆龙德庆县| 锡林郭勒盟| 突泉县| 安平县| 长泰县| 武邑县| 股票| 图木舒克市| 元氏县| 岐山县| 呼伦贝尔市| 抚松县| 乌兰县| 休宁县| 乃东县| 承德市| 双桥区| 右玉县| 尤溪县| 阿鲁科尔沁旗| 青神县| 太白县| 容城县| 清流县| 琼海市| 大埔区| 文化| 临漳县|