-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rule to get generated html tags from js
- Loading branch information
Showing
8 changed files
with
567 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
|
||
module.exports = function loader() { | ||
const callback = this.async(); | ||
const { plugin } = this.query; | ||
|
||
plugin.tags.promise.then((tags) => { | ||
callback(null, `module.exports = ['${tags.join("', '")}'];`); | ||
}, callback); | ||
}; | ||
|
||
module.exports.tags = () => { | ||
const tags = {}; | ||
|
||
tags.promise = new Promise((resolve, reject) => { | ||
tags.resolve = resolve; | ||
tags.reject = reject; | ||
}); | ||
|
||
tags.promise.catch(() => {}); | ||
|
||
return tags; | ||
}; | ||
|
||
module.exports.rule = (plugin) => { | ||
assert(path.isAbsolute(plugin.options.logo), '`logo` must be an absolute path'); | ||
|
||
const rule = { | ||
include: plugin.options.logo, | ||
loader: __filename, | ||
options: { plugin }, | ||
}; | ||
|
||
return rule; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./logo.png'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const test = require('ava'); | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
const FaviconsWebpackPlugin = require('../'); | ||
|
||
const { logo, mkdir, generate, snapshotCompilationAssets } = require('./util'); | ||
|
||
test.beforeEach(async t => t.context.root = await mkdir()); | ||
|
||
test('should fail to generate rule with relative logo', t => { | ||
const plugin = new FaviconsWebpackPlugin({ logo: './path' }); | ||
try { | ||
plugin.rule(); | ||
} catch (err) { | ||
t.is(err.message, '`logo` must be an absolute path'); | ||
} | ||
}); | ||
|
||
test('should fail to generate rule with module logo', t => { | ||
const plugin = new FaviconsWebpackPlugin({ logo: 'path' }); | ||
try { | ||
plugin.rule(); | ||
} catch (err) { | ||
t.is(err.message, '`logo` must be an absolute path'); | ||
} | ||
}); | ||
|
||
test('should succeed to generate rule with absolute logo', t => { | ||
const plugin = new FaviconsWebpackPlugin({ logo: '/path' }); | ||
const rule = plugin.rule(); | ||
t.pass(); | ||
}); | ||
|
||
test('should generate working rule for getting favicon tags', async t => { | ||
const dist = path.join(t.context.root, 'dist'); | ||
const plugin = new FaviconsWebpackPlugin({ logo }); | ||
const rule = plugin.rule(); | ||
|
||
const compilationStats = await generate({ | ||
entry: path.resolve(__dirname, 'fixtures/rule.js'), | ||
context: t.context.root, | ||
output: { | ||
path: dist, | ||
filename: 'main.jsx', | ||
libraryTarget: 'commonjs2', | ||
}, | ||
plugins: [ | ||
plugin, | ||
], | ||
module: { | ||
rules: [rule], | ||
}, | ||
}); | ||
|
||
snapshotCompilationAssets(t, compilationStats); | ||
}); | ||
|
||
test.afterEach(t => fs.remove(t.context.root)); |
Oops, something went wrong.