rollup#InputOption TypeScript Examples

The following examples show how to use rollup#InputOption. 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: build.ts    From vite-plugin-ssr with MIT License 6 votes vote down vote up
function normalizeRollupInput(input?: InputOption): Record<string, string> {
  if (!input) {
    return {}
  }
  // Usually `input` is an oject, but the user can set it as a `string` or `string[]`
  if (typeof input === 'string') {
    input = [input]
  }
  if (Array.isArray(input)) {
    return Object.fromEntries(input.map((input) => [input, input]))
  }
  assert(isObject(input))
  return input
}
Example #2
Source File: build.ts    From telefunc with MIT License 6 votes vote down vote up
function normalizeRollupInput(input?: InputOption): Record<string, string> {
  if (!input) {
    return {}
  }
  /*
  if (typeof input === "string") {
    return { [input]: input };
  }
  if (Array.isArray(input)) {
    return Object.fromEntries(input.map((i) => [i, i]));
  }
  */
  assert(isObject(input))
  return input
}
Example #3
Source File: getInputData.ts    From web with MIT License 5 votes vote down vote up
export function getInputData(
  pluginOptions: RollupPluginHTMLOptions,
  rollupInput?: InputOption,
): InputData[] {
  const {
    rootDir = process.cwd(),
    flattenOutput,
    extractAssets = true,
    absolutePathPrefix,
    exclude: ignore,
  } = pluginOptions;
  const allInputs = normalizeInputOptions(pluginOptions, rollupInput);

  const result: InputData[] = [];
  for (const input of allInputs) {
    if (typeof input.html === 'string') {
      const name = input.name ?? 'index.html';
      const data = createInputData({
        name,
        html: input.html,
        rootDir,
        extractAssets,
        absolutePathPrefix,
      });
      result.push(data);
    } else if (typeof input.path === 'string') {
      const filePaths = resolveGlob(input.path, { cwd: rootDir, ignore });
      if (filePaths.length === 0) {
        throw new Error(
          `Could not find any HTML files for pattern: ${input.path}, resolved relative to ${rootDir}`,
        );
      }

      for (const filePath of filePaths) {
        const name = input.name ?? getName(filePath, rootDir, flattenOutput);
        const html = fs.readFileSync(filePath, 'utf-8');
        const data = createInputData({
          name,
          html,
          rootDir,
          filePath,
          extractAssets,
          absolutePathPrefix,
        });
        result.push(data);
      }
    } else {
      throw createError('An input must specify either a path or html.');
    }
  }

  for (const input of result) {
    if (result.filter(r => r.name === input.name).length !== 1) {
      throw createError(
        `Found multiple HTML inputs configured with the same name, ` +
          'or with no name which defaults to index.html. Set a unique name on the' +
          'input option.',
      );
    }
  }

  return result;
}
Example #4
Source File: normalizeInputOptions.ts    From web with MIT License 5 votes vote down vote up
export function normalizeInputOptions(
  pluginOptions: RollupPluginHTMLOptions,
  rollupInput?: InputOption,
): InputHTMLOptions[] {
  if (pluginOptions.input == null) {
    if (rollupInput == null) {
      throw createError('Missing input option in rollup or in HTML plugin options.');
    }

    if (typeof rollupInput === 'string') {
      return [{ path: rollupInput }];
    }

    if (Array.isArray(rollupInput)) {
      return rollupInput.map(path => ({ path }));
    }

    if (typeof rollupInput === 'object') {
      return Object.entries(rollupInput).map(([name, path]) => ({ name, path }));
    }

    throw createError('Unable to parse rollup input option');
  }

  if (Array.isArray(pluginOptions.input)) {
    return pluginOptions.input.map(input => {
      if (typeof input === 'string') {
        return { path: input };
      }
      return input;
    });
  }

  if (typeof pluginOptions.input === 'object') {
    return [pluginOptions.input];
  }

  if (typeof pluginOptions.input === 'string') {
    return [{ path: pluginOptions.input }];
  }

  throw createError('Unable to parse html plugin input option');
}