forked from JonDum/polymer-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (94 loc) · 4.2 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
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
'use strict';
var path = require('path');
var fs = require('fs');
var loaderUtils = require('loader-utils');
var SourceMap = require('source-map');
module.exports = function(source, sourceMap) {
var query = loaderUtils.parseQuery(this.query);
if(this.cacheable) {
this.cacheable();
}
// /foo/bar/file.js
var srcFilepath = this.resourcePath;
// /foo/bar/file.js -> file
var srcFilename = path.basename(srcFilepath, path.extname(srcFilepath));
// /foo/bar/file.js -> /foo/bar
var srcDirpath = path.dirname(srcFilepath);
// /foo/bar -> bar
var srcDirname = srcDirpath.split(path.sep).pop();
var elementName = srcFilename == 'index' ? srcDirname : srcFilename;
var templateExtension = query.templateExt || query.templateExtension || 'html';
var styleExtension = query.styleExt || query.styleExtension || 'css';
var htmlExists = fs.existsSync(path.join(srcDirpath, elementName+'.'+templateExtension));
var cssExists = fs.existsSync(path.join(srcDirpath, elementName+'.'+styleExtension));
var inject = (htmlExists || cssExists) ? '\n/* inject from polymer-loader */\n' : '';
if(htmlExists & cssExists) {
inject += [
"(function(document) {",
"\tvar template = require('./"+elementName+"."+templateExtension+"');",
"\tvar styles = require('./"+elementName+"."+styleExtension+"');",
"\tvar el = document.createElement('div');",
"\tel.setAttribute('name', '"+elementName+"')",
"\tel.setAttribute('hidden','')",
"\tif(template.indexOf('<template>'))",
"\t\tel.innerHTML = template.replace('<template>', '<style>'+styles+'</style><template>');",
"\telse",
"\t\tel.innerHTML = template.replace('<dom-module id=\""+elementName+"\">', '<dom-module id=\"" +elementName+ "\"><style>'+styles+'</style>');",
"\tdocument.body.appendChild(el);",
"})(document);"
].join('\n');
}
else
if(htmlExists && !cssExists) {
inject += [
"(function(document) {",
"\tvar template = require('./"+elementName+"."+templateExtension+"');",
"\tvar el = document.createElement('div');",
"\tel.setAttribute('name', '"+elementName+"')",
"\tel.setAttribute('hidden','')",
"\tel.innerHTML = template;",
"\tdocument.body.appendChild(el);",
"})(document);"
].join('\n');
}
else
if(!htmlExists && cssExists) {
inject += [
"(function(document) {",
"\tvar styles = require('./"+elementName+"."+styleExtension+"');",
"\tvar el = document.createElement('div');",
"\tel.setAttribute('name', '"+elementName+"')",
"\tel.setAttribute('hidden','')",
"\tel.innerHTML = '<dom-module id=\""+elementName+"\"><style>'+styles+'</style><template></template></dom-module>';",
"\tdocument.body.appendChild(el);",
"})(document);"
].join('\n');
}
// support existing SourceMap
// https://github.com/mozilla/source-map#sourcenode
// https://github.com/webpack/imports-loader/blob/master/index.js#L34-L44
// https://webpack.github.io/docs/loaders.html#writing-a-loader
if (sourceMap) {
var currentRequest = loaderUtils.getCurrentRequest(this);
var SourceNode = SourceMap.SourceNode;
var SourceMapConsumer = SourceMap.SourceMapConsumer;
var sourceMapConsumer = new SourceMapConsumer(sourceMap);
var node = SourceNode.fromStringWithSourceMap(source, sourceMapConsumer);
node.prepend(inject);
var result = node.toStringWithSourceMap({
file: currentRequest
});
this.callback(null, result.code, result.map.toJSON());
return;
}
// prepend collected inject at the top of file
return source +'\n'+ inject;
// return the original source and sourceMap
if (sourceMap) {
this.callback(null, source, sourceMap);
return;
}
// return the original source
console.log("The source: ", source);
return source;
};