esbuild#BuildOptions TypeScript Examples

The following examples show how to use esbuild#BuildOptions. 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: dev.ts    From gdmod with MIT License 6 votes vote down vote up
/**
 * Builds a GDMod code bundle out of the main es-module.
 * @param {*} to
 * @param {*} config
 */
function buildBundle(to: string, config: BuildOptions = {}) {
  return build(
    merge({}, config, getDefaultBuildConfiguration(), {
      outfile: to,
      plugins: [
        globalExternals({
          "@gdmod/api": {
            varName: "GDAPI",
            namedExports: require("../GDAPI_Signature.json"),
          },
        }),
      ],
    })
  );
}
Example #2
Source File: dev.ts    From gdmod with MIT License 6 votes vote down vote up
getDefaultBuildConfiguration = (): BuildOptions => ({
  entryPoints: [getMain()],
  bundle: true,
  minify: true,
  format: "iife",
  platform: "browser",
  globalName: "Mod",
  banner: { js: "return (() => {" },
  footer: { js: "return Mod.default;\n})();\n" },
})
Example #3
Source File: index.test.ts    From esbuild-plugin-less with Do What The F*ck You Want To Public License 6 votes vote down vote up
buildLess = async ({ lessOptions }: { lessOptions?: Less.Options } = {}) => {
  const buildOptions: BuildOptions = {
    entryPoints: [path.resolve(__dirname, '../', 'example', 'index.ts')],
    bundle: true,
    write: false,
    minify: true,
    outdir: path.resolve(__dirname, 'output'),
    loader: {
      '.ts': 'ts',
    },
    plugins: [lessLoader(lessOptions)],
  };

  const { outputFiles } = await build(buildOptions);
  return outputFiles;
}
Example #4
Source File: index.test.ts    From esbuild-plugin-less with Do What The F*ck You Want To Public License 6 votes vote down vote up
buildLessWithOption = async ({
  lessOptions,
  loaderOptions,
}: { lessOptions?: Less.Options; loaderOptions?: LoaderOptions } = {}) => {
  const buildOptions: BuildOptions = {
    entryPoints: [path.resolve(__dirname, '../', 'example', 'index-custom-filter.ts')],
    bundle: true,
    write: false,
    minify: true,
    outdir: path.resolve(__dirname, 'output'),
    loader: {
      '.ts': 'ts',
    },
    plugins: [lessLoader(lessOptions, loaderOptions)],
  };

  const { outputFiles } = await build(buildOptions);
  return outputFiles;
}
Example #5
Source File: esbuild.ts    From vue-components-lib-seed with MIT License 6 votes vote down vote up
async function run(options?: BuildOptions) {
  await build({
    outdir: `${cwd()}/dist/es`,
    bundle: true,
    entryPoints: componentEntrys,
    plugins: [
      vue({
        sourceMap: false,
        style: {
          preprocessLang: 'styl',
          // preprocessOptions: {
          //   stylus: {
          //     additionalData: `@import '${process.cwd()}/src/styles/index.styl'`,
          //   },
          // },
        },
      }),
    ],
    loader: { '.png': 'dataurl' },
    external: [
      'vue',
      'my-lib/*',
      '@vue/*',
      '@better-scroll/*',
      'jpeg-js',
    ],
    format: 'esm',
    minify: false,
    ...options,
  })
}
Example #6
Source File: esbuild.ts    From vue-components-lib-seed with MIT License 6 votes vote down vote up
/**
 * @deprecated
 */
async function bundle(options?: BuildOptions) {
  await build({
    outfile: `${cwd()}/dist/es/my-lib.esm.js`,
    bundle: true,
    entryPoints: [`${cwd()}/src/packages/my-lib.ts`],
    plugins: [vue()],
    loader: { '.png': 'dataurl' },
    external: ['vue', 'my-lib/*', '@vue/*'],
    format: 'esm',
    minify: true,
    ...options,
  })
}
Example #7
Source File: cli.ts    From nota with MIT License 4 votes vote down vote up
cli = (
  externalOptions?: CliOptions
): ((extra: BuildOptions) => Promise<[BuildResult, BuildOptions]>) => {
  let options =
    externalOptions ||
    (program
      .option("-w, --watch", "Watch for changes and rebuild")
      .option("-g, --debug", "Do not minify and include source maps")
      .option("-t, --typescript", "Run typescript")
      .parse(process.argv)
      .opts() as CliOptions);

  let pkg = getManifest();
  let keys = (map?: IDependencyMap) => Object.keys(map || {});
  let pkgExternal = keys(pkg.dependencies).concat(keys(pkg.peerDependencies));

  return async (extra: BuildOptions = {}) => {
    let plugins = extra.plugins || [];
    let format = extra.format || "esm";

    let external = (format != "iife" ? pkgExternal : []).concat(extra.external || []);

    if (format == "esm") {
      plugins.push(esmExternalsPlugin({ externals: external }));
    }

    let loader: { [ext: string]: Loader } = {
      ".otf": "file",
      ".woff": "file",
      ".woff2": "file",
      ".ttf": "file",
      ".wasm": "file",
      ".bib": "text",
      ".png": "file",
      ".jpg": "file",
      ".jpeg": "file",
      ".gif": "file",
      ...(extra.loader || {}),
    };

    let outpaths: BuildOptions = {};
    if (extra.outdir) {
      outpaths.outdir = extra.outdir;
    } else if (extra.outfile) {
      outpaths.outfile = extra.outfile;
    } else {
      extra.outdir = "dist";
    }

    let debug = options.debug || options.watch !== undefined;
    let watch: WatchMode | undefined =
      typeof options.watch === "object"
        ? options.watch
        : options.watch
        ? {
            onRebuild() {
              log.info("Rebuilt.");
            },
          }
        : undefined;

    let entryPoints = extra.entryPoints;
    if (!entryPoints) {
      entryPoints = [(await fileExists("./lib/index.ts")) ? "./lib/index.ts" : "./lib/index.tsx"];
    }

    if (options.ts || _.some(entryPoints, (p: string) => path.extname(p).startsWith(".ts"))) {
      plugins.push(tscPlugin());
    }

    let opts = {
      ...extra,
      ...outpaths,
      entryPoints,
      watch,
      external,
      plugins,
      format,
      loader,
      bundle: extra.bundle !== undefined ? extra.bundle : true,
      minify: extra.minify !== undefined ? extra.minify : !debug,
      sourcemap: extra.sourcemap !== undefined ? extra.sourcemap : debug,
    };

    let start = _.now();
    let result = await esbuild.build(opts);
    log.info(`Built in ${((_.now() - start) / 1000).toFixed(2)}s.`);
    return [result, opts];
  };
}