Skip to content

Commit

Permalink
Allows proper 'define' option overwriting and specifying booleans/num…
Browse files Browse the repository at this point in the history
…bers/valid JSON + adds test case
  • Loading branch information
webketje committed Jun 7, 2023
1 parent 37d869e commit 2992b6b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ const debugNs = '@metalsmith/js-bundle'
function normalizeOptions(options = {}, metalsmith, debug) {
const entryPoints = options.entries || {}
const isProd = metalsmith.env('NODE_ENV') !== 'development'
const define = Object.entries(metalsmith.env()).reduce((acc, [name, value]) => {
const define = Object.entries(options.define || metalsmith.env()).reduce((acc, [name, value]) => {
if (typeof value === 'undefined') {
debug.warn('Define option "%s" value is undefined', name)
acc[`process.env.${name}`] = 'undefined'
return acc
}
// see notes at https://esbuild.github.io/api/#define, string values require explicit quotes
acc[`process.env.${name}`] = typeof value === 'string' ? `'${value}'` : value.toString()
acc[`process.env.${name}`] =
typeof value === 'string'
? `'${value}'`
: ['boolean', 'number', 'function'].includes(typeof value)
? value.toString()
: JSON.stringify(value)
return acc
}, {})

Expand All @@ -36,8 +41,7 @@ function normalizeOptions(options = {}, metalsmith, debug) {
platform: 'browser',
target: 'es6',
assetNames: '[dir]/[name]',
drop: isProd ? ['console', 'debugger'] : [],
define
drop: isProd ? ['console', 'debugger'] : []
}

/** @type {Options} */
Expand All @@ -46,7 +50,8 @@ function normalizeOptions(options = {}, metalsmith, debug) {
absWorkingDir: metalsmith.directory(),
outdir: relative(metalsmith.directory(), metalsmith.destination()),
write: false,
metafile: true
metafile: true,
define
}

// eslint-disable-next-line no-unused-vars
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/option-define/expected/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(()=>{var e=["hello",1];var s={hello:"world"};window.env={strValue:"a string",boolValue:!1,numValue:66,arrValue:e,objValue:s};})();
7 changes: 7 additions & 0 deletions test/fixtures/option-define/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
window.env = {
strValue: process.env.STRVALUE,
boolValue: process.env.BOOLVALUE,
numValue: process.env.NUMVALUE,
arrValue: process.env.ARRVALUE,
objValue: process.env.OBJVALUE
}
14 changes: 14 additions & 0 deletions test/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ const testcases = [
entries: { index: './index.js' },
target: 'es5'
}
},
{
name: 'should correctly output "define" (env var) option values',
dir: 'option-define',
options: {
entries: { index: './src/index.js' },
define: {
STRVALUE: 'a string',
BOOLVALUE: false,
NUMVALUE: 66,
ARRVALUE: ['hello', 1],
OBJVALUE: { hello: 'world' }
}
}
}
// @TODO: add testcase for React/JSX
// @TODO: add testcase for Typescript
Expand Down

0 comments on commit 2992b6b

Please sign in to comment.