-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
101 lines (75 loc) · 3.26 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
'use strict';
const path = require('path');
const fs = require('fs');
const loaderUtils = require('loader-utils');
const SourceMap = require('source-map');
module.exports = function (source, sourceMap)
{
let query = loaderUtils.parseQuery(this.query);
if (this.cacheable)
this.cacheable();
// /foo/bar/file.js
let srcFilepath = this.resourcePath;
// /foo/bar/file.js -> file
let srcFilename = path.basename(srcFilepath, path.extname(srcFilepath));
// /foo/bar/file.js -> /foo/bar
let srcDirpath = path.dirname(srcFilepath);
// /foo/bar -> bar
let srcDirname = srcDirpath.split(path.sep).pop();
let elementName = srcFilename == 'index' ? srcDirname : srcFilename;
let templateExtension = query.templateExt || query.templateExtension || 'html';
let styleExtension = query.styleExt || query.styleExtension || 'css';
let htmlExists = fs.existsSync(path.join(srcDirpath, elementName+'.'+templateExtension));
let cssExists = fs.existsSync(path.join(srcDirpath, elementName+'.'+styleExtension));
let buffer = (htmlExists || cssExists) ? ['\n/* inject from polymer-loader */\n'] : '';
buffer.push("(function() {");
buffer.push( "\tlet componentTemplate = \"\";");
if (cssExists)
buffer.push("\tcomponentTemplate +='<style>' + require('./"+elementName+"."+styleExtension+"') + '</style>\\n';");
if (htmlExists)
buffer.push("\tcomponentTemplate += require('./"+elementName+"."+templateExtension+"') + '\\n';");
buffer = buffer.concat([
"\ttry",
"\t{",
"\t\tlet html = require(\"@polymer/polymer\").html;",
"\t\tlet Component = require('./"+elementName+".js');",
"\t\tif (\"default\" in Component)",
"\t\t\tComponent = Component.default;",
"\t\tObject.defineProperty(Component, \"_template\", {value: html([componentTemplate]), writable: false, configurable: false});",
"\t\tObject.defineProperty(Component, \"template\", {get: function () { return this._template}, configurable: true, enumerable: false});",
"\t\tcustomElements.define(Component.is || \"" + elementName + "\", Component);",
"\t}",
"\tcatch (error)",
"\t{",
"\t\tconsole.error(error);",
"\t}",
"})();"
]);
let inject = buffer.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
return source;
};