forked from stylelint/stylelint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.mjs
74 lines (65 loc) · 1.8 KB
/
rollup.config.mjs
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
import assert from 'node:assert';
import { fileURLToPath } from 'node:url';
import { relative } from 'node:path';
import { rmSync } from 'node:fs';
import fg from 'fast-glob';
const inputFiles = fg.globSync([
'lib/**/*.mjs',
'!**/__tests__/**',
'!lib/testUtils/**',
// NOTE: We cannot support CommonJS for `cli.mjs` since the `meow` dependency is pure ESM.
'!lib/cli.mjs',
]);
// clean up
for (const input of inputFiles) {
rmSync(input.replace('.mjs', '.cjs'), { force: true });
}
const rootDir = fileURLToPath(new URL('.', import.meta.url));
/** @type {import('rollup').RollupOptions[]} */
export default inputFiles.map((input) => {
return {
input,
output: {
format: 'cjs',
dir: rootDir,
entryFileNames: ({ facadeModuleId }) => {
assert.ok(facadeModuleId);
return relative(rootDir, facadeModuleId).replace('.mjs', '.cjs');
},
generatedCode: {
preset: 'es2015',
symbols: false,
},
interop: 'default',
esModule: false,
preserveModules: true,
validate: true,
banner:
'// NOTICE: This file is generated by Rollup. To modify it,\n// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).',
plugins: [addWarningForCommonJS()],
},
treeshake: false,
};
});
/**
* @see https://rollupjs.org/plugin-development/
* @returns {import('rollup').OutputPlugin}
*/
function addWarningForCommonJS() {
return {
name: 'stylelint-add-warning-for-cjs',
generateBundle(_options, bundle) {
const asset = bundle['lib/createStylelint.cjs'];
if (asset?.type === 'chunk') {
asset.code = asset.code.replace(
' // [INSERT HERE] CommonJS deprecation code',
`
if (!options.quietDeprecationWarnings) {
console.warn('The CommonJS Node.js API is deprecated. See https://stylelint.io/migration-guide/to-16');
}
`,
);
}
},
};
}