@eaglesong/task-panorama
Version:
Eaglesong task for Panorama
121 lines • 5.14 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const lodash_1 = tslib_1.__importDefault(require("lodash"));
const memory_fs_1 = tslib_1.__importDefault(require("memory-fs"));
const p_props_1 = tslib_1.__importDefault(require("p-props"));
const path_1 = tslib_1.__importDefault(require("path"));
const read_pkg_1 = tslib_1.__importDefault(require("read-pkg"));
const resolve_from_1 = tslib_1.__importDefault(require("resolve-from"));
const util_1 = require("util");
const webpack_1 = tslib_1.__importDefault(require("webpack"));
async function loadCache(cacheFilePath, commons) {
try {
return lodash_1.default.pick(await fs_extra_1.default.readJson(cacheFilePath), commons);
}
catch {
return {};
}
}
exports.loadCache = loadCache;
exports.resolveVersions = async (context, modules) => Object.fromEntries(await Promise.all(modules.map(async (moduleName) => {
const resolved = resolve_from_1.default(context, `${moduleName}/package.json`);
if (resolved === null) {
return [moduleName, 'exotic'];
}
const pkg = await read_pkg_1.default({ cwd: path_1.default.dirname(resolved) });
return [moduleName, pkg.version];
})));
async function isDirty(isWatching, context, common, cached) {
if (cached == null)
return true;
if (common.lifetime === 'compiler')
return true;
if (typeof common.lifetime === 'number' && cached.time + common.lifetime >= Date.now()) {
return true;
}
if (cached.isWatching !== isWatching)
return true;
return !lodash_1.default.isEqual(cached.versions, await exports.resolveVersions(context, common.modules));
}
async function getDirtyCommons(isWatching, context, common, cache) {
const cacheStatus = await p_props_1.default(common, (cfg, n) => isDirty(isWatching, context, cfg, cache[n]));
return lodash_1.default.keys(lodash_1.default.pickBy(cacheStatus));
}
exports.getDirtyCommons = getDirtyCommons;
function makeConfigs(webpackContext, isWatching) {
const [sameRealmCfg, otherRealmCfg] = [true, false].map((isOtherRealm) => {
const library = isOtherRealm
? 'GameUI.__eaglesong_module__[name]'
: '__eaglesong_module__[name]';
return {
entry: {},
mode: isWatching ? 'development' : 'production',
devtool: false,
context: webpackContext,
output: {
path: '/',
filename: '[name].js',
library,
libraryTarget: isOtherRealm ? 'assign' : undefined,
},
plugins: [
new webpack_1.default.DllPlugin({ path: '/[name].json', name: library }),
new webpack_1.default.BannerPlugin({
test: /\.js$/,
banner: `This file is generated by Eaglesong.
name: [name]
file: [file]
hash: [hash]
chunkhash: [chunkhash]`,
}),
],
};
});
return [sameRealmCfg, otherRealmCfg];
}
exports.makeConfigs = makeConfigs;
async function runCompiler(isWatching, context, outputPath, commons, [sameRealmCfg, otherRealmCfg]) {
for (const [name, { modules, preserveRealm }] of Object.entries(commons)) {
const entry = (preserveRealm ? sameRealmCfg : otherRealmCfg).entry;
entry[name] = modules;
}
const mfs = new memory_fs_1.default();
await Promise.all([sameRealmCfg, otherRealmCfg].map(async (config) => {
const isEmptyConfig = config.entry == null ||
(typeof config.entry === 'object' &&
(Array.isArray(config.entry) ? config.entry : Object.keys(config.entry)).length === 0);
if (isEmptyConfig) {
return;
}
const compiler = webpack_1.default(config);
compiler.outputFileSystem = mfs;
const stats = await util_1.promisify(compiler.run).call(compiler);
if (stats.hasErrors() || stats.hasWarnings()) {
throw new Error(stats.toString());
}
}));
if (outputPath != null) {
await Promise.all(Object.keys(commons).map(name => fs_extra_1.default.outputFile(`${outputPath}/scripts/${name}.js`, mfs.readFileSync(`/${name}.js`, 'utf8'), 'utf8')));
}
const manifests = lodash_1.default.mapValues(commons, ({ preserveRealm }, name) => {
const manifest = JSON.parse(mfs.readFileSync(`/${name}.json`, 'utf8'));
// TODO: Think of some better way
if (!preserveRealm) {
const key = Object.keys(manifest.content).find(x => x.endsWith('webpack/buildin/global.js'));
if (key != null)
delete manifest.content[key];
}
return manifest;
});
const time = Date.now();
return p_props_1.default(manifests, async (manifest, name) => ({
time,
isWatching,
versions: await exports.resolveVersions(context, commons[name].modules),
manifest,
}));
}
exports.runCompiler = runCompiler;
//# sourceMappingURL=cache.js.map
;