Simple tool to upload files to cdn
node >= 8
npm install upload2cdn
This tool does not provide a service as uploading to cdn.
In fact, it actually depends on such service.
upload2cdn
relies on the existence a cdn
object with an upload
method described as below.
type cdnUrl = string
interface cdnRes {
[localPath: string]: cdnUrl
}
// this is what cdn package looks like
interface cdn {
upload: (localPaths: string[]) => Promise<cdnRes>
}
If typescript syntax is unfamiliar, here is another description in vanilla javascript.
/**
* @param {string[]} localPath: list of paths of local files
* @return Promise<cdnRes>: resolved Promise with structure like {localPath: cdnUrl}
*/
function upload(localPath) {
// code
}
const cdn = {
upload
}
const { upload } = require('upload2cdn')
const cdn = require('some-cdn-package')
upload(cdn, {
src: path.resolve('./src')
dist: path.resolve('./dist')
assets: path.resolve('./src')
})
src
,dist
,assets
work best with absolute path!
upload(cdn, option)
For option
, valid fields are showed below
Where your template files would be (with reference to local js/css files)
Where all assets (js/css/images) are, most likely the same as src property.
Where to emit newer template with cdn reference.
Only use this when there is a need to separate origin templates with ones using cdn reference.
Happens when original templates are static, aka not produced by any building tools.
Further alter cdn url here.
const urlCb = input => input.replace(/^https/, 'http')
Extra config to pass to cdn.upload
method. Something Like cdn.upload(location, passToCdn)
.
const cdn = require("any-cdn-package");
// If the cdn package can set up https through a config object
const passToCdn = {
https: true
}
cdn.upload(files, passToCdn);
// then you can use passToCdn object to turn on https just like this
const { upload } = require("upload2cdn");
upload(cdn, {
passToCdn
});
Using cache to speed up, aka skip some uploading work.
Place to put cache file.
Use this only when you want to manage the cache file by VCS, which is unlikely.
Called when things are done.
Or you can simply await
for upload
, and then do your own thing.
Compression can be done here. Two arguments are fileContent and fileLocation (with extension name of course). You need to return the compression result as string.
// if you want to compress js before upload
const UglifyJs = require('uglify-js')
const path = require('path')
const beforeUpload = (content, location) => {
if (path.extname(location) === '.js') {
return UglifyJs.minify(content).code
}
return content
}
Uploading files is not done by once. By using sliceLimit
, you can limit the number of files being uploaded at once.
When using, it basically means overriding assets
field, and only use the files you provide as assets.
Should be an array of absolute locations.
Give you the chance to update files' content before any further process.
Runs at the very beginning (including all valid files in src
and assets
), prior to any upload related process.
Extra types needed to cdn. Like json
(Do not prefix with .
)
Should replace the matching local path with the cdn url.
slice
is a 20 characters + matched path + 20 characters
If there are extraTypes
, then will try to upload them twice.
The meaning of the second time is to handle the potential dependency among them.
And shapeExtra
gives you a chance to change the order of those extra files.
Probably do not need to use this.
Copyright (c) 2017-present, Yuchen Liu