forked from SVG-Edit/svgedit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
157 lines (149 loc) · 4.97 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
/* eslint-env node */
// This rollup script is run by the command:
// 'npm run build'
import path from 'path';
import {lstatSync, readdirSync} from 'fs';
import rimraf from 'rimraf';
import babel from '@rollup/plugin-babel';
import copy from 'rollup-plugin-copy';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import nodePolyfills from 'rollup-plugin-node-polyfills';
import url from '@rollup/plugin-url'; // for XML/SVG files
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
import {terser} from 'rollup-plugin-terser';
// import progress from 'rollup-plugin-progress';
import filesize from 'rollup-plugin-filesize';
// utility function
const getDirectories = (source) => {
const isDirectory = (dir) => {
return lstatSync(dir).isDirectory();
};
return readdirSync(source).map((nme) => path.join(source, nme)).filter((i) => isDirectory(i));
};
// capture the list of files to build for extensions and ext-locales
const extensionDirs = getDirectories('src/editor/extensions');
const dest = ['dist/editor', 'dist/editor/system'];
// remove existing distribution
// eslint-disable-next-line no-console
rimraf('./dist', () => console.info('recreating dist'));
// config for svgedit core module
const config = [{
input: ['src/editor/index.js'],
output: [
{
format: 'es',
inlineDynamicImports: true,
sourcemap: true,
file: 'dist/editor/index.js'
},
{
format: 'es',
inlineDynamicImports: true,
sourcemap: true,
file: 'dist/editor/xdomain-index.js',
intro: 'const XDOMAIN = true;'
},
{
format: 'system',
dir: 'dist/editor/system',
inlineDynamicImports: true
}
],
plugins: [
// progress(),
copy({
targets: [
{
src: 'src/editor/index.html',
dest: 'dist/editor'
},
{
src: 'src/editor/index.html',
dest: 'dist/editor',
rename: 'xdomain-index.html',
transform: (contents) => contents.toString()
.replace('<script type="module" src="index.js">', '<script type="module" src="xdomain-index.js">')
},
{
src: 'src/editor/index.html',
dest: ['dist/editor/system'],
rename: 'index.html',
transform: (contents) => contents.toString()
.replace('<script type="module" src="index.js">',
`<script>
const systemJsLoaderTag = document.createElement('script');
systemJsLoaderTag.src = './s.min.js';
systemJsLoaderTag.addEventListener('load', function () {
System.import('./index.js');
});
document.head.appendChild(systemJsLoaderTag);
`)
},
{
src: ['node_modules/systemjs/dist/s.min.js', 'node_modules/systemjs/dist/s.min.js.map'],
dest: 'dist/editor/system'
},
{src: 'src/editor/images', dest},
{src: 'src/editor/shapelib', dest},
{src: 'src/editor/jgraduate', dest},
{src: 'src/editor/spinbtn', dest},
{src: 'src/editor/embedapi.html', dest},
{src: 'src/editor/embedapi.js', dest},
{src: 'src/editor/browser-not-supported.html', dest},
{src: 'src/editor/redirect-on-lacking-support.js', dest},
{src: 'src/editor/svgedit.css', dest}
]
}),
nodeResolve({
browser: true,
preferBuiltins: true
}),
commonjs(),
dynamicImportVars({include: `src/editor/locale.js`}),
babel({babelHelpers: 'bundled', exclude: [/\/core-js\//]}), // exclude core-js to avoid circular dependencies.
nodePolyfills(),
terser({keep_fnames: true}), // keep_fnames is needed to avoid an error when calling extensions.
filesize()
]
}];
// config for dynamic extensions
extensionDirs.forEach((extensionDir) => {
const extensionName = path.basename(extensionDir);
extensionName && config.push(
{
input: `./src/editor/extensions/${extensionName}/${extensionName}.js`,
output: [
{
format: 'es',
dir: `dist/editor/extensions/${extensionName}`,
inlineDynamicImports: true,
sourcemap: true
},
{
format: 'system',
dir: `dist/editor/system/extensions/${extensionName}`,
inlineDynamicImports: true
}
],
plugins: [
// progress(),
url({
include: ['**/*.svg', '**/*.png', '**/*.jpg', '**/*.gif', '**/*.xml'],
limit: 0,
fileName: '[name][extname]'
}),
nodeResolve({
browser: true,
preferBuiltins: true
}),
commonjs({exclude: `src/editor/extensions/${extensionName}/${extensionName}.js`}),
dynamicImportVars({include: `src/editor/extensions/${extensionName}/${extensionName}.js`}),
babel({babelHelpers: 'bundled', exclude: [/\/core-js\//]}),
nodePolyfills(),
terser({keep_fnames: true})
]
}
);
});
export default config;