
An easy way to prepend your CDN urls to your HTML and CSS files. It can be integrated into any existing workflow, and customized to meet the needs of your project. Use the CLI tool for quick use or use the API to implement programmatically.
What is this?
cdnex will take an input of either a file path or a string, prepend a given url to all urls in the source that are not pointing to an external source (ex. anything that starts with http://, https://, or //), and will output either a new file (if a file was supplied as the source), or the processed string. Take the following HTML as an example:
<html>
<head>
<link rel="stylesheet" type="text/css" href="/app.css">
</head>
<body>
<img src="/img/logo.png" class="logo">
<script src="https://ajax.googleapis.com/.../jquery.min.js"></script>
<script src="/app.js"></script>
</body>
</html>
If the above file is specified as the input with a CDN url of https://cdn.mysite.com, the output would look like this:
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdn.mysite.com/app.css">
</head>
<body>
<img src="https://cdn.mysite.com/img/logo.png" class="logo">
<script src="https://ajax.googleapis.com/.../jquery.min.js"></script>
<script src="https://cdn.mysite.com/app.js"></script>
</body>
</html>
cdnex is built handle HTML and CSS files by default, but it can be used with any kind of input string.
Install
npm install cdnex -g
npm install cdnex --save
Usage with CLI
If installed globally, cdnex will be in your system path.
$ cdnex src/index.html --output dist/index.html --url https://mycdn.com
$ cdnex src -o dist -u https://mycdn.com
$ cdnex src -o dist -p "**/*.html" -e "pdf,docx" -u https://mycdn.com
$ cdnex src -o dist -u https://mycdn.com -i "\/img\/"
$ cdnex --help
Usage with API
You can also use cdnex programmatically.
.render (options)
Examples
var cdnex = require('cdnex')
import { render } from 'cdnex'
cdnex.render({
input: 'src',
output: 'dist',
url: 'https://mycdn.com',
})
cdnex.render({
src: fs.readFileSync('index.html', 'utf8'),
output: 'dist/index.html',
url: 'https://mycdn.com',
})
cdnex.render({
src: fs.readFileSync('index.html', 'utf8'),
url: 'https://mycdn.com',
}).then(function(rendered) {
console.log(rendered)
}).catch(function(err) {
console.log(err)
})
cdnex.render({
...
pattern: '**/*.html',
onlyExtensions: ['.css', '.js'],
})
cdnex.render({
...
extensions: ['.pdf']
})
Extensions
By default, cdnex will prepend the CDN url to all internal paths with the following extensions: .html, .css, .js, .png, .jpg, .jpeg, .gif, .svg, .woff, .woff2, .eot, .ttf, .otf, .mp4, .webm, .ogg, .mp3, .wav. To add additional extensions, use the extensions option. To replace this list with a new list, use the onlyExtensions option.
License
MIT © Jason Maurer