Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
Refactor pipeline more.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Dec 4, 2019
1 parent 384fb6d commit b39a05a
Showing 1 changed file with 30 additions and 54 deletions.
84 changes: 30 additions & 54 deletions lib/fs_utils/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,48 +53,20 @@ async function lint(file) {

try {
await Promise.all(linters);
return true;
} catch (error) {
if (/^warn:\s/i.test(error)) {
logger.warn(`Linting of ${path}: ${error}`);
} else {
rethrow('Linting')(error);
}
return false;
}
return file;
}

async function compileStatic(file, compiler) {
const path = file.path;
debug(`Compiling asset ${path} @ ${compiler.brunchPluginName}`);

try {
const compiled = await compiler.compileStatic(file);
if (compiled == null) return file;
if (compiled.data == null) {
throw new BrunchError('FILE_DATA_INVALID', {path});
}

const dependencies = Array.isArray(compiled.dependencies) ?
compiled.dependencies :
await getDependencies(file, compiler);

const cpath = compiled.path || file.path;
return Object.assign({}, compiled, {
dependencies,
path: compiler.staticTargetExtension ?
cpath.replace(extRe, compiler.staticTargetExtension) :
cpath,
});
} catch (error) {
rethrow('Compiling asset')(error);
}
};

// Extract files' paths that depend on current file.
async function getDependencies(file, compiler) {
if (typeof compiler.getDependencies !== 'function') {
return Promise.resolve();
}
if (typeof compiler.getDependencies !== 'function') return [];

debug(`Fetching dependencies ${file.path} @ ${compiler.brunchPluginName}`);
try {
Expand All @@ -105,62 +77,66 @@ async function getDependencies(file, compiler) {
}
}

async function compile(file, compiler) {
const path = file.path;
debug(`Compiling ${path} @ ${compiler.brunchPluginName}`);
async function compile(file, compiler, isStatic) {
const {path} = file;
const op = isStatic ? 'Compiling asset' : 'Compiling';
debug(`${op} ${path} @ ${compiler.brunchPluginName}`);

try {
const compiled = await compiler.compile(file);
const method = isStatic ? 'compileStatic' : 'compile';
const compiled = await compiler[method](file);
if (compiled == null) return file;
if (compiled.data == null) {
throw new BrunchError('FILE_DATA_INVALID', {path});
}

const dependencies = Array.isArray(compiled.dependencies) ?
compiled.dependencies :
await getDependencies(file, compiler);

const _deps = compiled.dependencies;
const dependencies = Array.isArray(_deps) ? _deps : await getDependencies(file, compiler);
const cpath = compiled.path || file.path;
return Object.assign({}, compiled, {
dependencies: (file.dependencies || []).concat(dependencies || []),
const additional = isStatic ? {
dependencies,
path: compiler.staticTargetExtension ?
cpath.replace(extRe, compiler.staticTargetExtension) :
cpath,
} : {
dependencies: (file.dependencies || []).concat(dependencies),
type: compiler.type,
path: compiler.targetExtension ?
cpath.replace(extRe, compiler.targetExtension) :
cpath,
});
};

return Object.assign({}, compiled, additional);
} catch (error) {
rethrow('Compiling')(error);
rethrow(op)(error);
}
}

/**
* @returns {Promise<File>}
*/
async function nextCompiler(compilers, isStatic, file) {
async function nextCompiler(file, compilers, isStatic) {
const prop = isStatic ? 'staticPattern' : 'pattern';
const exec = isStatic ? compileStatic : compile;
const compiler = pull(compilers, comp => comp[prop].test(file.path));
if (!compiler) return file;
const compiled = await exec(file, compiler);
return nextCompiler(compilers, isStatic, compiled);
const newFile = await compile(file, compiler, isStatic);
return nextCompiler(newFile, compilers, isStatic);
}

async function processAsset(file) {
const compilers = respondTo('compileStatic').filter(comp => comp.type === 'template');
return nextCompiler(compilers, true, file);
return nextCompiler(file, compilers, true);
}

async function processFile(file) {
await lint(file);

const {path} = file;
const isNpm = deppack.isNpm(path);

const usePlugin = isNpm ?
compiler => npmCompilers.includes(compiler.brunchPluginName) :
comp => npmCompilers.includes(comp.brunchPluginName) :
() => true;

const lintedFile = await lint(file);
const compilers = respondTo('compile').filter(usePlugin);
const processed = await nextCompiler(compilers, false, lintedFile);
const processed = await nextCompiler(file, compilers, false);
if (!isNpm) return processed;

const wrapped = await deppack.wrapSourceInModule(processed.data, sysPath.resolve(path));
Expand Down

0 comments on commit b39a05a

Please sign in to comment.