Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed May 8, 2020
1 parent 42194f3 commit 6b07a63
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 126 deletions.
67 changes: 37 additions & 30 deletions src/index.js
Expand Up @@ -24,7 +24,7 @@ class CopyPlugin {

compilation.hooks.additionalAssets.tapAsync(
'copy-webpack-plugin',
(callback) => {
async (callback) => {
logger.debug('start to adding additionalAssets');

const globalRef = {
Expand All @@ -37,37 +37,44 @@ class CopyPlugin {
concurrency: this.options.concurrency,
};

Promise.all(
this.patterns.map((pattern) =>
Promise.resolve()
.then(() => preProcessPattern(globalRef, pattern))
// Every source (from) is assumed to exist here
// eslint-disable-next-line no-shadow
.then((pattern) =>
processPattern(globalRef, pattern).then((files) => {
if (!files) {
return Promise.resolve();
}
try {
await Promise.all(
this.patterns.map(async (pattern) => {
const patternAfterPreProcess = await preProcessPattern(
globalRef,
pattern
);

return Promise.all(
files
.filter(Boolean)
.map((file) =>
postProcessPattern(globalRef, pattern, file)
)
);
})
)
)
)
.catch((error) => {
compilation.errors.push(error);
})
.then(() => {
logger.debug('end to adding additionalAssets');
const files = await processPattern(
globalRef,
patternAfterPreProcess
);

callback();
});
if (!files) {
return Promise.resolve();
}

return Promise.all(
files
.filter(Boolean)
.map((file) =>
postProcessPattern(
globalRef,
patternAfterPreProcess,
file
)
)
);
})
);

logger.debug('end to adding additionalAssets');

callback();
} catch (error) {
compilation.errors.push(error);
callback();
}
}
);
});
Expand Down
36 changes: 19 additions & 17 deletions src/preProcessPattern.js
Expand Up @@ -5,7 +5,7 @@ import { stat } from './utils/promisify';

/* eslint-disable no-param-reassign */

export default function preProcessPattern(globalRef, pattern) {
export default async function preProcessPattern(globalRef, pattern) {
const { context, logger, inputFileSystem } = globalRef;

pattern =
Expand Down Expand Up @@ -57,22 +57,24 @@ export default function preProcessPattern(globalRef, pattern) {
`getting stats for '${pattern.absoluteFrom}' to determinate 'fromType'`
);

return stat(inputFileSystem, pattern.absoluteFrom)
.then((stats) => {
if (!stats) {
return pattern;
}

if (stats.isDirectory()) {
pattern.fromType = 'dir';
} else if (stats.isFile()) {
pattern.fromType = 'file';
pattern.stats = stats;
} else if (!pattern.fromType) {
logger.warn(`unrecognized file type for ${pattern.from}`);
}
try {
const stats = await stat(inputFileSystem, pattern.absoluteFrom);

if (!stats) {
return pattern;
})
.catch(() => pattern);
}

if (stats.isDirectory()) {
pattern.fromType = 'dir';
} else if (stats.isFile()) {
pattern.fromType = 'file';
pattern.stats = stats;
} else if (!pattern.fromType) {
logger.warn(`unrecognized file type for ${pattern.from}`);
}
} catch (error) {
return pattern;
}

return pattern;
}
159 changes: 80 additions & 79 deletions src/processPattern.js
Expand Up @@ -9,7 +9,7 @@ import createPatternGlob from './utils/createPatternGlob';

/* eslint-disable no-param-reassign */

export default function processPattern(globalRef, pattern) {
export default async function processPattern(globalRef, pattern) {
const { logger, output, concurrency, compilation } = globalRef;
createPatternGlob(pattern, globalRef);

Expand All @@ -19,103 +19,104 @@ export default function processPattern(globalRef, pattern) {
`begin globbing '${pattern.glob}' with a context of '${pattern.context}'`
);

return globby(pattern.glob, pattern.globOptions).then((paths) => {
if (paths.length === 0) {
const newWarning = new Error(
`unable to locate '${pattern.from}' at '${pattern.absoluteFrom}'`
);
const hasWarning = compilation.warnings.some(
// eslint-disable-next-line no-shadow
(warning) => warning.message === newWarning.message
);
const paths = await globby(pattern.glob, pattern.globOptions);

// Only display the same message once
if (!hasWarning) {
logger.warn(newWarning.message);
if (paths.length === 0) {
const newWarning = new Error(
`unable to locate '${pattern.from}' at '${pattern.absoluteFrom}'`
);
const hasWarning = compilation.warnings.some(
// eslint-disable-next-line no-shadow
(warning) => warning.message === newWarning.message
);

compilation.warnings.push(newWarning);
}
// Only display the same message once
if (!hasWarning) {
logger.warn(newWarning.message);

return Promise.resolve();
compilation.warnings.push(newWarning);
}
return Promise.all(
paths.map((from) =>
limit(() => {
const file = {
force: pattern.force,
absoluteFrom: path.resolve(pattern.context, from),
};

file.relativeFrom = path.relative(pattern.context, file.absoluteFrom);

if (pattern.flatten) {
file.relativeFrom = path.basename(file.relativeFrom);
}

logger.debug(`found ${from}`);
return Promise.resolve();
}

// Check the ignore list
let il = pattern.ignore.length;
return Promise.all(
paths.map((from) =>
limit(() => {
const file = {
force: pattern.force,
absoluteFrom: path.resolve(pattern.context, from),
};

// eslint-disable-next-line no-plusplus
while (il--) {
const ignoreGlob = pattern.ignore[il];
file.relativeFrom = path.relative(pattern.context, file.absoluteFrom);

let globParams = {
dot: true,
matchBase: true,
};
if (pattern.flatten) {
file.relativeFrom = path.basename(file.relativeFrom);
}

let glob;
logger.debug(`found ${from}`);

if (typeof ignoreGlob === 'string') {
glob = ignoreGlob;
} else if (isObject(ignoreGlob)) {
glob = ignoreGlob.glob || '';
// Check the ignore list
let il = pattern.ignore.length;

const ignoreGlobParams = Object.assign({}, ignoreGlob);
delete ignoreGlobParams.glob;
// eslint-disable-next-line no-plusplus
while (il--) {
const ignoreGlob = pattern.ignore[il];

// Overwrite minimatch defaults
globParams = Object.assign(globParams, ignoreGlobParams);
} else {
glob = '';
}
let globParams = {
dot: true,
matchBase: true,
};

logger.debug(`testing ${glob} against ${file.relativeFrom}`);
let glob;

if (minimatch(file.relativeFrom, glob, globParams)) {
logger.log(
`ignoring '${file.relativeFrom}', because it matches the ignore glob '${glob}'`
);
if (typeof ignoreGlob === 'string') {
glob = ignoreGlob;
} else if (isObject(ignoreGlob)) {
glob = ignoreGlob.glob || '';

return Promise.resolve();
}
const ignoreGlobParams = Object.assign({}, ignoreGlob);
delete ignoreGlobParams.glob;

logger.debug(`${glob} doesn't match ${file.relativeFrom}`);
// Overwrite minimatch defaults
globParams = Object.assign(globParams, ignoreGlobParams);
} else {
glob = '';
}

// Change the to path to be relative for webpack
if (pattern.toType === 'dir') {
file.webpackTo = path.join(pattern.to, file.relativeFrom);
} else if (pattern.toType === 'file') {
file.webpackTo = pattern.to || file.relativeFrom;
} else if (pattern.toType === 'template') {
file.webpackTo = pattern.to;
file.webpackToRegExp = pattern.test;
}
logger.debug(`testing ${glob} against ${file.relativeFrom}`);

if (path.isAbsolute(file.webpackTo)) {
file.webpackTo = path.relative(output, file.webpackTo);
}
if (minimatch(file.relativeFrom, glob, globParams)) {
logger.log(
`ignoring '${file.relativeFrom}', because it matches the ignore glob '${glob}'`
);

logger.log(
`determined that '${from}' should write to '${file.webpackTo}'`
);
return Promise.resolve();
}

return file;
})
)
);
});
logger.debug(`${glob} doesn't match ${file.relativeFrom}`);
}

// Change the to path to be relative for webpack
if (pattern.toType === 'dir') {
file.webpackTo = path.join(pattern.to, file.relativeFrom);
} else if (pattern.toType === 'file') {
file.webpackTo = pattern.to || file.relativeFrom;
} else if (pattern.toType === 'template') {
file.webpackTo = pattern.to;
file.webpackToRegExp = pattern.test;
}

if (path.isAbsolute(file.webpackTo)) {
file.webpackTo = path.relative(output, file.webpackTo);
}

logger.log(
`determined that '${from}' should write to '${file.webpackTo}'`
);

return file;
})
)
);
}

0 comments on commit 6b07a63

Please sign in to comment.