How to use the attachScopes function from rollup-pluginutils
Find comprehensive JavaScript rollup-pluginutils.attachScopes code examples handpicked from public code repositorys.
rollup-pluginutils.attachScopes is a function in the Rollup.js plugin-utils library that assigns variable scopes to a given AST (Abstract Syntax Tree) in order to track variable references and avoid collisions.
541 542 543 544 545 546 547 548 549 550
// Parser errors just skip analysis return this.callback(null, code, map); } } let scope = attachScopes(ast, 'scope'); let transformed = false; const importMetaUrl = pathToFileURL(id).href;
+ 33 other calls in file
GitHub: natwas00/node_modules
386 387 388 389 390 391 392 393 394 395
(isImport ? imports : deps).add(computed.then); if ('else' in computed && typeof computed.else === 'string') (isImport ? imports : deps).add(computed.else); } } let scope = rollup_pluginutils_1.attachScopes(ast, 'scope'); if (isAst(ast)) { wrappers_1.handleWrappers(ast); await special_cases_1.default({ id, ast, emitDependency: path => deps.add(path), emitAsset: path => assets.add(path), emitAssetDirectory, job }); }
+ 33 other calls in file
How does rollup-pluginutils.attachScopes work?
In Rollup.js, rollup-pluginutils.attachScopes is a function that assigns variable scopes to a given AST (Abstract Syntax Tree). This is important because Rollup.js needs to track variable references and avoid collisions when it performs tree-shaking, which is a technique for removing unused code from a JavaScript bundle. When you call rollup-pluginutils.attachScopes with an AST, it traverses the tree and assigns scopes to all the variables it encounters, based on the structure of the code. It does this by creating a new scope object for each block of code, and assigning variables to the scope object based on whether they are declared in that block or in a parent block. For example, if you have a function inside a module, rollup-pluginutils.attachScopes will create a new scope object for the function and assign variables declared inside the function to that scope. It will also assign variables declared outside the function to the scope of the module. Once rollup-pluginutils.attachScopes has assigned scopes to all the variables in the AST, it returns the AST with the scopes attached. This allows Rollup.js to track variable references and avoid collisions during tree-shaking, which can result in smaller, more efficient JavaScript bundles. Overall, rollup-pluginutils.attachScopes is an important function in Rollup.js that enables tree-shaking and other optimizations by assigning variable scopes to an AST.
77 78 79 80 81 82 83 84 85 86
} catch (err) { err.message += ' in ' + id; throw err; } // analyse scopes var scope = rollupPluginutils.attachScopes(ast, 'scope'); var imports = {}; ast.body.forEach(function (node) { if (node.type === 'ImportDeclaration') {
+ 27 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
const { parse } = require("@babel/parser"); const { attachScopes } = require("rollup-pluginutils"); function myPlugin() { return { transform(code, id) { // Parse the code into an AST using @babel/parser const ast = parse(code, { sourceType: "module", }); // Attach scopes to the AST using rollup-pluginutils.attachScopes attachScopes(ast, "scope"); // Log the modified AST to the console console.log(JSON.stringify(ast, null, 2)); // Return the original code return code; }, }; } module.exports = myPlugin;
In this example, we're using rollup-pluginutils.attachScopes to assign scopes to an AST in a Rollup.js plugin. We first parse the code into an AST using @babel/parser, and then call attachScopes on the AST, passing the AST and the string 'scope' as arguments. attachScopes then assigns scopes to all the variables in the AST, based on the structure of the code. We log the modified AST to the console using JSON.stringify, and then return the original code from the transform function. When you run this code on a JavaScript module, you'll see that the modified AST is printed to the console with scopes assigned to all the variables in the module. Note that the scope object is assigned the string 'scope' as its key, and that variables declared in inner blocks of code have their own scope objects with unique keys.
rollup-pluginutils.createFilter is the most popular function in rollup-pluginutils (190 examples)