Skip to content

Commit

Permalink
build(modules): update to handle cjs and esm modules (#14)
Browse files Browse the repository at this point in the history
* build(modules): update to handle cjs and esm modules

* chore(cleanup): remove extra comments
  • Loading branch information
joshuagraber committed Aug 24, 2023
1 parent 4918b8e commit 9d4ddf2
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 164 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
coverage
README.md
README.md
*.config.*
16 changes: 8 additions & 8 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
roots: ['<rootDir>/test'],
testMatch: ['**/__test__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
collectCoverage: true,
collectCoverageFrom: ['src/**/{!(b-tree),}.ts'],
export default {
roots: ['<rootDir>/test'],
testMatch: ['**/__test__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest'
},
collectCoverage: true,
collectCoverageFrom: ['src/**/{!(b-tree),}.ts']
};
21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
"name": "broccoli-and-carrots",
"version": "0.0.0",
"description": "Data structures and algorithms in TypeScript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "commonjs",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
},
"types": "dist/esm/index.d.ts",
"type": "module",
"license": "ISC",
"author": "Joshua D. Graber <joshua.d.graber@gmail.com>",
"repository": {
"type": "git",
"url": "https://github.com/joshuagraber/broccoli-and-carrots.git"
},
"scripts": {
"build": "tsc -p .",
"build": "npm run clean && tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && npm run build:package-file",
"build:package-file": "node ./scripts/setPackageType.js",
"clean": "rimraf dist",
"commit": "npx git-cz",
"lint": "eslint --ignore-path .gitignore src --ext .ts .",
Expand All @@ -21,7 +29,7 @@
"test": "jest --config jest.config.js --maxWorkers=1",
"test:ci": "test --watchAll=false"
},
"prepublish": "tsc",
"prepublish": "npm run build",
"devDependencies": {
"@types/jest": "^29.5.3",
"@types/lodash": "^4.14.197",
Expand All @@ -43,5 +51,8 @@
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"publishConfig": {
"directory": "dist"
}
}
22 changes: 22 additions & 0 deletions scripts/setPackageType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import packageFile from '../package.json' assert { type: 'json' };
import fs from 'fs';

packageFile.type = 'commonjs';
fs.writeFile('dist/cjs/package.json', JSON.stringify(packageFile), function writeJSON(err) {
if (err) {
console.log(err);
return;
}
console.log('writing to ' + 'dist/cjs/package.json:');
console.log(JSON.stringify(packageFile));
});

packageFile.type = 'module';
fs.writeFile('dist/esm/package.json', JSON.stringify(packageFile), function writeJSON(err) {
if (err) {
console.log(err);
return;
}
console.log('writing to ' + 'dist/esm/package.json: ');
console.log(JSON.stringify(packageFile));
});
8 changes: 2 additions & 6 deletions src/data-structures/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import LinkedList from './sequences/linked-list';
import { LinkedList } from './sequences/linked-list';

const StructuresCollection = {
LinkedList
};

export default StructuresCollection;
export { LinkedList };
4 changes: 2 additions & 2 deletions src/data-structures/sequences/linked-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,5 @@ class LinkedList<T> implements Iterable<T> {
}
}

export default LinkedList;
export type { List };
// export default LinkedList;
export { LinkedList, type List };
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default as StructuresCollection } from './data-structures'
export { LinkedList } from './data-structures';
2 changes: 1 addition & 1 deletion test/data-structures/sequences/linked-list.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LinkedList from '../../../src/data-structures/sequences/linked-list';
import { LinkedList } from '../../../src/data-structures/sequences/linked-list';

describe('Linked List - simple number', () => {
let list: LinkedList<number>;
Expand Down
26 changes: 26 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"declaration": true,
"esModuleInterop": true,
"inlineSourceMap": false,
"lib": ["esnext"],
"listEmittedFiles": false,
"listFiles": false,
"moduleResolution": "Node",
"noFallthroughCasesInSwitch": true,
"pretty": true,
"resolveJsonModule": true,
"rootDir": "src",
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"traceResolution": false,
"types": ["node", "jest"]
},
"compileOnSave": false,
"exclude": ["node_modules", "dist", "coverage"],
"include": ["src"]
}
8 changes: 8 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist/cjs",
"target": "es2015"
}
}
72 changes: 2 additions & 70 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,73 +1,5 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
// "lib": ["ESNext"] /* Specify library files to be included in the compilation. */,
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true /* Generates corresponding '.d.ts' file. */,
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
"sourceMap": true /* Generates corresponding '.map' file. */,
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "dist" /* Redirect output structure to the directory. */,
"rootDir": "./" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
// "composite": true /* Enable project compilation */,
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
"downlevelIteration": true /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */,
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
// "baseUrl": "./" /* Base directory to resolve non-absolute module names. */,
// "paths": {
// "*": ["./src/*"]
// } /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */,
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"extends": "./tsconfig.base.json",
"exclude": ["coverage", "dist", "node_modules"],
"include": ["src", "test", "release.config.ts"]
"include": ["src", "test", "scripts", "release.config.ts"]
}
75 changes: 5 additions & 70 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,73 +1,8 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
// "lib": ["ESNext"] /* Specify library files to be included in the compilation. */,
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true /* Generates corresponding '.d.ts' file. */,
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
"sourceMap": true /* Generates corresponding '.map' file. */,
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "dist" /* Redirect output structure to the directory. */,
"rootDir": "./" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
// "composite": true /* Enable project compilation */,
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
"downlevelIteration": true /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */,
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
// "baseUrl": "./" /* Base directory to resolve non-absolute module names. */,
// "paths": {
// "*": ["./src/*"]
// } /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */,
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"exclude": ["coverage", "dist", "node_modules"],
"include": ["src"]
"module": "esnext",
"outDir": "dist/esm",
"target": "esnext"
}
}

0 comments on commit 9d4ddf2

Please sign in to comment.