Skip to content

Commit 626e975

Browse files
Modularize rollup.config.js to improve readability (#4852)
* Modularize rollup.config.js to improve readability * globals and externals are related for our uses so removing an extra input
1 parent 8b9ef5d commit 626e975

File tree

1 file changed

+37
-133
lines changed

1 file changed

+37
-133
lines changed

packages/model-viewer/rollup.config.js

Lines changed: 37 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -30,48 +30,45 @@ const onwarn = (warning, warn) => {
3030
}
3131
};
3232

33-
let plugins = [resolve({ dedupe: 'three' }), replace({ 'Reflect.decorate': 'undefined' })];
33+
let commonPlugins = [
34+
resolve({ dedupe: 'three' }),
35+
replace({ 'Reflect.decorate': 'undefined' })
36+
];
3437

3538
const watchFiles = ['lib/**'];
3639

37-
const outputOptions = [
38-
{
39-
input: './lib/model-viewer.js',
40-
output: {
41-
file: './dist/model-viewer.js',
42-
sourcemap: true,
43-
format: 'esm',
44-
name: 'ModelViewerElement',
45-
},
46-
watch: {
47-
include: watchFiles,
48-
},
49-
plugins,
50-
onwarn,
51-
},
52-
{
40+
const createModelViewerOutput = (file, format, plugins = commonPlugins, external = []) => {
41+
const globals = external.reduce((acc, mod) => {
42+
acc[mod] = mod; // Assuming global variable names are the same as module names
43+
return acc;
44+
}, {});
45+
46+
return {
5347
input: './lib/model-viewer.js',
5448
output: {
55-
file: './dist/model-viewer-module.js',
49+
file,
50+
format,
5651
sourcemap: true,
57-
format: 'esm',
5852
name: 'ModelViewerElement',
59-
globals: {
60-
'three': 'three',
61-
},
53+
globals
6254
},
63-
external: ['three'],
55+
external,
6456
watch: {
65-
include: watchFiles,
57+
include: watchFiles
6658
},
6759
plugins,
68-
onwarn,
69-
}
60+
onwarn
61+
};
62+
};
63+
64+
const outputOptions = [
65+
createModelViewerOutput('./dist/model-viewer.js', 'esm'),
66+
createModelViewerOutput('./dist/model-viewer-module.js', 'esm', commonPlugins, ['three'])
7067
];
7168

7269
if (NODE_ENV !== 'development') {
7370
const pluginsIE11 = [
74-
...plugins,
71+
...commonPlugins,
7572
commonjs(),
7673
polyfill(['object.values/auto']),
7774
cleanup({
@@ -84,114 +81,21 @@ if (NODE_ENV !== 'development') {
8481

8582
// IE11 does not support modules, so they are removed here, as well as in a
8683
// dedicated unit test build which is needed for the same reason.
87-
outputOptions.push({
88-
input: './lib/model-viewer.js',
89-
output: {
90-
file: './dist/model-viewer-umd.js',
91-
sourcemap: true,
92-
format: 'umd',
93-
name: 'ModelViewerElement',
94-
},
95-
watch: {
96-
include: watchFiles,
97-
},
98-
plugins: pluginsIE11,
99-
onwarn,
100-
});
101-
102-
/** Bundled w/o three */
103-
104-
// IE11 does not support modules, so they are removed here, as well as in a
105-
// dedicated unit test build which is needed for the same reason.
106-
outputOptions.push({
107-
input: './lib/model-viewer.js',
108-
output: {
109-
file: './dist/model-viewer-module-umd.js',
110-
sourcemap: true,
111-
format: 'umd',
112-
name: 'ModelViewerElement',
113-
globals: {
114-
'three': 'three',
115-
},
116-
},
117-
external: ['three'],
118-
watch: {
119-
include: watchFiles,
120-
},
121-
plugins: pluginsIE11,
122-
onwarn,
123-
});
84+
outputOptions.push(
85+
createModelViewerOutput('./dist/model-viewer-umd.js', 'umd', pluginsIE11),
86+
/** Bundled w/o three */
87+
createModelViewerOutput('./dist/model-viewer-module-umd.js', 'umd', pluginsIE11, ['three'])
88+
);
12489

12590
// Minified Versions
126-
plugins = [...plugins, terser()];
127-
128-
outputOptions.push({
129-
input: './dist/model-viewer.js',
130-
output: {
131-
file: './dist/model-viewer.min.js',
132-
sourcemap: true,
133-
format: 'esm',
134-
name: 'ModelViewerElement',
135-
},
136-
watch: {
137-
include: watchFiles,
138-
},
139-
plugins,
140-
onwarn,
141-
});
142-
143-
outputOptions.push({
144-
input: './dist/model-viewer-umd.js',
145-
output: {
146-
file: './dist/model-viewer-umd.min.js',
147-
sourcemap: true,
148-
format: 'umd',
149-
name: 'ModelViewerElement',
150-
},
151-
watch: {
152-
include: watchFiles,
153-
},
154-
plugins,
155-
onwarn,
156-
});
157-
158-
outputOptions.push({
159-
input: './dist/model-viewer-module.js',
160-
output: {
161-
file: './dist/model-viewer-module.min.js',
162-
sourcemap: true,
163-
format: 'esm',
164-
name: 'ModelViewerElement',
165-
globals: {
166-
'three': 'three',
167-
},
168-
},
169-
external: ['three'],
170-
watch: {
171-
include: watchFiles,
172-
},
173-
plugins,
174-
onwarn,
175-
});
176-
177-
outputOptions.push({
178-
input: './dist/model-viewer-module-umd.js',
179-
output: {
180-
file: './dist/model-viewer-module-umd.min.js',
181-
sourcemap: true,
182-
format: 'umd',
183-
name: 'ModelViewerElement',
184-
globals: {
185-
'three': 'three',
186-
},
187-
},
188-
external: ['three'],
189-
watch: {
190-
include: watchFiles,
191-
},
192-
plugins,
193-
onwarn,
194-
});
91+
const minifiedPlugins = [...commonPlugins, terser()];
92+
93+
outputOptions.push(
94+
createModelViewerOutput('./dist/model-viewer.min.js', 'esm', minifiedPlugins),
95+
createModelViewerOutput('./dist/model-viewer-umd.min.js', 'umd', minifiedPlugins),
96+
createModelViewerOutput('./dist/model-viewer-module.min.js', 'esm', minifiedPlugins, ['three']),
97+
createModelViewerOutput('./dist/model-viewer-module-umd.min.js', 'umd', minifiedPlugins, ['three'])
98+
);
19599

196100
outputOptions.push({
197101
input: './lib/model-viewer.d.ts',

0 commit comments

Comments
 (0)