Skip to content

Commit

Permalink
Reduce npm package size by 75%
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Nov 29, 2020
1 parent 8a4cdcf commit 4d08b84
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 46 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
!/.*
/build/
/test-config/
10 changes: 7 additions & 3 deletions .eslintrc.base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const pkg = require("./package.json");
const fs = require("fs");

module.exports = {
extends: [
Expand All @@ -12,8 +12,12 @@ module.exports = {
],
plugins: [
"prettier",
...pkg.files
.filter((name) => !name.includes("/") && name !== "index.js")
...fs
.readdirSync(__dirname)
.filter(
(file) =>
!file.startsWith(".") && file.endsWith(".js") && file !== "index.js"
)
.map((ruleFileName) => ruleFileName.replace(/\.js$/, "")),
],
parserOptions: {
Expand Down
7 changes: 4 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"use strict";

const pkg = require("./package.json");
const fs = require("fs");

module.exports = {
extends: [
"./.eslintrc.base.js",
...pkg.files
.filter((name) => !name.includes("/"))
...fs
.readdirSync(__dirname)
.filter((file) => !file.startsWith(".") && file.endsWith(".js"))
.map((ruleFileName) => `./${ruleFileName}`),
],
rules: {
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ jobs:

- name: Prettier
run: npx --no-install prettier --check .

- name: Build
run: npm run build
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/build/
/node_modules/
/test-config/
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/build/
/test-config/
.vscode
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This lets you use your favorite shareable config without letting its stylistic c

Note that this config _only_ turns rules _off,_ so it only makes sense using it together with some other config.

## Contents
---

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
Expand Down Expand Up @@ -786,7 +786,6 @@ console.log();

Finally, you need to mention the plugin in several places:

- Add `"foobar.js"` to the "files" field in `package.json`.
- Add eslint-plugin-foobar to the "devDependencies" field in `package.json`.
- Make sure that at least one rule from eslint-plugin-foobar gets used in `.eslintrc.base.js`.
- Add it to the list of supported plugins and to the Contributing section in `README.md`.
Expand Down
13 changes: 13 additions & 0 deletions package-real.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "eslint-config-prettier",
"version": "6.15.0",
"license": "MIT",
"author": "Simon Lydell",
"description": "Turns off all rules that are unnecessary or might conflict with Prettier.",
"repository": "prettier/eslint-config-prettier",
"bin": "bin/cli.js",
"keywords": ["eslint", "eslintconfig", "prettier"],
"peerDependencies": {
"eslint": ">=7.0.0"
}
}
32 changes: 3 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
{
"name": "eslint-config-prettier",
"version": "6.15.0",
"license": "MIT",
"author": "Simon Lydell",
"description": "Turns off all rules that are unnecessary or might conflict with Prettier.",
"repository": "prettier/eslint-config-prettier",
"files": [
"bin/",
"@typescript-eslint.js",
"babel.js",
"flowtype.js",
"index.js",
"prettier.js",
"react.js",
"standard.js",
"unicorn.js",
"vue.js"
],
"bin": "bin/cli.js",
"keywords": [
"eslint",
"eslintconfig",
"prettier"
],
"private": true,
"scripts": {
"doctoc": "doctoc README.md && replace \"\\[\\[([\\w/-]+)\\](?:([^\\[\\]]+)\\[([\\w/-]+)\\])?\\]\" \"[\\$1\\$2\\$3]\" README.md",
"prettier": "prettier --write .",
Expand All @@ -33,9 +10,9 @@
"test:jest": "jest",
"test:cli-sanity": "node ./bin/cli.js index.js",
"test:cli-sanity-warning": "node ./bin/cli.js react.js ./bin/cli.js",
"test": "npm run test:lint && npm run test:jest && npm run test:cli-sanity && npm run test:cli-sanity-warning"
"test": "npm run test:lint && npm run test:jest && npm run test:cli-sanity && npm run test:cli-sanity-warning && npm run build",
"build": "node scripts/build.js"
},
"dependencies": {},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "4.8.2",
"@typescript-eslint/parser": "4.8.2",
Expand All @@ -56,8 +33,5 @@
"replace": "1.2.0",
"rimraf": "3.0.2",
"typescript": "4.1.2"
},
"peerDependencies": {
"eslint": ">=7.0.0"
}
}
45 changes: 45 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";

const fs = require("fs");
const path = require("path");

const DIR = path.join(__dirname, "..");
const BUILD = path.join(DIR, "build");

const READ_MORE =
"**[➡️ Full readme](https://github.com/prettier/eslint-config-prettier/)**";

const FILES_TO_COPY = [
{ src: "LICENSE" },
{ src: "package-real.json", dest: "package.json" },
{
src: "README.md",
transform: (content) => content.replace(/^---[^]*/m, READ_MORE),
},
...fs
.readdirSync(path.join(DIR, "bin"))
.filter((file) => !file.startsWith(".") && file.endsWith(".js"))
.map((file) => ({ src: path.join("bin", file) })),
...fs
.readdirSync(DIR)
.filter((file) => !file.startsWith(".") && file.endsWith(".js"))
.map((file) => ({ src: file })),
];

if (fs.existsSync(BUILD)) {
fs.rmdirSync(BUILD, { recursive: true });
}

fs.mkdirSync(BUILD);

for (const { src, dest = src, transform } of FILES_TO_COPY) {
if (transform) {
fs.writeFileSync(
path.join(BUILD, dest),
transform(fs.readFileSync(path.join(DIR, src), "utf8"))
);
} else {
fs.mkdirSync(path.dirname(path.join(BUILD, dest)), { recursive: true });
fs.copyFileSync(path.join(DIR, src), path.join(BUILD, dest));
}
}
9 changes: 0 additions & 9 deletions test/rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const childProcess = require("child_process");
const fs = require("fs");
const path = require("path");
const rimraf = require("rimraf");
const pkg = require("../package.json");
const eslintConfig = require("../.eslintrc");
const eslintConfigBase = require("../.eslintrc.base");

Expand Down Expand Up @@ -55,14 +54,6 @@ function createTestConfigDir() {
});
}

describe("all rule files are listed in package.json", () => {
ruleFiles.forEach((ruleFileName) => {
test(ruleFileName, () => {
expect(pkg.files).toContain(ruleFileName);
});
});
});

describe("all rule files have tests in test-lint/", () => {
ruleFiles.forEach((ruleFileName) => {
test(ruleFileName, () => {
Expand Down

0 comments on commit 4d08b84

Please sign in to comment.