Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (66 loc) · 2.28 KB
/
Copy pathindex.js
File metadata and controls
71 lines (66 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import ass
7486
ert from 'node:assert'
import { resolve, dirname } from 'node:path'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import equals from 'assert-dir-equal'
import Metalsmith from 'metalsmith'
import plugin from '../src/index.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
const { name } = JSON.parse(readFileSync(resolve(__dirname, '../package.json'), 'utf-8'))
function fixture(p) {
return resolve(__dirname, 'fixtures', p)
}
function metalsmith(fixtureName) {
return Metalsmith(fixture(fixtureName)).env('NODE_ENV', process.env.NODE_ENV).env('DEBUG', process.env.DEBUG)
}
describe('@metalsmith/minify', function () {
it('should export a named plugin function matching package.json name', function () {
const namechars = name.split('/')[1]
const camelCased = namechars.split('').reduce((str, char, i) => {
str += namechars[i - 1] === '-' ? char.toUpperCase() : char === '-' ? '' : char
return str
}, '')
assert.strictEqual(plugin().name, camelCased.replace(/~/g, ''))
})
it('should not crash the metalsmith build when using default options', function (done) {
metalsmith(fixture('default'))
.use(plugin({ html: true, css: false, svg: false }))
.build((err) => {
if (err) done(err)
try {
assert.strictEqual(err, null)
equals(fixture('default/build'), fixture('default/expected'))
done()
} catch (err) {
done(err)
}
})
})
it('should handle parse/ minify errors', function (done) {
metalsmith('parse-error')
.use(plugin())
.process((err) => {
try {
assert.notStrictEqual(err, null)
assert.match(err.message, /^Error while parsing\/minifying/)
done()
} catch (err) {
done(err)
}
})
})
describe('svg', function () {
it('should parse svg', async function () {
const ms = metalsmith('svg').use(plugin())
await ms.build()
equals(fixture('svg/build'), fixture('svg/expected'))
})
})
describe('json', function () {
it('should minify json', async function () {
const ms = metalsmith('json').use(plugin({ json: true }))
await ms.build()
equals(fixture('json/build'), fixture('json/expected'))
})
})
})