-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrollup.common-config.js
60 lines (49 loc) · 1.84 KB
/
rollup.common-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
import fs from 'fs';
import path from 'path';
import userScriptCss from 'rollup-plugin-userscript-css';
import alias from 'rollup-plugin-alias';
import replace from 'rollup-plugin-replace';
import glob from 'glob';
import resolve from 'resolve';
import packageJson from './package.json';
export const getMetatagLinesFromFile = (filePath) => {
const templateFileContent = fs.readFileSync(filePath, 'utf-8');
const templateLines = templateFileContent.split(/[\n\r]+/);
return templateLines.filter(text => /^\s*\/\/\s*@\w+\b/.test(text));
};
const getUserscriptVersion = (entryFile) => {
const versionMetatag = getMetatagLinesFromFile(entryFile).find(line => /@version\b/.test(line));
if (!versionMetatag) {
throw Error('Please specify a @version metatag');
}
const version = versionMetatag.match(/@version\s+(.+)\s*/)[1];
if (!version) {
throw Error('Please specify a @version metatag');
}
return version;
};
export const getCommonPlugins = entryFile => [
userScriptCss(),
alias({
__SITE_SPECIFIC_FUNCTIONS__: path.resolve(__dirname, entryFile),
}),
replace({
__CARD_STYLE__: () => fs.readFileSync('./src/card_style.css', 'utf-8').replace(/[\n\r\s]/gm, ''),
__PROJECT_GITHUB_ISSUES_URL__: packageJson.bugs.url,
__ANKI_ADD_HOOKS_VERSION__: packageJson.version,
__USERSCRIPT_VERSION__: getUserscriptVersion(entryFile),
})
];
const isDirectory = file => fs.statSync(file).isDirectory();
const isJsFile = file => /.m?js$/.test(file);
export const getEntryPoints = () =>
glob.sync('./src/hooks/*')
.filter(
file =>
(isDirectory(file) || isJsFile(file)) // Module as direct js file or a module folder
&& !/\bmy_website_com.js$/.test(file) // Not the new hook template
)
.reduce((obj, file) => {
obj[path.basename(file, '.js')] = resolve.sync(file);
return obj;
}, {});