-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathbundle-task.js
159 lines (149 loc) · 5.67 KB
/
bundle-task.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
import gulp from 'gulp';
import size from 'gulp-vinyl-size';
import terser from 'gulp-terser';
import vinylSourceStream from 'vinyl-source-stream';
import vinylBufferStream from 'vinyl-buffer';
import { observableFromStreams, targetDir } from './util.js';
import { memoizeTask } from './memoize-task.js';
import { forkJoin as ObservableForkJoin } from 'rxjs';
import { toArray as observableToArray } from 'rxjs/operators/index.js';
import * as Path from 'node:path';
import { execSync } from 'node:child_process';
import gulpEsbuild from 'gulp-esbuild';
import esbuildAlias from 'esbuild-plugin-alias';
import rollupStream from '@rollup/stream';
import rollupPluginCommonJS from '@rollup/plugin-commonjs';
import rollupPluginNodeResolve from '@rollup/plugin-node-resolve';
import rollupPluginAlias from '@rollup/plugin-alias';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import webpack from 'webpack-stream';
import vinylNamed from 'vinyl-named';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = Path.dirname(__filename);
const bundleDir = Path.resolve(__dirname, '../integration');
const srcDir = Path.join(bundleDir, 'src');
const srcGlob = Path.join(srcDir, '**/*.js');
export function bundleTask(bundler, target, format, ...rest) {
switch (bundler) {
case 'esbuild': return esbuildTask(target, format, ...rest);
case 'rollup': return rollupTask(target, format, ...rest);
case 'webpack': return webpackTask(target, format, ...rest);
}
}
export const esbuildTask = ((cache) => memoizeTask(cache, function pkgEsbuild(target, format, opts = { minify: true }) {
const pkgDir = Path.resolve(__dirname, '../', targetDir(target, format));
const outDir = Path.join(bundleDir, 'esbuild', target, format);
const aliases = [
'js',
'cjs',
'mjs',
'ts'
].reduce((aliases, ext) => {
return [
'Ix',
'dom',
'node',
'iterable',
'asynciterable',
'iterable/operators',
'asynciterable/operators'
].reduce((aliases, entrypoint) => ({
...aliases,
[`ix/${entrypoint}.${ext}`]: Path.join(pkgDir, `${entrypoint}.${ext}`)
}), aliases);
}, {});
return observableFromStreams(
gulp.src(srcGlob),
gulpEsbuild({
bundle: true,
minify: opts.minify,
treeShaking: true,
plugins: [
esbuildAlias(aliases),
],
}),
gulp.dest(outDir),
size({ gzip: true })
);
}))({});
export const rollupTask = ((cache) => memoizeTask(cache, async function pkgRollup(target, format, opts = { minify: true }) {
const pkgDir = Path.resolve(__dirname, '../', targetDir(target, format));
const outDir = Path.join(bundleDir, 'rollup', target, format);
const filePaths = (
await observableToArray()(
observableFromStreams(gulp.src(srcGlob))
).toPromise())
.filter(({ path }) => path.endsWith('.js'))
.map(({ path }) => path.replace(/\.js$/, ''));
return await ObservableForkJoin(
filePaths.map((filePath) => observableFromStreams(
rollupStream({
input: `${filePath}.js`,
output: { format: 'cjs' },
treeshake: true,
plugins: [
rollupPluginAlias({
entries: { 'ix': pkgDir }
}),
rollupPluginNodeResolve(),
rollupPluginCommonJS()
],
onwarn: (message) => {
if (message.code === 'CIRCULAR_DEPENDENCY') return
console.error(message);
}
}),
vinylSourceStream(Path.relative(srcDir, `${filePath}.js`)),
vinylBufferStream(),
...(opts.minify ? [terser()] : []),
gulp.dest(outDir),
size({ gzip: true })
))
).toPromise();
}))({});
export const webpackTask = ((cache) => memoizeTask(cache, function pkgWebpack(target, format, opts = { minify: true, analyze: false }) {
const pkgDir = Path.resolve(__dirname, '../', targetDir(target, format));
const outDir = Path.join(bundleDir, 'webpack', target, format);
return observableFromStreams(
gulp.src(srcGlob),
vinylNamed((file) => file.path),
webpack({
mode: opts?.minify == false ? 'development' : 'production',
optimization: {
usedExports: true
},
output: {
filename: ({ runtime: name }) => Path.relative(srcDir, name)
},
module: {
rules: [
{
resolve: {
fullySpecified: false,
}
}
]
},
resolve: {
extensions: ['.mjs', '.js'],
alias: { 'ix': pkgDir }
},
stats: 'errors-only',
plugins: opts?.analyze ? [new BundleAnalyzerPlugin()] : []
}),
gulp.dest(outDir),
size({ gzip: true })
);
}))({});
export const execBundleTask = ((cache) => memoizeTask(cache, function execBundle(bundler, target, format) {
return observableFromStreams(
gulp.src(Path.join(bundleDir, bundler, target, format, '**', '*.js')),
async (generator) => {
for await (const file of generator) {
console.log(`executing ${file.path}`);
execSync(`${process.argv[0]} ${file.path}`, { stdio: 'inherit' });
}
}
);
}))({});