forked from commercetools/merchant-center-application-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
160 lines (154 loc) · 5.03 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
// Do this as the first thing so that any code reading it knows the right env.
process.env.BUILD_ROLLUP = true;
const fs = require('fs');
const { babel } = require('@rollup/plugin-babel');
const readPkgUp = require('read-pkg-up');
const getBabelPreset = require('@commercetools-frontend/babel-preset-mc-app');
const {
browserslist,
} = require('@commercetools-frontend/mc-scripts/package.json');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const json = require('@rollup/plugin-json');
const commonjs = require('@rollup/plugin-commonjs');
const postcss = require('rollup-plugin-postcss');
const peerDeps = require('rollup-plugin-peer-deps-external');
const builtins = require('rollup-plugin-node-builtins');
const babelPluginImportGraphQL = require('babel-plugin-import-graphql');
const postcssCustomProperties = require('postcss-custom-properties');
const postcssCustomMediaQueries = require('postcss-custom-media');
const postcssColorModFunction = require('postcss-color-mod-function');
const postcssDiscardComments = require('postcss-discard-comments');
const postcssImport = require('postcss-import');
const postcssPresetEnv = require('postcss-preset-env');
const postcssReporter = require('postcss-reporter');
const { packageJson: pkg } = readPkgUp.sync({
cwd: fs.realpathSync(process.cwd()),
});
const [, packageName] = pkg.name.split('@commercetools-frontend/');
const extensions = ['.js', '.ts', '.tsx'];
const babelOptions = getBabelPreset();
const createPlugins = (format) => {
const isFormatEs = format === 'es';
return [
peerDeps({
includeDependencies: true,
}),
babel({
extensions,
babelHelpers: 'runtime',
...babelOptions,
plugins: [
babelPluginImportGraphQL.default,
...babelOptions.plugins,
'babel-plugin-typescript-to-proptypes',
isFormatEs && [
'transform-rename-import',
{
replacements: [{ original: 'lodash', replacement: 'lodash-es' }],
},
],
].filter(Boolean),
}),
// To convert CJS modules to ES6
commonjs({
include: 'node_modules/**',
}),
nodeResolve({
extensions,
mainFields: ['module', 'main', 'jsnext'],
preferBuiltins: true,
modulesOnly: true,
}),
json({ namedExports: false }),
builtins(),
postcss({
include: ['**/*.mod.css'],
exclude: ['node_modules/**/*.css'],
modules: true,
importLoaders: 1,
localIdentName: '[name]__[local]___[hash:base64:5]',
plugins: [
postcssImport(),
postcssPresetEnv({
autoprefixer: {
grid: true,
overrideBrowserslist: browserslist.production,
},
}),
postcssCustomMediaQueries({
importFrom: require.resolve(
'@commercetools-frontend/application-components/materials/media-queries.css'
),
}),
// we need to place the postcssDiscardComments BEFORE postcssCustomProperties,
// otherwise we will end up with a bunch of empty :root elements
// wherever there are imported comments
// see https://github.com/postcss/postcss-custom-properties/issues/123
// and https://github.com/commercetools/ui-kit/pull/173
postcssDiscardComments(),
postcssCustomProperties({
preserve: false,
importFrom: require.resolve(
'@commercetools-uikit/design-system/materials/custom-properties.css'
),
}),
postcssColorModFunction(),
postcssReporter(),
],
}),
];
};
const createConfig = (cliArgs) => {
if (/\/test-utils\//.test(cliArgs.input)) {
return {
input: cliArgs.input,
external: ['crypto'],
output: {
format: 'cjs',
file: `test-utils/index.js`,
},
plugins: createPlugins('cjs'),
};
}
return [
// Bundle for cjs format
{
input: cliArgs.input,
output: {
format: 'cjs',
// Determine by the existence of the `--dir` option if the bundle should generate
// multiple chunks, as `--file` and `--dir` cannot be used together.
...(cliArgs.dir
? {
chunkFileNames: `${packageName}-[name]-[hash].cjs.js`,
entryFileNames: `${packageName}-[name].cjs.js`,
}
: {
file: `dist/${packageName}.cjs.js`,
}),
sourcemap: true,
},
plugins: createPlugins('cjs'),
},
// Bundle for es format
{
input: cliArgs.input,
output: {
format: 'es',
// Determine by the existence of the `--dir` option if the bundle should generate
// multiple chunks, as `--file` and `--dir` cannot be used together.
...(cliArgs.dir
? {
chunkFileNames: `${packageName}-[name]-[hash].es.js`,
entryFileNames: `${packageName}-[name].es.js`,
}
: {
file: `dist/${packageName}.es.js`,
}),
sourcemap: true,
},
plugins: createPlugins('es'),
},
];
};
module.exports = createConfig;