seacms-theme/js/extract-files-from-node-modules.js
2023-02-17 15:03:32 +01:00

115 lines
2.9 KiB
JavaScript

/*
* SPDX-License-Identifier: EUPL-1.2
* Authors: see README.md
*/
// Extract files that we need from the node_modules folder
// The extracted files are integrated to the repository, so production server don't need to
// have node installed
// Include fs and path module
const fs = require('fs-extra')
const path = require('path')
const basePath = path.join(__dirname, '../')
function copySync(src, dest, opts) {
if (fs.existsSync(src)) {
fs.copySync(path.join(basePath, src), path.join(basePath, dest), opts)
} else {
console.log(`${src} is not existing !`)
}
}
// function mergeFilesSync(sources, dest) {
// const fullDest = path.join(basePath, dest)
// if (!fs.existsSync(fullDest)) {
// fs.mkdirSync(path.dirname(fullDest), { recursive: true })
// } else {
// fs.unlinkSync(fullDest)
// }
// sources.forEach((file) => {
// const fullSrc = path.join(basePath, file)
// if (!fs.existsSync(fullSrc)) {
// console.log(`${fullSrc} is not existing !`)
// } else {
// fs.appendFileSync(fullDest, fs.readFileSync(fullSrc))
// }
// })
// }
// knacss
copySync(
'node_modules/knacss/css/knacss-mini/knacss.css',
'css/vendor/knacss/knacss.min.css',
{ overwrite: true }
)
copySync(
'node_modules/knacss/LICENSE',
'css/vendor/knacss/LICENSE',
{ overwrite: true }
)
// modernizr
const modernizr = require("modernizr")
modernizr.build({
"minify": true,
"options": [
"setClasses"
],
"feature-detects": [
"test/requestanimationframe",
"test/css/transitions",
"test/dom/classlist"
]
}, function (result) {
let src = 'js/vendor/modernizr'
let currentPath = path.join(basePath, src)
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath,{recursive :true})
}
fs.writeFileSync(path.join(currentPath, 'modernizr.min.js'), result);// the build
});
copySync(
'node_modules/modernizr/LICENSE',
'js/vendor/modernizr/LICENSE',
{ overwrite: true }
)
// icomoon free
copySync(
'node_modules/icomoon-free-npm/license.txt',
'fonts/vendor/icomoon-free-npm/license.txt',
{ overwrite: true }
)
var previousCSSContent = fs.readFileSync( path.join(basePath,'node_modules/icomoon-free-npm/Font/demo-files/demo.css'),{encoding:'utf8', flag:'r'} )
var position = previousCSSContent.search(/@font-face/)
fs.writeFileSync(
path.join(
basePath,
'fonts/vendor/icomoon-free-npm/icomoon.css'
),
previousCSSContent.substr(position).replace(
/url\('..\/IcoMoon-Free.ttf'\) format\('truetype'\)/,
"url('./IcoMoon-Free.woff') format('woff')"
)
)
const execSync = require('child_process').execSync;
const output = execSync('node node_modules/ttf2woff/ttf2woff.js node_modules/icomoon-free-npm/Font/IcoMoon-Free.ttf fonts/vendor/icomoon-free-npm/IcoMoon-Free.woff', { encoding: 'utf-8' });
console.log('Output was:\n', output);
// example
// mergeFilesSync(
// [
// 'node_modules/file1.js',
// 'node_modules/file2.js',
// ],
// 'javascripts/vendor/output-file.js');