Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit dcf01f4

Browse files
committed
Move dependency loading to a function
Move all loading of dependencies to a function that is only called when it is first needed. Once dependencies are loaded they are no longer required.
1 parent 1b67722 commit dcf01f4

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

lib/index.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,54 @@
22

33
// eslint-disable-next-line import/extensions, import/no-extraneous-dependencies
44
import { CompositeDisposable } from 'atom';
5-
import { readFile as fsReadFile } from 'fs';
6-
import { dirname } from 'path';
75

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;
1314

1415
const getConfig = async (filePath) => {
15-
const readFile = tinyPromisify()(fsReadFile);
16+
const readFile = tinyPromisify(fsReadFile);
1617
const configPath = await findAsync(dirname(filePath), '.htmlhintrc');
1718
let conf = null;
1819
if (configPath !== null) {
1920
conf = await readFile(configPath, 'utf8');
2021
}
2122
if (conf) {
22-
return JSON.parse(stripJSONComments()(conf));
23+
return JSON.parse(stripJSONComments(conf));
2324
}
2425
return null;
2526
};
2627

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+
2753
export default {
2854
activate() {
2955
require('atom-package-deps').install('linter-htmlhint');
@@ -65,7 +91,8 @@ export default {
6591
return [];
6692
}
6793

68-
const { HTMLHint } = require('htmlhint');
94+
// Ensure that all dependencies are loaded
95+
loadDeps();
6996

7097
const ruleset = await getConfig(filePath);
7198

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"atom-linter": "^10.0.0",
4646
"atom-package-deps": "^4.0.1",
4747
"htmlhint": "0.9.13",
48-
"lazy-req": "^2.0.0",
4948
"strip-json-comments": "^2.0.1",
5049
"tiny-promisify": "^1.0.0"
5150
},

0 commit comments

Comments
 (0)