Skip to content

Commit e16414f

Browse files
author
Ivan Demidov
committed
feat: add disabled post-load-plugins option, close #162
1 parent e5a794c commit e16414f

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

cli.js

+30-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var argv = require('yargs')
1515
.alias('u', 'use')
1616
.array('use')
1717
.pkgConf('posthtml')
18-
.config('c')
18+
.config()
1919
.alias('c', 'config')
2020
.version(function () {
2121
return require('./package.json').version
@@ -36,24 +36,41 @@ var argv = require('yargs')
3636

3737
function processing(file, output) {
3838
// get htmls
39-
var html = fs.readFileSync(file, 'utf8')
39+
const html = fs.readFileSync(file, 'utf8')
40+
let plugins;
4041

41-
// config
42-
var config = {}
42+
console.log(argv);
4343

44-
// create config extends for post-load-plugins
45-
if (argv.use) {
46-
argv.use.forEach(function (plugin) {
47-
config[plugin] = argv[plugin] || {}
48-
})
49-
}
44+
if (argv.autoOff) {
45+
const use = argv.use ? argv.use : [];
46+
const cfg = argv.config ? Object.keys(require(path.resolve(argv.config))) : [];
47+
plugins = [].concat(use, cfg).map((plugin) => {
48+
try {
49+
return require(plugin)(argv[plugin])
50+
} catch (e) {
51+
if (err.code === 'MODULE_NOT_FOUND') {
52+
throw new TypeError(`Plugin Error: Cannot find module '${plugin}'`);
53+
}
54+
}
55+
});
56+
} else {
57+
// config
58+
var config = {}
5059

51-
if (argv.config) {
52-
config = Object.assign(require(path.resolve(argv.config)), config)
60+
// create config extends for post-load-plugins
61+
if (argv.use) {
62+
argv.use.forEach(function (plugin) {
63+
config[plugin] = argv[plugin] || {}
64+
})
65+
}
66+
67+
if (argv.config) {
68+
config = Object.assign(require(path.resolve(argv.config)), config)
69+
}
5370
}
5471

5572
// processing
56-
posthtml(require('post-load-plugins')(config))
73+
posthtml(argv.autoOff ? plugins : require('post-load-plugins')(config))
5774
.process(html)
5875
.then(function (result) {
5976
fs.writeFileSync(output, result.html)

0 commit comments

Comments
 (0)