How to use @rollup/pluginutils - 10 common examples

To help you get started, we’ve selected a few @rollup/pluginutils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rollup / plugins / packages / commonjs / src / transform.js View on Github external
sourceMap,
  allowDynamicRequire,
  astCache
) {
  const ast = astCache || tryParse(parse, code, id);

  const magicString = new MagicString(code);

  const required = {};
  // Because objects have no guaranteed ordering, yet we need it,
  // we need to keep track of the order in a array
  const sources = [];

  let uid = 0;

  let scope = attachScopes(ast, 'scope');
  const uses = { module: false, exports: false, global: false, require: false };

  let lexicalDepth = 0;
  let programDepth = 0;

  const globals = new Set();

  // TODO technically wrong since globals isn't populated yet, but ¯\_(ツ)_/¯
  const HELPERS_NAME = deconflict(scope, globals, 'commonjsHelpers');

  const namedExports = {};

  // TODO handle transpiled modules
  let shouldWrap = /__esModule/.test(code);

  function isRequireStatement(node) {
github rollup / plugins / packages / node-resolve / src / index.js View on Github external
// eslint-disable-next-line no-prototype-builtins
      browserMap.hasOwnProperty(pkg.main)
    ) {
      packageInfo.resolvedEntryPoint = browserMap[pkg.main];
      packageInfo.browserMappedMain = true;
    } else {
      // index.node is technically a valid default entrypoint as well...
      packageInfo.resolvedEntryPoint = resolve(pkgRoot, pkg.main || 'index.js');
      packageInfo.browserMappedMain = false;
    }

    const packageSideEffects = pkg.sideEffects;
    if (typeof packageSideEffects === 'boolean') {
      internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
    } else if (Array.isArray(packageSideEffects)) {
      internalPackageInfo.hasModuleSideEffects = createFilter(packageSideEffects, null, {
        resolve: pkgRoot
      });
    }

    packageInfoCache.set(pkgPath, internalPackageInfo);
    return internalPackageInfo;
  }
github rollup / plugins / packages / typescript / src / index.ts View on Github external
export default function typescript(options: RollupTypescriptOptions = {}): Plugin {
  const opts = Object.assign({}, options);

  const filter = createFilter(
    opts.include || ['*.ts+(|x)', '**/*.ts+(|x)'],
    opts.exclude || ['*.d.ts', '**/*.d.ts']
  );
  delete opts.include;
  delete opts.exclude;

  // Allow users to override the TypeScript version used for transpilation and tslib version used for helpers.
  const ts: typeof import('typescript') = opts.typescript || defaultTs;
  delete opts.typescript;

  const tslib = getTsLibCode(opts);
  delete opts.tslib;

  // Load options from `tsconfig.json` unless explicitly asked not to.
  const tsConfig =
    opts.tsconfig === false ? { compilerOptions: {} } : readTsConfig(ts, opts.tsconfig);
github rollup / plugins / packages / json / src / index.js View on Github external
export default function json(options = {}) {
  const filter = createFilter(options.include, options.exclude);
  const indent = 'indent' in options ? options.indent : '\t';

  return {
    name: 'json',

    // eslint-disable-next-line no-shadow
    transform(json, id) {
      if (id.slice(-5) !== '.json' || !filter(id)) return null;

      return {
        code: dataToEsm(JSON.parse(json), {
          preferConst: options.preferConst,
          compact: options.compact,
          namedExports: options.namedExports,
          indent
        }),
github rollup / plugins / packages / image / src / index.js View on Github external
export default function image(opts = {}) {
  const options = Object.assign({}, defaults, opts);
  const filter = createFilter(options.include, options.exclude);

  return {
    name: 'image',

    load(id) {
      if (!filter(id)) {
        return null;
      }

      const mime = mimeTypes[extname(id)];
      if (!mime) {
        // not an image
        return null;
      }

      const format = mime === mimeTypes['.svg'] ? 'utf-8' : 'base64';
github rollup / plugins / packages / inject / src / index.js View on Github external
export default function inject(options) {
  if (!options) throw new Error('Missing options');

  const filter = createFilter(options.include, options.exclude);

  let { modules } = options;

  if (!modules) {
    modules = Object.assign({}, options);
    delete modules.include;
    delete modules.exclude;
    delete modules.sourceMap;
    delete modules.sourcemap;
  }

  const modulesMap = new Map(Object.entries(modules));

  // Fix paths on Windows
  if (sep !== '/') {
    modulesMap.forEach((mod, key) => {
github rollup / plugins / packages / inject / src / index.js View on Github external
function handleReference(node, name, keypath) {
        let mod = modulesMap.get(keypath);
        if (mod && !imports.has(name) && !scope.contains(name)) {
          if (typeof mod === 'string') mod = [mod, 'default'];

          // prevent module from importing itself
          if (mod[0] === id) return false;

          const hash = `${keypath}:${mod[0]}:${mod[1]}`;

          const importLocalName =
            name === keypath ? name : makeLegalIdentifier(`$inject_${keypath}`);

          if (!newImports.has(hash)) {
            if (mod[1] === '*') {
              newImports.set(hash, `import * as ${importLocalName} from '${mod[0]}';`);
            } else {
              newImports.set(hash, `import { ${mod[1]} as ${importLocalName} } from '${mod[0]}';`);
            }
          }

          if (name !== keypath) {
            magicString.overwrite(node.start, node.end, importLocalName, {
              storeName: true
            });
          }

          return true;
github rollup / plugins / packages / inject / src / index.js View on Github external
}
      if (!ast) {
        return null;
      }

      const imports = new Set();
      ast.body.forEach((node) => {
        if (node.type === 'ImportDeclaration') {
          node.specifiers.forEach((specifier) => {
            imports.add(specifier.local.name);
          });
        }
      });

      // analyse scopes
      let scope = attachScopes(ast, 'scope');

      const magicString = new MagicString(code);

      const newImports = new Map();

      function handleReference(node, name, keypath) {
        let mod = modulesMap.get(keypath);
        if (mod && !imports.has(name) && !scope.contains(name)) {
          if (typeof mod === 'string') mod = [mod, 'default'];

          // prevent module from importing itself
          if (mod[0] === id) return false;

          const hash = `${keypath}:${mod[0]}:${mod[1]}`;

          const importLocalName =
github rollup / plugins / packages / babel / src / index.js View on Github external
const { customOptions, pluginOptionsWithOverrides } = getOptionsWithOverrides(
      pluginOptions,
      overrides
    );

    const {
      exclude,
      extensions,
      babelHelpers,
      include,
      skipPreflightCheck,
      ...babelOptions
    } = unpackInputPluginOptions(pluginOptionsWithOverrides);

    const extensionRegExp = new RegExp(`(${extensions.map(escapeRegExpCharacters).join('|')})$`);
    const includeExcludeFilter = createFilter(include, exclude);
    const filter = (id) => extensionRegExp.test(id) && includeExcludeFilter(id);

    return {
      name: 'babel',

      resolveId(id) {
        if (id !== HELPERS) {
          return null;
        }
        return id;
      },

      load(id) {
        if (id !== HELPERS) {
          return null;
        }
github rollup / plugins / packages / strip / src / index.js View on Github external
export default function strip(options = {}) {
  const include = options.include || '**/*.js';
  const { exclude } = options;
  const filter = createFilter(include, exclude);
  const sourceMap = options.sourceMap !== false;

  const removeDebuggerStatements = options.debugger !== false;
  const functions = (options.functions || ['console.*', 'assert.*']).map((keypath) =>
    keypath.replace(/\./g, '\\.').replace(/\*/g, '\\w+')
  );

  const labels = options.labels || [];

  const firstpass = new RegExp(`\\b(?:${functions.join('|')}|debugger)\\b`);
  const pattern = new RegExp(`^(?:${functions.join('|')})$`);

  return {
    name: 'strip',

    transform(code, id) {

@rollup/pluginutils

A set of utility functions commonly used by Rollup plugins

MIT
Latest version published 5 months ago

Package Health Score

97 / 100
Full package analysis