-
Notifications
You must be signed in to change notification settings - Fork 4
/
rollup.config.mjs
235 lines (231 loc) · 6.65 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import fs from "fs";
import cjs from 'rollup-plugin-cjs-es';
import {terser} from 'rollup-plugin-terser';
import babel from '@rollup/plugin-babel';
import resolve from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json";
import iife from "rollup-plugin-iife";
import alias from "@rollup/plugin-alias";
import re from "rollup-plugin-re";
import analyzer from "rollup-plugin-analyzer";
import inject from "@rollup/plugin-inject";
import esInfo from "rollup-plugin-es-info";
import {visualizer} from "rollup-plugin-visualizer";
import chalk from "chalk";
import {fileURLToPath} from "url";
import {resolve as resolvePath} from "path";
function resolvePkg(id) {
const url = import.meta.resolve(id);
return fileURLToPath(url)
}
const DEBUG = process.env.DEBUG === "1";
export default {
input: {
"stylelint-bundle.min": 'index.js'
},
output: [
{
dir: "dist",
format: 'es',
sourcemap: true,
freeze: false,
inlineDynamicImports: true
}
],
onwarn(e) {
if (!/doesn't export names expected by/.test(e.message)) {
console.warn(!e.loc ? e : (e.plugin ? `[${e.plugin}] ` : '') +
e.loc.file + '\n' +
chalk.red(`${e.loc.line}:${e.loc.column}: ${e.message}`) + '\n' +
chalk.gray(e.frame) + '\n\n');
}
},
// shimMissingExports: true,
plugins: [
re({
patterns: [
{
match: /stylelint[\\/]lib[\\/]rules[\\/]index\.mjs/,
test: /(const rules = {\s*)(get '[^']+'\(\) {\s*return import.+?)(\n};\n)/s,
replace: (_, pre, main, post) => {
const imports = [], rules = [];
const rx = /get ('[^']+')\(\) {\s*return import\(('[^']+')\).*?\s*}/sg;
for (let i = 1, m; (m = rx.exec(main)); i++) {
imports.push(`import r${i} from ${m[2]};\n`);
rules.push(`${m[1]}:r${i},\n`);
}
return imports.join('') + pre + rules.join('') + post;
},
},
{
match: /stylelint[\\/]lib[\\/]rules[\\/]function-no-unknown[\\/]index\.mjs/,
test: "JSON.parse(fs.readFileSync(functionsListPath.toString(), 'utf8'))",
replace: fs.readFileSync(resolvePkg('css-functions-list/index.json'), 'utf8'),
},
{
match: /stylelint[\\/]lib[\\/]formatters[\\/]index\.mjs/,
test: /(const formatters = {\s*).*?('[^']+?jsonFormatter\.mjs').+\n};/s,
replace: 'import json from $2; $1json};',
},
{
match: /stylelint[\\/]lib[\\/]getPostcssResult\.mjs/,
test: 'if (filePath) {',
replace: 'if (false) {',
},
{
match: /stylelint[\\/]lib[\\/]standalone\.mjs/,
test: /(const absoluteCodeFilename =)[^;]+/,
replace: '$1 false',
},
{
match: /stylelint[\\/]lib[\\/]standalone\.mjs/,
test: /let fileList = .+?return result;\n}/s,
replace: '}',
},
{
match: /postcss[\\/]lib[\\/](input|css-syntax-error)\.js/,
test: /(let ((path|sourceMap)Available|terminalHighlight|pico) =).*/g,
replace: '$1 false;',
},
{
match: /postcss[\\/]lib[\\/]css-syntax-error\.js/,
test: 'showSourceCode(color) {',
replace: 'showSourceCode() {let color=false;',
},
{
match: /postcss[\\/]lib[\\/]/,
test: "require('./previous-map')",
replace: 'false',
},
{
match: /.*/,
test: /source-map-js[\\/]lib[\\/]source-map-generator\.js/,
replace: resolvePkg("./shim/source-map-generator").replace(/\\/g, '/'),
},
]
}),
alias(makeAlias({
alias: {
"css-tree": "css-tree/dist/csstree.esm",
},
noop: [
"*/FileCache",
"*/getFileIgnorer",
"*/resolveSilent",
"css-functions-list",
"debug",
"fast-glob",
"file-entry-cache",
"global-modules",
"globby",
"ignore",
"meow",
"micromatch",
"node:path",
"node:process",
"picomatch",
"resolve-from",
"source-map-js/*",
"sourceMap",
"table",
"v8-compile-cache",
"write-file-atomic",
],
shim: [
"*/getConfigForFile",
"*/isPathIgnored",
"*/mathMLTags",
"cosmiconfig",
"node:os",
"node:tty",
"node:url",
"node:util",
]
})),
resolve(),
json(),
cjs({nested: true}),
inject({
process: resolvePkg("./shim/process")
}),
iife({
names: id => {
if (/stylelint-bundle\.min\.js/.test(id)) {
return "stylelint";
}
if (/^node:/.test(id)) {
// we only modify internal id so we can get an error when an external id goes wrong
id = id.replace(/^node:/, 'node_');
}
return `_external_${id}`;
}
}),
babel({
babelHelpers: "bundled",
presets: [
["@babel/env",
{
targets: {
/* https://github.com/openstyles/stylus/blob/master/manifest.json
* Chrome: minimum_chrome_version
* FF: strict_min_version
*/
chrome: "55",
firefox: "55"
},
// https://github.com/facebook/regenerator/issues/276
include: ["transform-template-literals"],
exclude: ["transform-regenerator"]
}]
]
}),
!DEBUG && terser({
module: false
}),
DEBUG && esInfo({
file: "stats.json"
}),
DEBUG && analyzer(),
DEBUG && visualizer({
open: true
})
]
};
function makeAlias({alias, noop, shim, ...opts}) {
const entries = [];
const compilePattern = pattern => {
if (typeof pattern === "string") {
const match = pattern.match(/^(\*\/)?(node:)?(.*)(\/\*)?$/);
if (match) {
let rx = match[3];
if (match[2]) {
rx = `(node:)?${rx}`;
}
if (match[1]) {
rx = `.*/${rx}`;
}
if (match[4]) {
rx = `${rx}(/.*)?`;
} else if (match[1]) {
rx = `${rx}(\\.[cm]?js)?`;
}
rx = `^${rx}$`;
return {find: new RegExp(rx), name: match[3]};
}
}
return {find: pattern};
};
for (const key in alias) {
const {find} = compilePattern(key);
entries.push({find, replacement: alias[key]});
}
for (const key of noop) {
const {find} = compilePattern(key);
entries.push({find, replacement: resolvePath("shim/empty")});
}
for (const key of shim) {
const {find, name} = compilePattern(key);
entries.push({find, replacement: resolvePath(`shim/${name}`)});
}
return {entries, ...opts};
}