Skip to content

Commit

Permalink
fix(build): configure globals and fixes
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pablo1v committed Mar 11, 2021
1 parent bf8aa71 commit 9444aa4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions reactools/build/browser.js
Original file line number Diff line number Diff line change
@@ -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',
}),
],
});
12 changes: 12 additions & 0 deletions reactools/build/build.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -16,6 +19,15 @@ const BASE_CONFIG = {
sourcemap: true,

external: ['react', 'react-dom'],

banner: {
js: `/**
* @version ${pkg.version}
*
* @license MIT
* @copyright Hitechline
*/`,
},
};

/**
Expand Down
48 changes: 48 additions & 0 deletions reactools/build/plugins/esbuild-plugin-globals.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit 9444aa4

Please sign in to comment.