/
loader.js
53 lines (46 loc) · 1.38 KB
/
loader.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
const path = require('path');
const babel = require('@babel/core');
const loaderUtils = require('loader-utils');
const babelPlugin = require('../babel.js');
const virtualModules = require('./virtualModules.js');
async function style9Loader(input, inputSourceMap) {
const {
inlineLoader = '',
outputCSS = true,
parserOptions = {
plugins: ['typescript', 'jsx']
},
...options
} = loaderUtils.getOptions(this) || {};
this.async();
try {
const { code, map, metadata } = await babel.transformAsync(input, {
plugins: [[babelPlugin, options]],
inputSourceMap: inputSourceMap || true,
sourceFileName: this.resourcePath,
filename: path.basename(this.resourcePath),
sourceMaps: true,
parserOpts: parserOptions,
babelrc: false
});
if (metadata.style9 === undefined) {
this.callback(null, input, inputSourceMap);
} else if (!outputCSS) {
this.callback(null, code, map);
} else {
const cssPath = loaderUtils.interpolateName(
this,
'[path][name].[hash:base64:7].css',
{
content: metadata.style9
}
);
virtualModules.writeModule(cssPath, metadata.style9);
const postfix = `\nimport '${inlineLoader + cssPath}';`;
this.callback(null, code + postfix, map);
}
} catch (error) {
this.callback(error);
}
}
module.exports = style9Loader;