-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
687 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const replace = require('replace-in-file'); | ||
const crypto = require('crypto'); | ||
const pkg = require('./package.json'); | ||
const pkgLock = require('./package-lock.json'); | ||
|
||
simplifyVersion = version => version.replace(/^[=<>~^]/g, ''); | ||
majorMinorVersion = version => version.split('.').slice(0, -1).join('.'); | ||
|
||
const urls = { | ||
'bootstrap_js': 'https://cdn.jsdelivr.net/npm/bootstrap@' + simplifyVersion(pkgLock.packages["node_modules/bootstrap"].version) + '/dist/js/bootstrap.bundle.min.js', | ||
'bootstrap_css': 'https://cdn.jsdelivr.net/npm/bootstrap@' + simplifyVersion(pkgLock.packages["node_modules/bootstrap"].version) + '/dist/css/bootstrap.min.css', | ||
'highcharts_js': 'https://cdn.jsdelivr.net/npm/highcharts@' + simplifyVersion(pkgLock.packages["node_modules/highcharts"].version) + '/highcharts.js', | ||
'highcharts_sankey_js': 'https://cdn.jsdelivr.net/npm/highcharts@' + simplifyVersion(pkgLock.packages["node_modules/highcharts"].version) + '/modules/sankey.js', | ||
'highcharts_css': 'https://cdn.jsdelivr.net/npm/highcharts@' + simplifyVersion(pkgLock.packages["node_modules/highcharts"].version) + '/css/highcharts.css', | ||
}; | ||
|
||
async function calculateSri(url) { | ||
return fetch(url) | ||
.then(response => response.text()) | ||
.then(data => { | ||
const hash = crypto.createHash('sha384').update(data, 'utf8'); | ||
const hashBase64 = hash.digest('base64'); | ||
return 'sha384-' + hashBase64; | ||
}); | ||
} | ||
|
||
async function calculateSris(urls) { | ||
return await Promise.all(urls.map(calculateSri)); | ||
} | ||
|
||
(async() => { | ||
const sris = await calculateSris(Object.values(urls)); | ||
// variable format {{ x }} has been chosen so that a JavaScript error is thrown in case that variable has not been replaced | ||
let variables = [/{{ version }}/g, /{{ inline_css }}/g, /{{ inline_js }}/g]; | ||
Object.keys(urls).forEach(key => variables.push(new RegExp('{{ ' + key + '_url }}',"g"))); | ||
Object.keys(urls).forEach(key => variables.push(new RegExp('{{ ' + key + '_sri }}',"g"))); | ||
|
||
return { | ||
files: pkg.config.outputDir + "/*.lua", // @todo read from stdin? | ||
from: variables, | ||
to: [majorMinorVersion(process.env.npm_package_version), process.env.INLINE_CSS, process.env.INLINE_JS, ...Object.values(urls), ...sris], | ||
countMatches: true, | ||
}; | ||
})().then(options => replace(options) | ||
.then(results => results.every(result => (! result.hasChanged || result.numReplacements !== options.from.length) && (() => { throw new Error('error during build: no replacements done in file ' + result.file) })())) | ||
); |
Oops, something went wrong.