Socket
Socket
Sign inDemoInstall

@linaria/shaker

Package Overview
Dependencies
Maintainers
4
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@linaria/shaker - npm Package Compare versions

Comparing version 4.2.4 to 4.2.5

11

esm/index.js

@@ -1,2 +0,1 @@

import { transformSync } from '@babel/core';
import { buildOptions, loadBabelOptions } from '@linaria/utils';

@@ -6,3 +5,2 @@ import { hasShakerMetadata } from './plugins/shaker-plugin';

const configCache = new Map();
const getShakerConfig = only => {

@@ -12,7 +10,5 @@ const sortedOnly = [...(only ?? [])];

const key = sortedOnly.join('\0');
if (configCache.has(key)) {
return configCache.get(key);
}
const config = {

@@ -34,17 +30,14 @@ ast: true,

};
const shaker = (filename, options, text, only, babel) => {
const transformOptions = loadBabelOptions(babel, filename, buildOptions(options?.babelOptions, getShakerConfig(only)));
const transformed = transformSync(text, { ...transformOptions,
const transformed = babel.transformSync(text, {
...transformOptions,
filename
});
if (!transformed || !hasShakerMetadata(transformed.metadata)) {
throw new Error(`${filename} has no shaker metadata`);
}
return [transformed.code ?? '', transformed.metadata.__linariaShaker.imports];
};
export default shaker;
//# sourceMappingURL=index.js.map

@@ -5,3 +5,2 @@ import { join } from 'path';

import shakerPlugin, { hasShakerMetadata } from '../shaker-plugin';
const keep = only => code => {

@@ -18,7 +17,5 @@ const filename = join(__dirname, 'source.js');

});
if (!transformed || !transformed.code || !hasShakerMetadata(transformed.metadata)) {
throw new Error(`${filename} has no shaker metadata`);
}
return {

@@ -29,3 +26,2 @@ code: transformed.code,

};
describe('shaker', () => {

@@ -32,0 +28,0 @@ it('should remove unused export', () => {

64

esm/plugins/shaker-plugin.js
import { createCustomDebug } from '@linaria/logger';
import { collectExportsAndImports, getFileIdx, isRemoved, removeWithRelated, sideEffectImport, reference, findActionForNode, dereference, mutate } from '@linaria/utils';
export const hasShakerMetadata = metadata => metadata !== undefined && '__linariaShaker' in metadata;
function getBindingForExport(exportPath) {

@@ -9,8 +8,5 @@ if (exportPath.isIdentifier()) {

}
const variableDeclarator = exportPath.findParent(p => p.isVariableDeclarator());
if (variableDeclarator) {
const id = variableDeclarator.get('id');
if (id.isIdentifier()) {

@@ -20,6 +16,4 @@ return variableDeclarator.scope.getBinding(id.node.name);

}
if (exportPath.isAssignmentExpression()) {
const left = exportPath.get('left');
if (left.isIdentifier()) {

@@ -29,10 +23,7 @@ return exportPath.scope.getBinding(left.node.name);

}
return undefined;
}
const withoutRemoved = items => items.filter(({
local
}) => !isRemoved(local));
function rearrangeExports({

@@ -47,11 +38,10 @@ types: t

}
const uid = rootScope.generateUid(name); // Define variable in the beginning
const uid = rootScope.generateUid(name);
// Define variable in the beginning
const [declaration] = root.unshiftContainer('body', [t.variableDeclaration('var', [t.variableDeclarator(t.identifier(uid))])]);
rootScope.registerDeclaration(declaration); // Replace every reference with defined variable
rootScope.registerDeclaration(declaration);
// Replace every reference with defined variable
refs.forEach(ref => {
const [replaced] = ref.replaceWith(t.identifier(uid));
if (replaced.isBindingIdentifier()) {

@@ -62,8 +52,10 @@ rootScope.registerConstantViolation(replaced);

}
}); // Assign defined variable to the export
});
// Assign defined variable to the export
const [pushed] = root.pushContainer('body', [t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier('exports'), t.identifier(name)), t.identifier(uid)))]);
const local = pushed.get('expression.right');
reference(local);
rearranged = rearranged.map(exp => exp.exported === name ? { ...exp,
rearranged = rearranged.map(exp => exp.exported === name ? {
...exp,
local

@@ -74,3 +66,2 @@ } : exp);

}
export default function shakerPlugin(babel, {

@@ -83,3 +74,2 @@ keepSideEffects = false,

name: '@linaria/shaker',
pre(file) {

@@ -91,5 +81,6 @@ this.filename = file.opts.filename;

const sideEffectImports = collected.imports.filter(sideEffectImport);
log('import-and-exports', [`imports: ${collected.imports.length} (side-effects: ${sideEffectImports.length})`, `exports: ${collected.exports.length}`, `reexports: ${collected.reexports.length}`].join(', ')); // We cannot just throw out exports if they are referred in the code
log('import-and-exports', [`imports: ${collected.imports.length} (side-effects: ${sideEffectImports.length})`, `exports: ${collected.exports.length}`, `reexports: ${collected.reexports.length}`].join(', '));
// We cannot just throw out exports if they are referred in the code
// Let's dome some replacements
const exports = rearrangeExports(babel, file.path, collected.exportRefs, collected.exports);

@@ -101,3 +92,2 @@ collected.exports.forEach(({

const left = local.get('left');
if (left.isIdentifier()) {

@@ -110,3 +100,2 @@ // For some reason babel does not mark id in AssignmentExpression as a reference

});
if (onlyExports.length === 1 && onlyExports[0] === '__linariaPreval' && !exports.find(i => i.exported === '__linariaPreval')) {

@@ -123,8 +112,12 @@ // Fast-lane: if only __linariaPreval is requested, and it's not exported,

}
if (!onlyExports.includes('*')) {
const aliveExports = new Set();
const importNames = collected.imports.map(({
imported
}) => imported);
exports.forEach(exp => {
if (onlyExports.includes(exp.exported)) {
aliveExports.add(exp);
} else if (importNames.includes(exp.local.node.name || '')) {
aliveExports.add(exp);
}

@@ -138,3 +131,2 @@ });

const isAllExportsFound = aliveExports.size === onlyExports.length;
if (!isAllExportsFound && ifUnknownExport !== 'ignore') {

@@ -144,3 +136,2 @@ if (ifUnknownExport === 'error') {

}
if (ifUnknownExport === 'reexport-all') {

@@ -159,3 +150,2 @@ // If there are unknown exports, we have keep alive all re-exports.

}
if (ifUnknownExport === 'skip-shaking') {

@@ -168,5 +158,3 @@ this.imports = collected.imports;

}
const forDeleting = [...exports, ...collected.reexports].filter(exp => !aliveExports.has(exp)).map(exp => exp.local);
if (!keepSideEffects && sideEffectImports.length > 0) {

@@ -176,10 +164,8 @@ // Remove all imports that don't import something explicitly

}
const deleted = new Set();
const dereferenced = [];
let changed = true;
while (changed && deleted.size < forDeleting.length) {
changed = false; // eslint-disable-next-line no-restricted-syntax
changed = false;
// eslint-disable-next-line no-restricted-syntax
for (const path of forDeleting) {

@@ -190,3 +176,2 @@ const binding = getBindingForExport(path);

const outerReferences = (binding?.referencePaths || []).filter(ref => ref !== parent && !parent?.isAncestor(ref));
if (outerReferences.length > 0 && path.isIdentifier()) {

@@ -197,3 +182,2 @@ // Temporary deref it in order to simplify further checks.

}
if (!deleted.has(path) && (!binding || outerReferences.length === 0)) {

@@ -203,3 +187,2 @@ if (action) {

if (isRemoved(p)) return;
if (action[0] === 'remove') {

@@ -214,3 +197,2 @@ p.remove();

}
deleted.add(path);

@@ -221,3 +203,2 @@ changed = true;

}
dereferenced.forEach(path => {

@@ -230,3 +211,2 @@ // If path is still alive, we need to reference it back

}
this.imports = withoutRemoved(collected.imports);

@@ -236,5 +216,3 @@ this.exports = withoutRemoved(exports);

},
visitor: {},
post(file) {

@@ -250,3 +228,2 @@ const log = createCustomDebug('shaker', getFileIdx(file.opts.filename));

}
if (imported) {

@@ -263,7 +240,7 @@ imports.get(source).push(imported);

}
imports.get(source).push(imported);
});
log('end', `remaining imports: %O`, imports); // eslint-disable-next-line no-param-reassign
log('end', `remaining imports: %O`, imports);
// eslint-disable-next-line no-param-reassign
file.metadata.__linariaShaker = {

@@ -273,5 +250,4 @@ imports

}
};
}
//# sourceMappingURL=shaker-plugin.js.map

@@ -13,15 +13,7 @@ "use strict";

});
var _core = require("@babel/core");
var _utils = require("@linaria/utils");
var _shakerPlugin = _interopRequireWildcard(require("./plugins/shaker-plugin"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const configCache = new Map();
const getShakerConfig = only => {

@@ -31,7 +23,5 @@ const sortedOnly = [...(only !== null && only !== void 0 ? only : [])];

const key = sortedOnly.join('\0');
if (configCache.has(key)) {
return configCache.get(key);
}
const config = {

@@ -53,20 +43,16 @@ ast: true,

};
const shaker = (filename, options, text, only, babel) => {
var _transformed$code;
const transformOptions = (0, _utils.loadBabelOptions)(babel, filename, (0, _utils.buildOptions)(options === null || options === void 0 ? void 0 : options.babelOptions, getShakerConfig(only)));
const transformed = (0, _core.transformSync)(text, { ...transformOptions,
const transformed = babel.transformSync(text, {
...transformOptions,
filename
});
if (!transformed || !(0, _shakerPlugin.hasShakerMetadata)(transformed.metadata)) {
throw new Error(`${filename} has no shaker metadata`);
}
return [(_transformed$code = transformed.code) !== null && _transformed$code !== void 0 ? _transformed$code : '', transformed.metadata.__linariaShaker.imports];
};
var _default = shaker;
exports.default = _default;
//# sourceMappingURL=index.js.map
"use strict";
var _path = require("path");
var _core = require("@babel/core");
var _dedent = _interopRequireDefault(require("dedent"));
var _shakerPlugin = _interopRequireWildcard(require("../shaker-plugin"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const keep = only => code => {

@@ -28,7 +21,5 @@ const filename = (0, _path.join)(__dirname, 'source.js');

});
if (!transformed || !transformed.code || !(0, _shakerPlugin.hasShakerMetadata)(transformed.metadata)) {
throw new Error(`${filename} has no shaker metadata`);
}
return {

@@ -39,3 +30,2 @@ code: transformed.code,

};
describe('shaker', () => {

@@ -42,0 +32,0 @@ it('should remove unused export', () => {

@@ -8,11 +8,6 @@ "use strict";

exports.hasShakerMetadata = void 0;
var _logger = require("@linaria/logger");
var _utils = require("@linaria/utils");
const hasShakerMetadata = metadata => metadata !== undefined && '__linariaShaker' in metadata;
exports.hasShakerMetadata = hasShakerMetadata;
function getBindingForExport(exportPath) {

@@ -22,8 +17,5 @@ if (exportPath.isIdentifier()) {

}
const variableDeclarator = exportPath.findParent(p => p.isVariableDeclarator());
if (variableDeclarator) {
const id = variableDeclarator.get('id');
if (id.isIdentifier()) {

@@ -33,6 +25,4 @@ return variableDeclarator.scope.getBinding(id.node.name);

}
if (exportPath.isAssignmentExpression()) {
const left = exportPath.get('left');
if (left.isIdentifier()) {

@@ -42,10 +32,7 @@ return exportPath.scope.getBinding(left.node.name);

}
return undefined;
}
const withoutRemoved = items => items.filter(({
local
}) => !(0, _utils.isRemoved)(local));
function rearrangeExports({

@@ -60,11 +47,10 @@ types: t

}
const uid = rootScope.generateUid(name); // Define variable in the beginning
const uid = rootScope.generateUid(name);
// Define variable in the beginning
const [declaration] = root.unshiftContainer('body', [t.variableDeclaration('var', [t.variableDeclarator(t.identifier(uid))])]);
rootScope.registerDeclaration(declaration); // Replace every reference with defined variable
rootScope.registerDeclaration(declaration);
// Replace every reference with defined variable
refs.forEach(ref => {
const [replaced] = ref.replaceWith(t.identifier(uid));
if (replaced.isBindingIdentifier()) {

@@ -75,8 +61,10 @@ rootScope.registerConstantViolation(replaced);

}
}); // Assign defined variable to the export
});
// Assign defined variable to the export
const [pushed] = root.pushContainer('body', [t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier('exports'), t.identifier(name)), t.identifier(uid)))]);
const local = pushed.get('expression.right');
(0, _utils.reference)(local);
rearranged = rearranged.map(exp => exp.exported === name ? { ...exp,
rearranged = rearranged.map(exp => exp.exported === name ? {
...exp,
local

@@ -87,3 +75,2 @@ } : exp);

}
function shakerPlugin(babel, {

@@ -96,3 +83,2 @@ keepSideEffects = false,

name: '@linaria/shaker',
pre(file) {

@@ -104,5 +90,6 @@ this.filename = file.opts.filename;

const sideEffectImports = collected.imports.filter(_utils.sideEffectImport);
log('import-and-exports', [`imports: ${collected.imports.length} (side-effects: ${sideEffectImports.length})`, `exports: ${collected.exports.length}`, `reexports: ${collected.reexports.length}`].join(', ')); // We cannot just throw out exports if they are referred in the code
log('import-and-exports', [`imports: ${collected.imports.length} (side-effects: ${sideEffectImports.length})`, `exports: ${collected.exports.length}`, `reexports: ${collected.reexports.length}`].join(', '));
// We cannot just throw out exports if they are referred in the code
// Let's dome some replacements
const exports = rearrangeExports(babel, file.path, collected.exportRefs, collected.exports);

@@ -114,3 +101,2 @@ collected.exports.forEach(({

const left = local.get('left');
if (left.isIdentifier()) {

@@ -123,3 +109,2 @@ // For some reason babel does not mark id in AssignmentExpression as a reference

});
if (onlyExports.length === 1 && onlyExports[0] === '__linariaPreval' && !exports.find(i => i.exported === '__linariaPreval')) {

@@ -136,8 +121,12 @@ // Fast-lane: if only __linariaPreval is requested, and it's not exported,

}
if (!onlyExports.includes('*')) {
const aliveExports = new Set();
const importNames = collected.imports.map(({
imported
}) => imported);
exports.forEach(exp => {
if (onlyExports.includes(exp.exported)) {
aliveExports.add(exp);
} else if (importNames.includes(exp.local.node.name || '')) {
aliveExports.add(exp);
}

@@ -151,3 +140,2 @@ });

const isAllExportsFound = aliveExports.size === onlyExports.length;
if (!isAllExportsFound && ifUnknownExport !== 'ignore') {

@@ -157,3 +145,2 @@ if (ifUnknownExport === 'error') {

}
if (ifUnknownExport === 'reexport-all') {

@@ -172,3 +159,2 @@ // If there are unknown exports, we have keep alive all re-exports.

}
if (ifUnknownExport === 'skip-shaking') {

@@ -181,5 +167,3 @@ this.imports = collected.imports;

}
const forDeleting = [...exports, ...collected.reexports].filter(exp => !aliveExports.has(exp)).map(exp => exp.local);
if (!keepSideEffects && sideEffectImports.length > 0) {

@@ -189,10 +173,8 @@ // Remove all imports that don't import something explicitly

}
const deleted = new Set();
const dereferenced = [];
let changed = true;
while (changed && deleted.size < forDeleting.length) {
changed = false; // eslint-disable-next-line no-restricted-syntax
changed = false;
// eslint-disable-next-line no-restricted-syntax
for (const path of forDeleting) {

@@ -203,3 +185,2 @@ const binding = getBindingForExport(path);

const outerReferences = ((binding === null || binding === void 0 ? void 0 : binding.referencePaths) || []).filter(ref => ref !== parent && !(parent !== null && parent !== void 0 && parent.isAncestor(ref)));
if (outerReferences.length > 0 && path.isIdentifier()) {

@@ -210,3 +191,2 @@ // Temporary deref it in order to simplify further checks.

}
if (!deleted.has(path) && (!binding || outerReferences.length === 0)) {

@@ -216,3 +196,2 @@ if (action) {

if ((0, _utils.isRemoved)(p)) return;
if (action[0] === 'remove') {

@@ -227,3 +206,2 @@ p.remove();

}
deleted.add(path);

@@ -234,3 +212,2 @@ changed = true;

}
dereferenced.forEach(path => {

@@ -243,3 +220,2 @@ // If path is still alive, we need to reference it back

}
this.imports = withoutRemoved(collected.imports);

@@ -249,5 +225,3 @@ this.exports = withoutRemoved(exports);

},
visitor: {},
post(file) {

@@ -263,3 +237,2 @@ const log = (0, _logger.createCustomDebug)('shaker', (0, _utils.getFileIdx)(file.opts.filename));

}
if (imported) {

@@ -276,7 +249,7 @@ imports.get(source).push(imported);

}
imports.get(source).push(imported);
});
log('end', `remaining imports: %O`, imports); // eslint-disable-next-line no-param-reassign
log('end', `remaining imports: %O`, imports);
// eslint-disable-next-line no-param-reassign
file.metadata.__linariaShaker = {

@@ -286,5 +259,4 @@ imports

}
};
}
//# sourceMappingURL=shaker-plugin.js.map
{
"name": "@linaria/shaker",
"description": "Blazing fast zero-runtime CSS in JS library",
"version": "4.2.4",
"version": "4.2.5",
"bugs": "https://github.com/callstack/linaria/issues",
"dependencies": {
"@babel/core": "^7.18.9",
"@babel/generator": "^7.18.9",
"@babel/plugin-transform-modules-commonjs": "^7.18.2",
"@babel/plugin-transform-runtime": ">=7",
"@babel/plugin-transform-template-literals": ">=7",
"@babel/preset-env": ">=7",
"@babel/core": "^7.20.2",
"@babel/generator": "^7.20.4",
"@babel/plugin-transform-modules-commonjs": "^7.19.6",
"@babel/plugin-transform-runtime": "^7.19.6",
"@babel/plugin-transform-template-literals": "^7.18.9",
"@babel/preset-env": "^7.20.2",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"ts-invariant": "^0.10.3",
"@linaria/logger": "^4.0.0",
"@linaria/utils": "^4.2.4",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"ts-invariant": "^0.10.3"
"@linaria/utils": "^4.2.5"
},
"devDependencies": {
"@babel/types": "^7.18.9",
"@babel/types": "^7.20.2",
"@types/babel__core": "^7.1.19",

@@ -58,3 +58,3 @@ "@types/babel__generator": "^7.6.4",

"scripts": {
"build": "npm run build:lib && npm run build:esm && npm run build:declarations",
"build": "pnpm build:lib && pnpm build:esm && pnpm build:declarations",
"build:declarations": "tsc --emitDeclarationOnly --outDir types",

@@ -65,4 +65,4 @@ "build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",

"typecheck": "tsc --noEmit --composite false",
"watch": "npm run build --watch"
"watch": "pnpm build:lib --watch & pnpm build:esm --watch & pnpm build:declarations --watch"
}
}

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc