esbuild#TransformResult TypeScript Examples

The following examples show how to use esbuild#TransformResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: transformJS.ts    From toy-vite with MIT License 5 votes vote down vote up
export function transformJS(opts: {
  appRoot: string;
  path: string;
  code: string;
}): TransformResult {
  const ext = extname(opts.path).slice(1);
  const ret = transformCode({
    code: opts.code,
    loader: ext as Loader,
  });

  let { code } = ret;
  code = code.replace(
    /\bimport(?!\s+type)(?:[\w*{}\n\r\t, ]+from\s*)?\s*("([^"]+)"|('[^']+'))/gm,
    (a, b, c) => {
      let from: string;
      if (c.charAt(0) === '.') {
        from = join(dirname(opts.path), c);
        const filePath = join(opts.appRoot, from);
        if (!existsSync(filePath)) {
          if (existsSync(`${filePath}.tsx`)) {
            from = `${from}.tsx`;
          } else if (existsSync(`${filePath}.ts`)) {
            from = `${from}.ts`;
          }
        }
        if (['svg'].includes(extname(from).slice(1))) {
          from = `${from}?import`;
        }
      } else {
        from = `/src/.cache/${c}.js`;
      }

      // TODO: support cjs
      // if (c === 'react') {
      //   a = 'import cjsImport0_react from "react"; ';
      //   // a = a.replace()
      // }

      return a.replace(b, `"${from}"`);
    },
  );

  return {
    ...ret,
    code,
  };
}