-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrollup.config.js
336 lines (311 loc) · 10.3 KB
/
rollup.config.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* eslint-disable no-undef */
const path = require("path");
import * as meta from "./package.json";
import babel from "@rollup/plugin-babel";
import eslint from '@rollup/plugin-eslint';
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import replace from "@rollup/plugin-replace";
import terser from "@rollup/plugin-terser";
import serve from "rollup-plugin-serve";
import livereload from "rollup-plugin-livereload";
import json from "@rollup/plugin-json";
import {visualizer} from "rollup-plugin-visualizer";
import trash from "rollup-plugin-delete";
import copy from "rollup-plugin-copy";
import url from "@rollup/plugin-url";
import globImport from "rollup-plugin-glob-import";
import stylus from "rollup-plugin-stylus-compiler";
import postcss from "rollup-plugin-postcss";
import alias from "@rollup/plugin-alias";
import virtual from "@rollup/plugin-virtual";
import autoprefixer from "autoprefixer";
import postcssUrl from "postcss-url";
import iife from "rollup-plugin-iife";
import html from "rollup-plugin-html2";
import sourcemaps from "rollup-plugin-sourcemaps";
import execute from "rollup-plugin-shell";
const copyright = `// ${meta.homepage} v${meta.version} Copyright ${(new Date).getFullYear()} ${meta.author.name}`;
const __PROD__ = process.env.NODE_ENV === "production";
const __STAGE__ = process.env.STAGE;
const __DEVSERVER__ = process.env.NODE_ENV === "devserver";
const allTools = require(path.resolve(__dirname, "vizabi-tools.json"));
const toolset = require(path.resolve(__dirname, "src", "config", `toolset.${__PROD__ ? (__STAGE__ || "prod") : "dev"}.json`));
const inToolsetTools = Object.keys(toolset.reduce((result, { tool, toolComponents }) => {
tool && (result[tool.toLowerCase()] = true);
toolComponents && toolComponents.forEach(tool => {
result[tool.toLowerCase()] = true;
})
return result;
}, {}));
const unbundledTools = allTools.unbundled.map(tool => ({
testRegExp: new RegExp(tool),
chunkName: tool
}));
function checkUnbundled(id) {
for (const tool of unbundledTools) {
if (tool.testRegExp.test(id)) return tool.chunkName;
}
}
function generateToolInputEntries() {
return (__PROD__ ? inToolsetTools : allTools.tools).reduce((result, tool) => {
//js
result.push(allTools.paths[tool] && allTools.paths[tool].js || `@vizabi/${tool}`);
return result;
}, []);
}
function getEntryToolsCssFilenames() {
return (__PROD__ ? inToolsetTools : allTools.tools).reduce((result, tool) => {
//css
result.push(allTools.paths[tool] && allTools.paths[tool].css || `@vizabi/${tool}/build/${tool}.css`);
return result;
}, []);
}
function getFilterOutToolsRegexp() {
const filterOutTools = allTools.tools.filter(tool => !inToolsetTools.includes(tool));
return filterOutTools.length ? new RegExp("(" + filterOutTools.join("|") + ")", "i") : null;
}
function getCustomToolsJsTestRegexp() {
const result = [];
Object.keys(allTools.paths).forEach(tool => {
if (allTools.paths[tool].js && path.extname(allTools.paths[tool].js) !== "") {
result.push(new RegExp(allTools.paths[tool].js.replace(".", "\\.").replace("/", "\\/")));
}
});
return result;
}
function getVizabiToolsCssTestRegexp() {
const result = [];
Object.keys(allTools.paths).forEach(tool => {
if (allTools.paths[tool].css) {
result.push(new RegExp(allTools.paths[tool].css.replace(".", "\\.").replace("/", "\\/")));
}
});
return result;
}
function mapLink(file) {
return `<link rel="stylesheet" href="${file}">\n`;
}
function mapScript(file) {
return `<script type="text/javascript" src="${file}"></script>\n`;
}
function varNameWithFileName(prefix) {
return function(path) {
const regex = /\b(\w+)\..*$/;
return prefix + regex[Symbol.match](path)[1];
};
}
function getHtmlAssets() {
return [
...["@vizabi/shared-components/build/VizabiSharedComponents.css", ...getEntryToolsCssFilenames()]
.map(f => "assets/css/" + path.basename(f)),
"styles.css",
...jsAssets
];
}
const jsonToJsEmitAssets = (plugin, varNameFunc, emitFilePath, nameRegex) => {
const pluginTransform = plugin.transform;
plugin.transform = function(code, id) {
const result = pluginTransform(code, id);
if (result !== null) {
const name = nameRegex || /\/([\w-]+)\.json$/;
this.emitFile({
type: "asset",
source: "var " + varNameFunc(id) + " = " + result.code.slice(15),
fileName: emitFilePath + name.exec(id)[1] + ".js"
});
result.dependencies && result.dependencies.forEach(this.addWatchFile);
return {
code: "export default 'default'",
map: null
};
}
return;
};
return plugin;
};
const jsAssets = [];
jsAssets.push(
`vendor${__PROD__ ? ".min" : ""}.js`,
`tools${__PROD__ ? ".min" : ""}.js`,
"config/properties.js",
"config/toolset.js",
"config/datasources.js",
"config/conceptMapping.js",
"config/entitysetMapping.js"
);
const deployDir = "tools";
export default [
//toolspage
{
//perf: true,
input: {
//"tools": "src/index.js",
//"toolspage": "src/app/app.js"
"toolspage": "src/index.js",
},
output: {
dir: "build/" + deployDir,
entryFileNames: `[name]${__PROD__ ? ".min" : ""}.js`,
chunkFileNames: `[name]${__PROD__ ? ".min" : ""}.js`,
//format: "cjs",
banner: copyright,
sourcemap: true,
// sourcemapPathTransform: sourcePath => {
// console.log(sourcePath)
// return sourcePath.replace(
// new RegExp(`^..${path.sep}`),
// '~/pkg-name/'
// )},
globals: {
"mobx": "mobx",
"Vizabi": "Vizabi",
"VizabiSharedComponents": "VizabiSharedComponents"
},
manualChunks(id) {
if (__PROD__) {
const result = checkUnbundled(id);
if (result) return result;
}
if (/Vizabi|vizabi/.test(id)) {
return "tools";
}
if (/rollupPluginBabelHelpers|d3|mobx.umd|urlon.umd/.test(id)) {
return "vendor";
}
}
},
//treeshake: __PROD__ ? {} : false,
context: "window",
external: ["mobx", "Vizabi", "VizabiSharedComponents"],
plugins: [
//__PROD__ &&
trash({
targets: ["build/*"],
runOnce: true
}),
virtual({
"vizabi-tools": generateToolInputEntries().map(entry => `import "${entry}";`).join("") + "export default '';"
}),
resolve(),
alias({
entries: [
{ find: "~d3", replacement: `d3/dist/d3${__PROD__ ? ".min" : ""}`},
{ find: "~mobx", replacement: `mobx/lib/mobx.umd${__PROD__ ? ".min" : ""}`},
{ find: "properties", replacement: path.resolve(__dirname, "src", "config", `properties.${__PROD__ ? (__STAGE__ || "prod") : "dev"}.json`) },
{ find: "toolset", replacement: path.resolve(__dirname, "src", "config", `toolset.${__PROD__ ? (__STAGE__ || "prod") : "dev"}.json`) },
{ find: "datasources", replacement: path.resolve(__dirname, "src", "config", `datasources.${__PROD__ ? (__STAGE__ || "prod") : "dev"}.json`) },
]
}),
commonjs({
include: "node_modules/core-js/**",
}),
globImport({
format: "import"
}),
copy({
targets: [
{ src: "src/assets", dest: "build/tools" },
{ src: "src/data", dest: "build/tools" },
{ src: "src/config/toolconfigs", dest: "build/tools/config" },
{ src: "node_modules/@gapminder/tools-page-chart-configs/src/*.js", dest: "build/tools/config/toolconfigs/" },
{ src: "src/config/conceptMapping.js", dest: "build/tools/config" },
{ src: "src/config/entitysetMapping.js", dest: "build/tools/config" },
{ src: "src/favicon.ico", dest: "build/tools" },
{ src: [ "@vizabi/shared-components/build/VizabiSharedComponents.css", ...getEntryToolsCssFilenames()].map(css=>require.resolve(css))
, dest: "build/tools/assets/css" }
],
copyOnce: true,
verbose: false
}),
//replace ES6 module declaration strings in all tool configs (for IE11 compatibility)
execute(["cd build/tools/config/toolconfigs/ && sed -i '' -e 's/export const VIZABI_MODEL/VIZABI_MODEL/g' *"]),
html({
template: "src/index.html",
externals: getHtmlAssets().map(file => {
return {
file,
pos: "before"
};
})
}),
__PROD__ && eslint(),
__PROD__ && babel({
babelHelpers: "bundled",
include: [
"src/**",
"node_modules/@vizabi/**"
],
presets: [["@babel/preset-env", {
targets: [
">0.2%",
"not dead",
"not op_mini all",
"not safari < 10",
"not chrome < 51",
"not android < 5",
"not ie < 12"
],
modules: false,
useBuiltIns: "entry",
corejs: { version: "3.9" }
}]],
plugins: ["@babel/plugin-proposal-class-properties"]
}),
sourcemaps(),
jsonToJsEmitAssets(
json({
include: /src\/config/,
namedExports:false,
indent: " "
}),
varNameWithFileName("toolsPage_"),
"config/",
/\/([\w-]+)[\w.-]+json$/
),
url({
limit: 0,
fileName: "vendor/[dirname][name][extname]",
include: [
//"**/*.css",
/\.(otf|eot|svg|ttf|woff2?)$/,
],
destDir: "build/tools/assets"
}),
stylus(),
postcss({
include: "**/app*.css",
extract: "styles.css",
plugins: [
postcssUrl({
// Only convert root relative URLs, which CSS-Loader won't process into require().
filter: ({ url }) => url.startsWith("/"),
url: ({ url }) => {
return url.replace(/^\//, "");
}
}),
autoprefixer()
]
}),
replace({
ENV: JSON.stringify(process.env.NODE_ENV || "development")
}),
__PROD__ && terser({
output: {
preamble: copyright,
},
keep_classnames: true,
keep_fnames: true
}),
iife(),
__DEVSERVER__ && serve({
contentBase: ["build"],
port: 4200,
verbose: true
}),
__DEVSERVER__ && livereload("build/"),
__PROD__ && visualizer({
filename: "./build/stats.html"
}),
]
}];