Initial commit
This commit is contained in:
commit
880e39de2e
511 files changed
+38363
No files matched your search
@@ -0,0 +1,110 @@
|
||||
function YarnFileCopy(options = {}) {
|
||||
|
||||
if (!options.assets === undefined || !Array.isArray(options.assets)) {
|
||||
throw new Error('No assets defined or assets are not in an array');
|
||||
};
|
||||
|
||||
if (options.indexFile === undefined) {
|
||||
throw new Error('No index file set. Please specify options.indexFile');
|
||||
}
|
||||
|
||||
options.outputDir = options.outputDir || '.';
|
||||
options.jsDir = options.jsDir || '.';
|
||||
options.cssDir = options.cssDir || '.';
|
||||
|
||||
//Defaults debug level to 'warning'
|
||||
options.debug = options.debug || 'warning';
|
||||
|
||||
// Defaults debugging to info if only true is specified
|
||||
if (options.debug === true) {
|
||||
options.debug = 'info';
|
||||
}
|
||||
|
||||
const debugLevels = ['warning', 'info', 'debug'];
|
||||
const debugLevelIndex = debugLevels.indexOf(options.debug);
|
||||
function log(msg, level) {
|
||||
if (level === 0) {
|
||||
msg = `WARNING - ${msg}`;
|
||||
} else {
|
||||
level = level || 1;
|
||||
}
|
||||
if (level <= debugLevelIndex) {
|
||||
console.log('[YarnFileCopy] ' + msg); // eslint-disable-line no-console
|
||||
}
|
||||
}
|
||||
|
||||
function warning(msg) {
|
||||
log(msg, 0);
|
||||
}
|
||||
|
||||
function info(msg) {
|
||||
log(msg, 1);
|
||||
}
|
||||
|
||||
function debug(msg) {
|
||||
log(msg, 2);
|
||||
}
|
||||
|
||||
const apply = (compiler) => {
|
||||
compiler.plugin('emit', (compilation, cb) => {
|
||||
debug('starting emit');
|
||||
const callback = () => {
|
||||
debug('finishing emit');
|
||||
cb();
|
||||
};
|
||||
var fs = require('fs'),
|
||||
readline = require('readline'),
|
||||
instream = fs.createReadStream(options.indexFile),
|
||||
outstream = new(require('stream'))(),
|
||||
rl = readline.createInterface(instream, outstream);
|
||||
|
||||
var content = '';
|
||||
rl.on('line', (line) => {
|
||||
debug(line);
|
||||
if (line.indexOf('yarn:css') > -1) {
|
||||
options.assets
|
||||
.filter(asset => asset.endsWith('.css'))
|
||||
.forEach((asset) => {
|
||||
content += ' <link href="' + options.cssDir + '/' + asset + '" data-remove="exclude" data-build="exclude" type="text/css" rel="stylesheet">\n';
|
||||
});
|
||||
} else if (line.indexOf('yarn:js') > -1) {
|
||||
options.assets
|
||||
.filter(asset => asset.endsWith('.js'))
|
||||
.forEach((asset) => {
|
||||
content += ' <script src="' + options.jsDir + '/' + asset + '" data-remove="exclude" data-build="exclude"></script>\n';
|
||||
});
|
||||
} else {
|
||||
content += line + '\n';
|
||||
}
|
||||
});
|
||||
|
||||
rl.on('close', (line) => {
|
||||
var file = require('fs');
|
||||
file.writeFile(options.outputDir + '/index.html', content, (err) =>{
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
debug('new index saved');
|
||||
});
|
||||
});
|
||||
|
||||
callback();
|
||||
});
|
||||
|
||||
compiler.plugin('after-emit', (compilation, cb) => {
|
||||
debug('starting after-emit');
|
||||
const callback = () => {
|
||||
debug('finishing after-emit');
|
||||
cb();
|
||||
};
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
apply
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = YarnFileCopy;
|
||||
Reference in new issue
Block a user