forked from regularjs/regular-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (135 loc) · 4.29 KB
/
index.js
File metadata and controls
155 lines (135 loc) · 4.29 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var loaderUtils = require( 'loader-utils' );
var parse = require( './parser' );
var assign = require('object-assign');
var hash = require('hash-sum');
var es6Promise = require('es6-promise');
var path = require('path')
es6Promise.polyfill();
module.exports = function( content ) {
this.cacheable();
var loaderContext = this;
var options = this.options.pure || {};
var filePath = this.resourcePath;
var fileName = path.basename( filePath );
var moduleId = '_r-' + hash( filePath );
var rewriterInjectRE = /\b(css(-loader)?(\?[^!]+)?)(?:!|$)/
var selectorPath = require.resolve( './selector' );
var precompileLoaderPath = require.resolve( './precompile' );
// use modified html-loader
var htmlLoaderPath = require.resolve( './html-loader' );
var defaultLoaders = {
html: precompileLoaderPath + '!' + htmlLoaderPath,
css: 'style-loader!css-loader',
js: 'babel-loader?babelrc=false&presets[]=' + require.resolve( 'babel-preset-es2015' ) + '&plugins[]=' + require.resolve( 'babel-plugin-transform-runtime' ) + '&comments=false'
};
var defaultLang = {
template: 'html',
style: 'css',
script: 'js'
};
var rewriters = {
style: require.resolve( './style-rewriter' ),
template: require.resolve( './template-rewriter.js' )
};
if( this.sourceMap && !this.minimize ) {
defaultLoaders.css = 'style-loader!css-loader?sourceMap';
}
var loaders = assign( {}, defaultLoaders, options.loaders );
function getSelectorString( type, index ) {
return selectorPath +
'?type=' + type +
'&index=' + index + '!';
}
function ensureBang( loader ) {
if (loader.charAt( loader.length - 1 ) !== '!') {
return loader + '!'
} else {
return loader
}
}
function getRewriter( type, scoped ) {
var meta = '?id=' + moduleId;
switch( type ) {
case 'template':
return rewriters.template + ( scoped ? meta + '&scoped=true!' : '!');
case 'style':
return rewriters.style + ( scoped ? meta + '&scoped=true!' : '!');
default:
return '';
}
}
function getLoaderString( type, part, scoped ) {
var lang = part.lang || defaultLang[ type ]
var loader = loaders[lang]
var rewriter = getRewriter( type, scoped )
if (loader !== undefined) {
if( type === 'style' && rewriterInjectRE.test( loader ) ) {
// ensure rewriter is executed before css-loader
loader = loader.replace(rewriterInjectRE, function (m, $1) {
return ensureBang($1) + rewriter
})
} else if( type === 'template' ) {
// can not change loaders for template
loader = rewriter + ensureBang( defaultLoaders.html )
} else {
loader = ensureBang( loader ) + rewriter
}
return ensureBang( loader )
} else {
// unknown lang, infer the loader to be used
switch (type) {
case 'template':
return rewriter + defaultLoaders.html + '!'
case 'style':
return defaultLoaders.css + '!' + rewriter + lang + '!'
case 'script':
return lang + '!'
}
}
}
function getRequireString( type, part, index, scoped ) {
return loaderUtils.stringifyRequest( loaderContext,
// disable all configuration loaders
'!!' +
// get loader string for pre-processors
getLoaderString( type, part, scoped ) +
// select the corresponding part from the rgl file
getSelectorString( type, index || 0 ) +
// the url to the actual rgl file
filePath
)
}
function getRequire( type, part, index, scoped ) {
return 'require(' +
getRequireString( type, part, index, scoped ) +
')\n'
}
var parts = parse( content, fileName, this.sourceMap );
var output = 'var __regular_script__, __regular_template__;\n';
var hasScopedStyle = false;
// require style
parts.style.forEach(function( style, i ) {
if( style.scoped ) {
hasScopedStyle = true;
}
output += getRequire( 'style', style, i, style.scoped );
});
// require script
var script;
if (parts.script.length) {
script = parts.script[0];
output += '__regular_script__ = ' + getRequire( 'script', script, 0 );
}
// require template
var template;
if (parts.template.length) {
template = parts.template[0]
output += '__regular_template__ = ' + getRequire( 'template', template, 0, hasScopedStyle )
}
output += 'var __rs__ = __regular_script__ || {};\n' +
'if (__rs__.__esModule) __rs__ = __rs__.default;\n' +
'__rs__.template = __regular_template__;\n' +
'module.exports = __rs__;';
// done
return output;
}