-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
56 lines (43 loc) · 1.58 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
const loaderUtils = require('loader-utils');
const babel = require('@babel/core');
const elmCssModulesPlugin = require('elm-css-modules-plugin');
const loader = function(source, inputSourceMap) {
this.cacheable && this.cacheable(); // for Webpack 1.x compatibility
const config = loaderUtils.getOptions(this) || {};
config.module = config['module'] || 'CssModules';
config.tagger = config['tagger'] || 'css';
const packageName = config['package'] || 'cultureamp/elm-css-modules-loader';
const taggerName = [
packageName.replace(/-/g, '_').replace(/\//g, '$'),
config.module.replace(/\./g, '_'),
config.tagger,
].join('$');
const transformerOptions = {
taggerName: taggerName,
localPath: config.localPath,
};
const webpackRemainingChain = loaderUtils
.getRemainingRequest(this)
.split('!');
const filename = webpackRemainingChain[webpackRemainingChain.length - 1];
const babelOptions = {
inputSourceMap: inputSourceMap,
sourceRoot: process.cwd(),
filename: filename,
compact: false,
babelrc: false,
};
const result = transform(source, this, transformerOptions, babelOptions);
this.callback(null, result.code, result.map);
};
function transform(source, loaderContext, transformerOptions, babelOptions) {
babelOptions.plugins = [
[elmCssModulesPlugin, { taggerName: transformerOptions.taggerName }],
];
const { code, map } = babel.transform(source, babelOptions);
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
map.sourcesContent = [source];
}
return { code, map };
}
module.exports = loader;