-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollup.config.js
160 lines (155 loc) · 4.76 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
import path from 'path';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import glob from 'glob';
import babel from 'rollup-plugin-babel';
import postcss from 'rollup-plugin-postcss';
import { string } from 'rollup-plugin-string';
import vue from 'rollup-plugin-vue';
/**
* Returns true if an import is not considered for inlining into the current file.
*/
const isExternalModule = (moduleId) => {
if (
/*
SCSS files are considered to be included, because they will be extracted
later with the help of rollup-plugin-postcss
*/
moduleId.endsWith('.scss') ||
/**
* We want to inline markdown in our documentation, in order to be able to
* use it directly in Pajamas
*/
moduleId.endsWith('.md') ||
/**
* scss_to_js/ contains generated variables from our scss.
* All of the variables would blow up the build size, so inlining only the
* variables actually used in our JavaScript
*/
moduleId.includes('scss_to_js/') ||
/**
* We want to inline the `<script>` portion of our Vue Single File components
* Those are added as `modules` with an url like filename:
* `component.js?rollup-plugin-vue=script.js`
*/
moduleId.includes('?rollup-plugin-vue=')
) {
return false;
}
/**
* Everything else is an "external module", this means we do not roll up external
* dependencies (bootstrap-vue / lodash / ...) or internal imports (e.g. ./button)
*
* This allows us to make @gitlab/ui treeshakeable.
*/
return true;
};
/**
* Fixes import files of compiled files
*
* @param {String} code Compiled code of the file
*/
const fixImports = (code) => {
return (
code
/**
* Remove `.vue` from imports, as we are compiling them to JS
*
* from './components/base/icon/icon.vue';
* =>
* from './components/base/icon/icon';
*/
.replace(/(from\s+(["']).+?)\.vue(\2)/g, '$1$3')
/**
* Replace direct imports from bootstrap-vue/src with bootstrap-vue/esm,
* as it is a pre-compiled, treeshakeable version we can use in GitLab
*
* from 'bootstrap-vue/src';
* =>
* from 'bootstrap-vue/esm/';
*/
.replace(/(from\s+["'])bootstrap-vue\/src\//g, '$1bootstrap-vue/esm/')
/**
* Replace direct imports from bootstrap-vue with bootstrap-vue/esm,
* as it is a pre-compiled, treeshakeable version we can use in GitLab
*
* from 'bootstrap-vue';
* =>
* from 'bootstrap-vue/esm/index.js';
*/
.replace(/(from\s+(["']))bootstrap-vue(\2)/g, '$1bootstrap-vue/esm/index.js$3')
);
};
export default glob
.sync('src/**/!(*.stories|*.spec).+(js|vue)')
.concat('utility_classes.js')
.map((input) => {
const outputFilename = input.replace(/^src\//, '').replace(/\.(vue|js)$/, '');
return {
external: isExternalModule,
input,
output: {
format: 'esm',
file: `dist/${outputFilename}.js`,
},
plugins: [
replace({
delimiters: ['/* ', ' */'],
include: 'src/index.js',
values: {
'auto-inject-styles': "import './scss/gitlab_ui.scss';",
},
}),
replace({
delimiters: ['/* ', ' */'],
include: 'src/scss/utilities.scss',
values: {
'auto-inject-scss-lib': `
@import './functions';
@import './variables';
@import './utility-mixins/index';
`,
},
}),
postcss({
extract: true,
minimize: true,
sourceMap: true,
use: [['sass', { includePaths: [path.resolve(__dirname, 'node_modules')] }]],
}),
string({
include: '**/*.md',
}),
vue({
/**
* Per default rollup-plugin-vue includes a `.mjs` version of
* the component normalizer, which doesn't play well with jest
* For this reason we include the common js version
*/
normalizer: '~vue-runtime-helpers/dist/normalize-component.js',
}),
babel({
exclude: ['node_modules/!(bootstrap-vue)/**'],
}),
resolve(),
commonjs({
namedExports: {
echarts: ['echarts'],
},
ignore: ['@gitlab/svgs/dist/icons.json'],
}),
{
name: 'fix-imports',
generateBundle(options, bundle) {
Object.keys(bundle).forEach((key) => {
if (bundle[key].code) {
// eslint-disable-next-line no-param-reassign
bundle[key].code = fixImports(bundle[key].code);
}
});
},
},
],
};
});