Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* global describe it */
const should = require('should');
const assert = require('stream-assert');
const path = require('path');
const fs = require('fs');
const vinyl = require('vinyl-file');
const gulp = require('gulp');
const shortbread = require('../');
require('mocha');
const js = vinyl.readSync(path.join(__dirname, 'fixtures/script.js'));
const jsHash = shortbread.createHash(js.contents.toString());
const jsUrl = 'https://example.com/script.js';
const jsUrlHash = shortbread.createHash(jsUrl);
const css = vinyl.readSync(path.join(__dirname, 'fixtures/style.css'));
const cssHash = shortbread.createHash(css.contents.toString());
const cssUrl = 'https://example.com/style.css';
const cssUrlHash = shortbread.createHash(cssUrl);
describe('shortbread()', () => {
it('should ignore null files', () => {
const result = shortbread();
should(result).be.Object();
should(result.initial).be.empty();
should(result.subsequent).be.empty();
should(Object.keys(result.resources)).be.empty();
should(result.hash).be.null();
const path = require('path');
const shortbread = require('../..');
const vinyl = require('vinyl-file');
const fs = require('fs-extra');
const handlebars = require('handlebars');
// Prepare the output directory
fs.mkdirsSync(path.join(__dirname, '../tmp'));
const criticalCSS = vinyl.readSync(path.join(__dirname, '../fixtures/critical.css'));
const criticalJS = vinyl.readSync(path.join(__dirname, '../fixtures/critical.js'));
criticalJS.path = `${criticalJS.base}/index.php?asset=critical.js`;
const script = vinyl.readSync(path.join(__dirname, '../fixtures/script.js'));
script.path = `${script.base}/index.php?asset=script.js`;
const js = [criticalJS, script];
const style = vinyl.readSync(path.join(__dirname, '../fixtures/style.css'));
style.path = `${style.base}/index.php?asset=style.css`;
const css = [style];
const result = shortbread(js, css, [criticalCSS, criticalJS], 'main', 'allLoaded');
// Compile and store the PHP test file
const hbs = fs.readFileSync(path.join(__dirname, 'index.hbs'));
const template = handlebars.compile(hbs.toString());
const php = template(result);
fs.mkdirsSync(path.join(__dirname, '../../tmp'));
fs.writeFileSync(path.join(__dirname, '../../tmp/index.php'), php);
gulp.task('default', (done) => {
const criticalJS = vinyl.readSync('test/fixtures/critical.js');
const criticalCSS = vinyl.readSync('test/fixtures/critical.css');
const tmpl = filter(['**/*.php'], { restore: true });
// Start with your JavaScript, CSS and template resources
gulp.src(['**/fixtures/*.js', '**/fixtures/style.css', 'gulp/*.php'], { cwd: path.join(__dirname, 'test') })
// Run shortbread
.pipe(shortbread([criticalJS, criticalCSS], 'main', null, { prefix: '/' }))
// Filter all but the template file
.pipe(tmpl)
// Run the template engine
.pipe(template())
// Restore all files
static load(
filePath: PathOsBased,
distTarget: PathOsBased,
base: PathOsBased = consumerPath, // TODO: change params order to fix lint error
consumerPath: PathOsBased,
extendedProps: Object
): SourceFile | null {
try {
const file = new SourceFile(vinylFile.readSync(filePath, { base, cwd: consumerPath }));
const addToFile = (value, key) => (file[key] = value); /* eslint-disable-line no-return-assign */
R.forEachObjIndexed(addToFile, extendedProps);
return file;
} catch (err) {
logger.errorAndAddBreadCrumb(
'source-file.load',
'failed loading file {filePath}. Error: {message}',
{ filePath, message: err.message },
err
);
if (err.code === 'ENOENT' && err.path) {
throw new FileSourceNotFound(err.path);
}
throw err;
}
}
gulp.task('default', (done) => {
const criticalJS = vinyl.readSync('test/fixtures/critical.js');
const criticalCSS = vinyl.readSync('test/fixtures/critical.css');
const tmpl = filter(['**/*.php'], { restore: true });
// Start with your JavaScript, CSS and template resources
gulp.src(['**/fixtures/*.js', '**/fixtures/style.css', 'gulp/*.php'], { cwd: path.join(__dirname, 'test') })
// Run shortbread
.pipe(shortbread([criticalJS, criticalCSS], 'main', null, { prefix: '/' }))
// Filter all but the template file
.pipe(tmpl)
// Run the template engine
.pipe(template())
// Restore all files
.pipe(tmpl.restore)
describe('should support options', () => {
const criticalcss = vinyl.readSync(path.join(__dirname, 'fixtures/critical.css'));
const criticaljs = vinyl.readSync(path.join(__dirname, 'fixtures/script.js'));
it('critical CSS & JavaScript', (done) => {
gulp.src(['fixtures/*.js', 'fixtures/style.css'], { cwd: __dirname })
.pipe(shortbread.stream([criticalcss, criticaljs]))
.pipe(assert.length(2))
.pipe(assert.nth(0, (d) => {
should(path.basename(d.path)).eql('initial.html');
should(d.contents.toString()).match(/criticalcss/);
should(d.contents.toString()).match(/criticaljs/);
}))
.pipe(assert.nth(1, (d) => {
should(path.basename(d.path)).eql('subsequent.html');
}))
.pipe(assert.end(done));
});
describe('should support options', () => {
const criticalcss = vinyl.readSync(path.join(__dirname, 'fixtures/critical.css'));
const criticaljs = vinyl.readSync(path.join(__dirname, 'fixtures/script.js'));
it('critical CSS & JavaScript', (done) => {
gulp.src(['fixtures/*.js', 'fixtures/style.css'], { cwd: __dirname })
.pipe(shortbread.stream([criticalcss, criticaljs]))
.pipe(assert.length(2))
.pipe(assert.nth(0, (d) => {
should(path.basename(d.path)).eql('initial.html');
should(d.contents.toString()).match(/criticalcss/);
should(d.contents.toString()).match(/criticaljs/);
}))
.pipe(assert.nth(1, (d) => {
should(path.basename(d.path)).eql('subsequent.html');
}))
.pipe(assert.end(done));
});
it('file extension filters', (done) => {
describe('should support options', () => {
const criticalcss = vinyl.readSync(path.join(__dirname, 'fixtures/critical.css'));
const criticaljs = vinyl.readSync(path.join(__dirname, 'fixtures/script.js'));
describe('Critical CSS', () => {
it('as Vinyl file', () => {
const result = shortbread(null, null, criticalcss);
should(result.initial).be.not.empty();
should(result.initial).startWith('<style>');
should(result.initial).endWith('</style>');
should(result.initial).match(/criticalcss/);
});
it('as Vinyl file array', () => {
const result = shortbread(null, null, [criticalcss]);
should(result.initial).be.not.empty();
should(result.initial).startWith('<style>');
should(result.initial).endWith('</style>');
should(result.initial).match(/criticalcss/);
});
it('as Vinyl file object', () => {
module.exports = describe('#processFile', function(){
var jpw = new JadePathWriter();
var entry = vinylFile.readSync(path.resolve(__dirname + '/start.jade'));
var processedFiles = jpw.processFile(entry, [path.resolve(__dirname)]);
it('should return an array', function() {
processedFiles.should.be.a('array');
});
it('should return each included file', function() {
processedFiles.should.have.length(3);
});
});
function transform(file, enc, cb) {
if (file.isNull()) return cb(null, file);
if (file.isStream()) return cb(new PluginError('gulp-ladb-i18n-dialog-compile', 'Streaming not supported'));
var data;
try {
var language = file.stem;
var templateFile = vinylFile.readSync(templateSource);
var template = twig({ data: templateFile.contents.toString('utf8') });
data = template.render({ language:language });
} catch (err) {
return cb(new PluginError('gulp-ladb-i18n-dialog-compile', err));
}
file.contents = Buffer.from(data);
file.path = path.join(file.base, 'dialog-' + language + '.html');
cb(null, file);
}