From 9444aa4c530746eb5eb6bd9ecea8a750545b9a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Vin=C3=ADcius?= Date: Thu, 11 Mar 2021 14:27:18 -0300 Subject: [PATCH] fix(build): configure globals and fixes * build: convert dependencies to globals to browser output * fix(build): do not emit sourcemap for browser output * build(support): compile to ES2015 target * build(banner): add library informations to output files --- reactools/build/browser.js | 7 +++ reactools/build/build.js | 12 +++++ .../build/plugins/esbuild-plugin-globals.js | 48 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 reactools/build/plugins/esbuild-plugin-globals.js diff --git a/reactools/build/browser.js b/reactools/build/browser.js index be345c97..82d7ff10 100644 --- a/reactools/build/browser.js +++ b/reactools/build/browser.js @@ -1,8 +1,15 @@ const { build } = require('./build'); +const { globals } = require('./plugins/esbuild-plugin-globals'); build({ platform: 'browser', globalName: 'Reactools', outfile: 'dist/index.browser.js', + sourcemap: false, entryPoints: ['src/index.ts'], + plugins: [ + globals({ + react: 'React', + }), + ], }); diff --git a/reactools/build/build.js b/reactools/build/build.js index a42e7da6..16dbdb92 100644 --- a/reactools/build/build.js +++ b/reactools/build/build.js @@ -1,12 +1,15 @@ const { build: ESBuild } = require('esbuild'); const { resolve } = require('path'); +const pkg = require('../package.json'); + const BASE_PATH = resolve(__dirname, '..'); /** * @type {import('esbuild').BuildOptions} */ const BASE_CONFIG = { + target: 'es2015', logLevel: 'info', tsconfig: 'tsconfig.json', absWorkingDir: BASE_PATH, @@ -16,6 +19,15 @@ const BASE_CONFIG = { sourcemap: true, external: ['react', 'react-dom'], + + banner: { + js: `/** + * @version ${pkg.version} + * + * @license MIT + * @copyright Hitechline + */`, + }, }; /** diff --git a/reactools/build/plugins/esbuild-plugin-globals.js b/reactools/build/plugins/esbuild-plugin-globals.js new file mode 100644 index 00000000..e0632e24 --- /dev/null +++ b/reactools/build/plugins/esbuild-plugin-globals.js @@ -0,0 +1,48 @@ +// By -> https://github.com/a-b-r-o-w-n/esbuild-plugin-globals + +const generateResolveFilter = globals => { + const moduleNames = Object.keys(globals); + + return new RegExp(`^(${moduleNames.join('|')})$`); +}; + +// eslint-disable-next-line consistent-return +const generateExport = (globals, name) => { + const match = Object.entries(globals).find(([pattern]) => { + return new RegExp(`^${pattern}$`).test(name); + }); + + if (match) { + const output = typeof match[1] === 'function' ? match[1](name) : match[1]; + + return output ? `module.exports = ${output}` : undefined; + } +}; + +const plugin = (globals = {}) => { + const filter = generateResolveFilter(globals); + + return { + name: 'globals', + setup(build) { + build.onResolve({ filter }, args => { + return { path: args.path, namespace: 'globals' }; + }); + + build.onLoad({ filter: /.*/, namespace: 'globals' }, args => { + const name = args.path; + const contents = generateExport(globals, name); + + if (contents) { + return { contents }; + } + + return null; + }); + }, + }; +}; + +module.exports = { + globals: plugin, +};