-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}), | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |