Skip to content

Instantly share code, notes, and snippets.

@jw-12138
Last active November 21, 2021 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jw-12138/11bd9da7c3f058fce9450137b43c5fab to your computer and use it in GitHub Desktop.
Save jw-12138/11bd9da7c3f058fce9450137b43c5fab to your computer and use it in GitHub Desktop.
Providing a way to copy static file folders to the distribution folder, works pretty nice for parcel v2 or v1. or whatever other packaging tools you are using.

Installation

First, be sure you have the following content in your package.json, very basic source-to-destination kinda stuff.

{
  "static": {
    "paths": [
      {
        "src": "src/assets",
        "dest": "dist/assets"
      }
    ]
  }
}

Second, you should install these two packages as devDependencies with npm.

npm i chokidar ncp -D

chokidar for watching files and folders, ncp for copying stuffs around.

Usage

You can use it with CLI like:

node copy-static-folder.js

or use it in npm scripts:

package.json

{
  "scripts": {
    "copy-static": "node copy-static-folder.js"
  }
}
npm run copy-static

For one-time build

node copy-static-folder.js build
const chokidar = require('chokidar');
const fs = require('fs');
const path = require('path')
const ncp = require('ncp').ncp
let data = fs.readFileSync(path.join(__dirname, 'package.json'), {
encoding: "utf-8",
flag:'r'
})
let watch = true
let arg = process.argv.slice(2)
if(arg.includes('build')){
watch = false
}
ncp.limit = 999
let canWatch = false
let waitTime = 3
let startTime = Date.now()
let _s = setInterval(() => {
let nowTime = Date.now()
if(nowTime - startTime > waitTime * 1000){
canWatch = true
clearInterval(_s)
}
}, 1)
if (typeof data !== 'object') {
data = JSON.parse(data)
}
let staticEntries = data.static.paths
if(!staticEntries.length){
console.warn('WARN: No static folder specified!')
}
staticEntries.forEach(el => {
let sourceFolder = path.normalize(el.src)
let destinationFolder = path.normalize(el.dest)
doCopy(sourceFolder, destinationFolder)
if(!watch){
return false
}
chokidar.watch(sourceFolder).on('all', (event, path) => {
if(canWatch){
doCopy(sourceFolder, destinationFolder)
}
})
})
function doCopy(sourceFolder, destinationFolder){
if(!fs.existsSync(sourceFolder)){
console.warn('\x1b[31m%s\x1b[0m', `WARN: [${sourceFolder}] is presented, but there no such folder! skipping...`)
return
}
// ⚠ ⚠ ⚠
// this action removes the old static folders in your `dist` everytime when a file/folder change gets fired.
// fs.rmdirSync(destinationFolder, {
// force: true,
// recursive: true
// })
ncp(sourceFolder, destinationFolder, function (err) {
if (err) {
return console.error(err)
}
console.log(`[√] Copied! [${sourceFolder}] => [${destinationFolder}]`)
})
}
{
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "npx parcel build src/index.html && node copy-static-folder.js"
},
"devDependencies": {
"chokidar": "^3.5.2",
"ncp": "^2.0.0",
},
"static": {
"paths": [
{
"src": "src/assets",
"dest": "dist/assets"
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment