-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
115 lines (89 loc) · 3.23 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
'use strict'
const path = require('path')
const fs = require('fs')
const SourceMap = require('source-map')
module.exports = function (source, sourceMap)
{
let query = new URLSearchParams(this.query)
if (this.cacheable)
this.cacheable()
// /foo/bar/file.js
const srcFilepath = this.resourcePath
// /foo/bar/file.js -> file
const srcFilename = path.basename(srcFilepath, path.extname(srcFilepath))
// /foo/bar/file.js -> /foo/bar
const srcDirpath = path.dirname(srcFilepath)
// /foo/bar -> bar
const srcDirname = srcDirpath.split(path.sep).pop()
const elementName = srcFilename == 'index' ? srcDirname : srcFilename
const templateExtension = query.get("templateExt") || query.get("templateExtension") || 'html'
const styleExtension = query.get("styleExt") || query.get("styleExtension") || 'css'
const htmlExists = fs.existsSync(
path.join(srcDirpath, elementName + '.' + templateExtension)
)
const cssExists = fs.existsSync(
path.join(srcDirpath, elementName + '.' + styleExtension)
)
let buffer = htmlExists || cssExists ? ['\n/* inject from polymer-loader */\n'] : null
if (buffer != null)
{
buffer.push('(function() {')
buffer.push('\tlet componentTemplate = "";')
if (cssExists)
{
buffer.push(`
let styleSheet = require('./${elementName}.${styleExtension}');
if ("default" in styleSheet)
styleSheet = styleSheet.default;
componentTemplate += '<style>' + styleSheet + '</style>\\n';
`);
}
if (htmlExists)
{
buffer.push(`
let htmlStr = require('./${elementName}.${templateExtension}');
if ("default" in htmlStr)
htmlStr = htmlStr.default;
componentTemplate += htmlStr + '\\n';
`);
}
buffer = buffer.concat([
'\ttry',
'\t{',
'\t\tlet html = require("@polymer/polymer").html;',
`\t\tlet Component = require('./${elementName}');`,
'\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}',
'})();'
])
const inject = buffer.join('\n')
source += '\n' + inject
}
// 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) {
const currentRequest = this.currentRequest; //loaderUtils.getCurrentRequest(this)
const SourceNode = SourceMap.SourceNode
const SourceMapConsumer = SourceMap.SourceMapConsumer
const sourceMapConsumer = new SourceMapConsumer(sourceMap)
const node = SourceNode.fromStringWithSourceMap(source, sourceMapConsumer)
//node.prepend(inject)
const result = node.toStringWithSourceMap({
file: currentRequest
})
this.callback(null, result.code, result.map.toJSON())
return
}
// prepend collected inject at the top of file
return source
}