-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemstone-loader-js.js
93 lines (83 loc) · 3.14 KB
/
gemstone-loader-js.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
/*
** GemstoneJS -- Gemstone JavaScript Technology Stack
** Copyright (c) 2016-2018 Gemstone Project <http://gemstonejs.com>
** Licensed under Apache License 2.0 <https://spdx.org/licenses/Apache-2.0>
*/
/* load internal requirements */
const path = require("path")
/* load external requirements */
const loaderUtils = require("loader-utils")
const babel = require("babel-core")
/* Error object conforming to Webpack reporting */
const WebpackBabelError = function (name, message, error) {
Error.call(this)
Error.captureStackTrace(this, WebpackBabelError)
this.name = "WebpackBabelError"
this.message = (name ? name + ": " : "") + message + "\n\n" + error.codeFrame + "\n"
this.hideStack = true
this.error = error
}
WebpackBabelError.prototype = Object.create(Error.prototype)
WebpackBabelError.prototype.constructor = WebpackBabelError
/* transpile the source code */
const transpile = (source, options) => {
let result
try {
result = babel.transform(source, options)
}
catch (error) {
if (error.message && error.codeFrame) {
let message = error.message
let name
if (error instanceof SyntaxError) {
message = message.replace(/^[^:]+: /, "")
name = "SyntaxError"
}
else if (error instanceof TypeError)
message = message.replace(/^[^:]+: /, "")
throw new WebpackBabelError(name, message, error)
}
else
throw error
}
const code = result.code
const map = result.map
if (map && (!map.sourcesContent || !map.sourcesContent.length))
map.sourcesContent = [ source ]
return { code: code, map: map }
}
/* the exported Webpack loader function */
module.exports = function (content, inputSourceMap) {
/* determine Webpack loader query parameters */
const options = Object.assign({}, {
scope: "none"
}, loaderUtils.getOptions(this), this.resourceQuery ? loaderUtils.parseQuery(this.resourceQuery) : null)
void (options)
/* indicate to Webpack that our results are
fully deterministic and can be cached */
this.cacheable(true)
/* determine filenames */
const filenameChain = loaderUtils.getRemainingRequest(this).split("!")
const filename = filenameChain[filenameChain.length - 1]
/* transpile content */
const opts = {
sourceMap: this.sourceMap,
inputSourceMap: inputSourceMap,
sourceRoot: process.cwd(),
sourceFileName: path.relative(process.cwd(), filename),
filename: filename,
presets: [
[ require.resolve("babel-preset-env"), { targets: [ "last 2 versions" ] } ],
require.resolve("babel-preset-stage-2"),
require.resolve("babel-preset-stage-3")
],
plugins: [
require.resolve("babel-plugin-transform-runtime")
]
}
if (this.minimize)
opts.presets.push(require.resolve("babel-preset-minify"))
const result = transpile(content, opts)
/* provide results to Webpack */
this.callback(null, result.code, result.map)
}