Skip to content

Commit

Permalink
feat: tweak distribution bundle and build process
Browse files Browse the repository at this point in the history
Now files are not compiled into one, each implementation is distributed
as separate file.

BREAKING CHANGE:
now distributed version contained in separate folders: `cjs`, `esm` and
`esnext`

To include esnext version of package you now have to do
`import { cnb } from 'cnbuilder/esnext'`
  • Loading branch information
xobotyi committed Jul 11, 2021
1 parent ce70c3e commit 09ee7ce
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 86 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
package-lock.json
node_modules
coverage
dist
cjs
esm
esnext
15 changes: 4 additions & 11 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
.github
coverage
tests
benchmark
src
.idea
.gitignore
.travis.yml
CODE_OF_CONDUCT.md
tsconfig.json
rollup.config.js
*
!/cjs
!/esm
!/esnext
2 changes: 1 addition & 1 deletion benchmark/src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const classnames = require('classnames');
// classcat somewhy uses ES6 export for browser
const clsx = require('clsx');
const cnbuilderNpm = require('cnbuilder/dist').cnb;
const cnbuilderLocal = require('../../dist').cnb;
const cnbuilderLocal = require('../../esnext').cnb;
const runTests = require('./run');

const libraries = {
Expand Down
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "cnbuilder",
"description": "Yet another classname string builder (the fastest one)",
"version": "2.7.1",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"esnext": "dist/index.esnext.js",
"types": "dist/index.d.ts",
"main": "cjs/index.js",
"module": "esm/index.esm.js",
"esnext": "esnext/index.esnext.js",
"types": "cjs/index.d.ts",
"sideEffects": false,
"files": [
"dist"
Expand Down Expand Up @@ -43,23 +43,27 @@
"@types/jest": "^26.0.0",
"@xobotyi/eslint-config": "^2.2.0",
"@xobotyi/preset-typescript": "^1.0.0",
"concurrently": "^6.2.0",
"eslint": "^7.0.0",
"husky": "4.3.8",
"jest": "^27.0.6",
"lint-staged": "^11.0.0",
"prettier": "^2.0.5",
"rimraf": "^3.0.2",
"rollup": "^2.10.5",
"rollup-plugin-terser": "^7.0.0",
"rollup-plugin-typescript2": "^0.30.0",
"semantic-release": "^17.0.7",
"ts-jest": "^27.0.3",
"ts-node": "^10.1.0",
"ttypescript": "^1.5.12",
"typescript": "^4.0.2"
},
"scripts": {
"build": "yarn build:cleanup && concurrently yarn:build:cjs yarn:build:esm yarn:build:esnext --kill-others-on-fail",
"build:cleanup": "rimraf ./cjs ./esm ./esnext ./types",
"build:cjs": "ttsc -p ./tsconfig.build.json --module CommonJS --target ES5 --outDir ./cjs",
"build:esm": "ttsc -p ./tsconfig.build.json --module ES6 --target ES5 --outDir ./esm",
"build:esnext": "ttsc -p ./tsconfig.build.json --module ESNext --target ESNext --outDir ./esnext",
"lint": "eslint ./{src,tests}/**/*.ts ./*.{ts,js}",
"lint:fix": "yarn lint --fix",
"build": "rimraf ./dist && rollup --config",
"test": "jest --coverage=false",
"test:coverage": "jest --coverage"
},
Expand Down
55 changes: 0 additions & 55 deletions rollup.config.js

This file was deleted.

20 changes: 20 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "./tsconfig.json",
"include": [
"src/index.ts"
],
"compilerOptions": {
"noEmit": false,
"outDir": "./cjs",
"declaration": true,
"target": "ES5",
"module": "CommonJS",
"moduleResolution": "Node",
"plugins": [
{
"transform": "./utility/ts-transformer-js-ext.ts",
"after": true
}
]
}
}
12 changes: 7 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"extends": "@xobotyi/preset-typescript/tsconfig.json",
"compilerOptions": {
"sourceMap": false,
"resolveJsonModule": false,
"target": "esnext",
"module": "esnext",
"moduleResolution": "node"
"strict": true,
"alwaysStrict": true,
"module": "ESNext",
"moduleResolution": "Node",
"target": "ESNext",
"noEmit": true,
"sourceMap": false
},
"include": [
"./src/**/*.ts"
Expand Down
58 changes: 58 additions & 0 deletions utility/ts-transformer-js-ext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable import/no-extraneous-dependencies, import/no-default-export */
import * as ts from 'typescript';
import * as path from 'path';

function shouldUpdateImportDeclaration(
node: ts.Node
): node is (ts.ImportDeclaration | ts.ExportDeclaration) & { moduleSpecifier: ts.StringLiteral } {
if (!ts.isImportDeclaration(node) && !ts.isExportDeclaration(node)) {
return false;
}
if (node.moduleSpecifier === undefined) {
return false;
}
if (!ts.isStringLiteral(node.moduleSpecifier)) {
return false;
}
if (!node.moduleSpecifier.text.startsWith('./') && !node.moduleSpecifier.text.startsWith('../')) {
return false;
}

return path.extname(node.moduleSpecifier.text) === '';
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default function transformer(_: ts.Program): ts.TransformerFactory<ts.SourceFile> {
return (context) => (sourceFile) => {
const fac = context.factory;
const visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {
if (shouldUpdateImportDeclaration(node)) {
if (ts.isImportDeclaration(node)) {
const newModuleSpecifier = fac.createStringLiteral(`${node.moduleSpecifier.text}.js`);
return fac.updateImportDeclaration(
node,
node.decorators,
node.modifiers,
node.importClause,
newModuleSpecifier
);
}
if (ts.isExportDeclaration(node)) {
const newModuleSpecifier = fac.createStringLiteral(`${node.moduleSpecifier.text}.js`);
return fac.updateExportDeclaration(
node,
node.decorators,
node.modifiers,
false,
node.exportClause,
newModuleSpecifier
);
}
}

return ts.visitEachChild(node, visitor, context);
};

return ts.visitNode(sourceFile, visitor);
};
}

0 comments on commit 09ee7ce

Please sign in to comment.