Skip to content

Commit

Permalink
add script to remove empty imports/requires (#735)
Browse files Browse the repository at this point in the history
* add script to remove empty imports/requires

* add changeset

* remove the minified entries because of CI
  • Loading branch information
JoviDeCroock authored Apr 22, 2020
1 parent e6997f4 commit dacf49a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-radios-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Add a babel-plugin that removes empty imports from the final build output.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"check": "tsc --noEmit",
"lint": "eslint --ext=js,jsx,ts,tsx .",
"build": "rollup -c ../../scripts/rollup/config.js",
"postbuild": "node ./scripts/remove-empty-imports.js",
"prepare": "../../scripts/prepare/index.js",
"prepublishOnly": "run-s clean build"
},
Expand Down
38 changes: 38 additions & 0 deletions packages/core/scripts/remove-empty-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { transformSync: transform } = require('@babel/core');
const fs = require('fs');

const nonMinified = [
`${process.cwd()}/dist/urql-core.js`,
`${process.cwd()}/dist/urql-core.mjs`,
`${process.cwd()}/dist/urql-core-internal.js`,
`${process.cwd()}/dist/urql-core-internal.mjs`,
];

const removeEmptyImports = (babel) => {
const { types: t } = babel;

return {
visitor: {
ImportDeclaration(path) {
const { specifiers } = path.node
if (specifiers.length === 0) {
path.remove();
}
},
CallExpression(path) {
if (
t.isIdentifier(path.node.callee) &&
path.node.callee.name === 'require' &&
path.parent.type !== 'VariableDeclarator'
) {
path.remove();
}
}
}
};
}

nonMinified.forEach(loc => {
const { code: newCode } = transform(fs.readFileSync(loc), { plugins: [removeEmptyImports], babelrc: false });
fs.writeFileSync(loc, newCode);
});

0 comments on commit dacf49a

Please sign in to comment.