/
Parser.js
90 lines (75 loc) 路 3.29 KB
/
Parser.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const path = require('path');
const RawAsset = require('./assets/RawAsset');
const GlobAsset = require('./assets/GlobAsset');
const {isGlob} = require('./utils/glob');
class Parser {
constructor(options = {}) {
this.extensions = {};
this.registerExtension('js', './assets/JSAsset');
this.registerExtension('jsx', './assets/JSAsset');
this.registerExtension('es6', './assets/JSAsset');
this.registerExtension('jsm', './assets/JSAsset');
this.registerExtension('mjs', './assets/JSAsset');
this.registerExtension('ml', './assets/ReasonAsset');
this.registerExtension('re', './assets/ReasonAsset');
this.registerExtension('ts', './assets/TypeScriptAsset');
this.registerExtension('tsx', './assets/TypeScriptAsset');
this.registerExtension('coffee', './assets/CoffeeScriptAsset');
this.registerExtension('elm', './assets/ElmAsset');
this.registerExtension('vue', './assets/VueAsset');
this.registerExtension('json', './assets/JSONAsset');
this.registerExtension('json5', './assets/JSONAsset');
this.registerExtension('jsonld', './assets/JSONLDAsset');
this.registerExtension('yaml', './assets/YAMLAsset');
this.registerExtension('yml', './assets/YAMLAsset');
this.registerExtension('toml', './assets/TOMLAsset');
this.registerExtension('gql', './assets/GraphqlAsset');
this.registerExtension('graphql', './assets/GraphqlAsset');
this.registerExtension('kt', './assets/KotlinAsset');
this.registerExtension('css', './assets/CSSAsset');
this.registerExtension('pcss', './assets/CSSAsset');
this.registerExtension('postcss', './assets/CSSAsset');
this.registerExtension('sss', './assets/SSSAsset');
this.registerExtension('styl', './assets/StylusAsset');
this.registerExtension('stylus', './assets/StylusAsset');
this.registerExtension('less', './assets/LESSAsset');
this.registerExtension('sass', './assets/SASSAsset');
this.registerExtension('scss', './assets/SASSAsset');
this.registerExtension('html', './assets/HTMLAsset');
this.registerExtension('htm', './assets/HTMLAsset');
this.registerExtension('rs', './assets/RustAsset');
this.registerExtension('webmanifest', './assets/WebManifestAsset');
this.registerExtension('glsl', './assets/GLSLAsset');
this.registerExtension('vert', './assets/GLSLAsset');
this.registerExtension('frag', './assets/GLSLAsset');
this.registerExtension('jade', './assets/PugAsset');
this.registerExtension('pug', './assets/PugAsset');
let extensions = options.extensions || {};
for (let ext in extensions) {
this.registerExtension(ext, extensions[ext]);
}
}
registerExtension(ext, parser) {
if (!ext.startsWith('.')) {
ext = '.' + ext;
}
this.extensions[ext.toLowerCase()] = parser;
}
findParser(filename, fromPipeline) {
if (!fromPipeline && isGlob(filename)) {
return GlobAsset;
}
let extension = path.extname(filename).toLowerCase();
let parser = this.extensions[extension] || RawAsset;
if (typeof parser === 'string') {
parser = this.extensions[extension] = require(parser);
}
return parser;
}
getAsset(filename, options = {}) {
let Asset = this.findParser(filename);
options.parser = this;
return new Asset(filename, options);
}
}
module.exports = Parser;