1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 | const extend = require('extend'),
|
8 | T = require('../tools'),
|
9 | util = require('../utils');
|
10 |
|
11 | class Statics {
|
12 | constructor(config) {
|
13 | this.config = extend(
|
14 | true,
|
15 | {
|
16 | testExt: /^\.(html|tpl|jade|md|css|scss|less|styl|vue|jsx)[^\.]*$/i
|
17 | },
|
18 | config || {}
|
19 | );
|
20 | this.isExecute = true;
|
21 | this.hostname = this.config.hostname;
|
22 | this.testExt = this.config.testExt;
|
23 | this.nodes = this.config.nodes;
|
24 |
|
25 | this.init();
|
26 | }
|
27 |
|
28 | init() {
|
29 | this.initProps();
|
30 | this.initNodes();
|
31 | }
|
32 |
|
33 | initProps() {
|
34 | for (let key in this.config) {
|
35 | if (this.config.hasOwnProperty(key)) {
|
36 | this[key] = this.config[key];
|
37 | }
|
38 | }
|
39 | this.hostname && (this.hostname = Statics.removeLastSlash(this.hostname));
|
40 | }
|
41 |
|
42 | initNodes() {
|
43 | if (util.isArray(this.nodes)) {
|
44 | this.nodes.forEach(node => {
|
45 | if (util.isObject(node)) {
|
46 | node.extname = Statics.setTest(node.extname);
|
47 | !!node.hostname && (node.hostname = Statics.removeLastSlash(node.hostname));
|
48 | !!node.pathname && (node.pathname = Statics.removeLastSlash(node.pathname));
|
49 | }
|
50 | });
|
51 | }
|
52 | }
|
53 |
|
54 | exectureNodes(content) {
|
55 | this.nodes.forEach(node => {
|
56 | let hostname = node.hostname || this.hostname;
|
57 | let pathname = node.pathname || '';
|
58 | T.pathRegxs.forEach(regx => {
|
59 | content = content.replace(regx, (match, $1) => {
|
60 | if (/^(https?:|\/\/|data:|about:|javascript:|\?|\<|\{|[@#&])/i.test($1)) {
|
61 | return match;
|
62 | }
|
63 | let srcExt = T.Path.extname($1.split('?')[0]);
|
64 | if (node.extname.test(srcExt)) {
|
65 | return match.replace($1, `${hostname}/${pathname}/${T.getStaticPath($1)}`);
|
66 | } else {
|
67 | return match;
|
68 | }
|
69 | });
|
70 | });
|
71 | });
|
72 |
|
73 | content = this.exectureContent(content);
|
74 |
|
75 | return content;
|
76 | }
|
77 |
|
78 | execture(file) {
|
79 | let extname = T.Path.extname(file.path);
|
80 | if (!this.isExecute || !this.testExt.test(extname) || !this.hostname) return file.contents;
|
81 |
|
82 | let content = file.contents.toString('utf8') || T.getFileContent(file.path);
|
83 |
|
84 | if (util.isArray(this.nodes)) {
|
85 | return this.exectureNodes(content);
|
86 | } else {
|
87 | return this.exectureContent(content);
|
88 | }
|
89 | }
|
90 |
|
91 | exectureContent(content) {
|
92 | T.pathRegxs.forEach(regx => {
|
93 | content = content.replace(regx, (match, $1) => {
|
94 | if (/^(https?:|\/\/|data:|about:|javascript:|\?|\<|\{|[@#&])/i.test($1)) {
|
95 | return match;
|
96 | }
|
97 | return match.replace($1, `${this.hostname}/${T.getStaticPath($1)}`);
|
98 | });
|
99 | });
|
100 | return content;
|
101 | }
|
102 |
|
103 | static setTest(test) {
|
104 | return util.isRegExp(test) ? test : util.isString(test) ? new RegExp(test, 'gi') : null;
|
105 | }
|
106 | static removeLastSlash(path) {
|
107 | return path.replace(/\/+$/i, '');
|
108 | }
|
109 | }
|
110 |
|
111 | module.exports = Statics;
|