@v4fire/core
Version:
V4Fire core library
77 lines • 2.3 kB
JavaScript
;
const fs = require('fs'),
path = require('upath');
const template = require('@babel/template').default,
{
ensureStatementsHoisted
} = require('@babel/helper-module-transforms');
exports.getProjectInfo = function getProjectInfo() {
const info = include('package.json');
return Object.select(info, ['name', 'version']);
};
exports.getHead = function getHead(withVersion) {
return fs.readFileSync(path.join(process.cwd(), 'disclaimer.txt'), 'utf-8').replace(/\* (\w.*?)(?=\n)/, str => str + (withVersion ? ` v${exports.getProjectInfo().version}` : ''));
};
exports.redefineRequire = function redefineRequire() {
const path = require('upath');
const {
src
} = require('@config/config'),
{
config: pzlr,
resolve
} = require('@pzlr/build-core');
const staticRequire = require,
cache = Object.create(null);
const outputDest = src.serverOutput();
const lib = path.join(outputDest, 'node_modules'),
deps = pzlr.dependencies;
require = url => {
if (url in cache) {
return staticRequire(cache[url]);
}
const resolvedURL = require.resolve(url);
cache[url] = resolvedURL;
return staticRequire(resolvedURL);
};
require.cache = staticRequire.cache;
require.extensions = staticRequire.extensions;
require.main = staticRequire.main;
require.resolve = (url, opts) => {
if (resolve.isNodeModule(url)) {
let resolveUrl;
for (let i = 0; i < deps.length + 1; i++) {
try {
if (i === 0) {
resolveUrl = staticRequire.resolve(path.join(outputDest, url), opts);
} else {
resolveUrl = staticRequire.resolve(path.join(lib, deps[i - 1], url), opts);
}
break;
} catch {}
}
if (resolveUrl) {
return resolveUrl;
}
}
return staticRequire.resolve(url, opts);
};
require('core/prelude');
};
exports.prependCode = function prependCode(code) {
return function prependCode() {
return {
visitor: {
Program: {
exit(path) {
const node = template.ast(code, {
preserveComments: true
});
ensureStatementsHoisted([node]);
path.unshiftContainer('body', node);
}
}
}
};
};
};