Skip to content

Commit

Permalink
feat: add base asset extensions, throw error if unsupported build for…
Browse files Browse the repository at this point in the history
…mat is specified
  • Loading branch information
Harrison Ifeanyichukwu authored and Harrison Ifeanyichukwu committed Oct 15, 2023
1 parent ee9caf5 commit dbd0526
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module.exports = createConfig({
* define list of file extensions to be considered as asset files.
* Asset files are copied over to the build directories.
*
* example, .json, .png, .jpg, .jpeg, etc
* default values are .json, .png, .jpg, .jpeg, .gif, .svg, etc
*/
assetExtensions: []

Expand Down
38 changes: 32 additions & 6 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,36 @@ const parseBoolean = (value) => {
return value === '' || value === true || value === 'true' || value === '1';
};

const parseList = (value) =>
value && typeof value === 'string'
? value.split(/\s*,\s*/gim).filter(Boolean)
: [];
/**
*
* @param {string} fieldName the field name
* @param {string[]} allowedValues - array of allowed values
* @returns
*/
const createListParser = (fieldName, allowedValues = []) => {
return (value) => {
const values =
value && typeof value === 'string'
? value.split(/\s*,\s*/gim).filter(Boolean)
: [];

if (!allowedValues.length) {
return values;
}

const allowed = new Set(allowedValues);

values.forEach((value) => {
if (!allowed.has(value)) {
throw new Error(
`Value for ${fieldName} must be one of ${allowedValues}. ${value} is not an acceptable value`
);
}
});

return values;
};
};

const assignDefaultBooleanValue = (flags, property, defaultValue) => {
if (typeof flags[property] === 'undefined') {
Expand All @@ -28,13 +54,13 @@ args.options([
{
name: 'formats',
description: 'defines desired build formats',
init: parseList,
init: createListParser('formats', ['cjs', 'es', 'iife', 'umd']),
},

{
name: 'envs',
description: 'build envs',
init: parseList,
init: createListParser('envs'),
},

{
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const config: Config = {
*/
extensions: ['.js', '.ts', '.jsx', '.tsx'],

assetExtensions: ['.stories.ts'],
assetExtensions: [],

/**
* defines string of file patterns to process
Expand Down
11 changes: 8 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export const REGEX_FIELDS = ['include', 'exclude'];

export const PACKAGE_NAME = '@teclone/rollup-all';
export const BASE_ASSET_EXTENSIONS = [
'.json',
'.png',
'.jpeg',
'.jpg',
'.gif',
'.svg',
];
6 changes: 5 additions & 1 deletion src/modules/Bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { copy } from '../utils/copy';
import { camelCase } from '../utils/camelCase';
import { rimrafSync } from 'rimraf';
import { forEach } from '../utils/forEach';
import { BASE_ASSET_EXTENSIONS } from '../constants';

const allExternal = () => true;

Expand All @@ -37,6 +38,9 @@ class Bundler {
this.entryPath = process.cwd();

const config = copy({}, defaultConfig, givenConfig);
config.assetExtensions = BASE_ASSET_EXTENSIONS.concat(
config.assetExtensions || []
).map((ext) => ext.toLowerCase());

// resolve entry file
config.src = path.resolve(this.entryPath, config.src);
Expand Down Expand Up @@ -112,7 +116,7 @@ class Bundler {
baseName = fileNameSegments[0];

if (fileNameSegments.length > 1) {
extName = '.' + fileNameSegments.slice(1).join('.');
extName = '.' + fileNameSegments.slice(1).join('.').toLowerCase();
}

const dirName = resolvedPath;
Expand Down
4 changes: 1 addition & 3 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export const getRollupPlugins = (opts: {
babelPresets,
} = opts;
const internalNodeModulesDir = getClosestPackageDir(__dirname);

const isDistBuild = format === 'umd' || format === 'iife';

return [
Expand All @@ -57,7 +56,6 @@ export const getRollupPlugins = (opts: {
commonjs({
include: 'node_modules/**',
}),

babel({
// we do not want to use any local babelrc
babelrc: false,
Expand Down Expand Up @@ -127,7 +125,7 @@ export const getRollupPlugins = (opts: {
: null,
].filter(Boolean),

// runtime for library builds and bundled for bundled builds
// runtime for library builds are bundled for bundled builds
babelHelpers: isDistBuild ? 'bundled' : 'runtime',

// do not process node module files, it is believed that all files in the node modules directory has been properly worked on
Expand Down

0 comments on commit dbd0526

Please sign in to comment.