forked from finos/perspective
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (111 loc) · 3.88 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
118
119
120
121
122
123
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/
const PSP_WORKER_LOADER = require.resolve("./src/js/psp-worker-loader");
const WASM_LOADER = require.resolve("./src/js/wasm-loader.js");
const PSP_WORKER_COMPILER_LOADER = require.resolve("./src/js/psp-worker-compiler-loader.js");
const {get_config} = require("@finos/perspective/dist/esm/config");
class PerspectiveWebpackPlugin {
constructor(options = {}) {
this.options = Object.assign(
{},
{
load_path: [/@finos[\\/]perspective/, __dirname.replace("-webpack-plugin", "")],
workerLoaderOptions: {
name: "[name].worker.js"
},
wasmLoaderOptions: {
name: "[name]"
}
},
options
);
}
apply(compiler) {
const compilerOptions = compiler.options;
const moduleOptions = compilerOptions.module || (compilerOptions.module = {});
const rules = [];
if (!this.options.inline) {
rules.push({
test: /perspective\.inline\.js/,
include: this.options.load_path,
use: [
{
loader: require.resolve("./src/js/switch-inline-loader.js")
}
]
});
if (compilerOptions.target !== "node") {
rules.push({
test: /__node\.js$/,
include: this.options.load_path,
use: [
{
loader: require.resolve("./src/js/null-loader.js")
}
]
});
}
}
if (this.options.build_worker) {
rules.push({
test: /perspective\.wasm\.js$/,
include: this.options.load_path,
use: [
{
loader: PSP_WORKER_LOADER,
options: Object.assign({}, this.options.workerLoaderOptions, {compiled: true})
},
{
loader: PSP_WORKER_COMPILER_LOADER,
options: {name: "[name].worker.js"}
}
]
});
} else {
rules.push({
test: /perspective\.wasm\.js$/,
include: this.options.load_path,
use: {
loader: PSP_WORKER_LOADER,
options: this.options.workerLoaderOptions
}
});
}
rules.push({
test: /\.js$/,
loader: "source-map-loader"
});
const perspective_config = get_config();
if (perspective_config) {
rules.push({
test: /\.js$/,
include: /perspective[\\/].+?[\\/]config[\\/]index\.js$/,
use: [
{
loader: "string-replace-loader",
options: {
search: "global.__TEMPLATE_CONFIG__",
replace: JSON.stringify(perspective_config, null, 4)
}
}
]
});
}
rules.push({
test: /psp\.async\.wasm\.js$/,
include: this.options.load_path,
use: {
loader: WASM_LOADER,
options: this.options.wasmLoaderOptions
}
});
moduleOptions.rules = (moduleOptions.rules || []).concat(rules);
}
}
module.exports = PerspectiveWebpackPlugin;