Skip to content

Commit

Permalink
Support shorthand syntax for type assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 3, 2022
1 parent ca4d84c commit e1786e5
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/rollup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,10 @@ export type OptionsPaths = Record<string, string> | ((id: string) => string);

type GetExternalImportAssertions = (moduleInfo: ModuleInfo) => Record<string, string> | null | void;

type ExternalImportAssertionsOption = GetExternalImportAssertions | false;
type ExternalImportAssertionsOption =
| GetExternalImportAssertions
| false
| Record<string, string | null>;

export type InteropType = 'compat' | 'auto' | 'esModule' | 'default' | 'defaultOnly';

Expand Down
10 changes: 8 additions & 2 deletions src/utils/options/normalizeOutputOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from '../../rollup/types';
import { ensureArray } from '../ensureArray';
import { errInvalidExportOptionValue, errInvalidOption, error, warnDeprecation } from '../error';
import { resolve } from '../path';
import { extname, resolve } from '../path';
import { sanitizeFileName as defaultSanitizeFileName } from '../sanitizeFileName';
import { isValidUrl } from '../url';
import {
Expand Down Expand Up @@ -355,6 +355,8 @@ function getExports(
return configExports || 'auto';
}

const defaultAssertions = { '.json': 'json' };

function getExternalImportAssertions(
config: OutputOptions
): NormalizedOutputOptions['externalImportAssertions'] {
Expand All @@ -365,7 +367,11 @@ function getExternalImportAssertions(
if (configExternalImportAssertions === false) {
return () => null;
}
return ({ id }) => (id.endsWith('.json') ? { type: 'json' } : null);
const typeAssertions = configExternalImportAssertions || defaultAssertions;
return ({ id }) => {
const type = typeAssertions[extname(id)];
return type ? { type } : null;
};
}

const getGeneratedCode = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
description: 'allows to configure the "type" assertion via a shorthand',
options: {
external: true,
output: {
name: 'bundle',
externalImportAssertions: {
'.json': null,
'.css': 'css',
'.foo': 'special',
'': 'empty'
}
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'external.json';
import 'external.css' assert { type: 'css' };
import 'external.foo' assert { type: 'special' };
import 'external' assert { type: 'empty' };
import 'external.ignored';

import('external.json');
import('external.css', { assert: { type: 'css' } });
import('external.foo', { assert: { type: 'special' } });
import('external', { assert: { type: 'empty' } });
import('external.ignored');
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'external.json';
import 'external.css';
import 'external.foo';
import 'external';
import 'external.ignored';

import('external.json');
import('external.css');
import('external.foo');
import('external');
import('external.ignored');
4 changes: 2 additions & 2 deletions test/misc/optionList.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e1786e5

Please sign in to comment.