text: const BUILTIN_PREFIX = '@bext/';
  const bext = ({ builtins, meta, env }) => {
    const context = ['id', 'name', 'version', ...Object.keys(env || {})]
      .map(
        (prop) =>
          `export const ${prop} = decodeURIComponent('${encodeURIComponent(
            meta[prop] || env[prop],
          )}');`,
      )
      .join('\n');
    Object.assign(builtins, {
      context,
      entry: meta.source,
    });
    return {
      name: 'bext',
      resolveId(source) {
        if (
          source.startsWith(BUILTIN_PREFIX) &&
          source.replace(BUILTIN_PREFIX, '') in builtins
        ) {
          return source;
        }
        return null;
      },
      load(id) {
        if (id.startsWith(BUILTIN_PREFIX)) {
          const index = id.replace(BUILTIN_PREFIX, '');
          return builtins[index];
        }
        return null;
      },
      renderChunk(code) {
        const comments = [];
        this.parse(code, {
          onComment: comments,
        });
        const sortedComments = comments.sort((a, b) => b.start - a.start);
        for (const { start, end } of sortedComments) {
          code = code.slice(0, start) + code.slice(end);
        }
        return code;
      },
    };
  };
  
  const URL_REG = /^(\w+):\/\//;
  
  const urlFix = (id) =>
    id
      .replace('cdn.jsdelivr.net/npm', 'unpkg.com')
      .replace('cdn.jsdelivr.net/gh', 'cdn.staticaly.com/gh');
  
  const urlResolve = (type, path) => {
    switch (type) {
      case 'npm':
        return `https://cdn.jsdelivr.net/npm/${path}`;
      case 'gh':
        return `https://cdn.jsdelivr.net/gh/${path}`;
      default:
        return `${type}://${path}`;
    }
  };
  
  const buildCode = (path, content) => {
    if (path.endsWith('.css')) {
      return `import { addStyle, runOnce } from '@bext/util';
  runOnce(function(){addStyle(${JSON.stringify(content)})}, ${JSON.stringify(
        encodeURIComponent(path),
      )});`;
    }
    return content;
  };
  
  const url = () => {
    return {
      name: 'url',
      resolveId(source, importer) {
        if (source.startsWith(BUILTIN_PREFIX)) {
          return null;
        }
        if (URL_REG.test(source)) {
          return source;
        }
        if (URL_REG.test(importer)) {
          const url = new URL(importer);
          url.pathname = path.resolve(url.pathname, '..', source);
          return url.toString();
        }
        return null;
      },
      async load(id) {
        if (URL_REG.test(id)) {
          const url = new URL(id);
          const urlType = id.match(URL_REG)[1];
          const urlPath = id.replace(URL_REG, '');
  
          const response = await fetch(urlFix(urlResolve(urlType, urlPath)));
          if (!response.ok) {
            throw Error(response.statusText);
          }
          const content = await response.text();
  
          return buildCode(url.pathname, content);
        }
        return null;
      },
    };
  };
  
  export async function compile(payload, options) {
    const bundle = await rollup.rollup({
      input: '@bext/entry',
      plugins: [bext(payload), url()],
      context: 'window',
      ...options,
    });
    const { output } = await bundle.generate({
      format: 'iife',
      output: { strict: false },
    });
    return `(function(){
  ${output[0].code}
  })();`;
  }
  