node-css-require
Advanced tools
Comparing version 0.1.4 to 0.4.0
@@ -11,4 +11,5 @@ import { AcceptedPlugin } from 'postcss'; | ||
} | ||
export declare function compileCss(code: string, filename: string, config?: CompileCssConfiguration): CompileCssResult; | ||
export declare function compileCss(code: string, filename: string, config?: CompileCssConfiguration): Promise<CompileCssResult>; | ||
export declare function compileCssSync(code: string, filename: string, config?: CompileCssConfiguration): CompileCssResult; | ||
export declare const register: () => void; | ||
//# sourceMappingURL=nodeCssRequire.d.ts.map |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.register = exports.compileCss = void 0; | ||
exports.register = exports.compileCssSync = exports.compileCss = void 0; | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
@@ -12,8 +12,40 @@ const pirates_1 = require("pirates"); | ||
const postcss_modules_sync_1 = __importDefault(require("postcss-modules-sync")); | ||
const postcss_modules_1 = __importDefault(require("postcss-modules")); | ||
const register_1 = require("@swc-node/register/register"); | ||
function compileCss(code, filename, config) { | ||
const crypto_1 = require("crypto"); | ||
function generateScopedName(name, filename, css) { | ||
const i = css.indexOf(`.${name}`); | ||
const lineNumber = css.substr(0, i).split(/[\r\n]/).length; | ||
const hash = (0, crypto_1.createHash)('md5').update(css).digest('hex'); | ||
return `${name}_${hash}_${lineNumber}`; | ||
} | ||
async function compileCss(code, filename, config) { | ||
let exportedTokens = {}; | ||
const plugins = [ | ||
(0, postcss_modules_1.default)({ | ||
generateScopedName: generateScopedName, | ||
getJSON: (cssFileName, json, outputFileName) => { | ||
exportedTokens = json; | ||
}, | ||
}), | ||
]; | ||
let res = (0, postcss_1.default)(plugins).process(code, { from: filename }); | ||
await res; | ||
// apply other postcss magic after class names have been mapped | ||
if ((config === null || config === void 0 ? void 0 : config.plugins) && config.plugins.length > 0) { | ||
res = (0, postcss_1.default)(config.plugins).process(res.css, { from: filename }); | ||
await res; | ||
} | ||
const js = `module.exports = JSON.parse('${JSON.stringify(exportedTokens)}');`; | ||
return { | ||
js, | ||
css: res.css, | ||
}; | ||
} | ||
exports.compileCss = compileCss; | ||
function compileCssSync(code, filename, config) { | ||
let exportedTokens = {}; | ||
const plugins = [ | ||
(0, postcss_modules_sync_1.default)({ | ||
generateScopedName: (config === null || config === void 0 ? void 0 : config.generateScopedName) || '[path][local]-[hash:base64:10]', | ||
generateScopedName: generateScopedName, | ||
getJSON: (tokens) => { | ||
@@ -42,8 +74,8 @@ exportedTokens = tokens; | ||
} | ||
exports.compileCss = compileCss; | ||
function compileJs(code, filename) { | ||
return compileCss(code, filename).js; | ||
exports.compileCssSync = compileCssSync; | ||
function compileJsSync(code, filename) { | ||
return compileCssSync(code, filename).js; | ||
} | ||
const register = () => { | ||
(0, pirates_1.addHook)((code, filename) => compileJs(code, filename), { | ||
(0, pirates_1.addHook)((code, filename) => compileJsSync(code, filename), { | ||
exts: ['.css'], | ||
@@ -50,0 +82,0 @@ }); |
{ | ||
"name": "node-css-require", | ||
"version": "0.1.4", | ||
"version": "0.4.0", | ||
"description": "Load CSS modules when running Node.js CLI and server applications", | ||
@@ -39,6 +39,7 @@ "keywords": [ | ||
"postcss": "^8.4.14", | ||
"postcss-modules": "^5.0.0", | ||
"postcss-modules-sync": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@goldstack/utils-git": "0.1.37", | ||
"@goldstack/utils-git": "0.2.0", | ||
"@types/jest": "^28.1.8", | ||
@@ -45,0 +46,0 @@ "@types/node": "^18.7.13", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
10612
96
5
+ Addedpostcss-modules@^5.0.0
+ Addedgeneric-names@4.0.0(transitive)
+ Addedicss-utils@5.1.0(transitive)
+ Addedloader-utils@3.3.1(transitive)
+ Addedlodash.camelcase@4.3.0(transitive)
+ Addedpostcss-modules@5.0.0(transitive)
+ Addedpostcss-modules-extract-imports@3.1.0(transitive)
+ Addedpostcss-modules-local-by-default@4.2.0(transitive)
+ Addedpostcss-modules-scope@3.2.1(transitive)
+ Addedpostcss-modules-values@4.0.0(transitive)
+ Addedpostcss-selector-parser@7.0.0(transitive)
+ Addedpostcss-value-parser@4.2.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedchalk@1.1.3(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedsource-map@0.5.7(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)