Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: evanw/esbuild
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: thr0wforks/esbuild
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 5 commits
  • 17 files changed
  • 1 contributor

Commits on Dec 23, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    5da693c View commit details
  2. debugTool

    teintinu committed Dec 23, 2020
    Copy the full SHA
    be53aae View commit details
  3. Copy the full SHA
    87a3f00 View commit details
  4. cleanup

    teintinu committed Dec 23, 2020
    Copy the full SHA
    8fa8ee8 View commit details

Commits on Dec 29, 2020

  1. Copy the full SHA
    f860c91 View commit details
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Proposal

* Add `--remove-console` option that avoids print console calls to target
* Add `--remove-debugger` option that avoids print debugger statement to target
* Add `--debug-tool=...` enable debug tool in target ([more info](https://github.com/evanw/esbuild/blob/master/docs/debugtool.md))
* Add `--removedebug-tool=...` remove debug tool from target([more info](https://github.com/evanw/esbuild/blob/master/docs/debugtool.md))
## Unreleased

* Mark `import.meta` as supported in node 10.4+ ([#626](https://github.com/evanw/esbuild/issues/626))
6 changes: 5 additions & 1 deletion cmd/esbuild/main.go
Original file line number Diff line number Diff line change
@@ -78,7 +78,11 @@ var helpText = func(colors logger.Colors) string {
--sources-content=false Omit "sourcesContent" in generated source maps
--tree-shaking=... Set to "ignore-annotations" to work with packages
that have incorrect tree-shaking annotations
--tsconfig=... Use this tsconfig.json file instead of other ones
--tsconfig=... Use this tsconfig.json file instead of other ones
--remove--console Removes console calls
--remove--debugger Removes debugger statements
--debugTool=... debugTool
--removeDebugTool remove debugTool
--version Print the current version and exit (` + esbuildVersion + `)
` + colors.Bold + `Examples:` + colors.Default + `
158 changes: 158 additions & 0 deletions docs/debugtool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Debug tool
Debug Tool helps you insert debug code and easily to remove it when deploy to production

### LOG
Similar to console.log but logs also identifiers and source code position
```javascript
// app.js
import { DEBUGTOOL } from './debug'
function sum(a,b) {
DEBUGTOOL.LOG(a,b);
console.log('a='+ a, 'b='+ b, ' at app.js:3:3');
return a+b;
}
```
### ASSERT
Similar to assert but logs also identifier and source code position
```javascript
// app.js
import { DEBUGTOOL } from './debug'
function sum(a,b) {
DEBUGTOOL.ASSERT(a>0,b>0)
if (!(a>0)) throw new Error('assert a>0 at app.js:3:3');
if (!(b>0)) throw new Error('assert b>0 at app.js:3:3');
return a+b
}
```
### TRACE
Useful for test local variables on test units. It's not for production mode.
```javascript
// lib.js
import { DEBUGTOOL } from './debug'
function quadraticEquation(a,b,c) {
const delta = b*b - 4*a*c; ← b² – 4ac
DEBUGTOOL.TRACE(a, b, c, delta)
tracelog.push('a=' +a + ' b=' + b +' c=' + c + ' delta=' + delta);
if (delta<0) return null
const x1 = (-b + sqrt(delta)) / (2*a)
const x2 = (-b - sqrt(delta)) / (2*a)
return {x1, x2}
}
// test.js
import { DEBUGTOOL } from './debug'
it(()=>{
DEBUGTOOL.RESET()
→ tracelog = [] // traceLog is a global
const result = quadraticEquation(1, 8, -9) // x² + 8x - 9 = 0
const deltaWasTraced = DEBUGTOOL.ASSERT(/delta=100/}
const deltaWasTraced = traceLog.some(l=>/delta=100/.test(l)))
except(deltaWasTraced).toBe(true)
except(DEBUGTOOL.HISTORY()).toBe('a=1 b=8 c=-9 delta=100')
...
})
```
## Using with esbuilder

## CLI

In development or test mode
```
./node_modules/.bin/esbuild app.jsx --bundle --debug-tool=DEBUGTOOL --outfile=out.js
```

In production mode
```
./node_modules/.bin/esbuild app.jsx --bundle --remove-debug-tool=DEBUGTOOL --outfile=out.js
```

##### API
In development or test mode
```
require('esbuild').buildSync({
entryPoints: ['in.ts'],
outfile: 'out.js',
debugTool: 'DEBUGTOOL',
})
```

In production mode
```
require('esbuild').buildSync({
entryPoints: ['in.ts'],
outfile: 'out.js',
removeDebugTool: 'DEBUGTOOL',
})
```

### why use debugtoos
- Sugar syntax alternative for `if (process.env.NODE_ENV === 'development')`
- tests can take benefits from historical debugging
- No globals
- Compatible syntax

### debug.js module
You will need a debug module, like that but customized to your needs.

See samples in https://github.com/thr0w/babel-plugin-debug-tools

```javascript

var traceLog = [];
var DEBUGTOOL = {
LOG(...args) {
console.log(...args.slice(1).map((a) => JSON.stringify(a)));
},
ASSERT(...args) {
const loc = args[0];
for (let i = 1; i < args.length; i++) {
const arg = args[i];
if (Array.isArray(arg)) {
if (!arg[1])
throw new Error("ASSERT FAIL: " + arg[0] + formatLoc(loc) + (arg[2] ? JSON.stringify(arg[2]) : ""));
} else if (typeof arg === "string") {
if (!traceLog.some((l) => l.indexOf(arg) > -1))
throw new Error("NOT FOUND IN HISTORY: " + arg + formatLoc(loc));
} else {
if (!traceLog.some((l) => arg.test(l)))
throw new Error("NOT FOUND IN HISTORY: " + arg.toString() + " at " + formatLoc(loc));
}
}
},
RESET() {
traceLog = [];
},
HISTORY() {
return traceLog.join("\n");
},
TRACE(...args) {
traceLog.push(formatArgs(args.slice(1), 0));
},
CHECK(regExp) {
return traceLog.some((l) => regExp.test(l));
}
};
function formatLoc(loc) {
if (loc)
return " at " + (loc.filename || "") + ":" + loc.line + ":" + loc.column + " ";
return "";
}
function formatArg(arg) {
if (typeof arg === "string")
return arg;
return JSON.stringify(arg);
}
function formatArgs(args, sLoc) {
const flatArgs = [];
for (let i = 0; i < args.length - sLoc; i++) {
const arg = args[i];
if (Array.isArray(arg) && arg.length == 2) {
flatArgs.push(formatArg(arg[0]) + ": " + formatArg(arg[1]));
} else
flatArgs.push(formatArg(arg));
}
if (sLoc)
flatArgs.push(formatArg(formatLoc(args[args.length - 1])));
return flatArgs.join(" ");
}

```
179 changes: 179 additions & 0 deletions internal/bundler/bundler_transpile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package bundler

import (
"testing"

"github.com/evanw/esbuild/internal/config"
)

var transpile_suite = suite{
name: "transpile",
}

func TestNoRemoveConsole(t *testing.T) {
transpile_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.ts": `
const a=1;
console.log(a)
`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
}

func TestRemoveConsole(t *testing.T) {
transpile_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.ts": `
const a=1;
console.log(a)
`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Mode: config.ModeBundle,
RemoveConsole: true,
AbsOutputFile: "/out.js",
},
})
}
func TestNoRemoveDebbuger(t *testing.T) {
transpile_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.ts": `
const a=1;
debugger;
console.log(a)
`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
}

func TestRemoveDebbuger(t *testing.T) {
transpile_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.ts": `
const a=1;
debugger;
console.log(a);
`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Mode: config.ModeBundle,
RemoveDebugger: true,
AbsOutputFile: "/out.js",
},
})
}

func TestDebugToolsBhaskara(t *testing.T) {
transpile_suite.expectBundled(t, bundled{
files: map[string]string{
"/bhaskara.js": `
import { BHASKARA } from './debug'
export function quadraticEquation(a, b, c) {
BHASKARA.ASSERT(a !== 0)
const delta = b * b - 4 * a * c; // b² – 4ac
BHASKARA.TRACE(a, b, c, delta)
if (delta < 0) return null
const x1 = (-b + Math.sqrt(delta)) / (2 * a)
const x2 = (-b - Math.sqrt(delta)) / (2 * a)
BHASKARA.TRACE(x1, x2)
return { x1, x2 }
}
`,
"/bhaskara.test.js": `
import { quadraticEquation } from './bhaskara'
import { BHASKARA } from './debug'
test('x² + 8x - 9 = 0', () => {
BHASKARA.RESET()
BHASKARA.ASSERT(typeof quadraticEquation === 'function')
const { x1, x2 } = quadraticEquation(1, 8, -9)
BHASKARA.ASSERT(/delta: 100/, x1 === 1, x2 === -9)
expect(BHASKARA.HISTORY()).toEqual('a: 1 b: 8 c: -9 delta: 100\nx1: 1 x2: -9')
});
`,
"/debug.js": `
let traceLog = [];
export const BHASKARA = {
LOG(...args) {
console.log(...args.slice(1).map( (a)=>JSON.stringify(a)));
},
ASSERT(...args) {
const loc = args[0]
for (let i = 1; i < args.length; i++) {
const arg = args[i]
if (Array.isArray(arg)) {
if (!arg[1]) throw new Error(
'ASSERT FAIL: ' + arg[0] + formatLoc(loc) +
(arg[2] ? JSON.stringify(arg[2]) : '')
);
} else if (typeof arg === 'string') {
if (!traceLog.some(l => l.indexOf(arg) > -1))
throw new Error('NOT FOUND IN HISTORY: ' + arg + formatLoc(loc))
} else {
if (!traceLog.some(l => arg.test(l)))
throw new Error('NOT FOUND IN HISTORY: ' + arg.toString() + ' at ' + formatLoc(loc))
}
}
},
RESET() {
traceLog = [];
},
HISTORY() {
return traceLog.join('\n')
},
TRACE(...args) {
traceLog.push(formatArgs(args.slice(1), 0));
},
CHECK(regExp) {
return traceLog.some(l => regExp.test(l))
}
};
function formatLoc(loc) {
if (loc)
return ' at ' + (loc.filename || '') + ':' + loc.line + ':' + loc.column + ' ';
return ''
}
function formatArg(arg) {
if (typeof arg === 'string') return arg
return JSON.stringify(arg)
}
function formatArgs(args, sLoc) {
const flatArgs = []
for (let i = 0; i < args.length - sLoc; i++) {
const arg = args[i]
if (Array.isArray(arg) && arg.length == 2) {
flatArgs.push(formatArg(arg[0]) + ': ' + formatArg(arg[1]))
}
else flatArgs.push(formatArg(arg))
}
if (sLoc)
flatArgs.push(formatArg(formatLoc(args[args.length - 1])))
return flatArgs.join(' ')
}
`,
},
entryPaths: []string{"/bhaskara.test.js"},
options: config.Options{
Mode: config.ModeBundle,
DebugTool: "BHASKARA",
AbsOutputFile: "/out.js",
},
})
}
4 changes: 4 additions & 0 deletions internal/bundler/linker.go
Original file line number Diff line number Diff line change
@@ -3276,6 +3276,10 @@ func (c *linkerContext) generateCodeForFileInChunkJS(
Indent: indent,
OutputFormat: c.options.OutputFormat,
RemoveWhitespace: c.options.RemoveWhitespace,
RemoveConsole: c.options.RemoveConsole,
RemoveDebugger: c.options.RemoveDebugger,
DebugTool: c.options.DebugTool,
RemoveDebugTool: c.options.RemoveDebugTool,
MangleSyntax: c.options.MangleSyntax,
ASCIIOnly: c.options.ASCIIOnly,
ToModuleRef: toModuleRef,
Loading