2021-10-13 15:05:52 +03:00
|
|
|
// Switch these lines once there are useful utils
|
|
|
|
// const testUtils = require('./utils');
|
|
|
|
require('./utils');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs').promises;
|
|
|
|
const os = require('os');
|
2022-07-27 23:04:39 +03:00
|
|
|
const Minifier = require('../');
|
2021-10-13 15:05:52 +03:00
|
|
|
|
|
|
|
describe('Minifier', function () {
|
|
|
|
let minifier;
|
|
|
|
let testDir;
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
testDir = await fs.mkdtemp(path.join(os.tmpdir(), 'minifier-tests-'));
|
|
|
|
|
|
|
|
minifier = new Minifier({
|
|
|
|
src: path.join(__dirname, 'fixtures', 'basic-cards'),
|
2021-10-19 15:05:23 +03:00
|
|
|
dest: testDir
|
2021-10-13 15:05:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async function () {
|
2021-11-03 17:14:23 +03:00
|
|
|
await fs.rmdir(testDir, {recursive: true});
|
2021-10-13 15:05:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getMatchingFiles expands globs correctly', function () {
|
|
|
|
it('star glob e.g. css/*.css', async function () {
|
|
|
|
let result = await minifier.getMatchingFiles('css/*.css');
|
|
|
|
|
2021-11-03 17:14:23 +03:00
|
|
|
result.should.be.an.Array().with.lengthOf(3);
|
2021-10-13 15:05:52 +03:00
|
|
|
result[0].should.eql('test/fixtures/basic-cards/css/bookmark.css');
|
2021-11-03 17:14:23 +03:00
|
|
|
result[1].should.eql('test/fixtures/basic-cards/css/empty.css');
|
|
|
|
result[2].should.eql('test/fixtures/basic-cards/css/gallery.css');
|
2021-10-13 15:05:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('reverse match glob e.g. css/!(bookmark).css', async function () {
|
|
|
|
let result = await minifier.getMatchingFiles('css/!(bookmark).css');
|
|
|
|
|
2021-11-03 17:14:23 +03:00
|
|
|
result.should.be.an.Array().with.lengthOf(2);
|
|
|
|
result[0].should.eql('test/fixtures/basic-cards/css/empty.css');
|
|
|
|
result[1].should.eql('test/fixtures/basic-cards/css/gallery.css');
|
2021-10-13 15:05:52 +03:00
|
|
|
});
|
|
|
|
it('reverse match glob e.g. css/!(bookmark|gallery).css', async function () {
|
|
|
|
let result = await minifier.getMatchingFiles('css/!(bookmark|gallery).css');
|
|
|
|
|
2021-11-03 17:14:23 +03:00
|
|
|
result.should.be.an.Array().with.lengthOf(1);
|
|
|
|
result[0].should.eql('test/fixtures/basic-cards/css/empty.css');
|
2021-10-13 15:05:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Minify', function () {
|
|
|
|
it('single type, single file', async function () {
|
|
|
|
let result = await minifier.minify({
|
|
|
|
'card.min.js': 'js/*.js'
|
|
|
|
});
|
|
|
|
result.should.be.an.Array().with.lengthOf(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('single type, multi file', async function () {
|
|
|
|
let result = await minifier.minify({
|
|
|
|
'card.min.css': 'css/*.css'
|
|
|
|
});
|
|
|
|
result.should.be.an.Array().with.lengthOf(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('both css and js types + multiple files', async function () {
|
|
|
|
let result = await minifier.minify({
|
|
|
|
'card.min.js': 'js/*.js',
|
|
|
|
'card.min.css': 'css/*.css'
|
|
|
|
});
|
|
|
|
|
|
|
|
result.should.be.an.Array().with.lengthOf(2);
|
|
|
|
});
|
2022-08-03 16:59:08 +03:00
|
|
|
|
|
|
|
it('can replace the content', async function () {
|
|
|
|
let result = await minifier.minify({
|
|
|
|
'card.min.js': 'js/*.js'
|
|
|
|
}, {
|
|
|
|
replacements: {
|
|
|
|
'.kg-gallery-image': 'randomword'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
result.should.be.an.Array().with.lengthOf(1);
|
|
|
|
|
|
|
|
const outputPath = minifier.getFullDest(result[0]);
|
|
|
|
const content = await fs.readFile(outputPath, {encoding: 'utf8'});
|
|
|
|
content.should.match(/randomword/);
|
|
|
|
});
|
2021-10-13 15:05:52 +03:00
|
|
|
});
|
2021-11-03 17:14:23 +03:00
|
|
|
|
|
|
|
describe('Bad inputs', function () {
|
|
|
|
it('cannot create a minifier without src and dest', function () {
|
|
|
|
(function noObject(){
|
|
|
|
new Minifier();
|
|
|
|
}).should.throw();
|
|
|
|
|
|
|
|
(function emptyObject() {
|
|
|
|
new Minifier({});
|
|
|
|
}).should.throw();
|
|
|
|
|
|
|
|
(function missingSrc() {
|
|
|
|
new Minifier({dest: 'a'});
|
|
|
|
}).should.throw();
|
|
|
|
|
|
|
|
(function missingDest() {
|
|
|
|
new Minifier({src: 'a'});
|
|
|
|
}).should.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can only handle css and js files', async function () {
|
|
|
|
try {
|
|
|
|
await minifier.minify({
|
|
|
|
'card.min.ts': 'js/*.ts'
|
|
|
|
});
|
|
|
|
should.fail(minifier, 'Should have errored');
|
|
|
|
} catch (err) {
|
|
|
|
should.exist(err);
|
|
|
|
err.errorType.should.eql('IncorrectUsageError');
|
|
|
|
err.message.should.match(/Unexpected destination/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can handle missing files and folders gracefully', async function () {
|
|
|
|
try {
|
|
|
|
await minifier.minify({
|
|
|
|
'card.min.ts': 'ts/*.ts',
|
|
|
|
'card.min.js': 'js/fake.js'
|
|
|
|
});
|
|
|
|
should.fail(minifier, 'Should have errored');
|
|
|
|
} catch (err) {
|
|
|
|
should.exist(err);
|
|
|
|
err.errorType.should.eql('IncorrectUsageError');
|
|
|
|
err.message.should.match(/Unable to read/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can minify empty js correctly to no result', async function () {
|
|
|
|
let result = await minifier.minify({
|
|
|
|
'card.min.js': 'js/empty.js'
|
|
|
|
});
|
|
|
|
|
|
|
|
result.should.be.an.Array().with.lengthOf(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can minify empty css correctly to no result', async function () {
|
|
|
|
let result = await minifier.minify({
|
|
|
|
'card.min.css': 'css/empty.css'
|
|
|
|
});
|
|
|
|
|
|
|
|
result.should.be.an.Array().with.lengthOf(0);
|
|
|
|
});
|
|
|
|
});
|
2021-10-13 15:05:52 +03:00
|
|
|
});
|