Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack 4 support #31

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,20 @@ const prepareEntry = async (code, file) => {
};

const teardown = path => {
fs.readdir(path, (err, filenames) => {
filenames.forEach(file => {
const filenames = fs.readdirSync(path);
filenames.forEach(file => {
const curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) teardown(curPath);
else fs.unlinkSync(curPath);
});
fs.rmdirSync(path);
});

fs.rmdirSync(path);
};

describe("unit tests", () => {
beforeAll(() => {
if (!fs.existsSync("./temp")) fs.mkdirSync("./temp");
if (!fs.existsSync('./temp/ignored')) fs.mkdirSync('./temp/ignored');
});

afterAll(() => {
Expand Down Expand Up @@ -154,6 +155,7 @@ describe("unit tests", () => {
bundle(
{
entry: entry,
mode: 'development',
output: { filename: output },
plugins: [new PrettierPlugin({ extensions: [".js"] })]
},
Expand Down
55 changes: 27 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,37 @@ module.exports = class PrettierPlugin {
}

apply(compiler) {
compiler.plugin("emit", (compilation, callback) => {
// Explore each chunk (build output):
compilation.chunks.forEach(chunk => {
// Explore each module within the chunk (built inputs):
chunk.modules.forEach(module => {
if (!module.fileDependencies) return;
compiler.hooks.emit.tapAsync('Prettier', (compilation, callback) => {

// Explore each source file path that was included into the module
module.fileDependencies.forEach(filepath => {
// If it matches
if (this.extensions.indexOf(path.extname(filepath)) !== -1) {
// Read the file
fs.readFile(filepath, this.encoding, (err, source) => {
// Format via prettier
var prettierSource = prettier.format(
source,
this.prettierOptions
);
const promises = [];
compilation.fileDependencies.forEach(filepath => {
if (this.extensions.indexOf(path.extname(filepath)) === -1) {
return;
}

// Rewrite file if prettier returned different source
if (prettierSource !== source) {
fs.writeFile(filepath, prettierSource, this.encoding, err => {
if (err) throw err;
});
}
});
}
});
promises.push(new Promise((resolve, reject) => {
fs.readFile(filepath, this.encoding, (err, source) => {
if (err) {
return reject(err);
}
const prettierSource = prettier.format(source, this.prettierOptions);
if (prettierSource !== source) {
fs.writeFile(filepath, prettierSource, this.encoding, err => {
if (err) {
return reject(err);
}
resolve();
});
} else {
resolve();
}
});
});
}));
});

callback();
Promise.all(promises).then(() => {
callback();
});
});
}
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"ac:generate": "all-contributors generate"
},
"jest": {
"testEnvironment": "node",
"testPathIgnorePatterns": [
"sample-code.js"
]
Expand All @@ -32,10 +33,10 @@
"prettier": "^1.0.0"
},
"devDependencies": {
"all-contributors-cli": "^4.3.0",
"jest": "^22.0.1",
"all-contributors-cli": "^4.11.1",
"jest": "^22.4.3",
"prettier": "^1.0.0",
"uuid": "^3.0.1",
"webpack": "^3.1.0"
"uuid": "^3.2.1",
"webpack": "^4.2.0"
}
}
Loading