rollup#OutputAsset TypeScript Examples

The following examples show how to use rollup#OutputAsset. 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: report.ts    From reskript with MIT License 6 votes vote down vote up
drawBuildReport = (outputs: RollupOutput[]) => {
    const toAsset = (value: OutputAsset | OutputChunk): Asset => {
        return {
            name: value.fileName,
            size: Buffer.byteLength(value.type === 'asset' ? value.source : value.code),
            initial: value.type === 'chunk' && value.isEntry,
        };
    };
    const assets = outputs.flatMap(v => v.output.map(toAsset));
    drawAssetReport(assets);
}
Example #2
Source File: index.test.ts    From reskript with MIT License 6 votes vote down vote up
build = async (options: Pick<VirtualEntryOptions, 'favicon'>) => {
    const entries = [
        createEntry('index'),
        createEntry('about'),
    ];
    const entryOptions: VirtualEntryOptions = {
        ...options,
        entries,
        publicPath: '/',
        defaultEntry: entries[0],
        buildTarget: 'stable',
        customizeMiddleware: () => {},
    };
    const config: InlineConfig = {
        root: path.join(currentDirectory, 'fixtures'),
        logLevel: 'warn',
        build: {
            rollupOptions: {
                input: Object.entries(entries).reduce(
                    (input, [name, {entry}]) => Object.assign(input, {[name]: entry}),
                    {} as Record<string, string>
                ),
            },
        },
        plugins: [
            virtualEntry(entryOptions),
        ],
    };
    const bundle = await vite.build(config) as RollupOutput;
    return {
        assets: bundle.output.filter((v: any): v is OutputAsset => v.type === 'asset'),
        chunks: bundle.output.filter((v: any): v is OutputChunk => v.type === 'chunk'),
    };
}
Example #3
Source File: rollup-plugin-html.test.ts    From web with MIT License 5 votes vote down vote up
function getAsset(output: Output, name: string) {
  return output.find(o => o.name === name && o.type === 'asset') as OutputAsset & {
    source: string;
  };
}
Example #4
Source File: rollupPluginPolyfillsLoader.test.ts    From web with MIT License 5 votes vote down vote up
function getAsset(output: Output, name: string) {
  return output.find(o => o.fileName === name && o.type === 'asset') as OutputAsset & {
    source: string;
  };
}
Example #5
Source File: plugins.test.ts    From backstage with Apache License 2.0 4 votes vote down vote up
describe('forwardFileImports', () => {
  it('should be created', () => {
    const plugin = forwardFileImports({ include: /\.png$/ });
    expect(plugin.name).toBe('forward-file-imports');
  });

  it('should call through to original external option', async () => {
    const plugin = forwardFileImports({ include: /\.png$/ });
    const external = jest.fn((id: string) => id.endsWith('external'));

    const options = (await plugin.options?.call(context, { external }))!;
    if (typeof options.external !== 'function') {
      throw new Error('options.external is not a function');
    }

    expect(external).toHaveBeenCalledTimes(0);
    expect(options.external('./my-module', '/dev/src/index.ts', false)).toBe(
      false,
    );
    expect(external).toHaveBeenCalledTimes(1);
    expect(options.external('./my-external', '/dev/src/index.ts', false)).toBe(
      true,
    );
    expect(external).toHaveBeenCalledTimes(2);
    expect(options.external('./my-image.png', '/dev/src/index.ts', false)).toBe(
      true,
    );
    expect(external).toHaveBeenCalledTimes(3);
    expect(options.external('./my-image.png', '/dev/src/index.ts', true)).toBe(
      true,
    );
    expect(external).toHaveBeenCalledTimes(4);

    expect(() =>
      (options as any).external('./my-image.png', undefined, false),
    ).toThrow('Unknown importer of file module ./my-image.png');
  });

  it('should handle original external array', async () => {
    const plugin = forwardFileImports({ include: /\.png$/ });

    const options = (await plugin.options?.call(context, {
      external: ['my-external'],
    }))!;
    if (typeof options.external !== 'function') {
      throw new Error('options.external is not a function');
    }

    expect(options.external('my-module', '/dev/src/index.ts', false)).toBe(
      false,
    );
    expect(options.external('my-external', '/dev/src/index.ts', false)).toBe(
      true,
    );
    expect(options.external('my-image.png', '/dev/src/index.ts', false)).toBe(
      true,
    );
  });

  describe('with mock fs', () => {
    beforeEach(() => {
      mockFs({
        '/dev/src/my-module.ts': '',
        '/dev/src/dir/my-image.png': 'my-image',
      });
    });

    afterEach(() => {
      mockFs.restore();
    });

    it('should extract files', async () => {
      const plugin = forwardFileImports({ include: /\.png$/ });

      const options = (await plugin.options?.call(context, {}))!;
      if (typeof options.external !== 'function') {
        throw new Error('options.external is not a function');
      }

      expect(options.external('./my-module', '/dev/src/index.ts', false)).toBe(
        false,
      );
      expect(
        options.external('./my-image.png', '/dev/src/dir/index.ts', false),
      ).toBe(true);

      const outPath = '/dev/dist/dir/my-image.png';
      await expect(fs.pathExists(outPath)).resolves.toBe(false);

      await plugin.generateBundle?.call(
        context,
        { dir: '/dev/dist' } as NormalizedOutputOptions,
        {
          ['index.js']: {
            type: 'chunk',
            facadeModuleId: '/dev/src/index.ts',
          } as OutputChunk,
        },
        false, // isWrite = false -> no write
      );
      await expect(fs.pathExists(outPath)).resolves.toBe(false);

      await plugin.generateBundle?.call(
        context,
        { dir: '/dev/dist' } as NormalizedOutputOptions,
        {
          // output assets should not cause a write
          ['index.js']: { type: 'asset' } as OutputAsset,
          // missing facadeModuleId should not cause a write either
          ['index2.js']: { type: 'chunk' } as OutputChunk,
        },
        true,
      );
      await expect(fs.pathExists(outPath)).resolves.toBe(false);

      // output chunk + isWrite -> generate files
      await plugin.generateBundle?.call(
        context,
        { dir: '/dev/dist' } as NormalizedOutputOptions,
        {
          ['index.js']: {
            type: 'chunk',
            facadeModuleId: '/dev/src/index.ts',
          } as OutputChunk,
        },
        true,
      );
      await expect(fs.pathExists(outPath)).resolves.toBe(true);

      // should not break when triggering another write
      await plugin.generateBundle?.call(
        context,
        { file: '/dev/dist/my-output.js' } as NormalizedOutputOptions,
        {
          ['index.js']: {
            type: 'chunk',
            facadeModuleId: '/dev/src/index.ts',
          } as OutputChunk,
        },
        true,
      );
    });
  });
});