improve rollup build

This commit is contained in:
Rene Haas
2022-08-21 10:32:20 +02:00
parent 52c3f83e21
commit 4016920c9b
8 changed files with 54 additions and 84 deletions
@@ -0,0 +1,13 @@
const fs = require('fs');
module.exports = (files) => ({
buildStart() {
if (files) {
files.forEach((file) => {
if (fs.existsSync(file)) {
this.addWatchFile(file);
}
});
}
},
});
+46
View File
@@ -0,0 +1,46 @@
const { extname } = require('path');
const { transform } = require('esbuild');
const { createFilter } = require('@rollup/pluginutils');
const defaultLoader = {
'.js': 'js',
'.jsx': 'jsx',
'.ts': 'ts',
'.tsx': 'tsx',
};
module.exports = ({ include, exclude, ...esbuildOptions } = {}) => {
const extensions = Object.keys(defaultLoader);
const INCLUDE_REGEXP = new RegExp(`\\.(${extensions.map((ext) => ext.slice(1)).join('|')})$`);
const EXCLUDE_REGEXP = /node_modules/;
const filter = createFilter(include || INCLUDE_REGEXP, exclude || EXCLUDE_REGEXP);
return {
name: 'esbuild',
async transform(code, id) {
if (!filter(id)) {
return null;
}
const ext = extname(id);
const loader = defaultLoader[ext];
if (!loader) {
return null;
}
const result = await transform(code, {
sourcefile: id,
loader,
...esbuildOptions,
});
return (
result.code && {
code: result.code,
map: result.map || null,
}
);
},
};
};
+102
View File
@@ -0,0 +1,102 @@
const rollupPluginHtml = require('@rollup/plugin-html');
const makeHtmlAttributes = (attributes) => {
if (!attributes) {
return '';
}
const keys = Object.keys(attributes);
// eslint-disable-next-line no-param-reassign
// eslint-disable-next-line no-return-assign
return keys.reduce((result, key) => (result += ` ${key}="${attributes[key]}"`), '');
};
const genHtmlTemplateFunc = (contentOrContentFn) => ({
attributes,
files,
meta,
publicPath,
title,
}) => {
const scripts = (files.js || [])
.map(
({ fileName }) =>
`<script src="${publicPath}${fileName}"${makeHtmlAttributes(attributes.script)}></script>`
)
.join('\n');
const links = (files.css || [])
.map(
({ fileName }) =>
`<link href="${publicPath}${fileName}" rel="stylesheet"${makeHtmlAttributes(
attributes.link
)}>`
)
.join('\n');
const metas = meta.map((input) => `<meta${makeHtmlAttributes(input)}>`).join('\n');
return `<!doctype html>
<html${makeHtmlAttributes(attributes.html)}>
<head>
${metas}
<title>${title}</title>
<style>
html,
body {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
box-sizing: border-box;
}
body {
padding: 10px;
}
*::before,
*::after {
box-sizing: border-box;
}
* {
box-sizing: inherit;
}
#testResult {
display: none;
position: fixed;
top: 0;
right: 0;
padding: 5px;
background: white;
}
#testResult.passed {
display: block;
background: lime;
}
#testResult.passed::before {
content: 'passed';
}
#testResult.failed {
display: block;
background: red;
}
#testResult.failed::before {
content: 'failed';
}
</style>
${links}
</head>
<body>
${(typeof contentOrContentFn === 'function' ? contentOrContentFn() : contentOrContentFn) || ''}
${scripts}
<div id="testResult"></div>
</body>
</html>`;
};
module.exports = (title, fileName, getHtmlContent) =>
rollupPluginHtml({
title,
fileName,
template: genHtmlTemplateFunc(getHtmlContent),
meta: [{ charset: 'utf-8' }, { 'http-equiv': 'X-UA-Compatible', content: 'IE=edge' }],
});
+33
View File
@@ -0,0 +1,33 @@
const { createFilter } = require('@rollup/pluginutils');
const istanbul = require('istanbul-lib-instrument');
module.exports = (options = {}) => {
const filter = createFilter(options.include, options.exclude);
return {
name: 'istanbul',
transform(code, id) {
if (!filter(id)) {
return;
}
const instrumenter = istanbul.createInstrumenter({
esModules: true,
compact: true,
produceSourceMap: true,
autoWrap: true,
preserveComments: true,
...options.instrumenterConfig,
});
const combinedSourceMap = this.getCombinedSourcemap();
const instrumentedCode = instrumenter.instrumentSync(code, id, {
...combinedSourceMap,
version: String(combinedSourceMap.version),
});
const sourceMap = instrumenter.lastSourceMap();
return { code: instrumentedCode, map: { ...sourceMap, version: Number(sourceMap.version) } };
},
};
};