@linaria/shaker
Advanced tools
Comparing version 3.0.0-beta.12 to 3.0.0-beta.13
@@ -6,2 +6,13 @@ # Change Log | ||
# [3.0.0-beta.13](https://github.com/callstack/linaria/compare/v3.0.0-beta.12...v3.0.0-beta.13) (2021-09-13) | ||
### Bug Fixes | ||
* **shaker:** partial support for ts compiled code (fixes [#820](https://github.com/callstack/linaria/issues/820)) ([#836](https://github.com/callstack/linaria/issues/836)) ([ec8ee68](https://github.com/callstack/linaria/commit/ec8ee684b6e90ead46295733ccd8cfefe4eaa04d)) | ||
# [3.0.0-beta.12](https://github.com/callstack/linaria/compare/v3.0.0-beta.11...v3.0.0-beta.12) (2021-08-31) | ||
@@ -8,0 +19,0 @@ |
@@ -82,2 +82,9 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
addExport(name, node) { | ||
const existed = this.exports.get(name); | ||
if (existed) { | ||
// Sometimes export can be defined more than once and in that case we have to keep all export statements | ||
this.addEdge(node, existed); | ||
} | ||
this.exports.set(name, node); | ||
@@ -84,0 +91,0 @@ } |
@@ -20,7 +20,11 @@ import { types as t } from '@babel/core'; | ||
isExportsIdentifier(node) { | ||
if (t.isIdentifier(node) && this.scope.getDeclaration(node) === ScopeManager.globalExportsIdentifier) { | ||
return true; | ||
if (t.isIdentifier(node)) { | ||
return this.scope.getDeclaration(node) === ScopeManager.globalExportsIdentifier; | ||
} | ||
return t.isMemberExpression(node) && t.isIdentifier(node.property) && node.property.name === 'exports' && t.isIdentifier(node.object) && this.scope.getDeclaration(node.object) === ScopeManager.globalModuleIdentifier; | ||
if (t.isMemberExpression(node)) { | ||
return t.isIdentifier(node.property) && node.property.name === 'exports' && t.isIdentifier(node.object) && this.scope.getDeclaration(node.object) === ScopeManager.globalModuleIdentifier; | ||
} | ||
return false; | ||
} | ||
@@ -43,2 +47,11 @@ | ||
} | ||
isTSExporterCall(node) { | ||
if (!t.isCallExpression(node) || node.arguments.length !== 2) { | ||
return false; | ||
} // FIXME: more precisely check | ||
return !(!t.isIdentifier(node.callee) || node.callee.name !== 'exporter'); | ||
} | ||
/* | ||
@@ -123,2 +136,6 @@ * Implements a default behaviour for AST-nodes: | ||
} | ||
} else if (this.isTSExporterCall(node)) { | ||
const [name, identifier] = node.arguments; | ||
this.graph.addExport(name.value, node); | ||
this.graph.addEdge(node, identifier); | ||
} | ||
@@ -125,0 +142,0 @@ |
@@ -26,2 +26,41 @@ import { types as t } from '@babel/core'; | ||
function isTSLib(node, scope) { | ||
if (!t.isIdentifier(node)) { | ||
return false; | ||
} | ||
const declaration = scope.getDeclaration(node); | ||
return t.isIdentifier(declaration) && declaration.name === 'tslib_1'; | ||
} | ||
function isTSReexport(node, scope) { | ||
if (!t.isCallExpression(node)) { | ||
return false; | ||
} | ||
const { | ||
callee, | ||
arguments: [, exportsIdentifier] | ||
} = node; | ||
if (!t.isIdentifier(exportsIdentifier) || exportsIdentifier.name !== 'exports' || scope.getDeclaration(exportsIdentifier) !== ScopeManager.globalExportsIdentifier) { | ||
return false; | ||
} | ||
if (!t.isMemberExpression(callee)) { | ||
return false; | ||
} | ||
const { | ||
object, | ||
property | ||
} = callee; | ||
if (!t.isIdentifier(property) || property.name !== '__exportStar') { | ||
return false; | ||
} | ||
return isTSLib(object, scope); | ||
} | ||
function findWildcardReexportStatement(node, identifierName, graph) { | ||
@@ -112,2 +151,6 @@ if (!t.isIdentifier(node.callee) || node.callee.name !== 'require') return null; | ||
} | ||
if (t.isFunctionDeclaration(node) && node.id) { | ||
this.graph.addEdge(node, node.id); | ||
} | ||
}, | ||
@@ -171,2 +214,7 @@ | ||
CatchClause(node) { | ||
this.baseVisit(node); | ||
this.graph.addEdge(node, node.body); | ||
}, | ||
IfStatement(node) { | ||
@@ -409,3 +457,15 @@ this.baseVisit(node); | ||
if (!declared) { | ||
// This is a standalone `require` | ||
// Is it a ts reexport? | ||
// tslib_1.__exportStar(require("./Async"), exports); | ||
if (parent && isTSReexport(parent, this.scope)) { | ||
if (!this.graph.imports.has(source)) { | ||
this.graph.imports.set(source, []); | ||
} | ||
this.graph.addEdge(parent.callee.object, parent); | ||
this.graph.reexports.push(parent.callee.object); | ||
this.graph.importTypes.set(source, 'reexport'); | ||
} // This is a standalone `require` | ||
return; | ||
@@ -412,0 +472,0 @@ } // Define all declared variables as external dependencies. |
@@ -42,2 +42,3 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
const globalIdentifiers = new Set(['exports', 'module']); | ||
export default class ScopeManager { | ||
@@ -108,3 +109,3 @@ get global() { | ||
if (this.global.has(idName)) { | ||
if (this.global.has(idName) && !globalIdentifiers.has(idName)) { | ||
// It's probably a declaration of a previous referenced identifier | ||
@@ -111,0 +112,0 @@ // Let's use naïve implementation of hoisting |
@@ -90,2 +90,9 @@ "use strict"; | ||
addExport(name, node) { | ||
const existed = this.exports.get(name); | ||
if (existed) { | ||
// Sometimes export can be defined more than once and in that case we have to keep all export statements | ||
this.addEdge(node, existed); | ||
} | ||
this.exports.set(name, node); | ||
@@ -92,0 +99,0 @@ } |
@@ -33,7 +33,11 @@ "use strict"; | ||
isExportsIdentifier(node) { | ||
if (_core.types.isIdentifier(node) && this.scope.getDeclaration(node) === _scope.default.globalExportsIdentifier) { | ||
return true; | ||
if (_core.types.isIdentifier(node)) { | ||
return this.scope.getDeclaration(node) === _scope.default.globalExportsIdentifier; | ||
} | ||
return _core.types.isMemberExpression(node) && _core.types.isIdentifier(node.property) && node.property.name === 'exports' && _core.types.isIdentifier(node.object) && this.scope.getDeclaration(node.object) === _scope.default.globalModuleIdentifier; | ||
if (_core.types.isMemberExpression(node)) { | ||
return _core.types.isIdentifier(node.property) && node.property.name === 'exports' && _core.types.isIdentifier(node.object) && this.scope.getDeclaration(node.object) === _scope.default.globalModuleIdentifier; | ||
} | ||
return false; | ||
} | ||
@@ -56,2 +60,11 @@ | ||
} | ||
isTSExporterCall(node) { | ||
if (!_core.types.isCallExpression(node) || node.arguments.length !== 2) { | ||
return false; | ||
} // FIXME: more precisely check | ||
return !(!_core.types.isIdentifier(node.callee) || node.callee.name !== 'exporter'); | ||
} | ||
/* | ||
@@ -138,2 +151,6 @@ * Implements a default behaviour for AST-nodes: | ||
} | ||
} else if (this.isTSExporterCall(node)) { | ||
const [name, identifier] = node.arguments; | ||
this.graph.addExport(name.value, node); | ||
this.graph.addEdge(node, identifier); | ||
} | ||
@@ -140,0 +157,0 @@ |
@@ -37,2 +37,41 @@ "use strict"; | ||
function isTSLib(node, scope) { | ||
if (!_core.types.isIdentifier(node)) { | ||
return false; | ||
} | ||
const declaration = scope.getDeclaration(node); | ||
return _core.types.isIdentifier(declaration) && declaration.name === 'tslib_1'; | ||
} | ||
function isTSReexport(node, scope) { | ||
if (!_core.types.isCallExpression(node)) { | ||
return false; | ||
} | ||
const { | ||
callee, | ||
arguments: [, exportsIdentifier] | ||
} = node; | ||
if (!_core.types.isIdentifier(exportsIdentifier) || exportsIdentifier.name !== 'exports' || scope.getDeclaration(exportsIdentifier) !== _scope.default.globalExportsIdentifier) { | ||
return false; | ||
} | ||
if (!_core.types.isMemberExpression(callee)) { | ||
return false; | ||
} | ||
const { | ||
object, | ||
property | ||
} = callee; | ||
if (!_core.types.isIdentifier(property) || property.name !== '__exportStar') { | ||
return false; | ||
} | ||
return isTSLib(object, scope); | ||
} | ||
function findWildcardReexportStatement(node, identifierName, graph) { | ||
@@ -125,2 +164,6 @@ var _program$body$find; | ||
} | ||
if (_core.types.isFunctionDeclaration(node) && node.id) { | ||
this.graph.addEdge(node, node.id); | ||
} | ||
}, | ||
@@ -184,2 +227,7 @@ | ||
CatchClause(node) { | ||
this.baseVisit(node); | ||
this.graph.addEdge(node, node.body); | ||
}, | ||
IfStatement(node) { | ||
@@ -422,3 +470,15 @@ this.baseVisit(node); | ||
if (!declared) { | ||
// This is a standalone `require` | ||
// Is it a ts reexport? | ||
// tslib_1.__exportStar(require("./Async"), exports); | ||
if (parent && isTSReexport(parent, this.scope)) { | ||
if (!this.graph.imports.has(source)) { | ||
this.graph.imports.set(source, []); | ||
} | ||
this.graph.addEdge(parent.callee.object, parent); | ||
this.graph.reexports.push(parent.callee.object); | ||
this.graph.importTypes.set(source, 'reexport'); | ||
} // This is a standalone `require` | ||
return; | ||
@@ -425,0 +485,0 @@ } // Define all declared variables as external dependencies. |
@@ -59,2 +59,4 @@ "use strict"; | ||
const globalIdentifiers = new Set(['exports', 'module']); | ||
class ScopeManager { | ||
@@ -125,3 +127,3 @@ get global() { | ||
if (this.global.has(idName)) { | ||
if (this.global.has(idName) && !globalIdentifiers.has(idName)) { | ||
// It's probably a declaration of a previous referenced identifier | ||
@@ -128,0 +130,0 @@ // Let's use naïve implementation of hoisting |
{ | ||
"name": "@linaria/shaker", | ||
"version": "3.0.0-beta.12", | ||
"version": "3.0.0-beta.13", | ||
"publishConfig": { | ||
@@ -50,5 +50,5 @@ "access": "public" | ||
"@babel/preset-env": ">=7", | ||
"@linaria/babel-preset": "^3.0.0-beta.12", | ||
"@linaria/babel-preset": "^3.0.0-beta.13", | ||
"@linaria/logger": "^3.0.0-beta.3", | ||
"@linaria/preeval": "^3.0.0-beta.12", | ||
"@linaria/preeval": "^3.0.0-beta.13", | ||
"babel-plugin-transform-react-remove-prop-types": "^0.4.24", | ||
@@ -60,3 +60,3 @@ "ts-invariant": "^0.9.0" | ||
}, | ||
"gitHead": "6a062e1f9cfcccae1448414fdb3fa35f11097468" | ||
"gitHead": "c49df269ebbbc956d43720b007a92f9f865662c5" | ||
} |
@@ -10,2 +10,3 @@ import type { Node, VisitorKeys } from '@babel/types'; | ||
private isExportsAssigment; | ||
private isTSExporterCall; | ||
baseVisit<TNode extends Node>(node: TNode, ignoreDeps?: boolean): void; | ||
@@ -12,0 +13,0 @@ visit<TNode extends Node, TParent extends Node>(node: TNode, parent: TParent | null, parentKey: VisitorKeys[TParent['type']] | null, listIdx?: number | null): VisitorAction; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
326064
2533