diff --git a/.DS_Store b/.DS_Store index 918b049..cec08e0 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/README.md b/README.md index 300b79e..72b190c 100644 --- a/README.md +++ b/README.md @@ -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: [] diff --git a/bin/index.js b/bin/index.js index c32b308..0de12c0 100755 --- a/bin/index.js +++ b/bin/index.js @@ -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') { @@ -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'), }, { diff --git a/src/config.ts b/src/config.ts index b7b0c0a..ff8f76e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -18,7 +18,7 @@ export const config: Config = { */ extensions: ['.js', '.ts', '.jsx', '.tsx'], - assetExtensions: ['.stories.ts'], + assetExtensions: [], /** * defines string of file patterns to process diff --git a/src/constants.ts b/src/constants.ts index 3e1fc0c..d696655 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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', +]; diff --git a/src/modules/Bundler.ts b/src/modules/Bundler.ts index 50eedb3..5de5861 100644 --- a/src/modules/Bundler.ts +++ b/src/modules/Bundler.ts @@ -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; @@ -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); @@ -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; diff --git a/src/modules/utils.ts b/src/modules/utils.ts index eab2cab..3ccb2eb 100644 --- a/src/modules/utils.ts +++ b/src/modules/utils.ts @@ -46,7 +46,6 @@ export const getRollupPlugins = (opts: { babelPresets, } = opts; const internalNodeModulesDir = getClosestPackageDir(__dirname); - const isDistBuild = format === 'umd' || format === 'iife'; return [ @@ -57,7 +56,6 @@ export const getRollupPlugins = (opts: { commonjs({ include: 'node_modules/**', }), - babel({ // we do not want to use any local babelrc babelrc: false, @@ -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