reboost
Version:
A super fast dev server for rapid web development
112 lines (111 loc) • 4.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginName = exports.esbuildPlugin = void 0;
const tslib_1 = require("tslib");
const esbuild = (0, tslib_1.__importStar)(require("esbuild"));
const utils_1 = require("../utils");
const PluginName = 'core-esbuild-plugin';
exports.PluginName = PluginName;
function esbuildPlugin(options = {}) {
const defaultOptions = {
loaders: {
js: 'ts',
jsx: 'tsx',
mjs: 'ts',
cjs: 'ts',
es6: 'ts',
es: 'ts',
ts: 'ts',
tsx: 'tsx'
},
jsx: {
factory: 'React.createElement',
fragment: 'React.Fragment'
},
target: 'es2020',
minify: true,
define: undefined,
};
let compatibleTypes;
let minifyOptions;
return {
name: PluginName,
getCacheKey: ({ serializeObject }) => serializeObject(options),
setup({ config, chalk }) {
defaultOptions.minify = !config.debugMode;
options = (0, utils_1.merge)(defaultOptions, options);
compatibleTypes = Object.keys(options.loaders);
const minifyOption = (key) => (typeof options.minify === 'object' ? options.minify[key] : options.minify);
minifyOptions = {
syntax: minifyOption('syntax'),
whitespace: minifyOption('whitespace')
};
// TODO: Remove in v1.0
const aOpts = options;
const showWarning = (oldOpt, newOpt) => {
if (!config.log)
return;
let message = `esbuildPlugin: options.${oldOpt} is deprecated and will be removed in next major release. `;
message += `Use options.${newOpt} instead.`;
console.log(chalk.yellow(message));
};
if (aOpts.jsxFactory) {
showWarning('jsxFactory', 'jsx.factory');
aOpts.jsx.factory = aOpts.jsxFactory;
}
if (aOpts.jsxFragment) {
showWarning('jsxFragment', 'jsx.fragment');
aOpts.jsx.fragment = aOpts.jsxFragment;
}
},
async transformContent(data, filePath) {
if (compatibleTypes.includes(data.type)) {
try {
const { code, map, warnings } = await esbuild.transform(data.code, {
sourcemap: 'external',
sourcefile: this.rootRelative(filePath),
loader: options.loaders[data.type],
jsxFactory: options.jsx.factory,
jsxFragment: options.jsx.fragment,
target: options.target,
minifyIdentifiers: false,
minifySyntax: minifyOptions.syntax,
minifyWhitespace: minifyOptions.whitespace,
define: options.define
});
if (this.config.log) {
warnings.forEach(({ location: { line, column, lineText, file }, text }) => {
const lText = text.toLowerCase();
if (lText.includes('unsupported source map'))
return;
let msg = `esbuildPlugin: Warning "${file}"\n\n`;
msg += `(${line}:${column}) ${text}\n`;
msg += `| ${lineText}`;
this.emitWarning(msg);
});
}
return {
code,
map: JSON.parse(map),
type: 'js'
};
}
catch (e) {
if (e.errors != null) {
const error = e.errors[0];
let msg = `esbuildPlugin: Error when processing "${error.location.file}"\n`;
msg += `${error.text} on line ${error.location.line} at column ${error.location.column}\n\n`;
msg += `| ${error.location.lineText}`;
return new Error(msg);
}
else {
console.error(e);
return null;
}
}
}
return null;
}
};
}
exports.esbuildPlugin = esbuildPlugin;