esbuild#PluginBuild TypeScript Examples

The following examples show how to use esbuild#PluginBuild. 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: externalize-deps.ts    From fect with MIT License 6 votes vote down vote up
externalizeDeps = {
  name: 'externalize-deps',
  setup(build: PluginBuild) {
    build.onResolve({ filter: /.*/ }, (args) => {
      const id = args.path
      if (id[0] !== '.' && !path.isAbsolute(id)) {
        return {
          external: true
        }
      }
    })
  }
}
Example #2
Source File: index.test.ts    From esbuild-plugin-less with Do What The F*ck You Want To Public License 4 votes vote down vote up
describe('less-loader', () => {
  it('exported module', () => {
    expect(lessLoader).toBeDefined();
  });

  it('onResolve with watch mode', () => {
    const plugin = lessLoader();

    let onResolveCallback = null;
    const build = {
      initialOptions: {
        watch: true,
      },
      onResolve: (opts, callback) => {
        onResolveCallback = callback;
      },
      onStart: jest.fn(),
      onEnd: jest.fn(),
      onLoad: jest.fn(),
    } as unknown as PluginBuild;

    plugin.setup(build);

    const path = '/path';
    const onResolveResult = onResolveCallback({ resolveDir: '/', path });

    expect(onResolveResult).toMatchObject({
      path,
      watchFiles: [path],
    });
  });

  it('builds successful', async () => {
    const primaryColor = '#ff0000';
    const result = await buildLess({
      lessOptions: {
        globalVars: {
          primaryColor,
        },
      },
    });

    expect(result.length).toStrictEqual(2);

    expect(path.extname(result[0].path)).toStrictEqual('.js');
    expect(path.extname(result[1].path)).toStrictEqual('.css');

    // Result has compiled .less
    const css = result[1].text;
    expect(css).toMatch(`background:${primaryColor}`);
    expect(css).toMatch(`body article{width:100px}`);
    expect(css).toMatch(`body article:first-child{width:200px}`);
  });

  it('builds successful custom filter', async () => {
    const primaryColor = '#ff0000';
    const result = await buildLessWithOption({
      lessOptions: {
        globalVars: {
          primaryColor,
        },
      },
      loaderOptions: {
        filter: /\._?less_?$/,
      },
    });

    expect(result.length).toStrictEqual(2);

    expect(path.extname(result[0].path)).toStrictEqual('.js');
    expect(path.extname(result[1].path)).toStrictEqual('.css');

    // Result has compiled .less
    const css = result[1].text;
    expect(css).toMatch(`background:${primaryColor}`);
    expect(css).toMatch(`body article{width:100px}`);
    expect(css).toMatch(`body article:first-child{width:200px}`);
  });

  it('builds imported .less files', async () => {
    const result = await buildLess({
      lessOptions: {
        globalVars: {
          primaryColor: '#ff0000',
        },
      },
    });

    const css = result[1].text;

    expect(css).toMatch(`.style-2-less`);
    expect(css).toMatch(`.style-3-less`);
  });

  it('builds imported ._less_ files', async () => {
    const result = await buildLessWithOption({
      lessOptions: {
        globalVars: {
          primaryColor: '#ff0000',
        },
      },
      loaderOptions: {
        filter: /\._?less_?$/,
      },
    });

    const css = result[1].text;

    expect(css).toMatch(`.style-2-less`);
    expect(css).toMatch(`.style-3-less`);
  });

  it('builds imported .css files', async () => {
    const result = await buildLess({
      lessOptions: {
        globalVars: {
          primaryColor: '#ff0000',
        },
      },
    });

    const css = result[1].text;
    expect(css).toMatch(`#style-4-css`);
    expect(css).toMatch(`#style-5-css`);
  });

  it('catches less error', async () => {
    await expect(
      buildLess({
        lessOptions: {
          globalVars: {},
        },
      }),
    ).rejects.toThrow();
  });
});