typed-css-modules
Advanced tools
Comparing version 0.8.1 to 0.9.0
@@ -48,2 +48,7 @@ #!/usr/bin/env node | ||
}, | ||
a: { | ||
type: 'boolean', | ||
desc: 'Use the ".d.css.ts" extension to be compatible with the equivalent TypeScript option', | ||
alias: 'allowArbitraryExtensions', | ||
}, | ||
d: { | ||
@@ -50,0 +55,0 @@ type: 'boolean', |
@@ -10,2 +10,3 @@ export type CamelCaseOption = boolean | 'dashes' | undefined; | ||
namedExports: boolean; | ||
allowArbitraryExtensions: boolean; | ||
camelCase: CamelCaseOption; | ||
@@ -22,2 +23,3 @@ EOL: string; | ||
private namedExports; | ||
private allowArbitraryExtensions; | ||
private camelCase; | ||
@@ -24,0 +26,0 @@ private resultList; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -30,12 +7,8 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
exports.DtsContent = void 0; | ||
const fs = __importStar(require("fs")); | ||
const path = __importStar(require("path")); | ||
const promises_1 = __importDefault(require("node:fs/promises")); | ||
const node_path_1 = __importDefault(require("node:path")); | ||
const is_there_1 = __importDefault(require("is-there")); | ||
const mkdirp = __importStar(require("mkdirp")); | ||
const util = __importStar(require("util")); | ||
const mkdirp_1 = require("mkdirp"); | ||
const camelcase_1 = __importDefault(require("camelcase")); | ||
const chalk_1 = __importDefault(require("chalk")); | ||
const writeFile = util.promisify(fs.writeFile); | ||
const readFile = util.promisify(fs.readFile); | ||
const unlinkFile = util.promisify(fs.unlink); | ||
class DtsContent { | ||
@@ -50,2 +23,3 @@ constructor(options) { | ||
this.namedExports = options.namedExports; | ||
this.allowArbitraryExtensions = options.allowArbitraryExtensions; | ||
this.camelCase = options.camelCase; | ||
@@ -78,12 +52,12 @@ this.EOL = options.EOL; | ||
get outputFilePath() { | ||
return path.join(this.rootDir, this.outDir, this.outputFileName); | ||
return node_path_1.default.join(this.rootDir, this.outDir, this.outputFileName); | ||
} | ||
get relativeOutputFilePath() { | ||
return path.join(this.outDir, this.outputFileName); | ||
return node_path_1.default.join(this.outDir, this.outputFileName); | ||
} | ||
get inputFilePath() { | ||
return path.join(this.rootDir, this.searchDir, this.rInputPath); | ||
return node_path_1.default.join(this.rootDir, this.searchDir, this.rInputPath); | ||
} | ||
get relativeInputFilePath() { | ||
return path.join(this.searchDir, this.rInputPath); | ||
return node_path_1.default.join(this.searchDir, this.rInputPath); | ||
} | ||
@@ -96,3 +70,3 @@ async checkFile(postprocessor = (formatted) => formatted) { | ||
const finalOutput = postprocessor(this.formatted); | ||
const fileContent = (await readFile(this.outputFilePath)).toString(); | ||
const fileContent = (await promises_1.default.readFile(this.outputFilePath)).toString(); | ||
if (fileContent !== finalOutput) { | ||
@@ -106,5 +80,5 @@ console.error(chalk_1.default.red(`[ERROR] Check type definitions for '${this.relativeOutputFilePath}'`)); | ||
const finalOutput = await postprocessor(this.formatted); | ||
const outPathDir = path.dirname(this.outputFilePath); | ||
const outPathDir = node_path_1.default.dirname(this.outputFilePath); | ||
if (!(0, is_there_1.default)(outPathDir)) { | ||
mkdirp.sync(outPathDir); | ||
await (0, mkdirp_1.mkdirp)(outPathDir); | ||
} | ||
@@ -116,3 +90,3 @@ let isDirty = false; | ||
else { | ||
const content = (await readFile(this.outputFilePath)).toString(); | ||
const content = (await promises_1.default.readFile(this.outputFilePath)).toString(); | ||
if (content !== finalOutput) { | ||
@@ -123,3 +97,3 @@ isDirty = true; | ||
if (isDirty) { | ||
await writeFile(this.outputFilePath, finalOutput, 'utf8'); | ||
await promises_1.default.writeFile(this.outputFilePath, finalOutput, 'utf8'); | ||
} | ||
@@ -129,3 +103,3 @@ } | ||
if ((0, is_there_1.default)(this.outputFilePath)) { | ||
await unlinkFile(this.outputFilePath); | ||
await promises_1.default.unlink(this.outputFilePath); | ||
} | ||
@@ -158,9 +132,12 @@ } | ||
dashesCamelCase(str) { | ||
return str.replace(/-+(\w)/g, function (match, firstLetter) { | ||
return firstLetter.toUpperCase(); | ||
}); | ||
return str.replace(/-+(\w)/g, (_, firstLetter) => firstLetter.toUpperCase()); | ||
} | ||
get outputFileName() { | ||
const outputFileName = this.dropExtension ? removeExtension(this.rInputPath) : this.rInputPath; | ||
return outputFileName + '.d.ts'; | ||
// Original extension must be dropped when using the allowArbitraryExtensions option | ||
const outputFileName = this.dropExtension || this.allowArbitraryExtensions ? removeExtension(this.rInputPath) : this.rInputPath; | ||
/** | ||
* Handles TypeScript 5.0 addition of arbitrary file extension patterns for ESM compatibility | ||
* https://www.typescriptlang.org/tsconfig#allowArbitraryExtensions | ||
*/ | ||
return outputFileName + (this.allowArbitraryExtensions ? '.d.css.ts' : '.d.ts'); | ||
} | ||
@@ -170,5 +147,5 @@ } | ||
function removeExtension(filePath) { | ||
const ext = path.extname(filePath); | ||
const ext = node_path_1.default.extname(filePath); | ||
return filePath.replace(new RegExp(ext + '$'), ''); | ||
} | ||
//# sourceMappingURL=dts-content.js.map |
@@ -0,3 +1,3 @@ | ||
import { Plugin } from 'postcss'; | ||
import { DtsContent, CamelCaseOption } from './dts-content'; | ||
import { Plugin } from 'postcss'; | ||
interface DtsCreatorOptions { | ||
@@ -9,2 +9,3 @@ rootDir?: string; | ||
namedExports?: boolean; | ||
allowArbitraryExtensions?: boolean; | ||
dropExtension?: boolean; | ||
@@ -20,5 +21,5 @@ EOL?: string; | ||
private inputDirectory; | ||
private outputDirectory; | ||
private camelCase; | ||
private namedExports; | ||
private allowArbitraryExtensions; | ||
private dropExtension; | ||
@@ -25,0 +26,0 @@ private EOL; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -30,5 +7,5 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
exports.DtsCreator = void 0; | ||
const process = __importStar(require("process")); | ||
const path = __importStar(require("path")); | ||
const os = __importStar(require("os")); | ||
const node_process_1 = __importDefault(require("node:process")); | ||
const node_path_1 = __importDefault(require("node:path")); | ||
const node_os_1 = __importDefault(require("node:os")); | ||
const file_system_loader_1 = __importDefault(require("./file-system-loader")); | ||
@@ -40,20 +17,20 @@ const dts_content_1 = require("./dts-content"); | ||
options = {}; | ||
this.rootDir = options.rootDir || process.cwd(); | ||
this.rootDir = options.rootDir || node_process_1.default.cwd(); | ||
this.searchDir = options.searchDir || ''; | ||
this.outDir = options.outDir || this.searchDir; | ||
this.loader = new file_system_loader_1.default(this.rootDir, options.loaderPlugins); | ||
this.inputDirectory = path.join(this.rootDir, this.searchDir); | ||
this.outputDirectory = path.join(this.rootDir, this.outDir); | ||
this.inputDirectory = node_path_1.default.join(this.rootDir, this.searchDir); | ||
this.camelCase = options.camelCase; | ||
this.namedExports = !!options.namedExports; | ||
this.allowArbitraryExtensions = !!options.allowArbitraryExtensions; | ||
this.dropExtension = !!options.dropExtension; | ||
this.EOL = options.EOL || os.EOL; | ||
this.EOL = options.EOL || node_os_1.default.EOL; | ||
} | ||
async create(filePath, initialContents, clearCache = false, isDelete = false) { | ||
let rInputPath; | ||
if (path.isAbsolute(filePath)) { | ||
rInputPath = path.relative(this.inputDirectory, filePath); | ||
if (node_path_1.default.isAbsolute(filePath)) { | ||
rInputPath = node_path_1.default.relative(this.inputDirectory, filePath); | ||
} | ||
else { | ||
rInputPath = path.relative(this.inputDirectory, path.join(process.cwd(), filePath)); | ||
rInputPath = node_path_1.default.relative(this.inputDirectory, node_path_1.default.join(node_process_1.default.cwd(), filePath)); | ||
} | ||
@@ -78,2 +55,3 @@ if (clearCache) { | ||
namedExports: this.namedExports, | ||
allowArbitraryExtensions: this.allowArbitraryExtensions, | ||
camelCase: this.camelCase, | ||
@@ -80,0 +58,0 @@ EOL: this.EOL, |
@@ -7,2 +7,3 @@ interface RunOptions { | ||
namedExports?: boolean; | ||
allowArbitraryExtensions?: boolean; | ||
dropExtension?: boolean; | ||
@@ -9,0 +10,0 @@ silent?: boolean; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -31,3 +8,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const chalk_1 = __importDefault(require("chalk")); | ||
const chokidar = __importStar(require("chokidar")); | ||
const chokidar_1 = __importDefault(require("chokidar")); | ||
const glob_1 = require("glob"); | ||
@@ -43,2 +20,3 @@ const dts_creator_1 = require("./dts-creator"); | ||
namedExports: options.namedExports, | ||
allowArbitraryExtensions: options.allowArbitraryExtensions, | ||
dropExtension: options.dropExtension, | ||
@@ -92,3 +70,3 @@ }); | ||
console.log('Watch ' + filesPattern + '...'); | ||
const watcher = chokidar.watch([filesPattern]); | ||
const watcher = chokidar_1.default.watch([filesPattern]); | ||
watcher.on('add', writeFile); | ||
@@ -95,0 +73,0 @@ watcher.on('change', writeFile); |
{ | ||
"name": "typed-css-modules", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "Creates .d.ts files from CSS Modules .css files", | ||
@@ -13,2 +13,3 @@ "main": "lib/index.js", | ||
"lint": "npm run prettier -- --check", | ||
"compile": "tsc --noEmit", | ||
"test": "jest", | ||
@@ -52,13 +53,13 @@ "test:watch": "jest --watch", | ||
"devDependencies": { | ||
"@types/jest": "29.5.7", | ||
"@types/node": "20.8.10", | ||
"@types/yargs": "17.0.29", | ||
"@types/jest": "29.5.11", | ||
"@types/node": "20.11.5", | ||
"@types/yargs": "17.0.32", | ||
"husky": "8.0.3", | ||
"jest": "29.7.0", | ||
"prettier": "2.8.8", | ||
"pretty-quick": "3.1.3", | ||
"pretty-quick": "3.3.1", | ||
"rimraf": "5.0.5", | ||
"ts-jest": "29.1.1", | ||
"typescript": "5.2.2" | ||
"typescript": "5.3.3" | ||
} | ||
} |
@@ -152,2 +152,8 @@ # typed-css-modules [![github actions](https://github.com/Quramy/typed-css-modules/workflows/build/badge.svg)](https://github.com/Quramy/typed-css-modules/actions) [![npm version](https://badge.fury.io/js/typed-css-modules.svg)](http://badge.fury.io/js/typed-css-modules) | ||
#### arbitrary file extensions | ||
With `-a` or `--allowArbitraryExtensions`, output filenames will be compatible with the "arbitrary file extensions" feature that was introduce in TypeScript 5.0. See [the docs](https://www.typescriptlang.org/tsconfig#allowArbitraryExtensions) for more info. | ||
In essence, the `*.css.d.ts` extension now becomes `*.d.css.ts` so that you can import CSS modules in projects using ESM module resolution. | ||
## API | ||
@@ -182,2 +188,3 @@ | ||
- `option.namedExports`: Use named exports as opposed to default exports to enable tree shaking. Requires `import * as style from './file.module.css';` (default: `false`) | ||
- `option.allowArbitraryExtensions`: Output filenames that will be compatible with the "arbitrary file extensions" TypeScript feature | ||
- `option.EOL`: EOL (end of line) for the generated `d.ts` files. Possible values `'\n'` or `'\r\n'`(default: `os.EOL`). | ||
@@ -184,0 +191,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
273
1
53898
677