-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add script to remove empty imports/requires (#735)
* add script to remove empty imports/requires * add changeset * remove the minified entries because of CI
- Loading branch information
1 parent
e6997f4
commit dacf49a
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |