-
Notifications
You must be signed in to change notification settings - Fork 5
/
css-module-ident.js
43 lines (39 loc) · 1.77 KB
/
css-module-ident.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const fs = require('fs');
const path = require('path');
const loaderUtils = require('loader-utils');
const app = require('./option-parser');
const packageRoot = require('./package-root');
const fileIdentPattern = /(?:@(enact[/\\].*?)|^((?:(?!@enact).)*?))\.(?:module\.)?(?:less|css|sass|scss)/;
// A simplified development-focused `getLocalIdent` function for webpack CSS loader
// Outputs similar to `[path][name]_[local]`, except with `.module.(css|less|sass|scss)` extension
// filtering and using underscores for separators and condensed `@enact/*` naming.
module.exports = function (context, localIdentName, localName) {
let rel = path.relative(app.context, context.resourcePath);
if (rel.startsWith('..')) {
const parentMeta = packageRoot(fs.realpathSync(rel));
if (parentMeta && parentMeta.meta.name.startsWith('@enact/')) {
// Create a pseudo relative path. as though it were a local package, by resolving
// from the nearest parent package root
const externalRel = path.relative(parentMeta.path, context.resourcePath);
rel = path.join('node_modules', parentMeta.meta.name, externalRel);
} else {
// Out-of-app, and not within a package; just try our best and use absolute path
rel = rel.replace(/\.\./g, '-');
}
}
const fileMatch = rel.match(fileIdentPattern);
const identFile = (fileMatch && (fileMatch[1] || fileMatch[2])) || 'unknown';
// Create a hash based on a the file location and class name. Will be unique across a project, and close to globally unique.
let hash = '';
if (process.env.SIMPLE_CSS_IDENT !== 'true') {
hash =
'__' +
loaderUtils.getHashDigest(
path.posix.relative(context.rootContext, context.resourcePath) + localName,
'md5',
'base62',
5
);
}
return identFile.replace(/[/\\]+/g, '_') + '_' + localName + hash;
};