Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed minifier for Windows users #15893

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ghost/minifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ minifier.minify({

- Minfier constructor requires a src and a dest
- minify() function takes an object with destination file as the key and source glob as the value
- globs can be anything tiny-glob supports
- globs can be anything tiny-glob supports (must use forward slash regardless of platform)
- destination files must end with .css or .js
- src files will be minified according to their destination file extension
- src files will be minified according to their destination file extension
32 changes: 18 additions & 14 deletions ghost/minifier/lib/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const terser = require('terser');
const glob = require('tiny-glob');
const path = require('path');
const fs = require('fs').promises;
const isWin = process.platform === 'win32';

const messages = {
badDestination: {
Expand Down Expand Up @@ -69,7 +70,12 @@ class Minifier {
}

async getMatchingFiles(src) {
return await glob(this.getFullSrc(src));
let fullSrc = this.getFullSrc(src);
// must feed glob forward slashes to function properly
if (isWin) {
fullSrc = fullSrc.replace(/\\/g,'/');
};
return await glob(fullSrc);
}

async readFiles(files) {
Expand All @@ -83,13 +89,10 @@ class Minifier {
}

async getSrcFileContents(src) {
try {
const files = await this.getMatchingFiles(src);

if (files) {
return await this.readFiles(files);
}
} catch (error) {
const files = await this.getMatchingFiles(src);
if (files.length > 0) {
return await this.readFiles(files);
} else {
throw new errors.IncorrectUsageError({
message: tpl(messages.badSource.message, {src}),
context: tpl(messages.badSource.context),
Expand Down Expand Up @@ -129,6 +132,13 @@ class Minifier {
const minifiedFiles = [];

for (const dest of destinations) {
if (!dest.endsWith('.css') && !dest.endsWith('.js')) {
throw new errors.IncorrectUsageError({
message: tpl(messages.badDestination.message, {dest}),
context: tpl(messages.badDestination.context),
help: tpl(messages.globalHelp)
})};

const src = globs[dest];
let contents = await this.getSrcFileContents(src);

Expand All @@ -143,12 +153,6 @@ class Minifier {
minifiedContents = await this.minifyCSS(contents);
} else if (dest.endsWith('.js')) {
minifiedContents = await this.minifyJS(contents);
} else {
throw new errors.IncorrectUsageError({
message: tpl(messages.badDestination.message, {dest}),
context: tpl(messages.badDestination.context),
help: tpl(messages.globalHelp)
});
}

const result = await this.writeFile(minifiedContents, dest);
Expand Down
15 changes: 7 additions & 8 deletions ghost/minifier/test/minifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ describe('Minifier', function () {
let result = await minifier.getMatchingFiles('css/*.css');

result.should.be.an.Array().with.lengthOf(3);
result[0].should.eql('test/fixtures/basic-cards/css/bookmark.css');
result[1].should.eql('test/fixtures/basic-cards/css/empty.css');
result[2].should.eql('test/fixtures/basic-cards/css/gallery.css');
});
result[0].should.eql(path.join('test','fixtures','basic-cards','css','bookmark.css'));
result[1].should.eql(path.join('test','fixtures','basic-cards','css','empty.css'));
result[2].should.eql(path.join('test','fixtures','basic-cards','css','gallery.css'));
});

it('reverse match glob e.g. css/!(bookmark).css', async function () {
let result = await minifier.getMatchingFiles('css/!(bookmark).css');

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');
result[0].should.eql(path.join('test','fixtures','basic-cards','css','empty.css'));
result[1].should.eql(path.join('test','fixtures','basic-cards','css','gallery.css'));
});
it('reverse match glob e.g. css/!(bookmark|gallery).css', async function () {
let result = await minifier.getMatchingFiles('css/!(bookmark|gallery).css');

result.should.be.an.Array().with.lengthOf(1);
result[0].should.eql('test/fixtures/basic-cards/css/empty.css');
result[0].should.eql(path.join('test','fixtures','basic-cards','css','empty.css'));
});
});

Expand Down Expand Up @@ -123,7 +123,6 @@ describe('Minifier', function () {
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');
Expand Down