-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.js
114 lines (103 loc) · 4.01 KB
/
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
"use strict";
let generateTranspiler = require("./babel");
let { requireOptional } = require("../util");
let determineBrowserslist = require("faucet-pipeline/lib/util/browserslist");
let commonjs = require("rollup-plugin-commonjs");
let nodeResolve = require("rollup-plugin-node-resolve");
let GLOBAL_SHIM = `
if(typeof global === "undefined" && typeof window !== "undefined") {
window.global = window;
}`.trim();
module.exports = generateConfig;
// generates Rollup configuration
// * `extensions` is a list of additional file extensions for loading modules
// (e.g. `[".jsx"]`)
// * `externals` determines which modules/packages to exclude from the bundle
// (e.g. `{ jquery: "jQuery" }` - the key refers to the respective
// module/package name, the value refers to a corresponding global variable)
// * `format` determines the bundle format (defaults to IIFE); cf.
// https://github.com/rollup/rollup/wiki/JavaScript-API#format
// * `moduleName` determines the global variable to hold the entry point's
// exports (if any)
// * `esnext`, if truthy, activates ESNext transpilation
// * `esnext.browserslist`, if `false`, suppresses automatic configuration
// via Browserslist - alternatively, a path to a directory containing a
// custom Browserslist configuration
// * `esnext.exclude` is a list of modules for which to skip transpilation
// (e.g. `esnext: { exclude: ["jquery"] }`, perhaps due to a distribution
// already optimized for ES5)
// * `jsx`, if truthy, activates JSX transpilation
// * `jsx.pragma` determines the function to use for JSX expressions
// (e.g. `jsx: { pragma: "createElement" }`)
// * additionally accepts the same options as `esnext`
// * `typescript`, if truthy, activates TypeScript transpilation - anything
// other than `true` will be passed through as TypeScript compiler options
// * `compact`, if truthy, compresses the bundle's code while retaining the
// source code's original structure
function generateConfig({ extensions = [], externals, format, moduleName,
esnext, jsx, typescript, compact }, { browsers, resolvePath }) {
let plugins = [];
if(esnext || jsx) {
let transpiler = Object.assign({}, esnext, jsx);
if(esnext) {
transpiler.esnext = true;
}
if(jsx) {
delete transpiler.pragma; // just to be safe, clean up JSX-specifics
transpiler.jsx = jsx;
}
let { browserslist } = transpiler
if(browserslist === false) {
browsers = null;
} else if(browserslist) {
let dir = resolvePath(browserslist, { enforceRelative: true });
browsers = determineBrowserslist(dir);
if(!browsers) {
throw new Error(`failed to detect Browserslist in ${browserslist}`);
}
}
let { plugin, extensions: ext } = generateTranspiler(transpiler, { browsers });
extensions = ext.concat(extensions);
plugins.push(plugin);
}
if(typescript) {
let ts = requireOptional("rollup-plugin-typescript2",
"failed to activate TypeScript", "faucet-pipeline-typescript");
extensions.push(".ts");
// TODO: provide defaults and abstractions for low-level options?
plugins.push(typescript === true ? ts() : ts(typescript));
}
let resolve = { jsnext: true };
if(extensions.length) {
resolve.extensions = [".js"].concat(extensions);
}
plugins = plugins.concat([
nodeResolve(resolve),
commonjs({ include: "node_modules/**" })
]);
if(compact) {
let cleanup = requireOptional("rollup-plugin-cleanup",
"failed to activate compacting");
plugins = plugins.concat(cleanup());
}
let cfg = { format, plugins };
if(moduleName) {
cfg.name = moduleName;
}
if(externals) { // excluded from bundle
cfg.external = Object.keys(externals);
cfg.globals = externals;
}
// distinguish between (roughly) read and write settings
let read = ["external", "paths", "plugins"];
return Object.keys(cfg).reduce((memo, key) => {
let type = read.includes(key) ? "readConfig" : "writeConfig";
memo[type][key] = cfg[key];
return memo;
}, {
readConfig: {},
writeConfig: {
intro: GLOBAL_SHIM // cf. http://2ality.com/2016/09/global.html
}
});
}