Skip to content

Commit

Permalink
feature: add support for defining files to be considered as asset files
Browse files Browse the repository at this point in the history
  • Loading branch information
teclone committed Aug 30, 2023
1 parent 269052a commit 8df9e64
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
Binary file modified .DS_Store
Binary file not shown.
15 changes: 15 additions & 0 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export interface Config {
*/
extensions?: string[];

/**
* extensions of files to copy over as assets
*/
assetExtensions?: string[];

/**
* defines file patterns to process for all builds
*/
Expand Down Expand Up @@ -134,6 +139,16 @@ export interface Module {
* boolean indicating if file is a build file
*/
isBuildFile: boolean;

/**
* boolean indicating if file is an asset file
*/
isAssetFile: boolean;

/**
* boolean indicating if file is a type definition file
*/
isTypeDefinitionFile?: boolean;
}

export interface ModuleFiles {
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const config: Config = {
*/
extensions: ['.js', '.ts', '.jsx', '.tsx'],

assetExtensions: ['.stories.ts'],

/**
* defines string of file patterns to process
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/index.spec.ts → src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createConfig } from '../src';
import { Config } from '../src/@types';
import { createConfig } from '.';
import { Config } from './@types';

describe('config', function () {
it(`returns the given options`, function () {
Expand Down
34 changes: 22 additions & 12 deletions src/modules/Bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ class Bundler {

/**
* parses all files into module targets
* @param modules
* @param resolvedPath
* @param entryFile
* @param moduleName
* @param currentRelativeDir
* @param extensions
*/
private getModules(
modules: Module[],
Expand All @@ -96,7 +90,7 @@ class Bundler {

for (let i = 0; i < files.length; i++) {
const fileName = files[i];
if (['.DS_Store', '.git', 'node_modules'].includes(fileName)) {
if (fileName.startsWith('.') || ['node_modules'].includes(fileName)) {
continue;
}

Expand All @@ -111,11 +105,19 @@ class Bundler {
)
);
} else {
const dirName = path.dirname(filePath);
const extName = path.extname(fileName);
const baseName = path.basename(fileName, extName);
let baseName = '';
let extName = '';

const isTypeDefinitionFile = baseName.endsWith('.d');
const fileNameSegments = fileName.split('.');
baseName = fileNameSegments[0];

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

const dirName = resolvedPath;

const isTypeDefinitionFile = extName === '.d.ts';

const filePathWithoutExtension = path.join(dirName, baseName);

Expand All @@ -127,6 +129,11 @@ class Bundler {
(this.entryFile === filePath ||
this.entryFile === filePathWithoutExtension);

const isAssetFile =
!isTypeDefinitionFile &&
!isBuildFile &&
this.config.assetExtensions.includes(extName);

modules.push({
id: modules.length + 1,
ext: extName,
Expand All @@ -138,6 +145,8 @@ class Bundler {
: camelCase(baseName),

isBuildFile,
isAssetFile,
isTypeDefinitionFile,
baseName,
fileName,
location: filePath,
Expand All @@ -164,6 +173,7 @@ class Bundler {

for (let i = 0; i < modules.length; i++) {
const current = modules[i];

// if file is excluded, return
const oldRelativePath = path.join(
current.locationRelativeToSrc,
Expand All @@ -187,7 +197,7 @@ class Bundler {

if (current.isBuildFile) {
result.buildFiles.push(current);
} else {
} else if (current.isAssetFile || current.isTypeDefinitionFile) {
result.copyFiles.push(current);
}
}
Expand Down

0 comments on commit 8df9e64

Please sign in to comment.