Skip to content

Commit

Permalink
fix: add esbuild global externals plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
luwes committed Oct 6, 2023
1 parent 56238e3 commit a7f6209
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"build:demo": "cp -r examples/react/public demo && npm run build:lib && builder examples/react/src/index.js --format=iife --bundle --outdir=demo --minify --sourcemap",
"build:lib": "builder src/*.js src/players/*.js --outdir=lib --format=cjs",
"build:lazy": "builder src/*.js src/players/*.js --outdir=lazy --format=cjs",
"build:dist": "builder src/index.js --outfile=dist/ReactPlayer.js --format=iife --bundle --minify --sourcemap --global-name=ReactPlayer --external:react --footer:js=\"ReactPlayer = ReactPlayer.default;\"",
"build:standalone": "builder src/standalone.js --outfile=dist/ReactPlayer.standalone.js --format=iife --bundle --minify --sourcemap --global-name=renderReactPlayer --footer:js=\"renderReactPlayer = renderReactPlayer.default;\"",
"build:dist": "builder src/index.js --outfile=dist/ReactPlayer.js --format=iife --bundle --minify --sourcemap --global-name=ReactPlayer --external:react --plugin:global-externals='{\"react\":\"globalThis.React\"}' --footer:js='ReactPlayer = ReactPlayer.default;'",
"build:standalone": "builder src/standalone.js --outfile=dist/ReactPlayer.standalone.js --format=iife --bundle --minify --sourcemap --global-name=renderReactPlayer --footer:js='renderReactPlayer = renderReactPlayer.default;'",
"build:es6": "builder src/standalone.js --outfile=dist/ReactPlayer.standalone.es6.js --format=esm --bundle --minify",
"preversion": "npm run lint && npm run test",
"version": "auto-changelog -p && npm run build:dist && npm run build:standalone && git add CHANGELOG.md dist",
Expand Down
33 changes: 26 additions & 7 deletions scripts/builder/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,32 @@ export async function cliBuild () {
}

export async function build (positionals, args) {
// https://esbuild.github.io/api/#live-reload
// https://esbuild.github.io/api/#live-reload
const livereloadJs = 'new EventSource(\'/esbuild\').addEventListener(\'change\', () => location.reload());'

// Assigns external modules to global variables.
// https://github.com/evanw/esbuild/issues/337
const plugins = {
'global-externals': (arg) => {
const options = JSON.parse(arg)
const filter = new RegExp(`^${Object.keys(options)}$`)

return {
name: 'global-externals-plugin',
setup (build) {
build.onResolve({ filter }, (args) => ({
path: args.path,
namespace: 'global-externals-plugin'
}))
build.onLoad({ filter: /.*/, namespace: 'global-externals-plugin' }, (args) => {
const contents = `module.exports = ${options[args.path]}`
return { contents }
})
}
}
}
}

const options = {
logLevel: 'info',
entryPoints: positionals,
Expand All @@ -40,7 +63,8 @@ export async function build (positionals, args) {
external: argsArray(args, 'external'),
outExtension: argsObject(args, 'out-extension'),
banner: argsObject(args, 'banner'),
plugins: [],
plugins: Object.entries(argsObject(args, 'plugin'))
.map(([name, options]) => plugins[name](options)),
define: {
'globalThis.__TEST__': 'false',
...argsObject(args, 'define')
Expand All @@ -56,11 +80,6 @@ export async function build (positionals, args) {
}
}

if (args.stdin) {
delete options.entryPoints
options.stdin = args.stdin
}

if (process.env.NODE_ENV) {
options.define['process.env.NODE_ENV'] ||= `"${process.env.NODE_ENV}"`
}
Expand Down

0 comments on commit a7f6209

Please sign in to comment.