Skip to content

Commit ee61bd6

Browse files
committed
update to support webpack 5
update to support webpack 5
1 parent 1397ed4 commit ee61bd6

File tree

2 files changed

+77
-45
lines changed

2 files changed

+77
-45
lines changed

index.js

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,68 @@
1-
var eslintStandardConfig = require('eslint-config-standard')
1+
const prettier = require("prettier");
22
var format = require('prettier-eslint')
3-
var fsReadFile = require('fs-readfile-promise')
4-
var fsWriteFile = require('fs-writefile-promise/lib/node7')
3+
const fs = require("fs");
54
var path = require('path')
65

7-
function processFilePath({ file, encoding, filePath, eslintConfig, prettierOptions, logLevel, eslintPath, prettierPath }) {
6+
const PRETTIER_ESLINT_PLUGIN = 'PrettierEslintPlugin';
7+
const DEFAULT_ENCODING = "utf-8";
8+
const DEFAULT_EXTENSIONS = prettier.getSupportInfo
9+
? prettier
10+
.getSupportInfo()
11+
.languages.map(l => l.extensions)
12+
.reduce((accumulator, currentValue) => accumulator.concat(currentValue))
13+
: [
14+
".css",
15+
".graphql",
16+
".js",
17+
".json",
18+
".jsx",
19+
".less",
20+
".sass",
21+
".scss",
22+
".ts",
23+
".tsx",
24+
".vue",
25+
".yaml",
26+
];
27+
28+
function processFilePath({ fileCurrent, encoding, filePath, eslintConfig, prettierOptions, logLevel, eslintPath, prettierPath }) {
829
return new Promise((resolve, reject) => {
9-
fsReadFile(file, { encoding: encoding })
10-
.then(buffer => buffer.toString())
11-
.catch(err => { reject(err.message) })
12-
.then(source => {
30+
fs.readFile(fileCurrent, encoding, (err, source) => {
31+
if (err) {
32+
return reject(err);
33+
}
34+
35+
try{
1336
const fmtOptions = {
1437
text: source,
1538
filePath, eslintConfig, prettierOptions, logLevel, eslintPath, prettierPath
1639
}
40+
1741
const formatted = format(fmtOptions)
42+
1843
if (formatted !== source) {
19-
fsWriteFile(file, formatted, { encoding: encoding })
20-
.catch(err => reject(err.message))
21-
.then(() => { resolve('success!') })
44+
fs.writeFile(fileCurrent, prettierSource, this.encoding, err => {
45+
if (err) {
46+
return reject(err);
47+
}
48+
resolve('success!');
49+
});
50+
} else {
51+
resolve('success!');
2252
}
23-
})
24-
.catch(err => { reject(err.message) })
53+
}
54+
catch(err){
55+
return reject(err);
56+
}
57+
});
2558
})
2659
}
2760

2861
class PrettierEslintPlugin {
2962
constructor (
3063
{
31-
encoding = 'utf-8',
32-
extensions = ['.js', '.jsx'],
64+
encoding = DEFAULT_ENCODING,
65+
extensions = DEFAULT_EXTENSIONS,
3366
// prettier-eslint API
3467
filePath, eslintConfig, prettierOptions, logLevel, eslintPath,
3568
prettierPath, sillyLogs, config
@@ -49,38 +82,36 @@ class PrettierEslintPlugin {
4982
}
5083

5184
apply (compiler) {
52-
compiler.plugin('emit', (compilation, callback) => {
53-
// Explore each chunk (build output):
54-
compilation.chunks.forEach(chunk => {
55-
// Explore each module within the chunk (built inputs):
56-
chunk.modules.forEach(module => {
57-
if (!module.fileDependencies) return
58-
// Explore each source file path that was included into the module
59-
module.fileDependencies.forEach(file => {
60-
// match extensions and exclude node modules
61-
if (
62-
this.extensions.indexOf(path.extname(file)) !== -1 &&
63-
file.indexOf('node_modules') === -1
64-
) {
65-
processFilePath({
66-
file: file,
67-
encoding: this.encoding,
85+
compiler.hooks.emit.tapAsync(PRETTIER_ESLINT_PLUGIN, (compilation, callback) => {
86+
const promises = [];
87+
if (!compilation.fileDependencies) return;
88+
compilation.fileDependencies.forEach(fileCurrent=> {
89+
if (this.extensions.indexOf(path.extname(fileCurrent)) === -1 &&
90+
fileCurrent.indexOf('node_modules') === -1 ) {
91+
return;
92+
}
6893

69-
filePath : this.filePath,
70-
eslintConfig : this.eslintConfig,
71-
prettierOptions : this.prettierOptions,
72-
logLevel : this.logLevel,
73-
eslintPath : this.eslintPath,
74-
prettierPath : this.prettierPath,
75-
})
76-
.then(() => { console.log('succeed') })
77-
.catch(err => { console.error(err) })
78-
}
94+
promises.push(
95+
processFilePath({
96+
fileCurrent: fileCurrent,
97+
encoding: this.encoding,
98+
99+
filePath : this.filePath,
100+
eslintConfig : this.eslintConfig,
101+
prettierOptions : this.prettierOptions,
102+
logLevel : this.logLevel,
103+
eslintPath : this.eslintPath,
104+
prettierPath : this.prettierPath,
79105
})
80-
})
81-
})
82-
callback()
83-
})
106+
);
107+
});
108+
109+
Promise.all(promises).then(() => {
110+
callback();
111+
}).catch(err => {
112+
callback(err);
113+
});
114+
});
84115
}
85116
}
86117

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"author": "Daniël Terwiel",
1010
"license": "MIT",
1111
"dependencies": {
12+
"core-js": "^3.15.2",
1213
"eslint-config-standard": "^7.1.0",
1314
"fs-readfile-promise": "^3.0.0",
1415
"fs-writefile-promise": "^2.0.0",

0 commit comments

Comments
 (0)