|
2 | 2 |
|
3 | 3 | // eslint-disable-next-line import/extensions, import/no-extraneous-dependencies
|
4 | 4 | import { CompositeDisposable } from 'atom';
|
5 |
| -import { readFile as fsReadFile } from 'fs'; |
6 |
| -import { dirname } from 'path'; |
7 | 5 |
|
8 |
| -const lazyReq = require('lazy-req')(require); |
9 |
| - |
10 |
| -const { findAsync, generateRange } = lazyReq('atom-linter')('findAsync', 'generateRange'); |
11 |
| -const stripJSONComments = lazyReq('strip-json-comments'); |
12 |
| -const tinyPromisify = lazyReq('tiny-promisify'); |
| 6 | +// Dependencies |
| 7 | +let dirname; |
| 8 | +let HTMLHint; |
| 9 | +let findAsync; |
| 10 | +let fsReadFile; |
| 11 | +let generateRange; |
| 12 | +let tinyPromisify; |
| 13 | +let stripJSONComments; |
13 | 14 |
|
14 | 15 | const getConfig = async (filePath) => {
|
15 |
| - const readFile = tinyPromisify()(fsReadFile); |
| 16 | + const readFile = tinyPromisify(fsReadFile); |
16 | 17 | const configPath = await findAsync(dirname(filePath), '.htmlhintrc');
|
17 | 18 | let conf = null;
|
18 | 19 | if (configPath !== null) {
|
19 | 20 | conf = await readFile(configPath, 'utf8');
|
20 | 21 | }
|
21 | 22 | if (conf) {
|
22 |
| - return JSON.parse(stripJSONComments()(conf)); |
| 23 | + return JSON.parse(stripJSONComments(conf)); |
23 | 24 | }
|
24 | 25 | return null;
|
25 | 26 | };
|
26 | 27 |
|
| 28 | +const loadDeps = () => { |
| 29 | + if (loadDeps.loaded) { |
| 30 | + return; |
| 31 | + } |
| 32 | + if (!dirname) { |
| 33 | + ({ dirname } = require('path')); |
| 34 | + } |
| 35 | + if (!HTMLHint) { |
| 36 | + ({ HTMLHint } = require('htmlhint')); |
| 37 | + } |
| 38 | + if (!findAsync || !generateRange) { |
| 39 | + ({ findAsync, generateRange } = require('atom-linter')); |
| 40 | + } |
| 41 | + if (!fsReadFile) { |
| 42 | + ({ readFile: fsReadFile } = require('fs')); |
| 43 | + } |
| 44 | + if (!tinyPromisify) { |
| 45 | + tinyPromisify = require('tiny-promisify'); |
| 46 | + } |
| 47 | + if (!stripJSONComments) { |
| 48 | + stripJSONComments = require('strip-json-comments'); |
| 49 | + } |
| 50 | + loadDeps.loaded = true; |
| 51 | +}; |
| 52 | + |
27 | 53 | export default {
|
28 | 54 | activate() {
|
29 | 55 | require('atom-package-deps').install('linter-htmlhint');
|
@@ -65,7 +91,8 @@ export default {
|
65 | 91 | return [];
|
66 | 92 | }
|
67 | 93 |
|
68 |
| - const { HTMLHint } = require('htmlhint'); |
| 94 | + // Ensure that all dependencies are loaded |
| 95 | + loadDeps(); |
69 | 96 |
|
70 | 97 | const ruleset = await getConfig(filePath);
|
71 | 98 |
|
|
0 commit comments