Skip to content

Commit

Permalink
automatically generate the separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 29, 2022
1 parent 5382d3f commit 698898f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/api/api_js_table.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This file was automatically generated by "compat-table.js"

package api

import "github.com/evanw/esbuild/internal/compat"
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/cli_js_table.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This file was automatically generated by "compat-table.js"

package cli

import "github.com/evanw/esbuild/pkg/api"
Expand Down
52 changes: 49 additions & 3 deletions scripts/compat-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ const features = {

const versions = {}
const engines = [
// The JavaScript standard
'es',

// Common JavaScript runtimes
'chrome',
'edge',
'es',
'firefox',
'ie',
'ios',
Expand Down Expand Up @@ -362,6 +365,15 @@ function jsTableMap(obj) {
}).join('\n')}\n\t}`
}

function jsTableValidEnginesMap(engines) {
const keys = engines.slice().sort()
const maxLength = keys.reduce((a, b) => Math.max(a, b.length + 4), 0)
if (keys.length === 0) return '{}'
return keys.map(x => {
return `\t${`"${x}": `.padEnd(maxLength)}api.Engine${upper(x)},`
}).join('\n')
}

fs.writeFileSync(__dirname + '/../internal/compat/js_table.go',
`// This file was automatically generated by "${path.basename(__filename)}"
Expand All @@ -370,12 +382,12 @@ package compat
type Engine uint8
const (
${engines.map((x, i) => `\t${upper(x)}${i ? '' : ' Engine = iota'}`).join('\n')}
${engines.slice().sort().map((x, i) => `\t${upper(x)}${i ? '' : ' Engine = iota'}`).join('\n')}
)
func (e Engine) String() string {
\tswitch e {
${engines.map((x, i) => `\tcase ${upper(x)}:\n\t\treturn "${x}"`).join('\n')}
${engines.slice().sort().map(x => `\tcase ${upper(x)}:\n\t\treturn "${x}"`).join('\n')}
\t}
\treturn ""
}
Expand Down Expand Up @@ -418,3 +430,37 @@ func UnsupportedJSFeatures(constraints map[Engine][]int) (unsupported JSFeature)
\treturn
}
`)

fs.writeFileSync(__dirname + '/../pkg/api/api_js_table.go',
`// This file was automatically generated by "${path.basename(__filename)}"
package api
import "github.com/evanw/esbuild/internal/compat"
type EngineName uint8
const (
${engines.filter(x => x !== 'es').map((x, i) => `\tEngine${upper(x)}${i ? '' : ' EngineName = iota'}`).join('\n')}
)
func convertEngineName(engine EngineName) compat.Engine {
\tswitch engine {
${engines.filter(x => x !== 'es').map(x => `\tcase Engine${upper(x)}:\n\t\treturn compat.${upper(x)}`).join('\n')}
\tdefault:
\t\tpanic("Invalid engine name")
\t}
}
`)

fs.writeFileSync(__dirname + '/../pkg/cli/cli_js_table.go',
`// This file was automatically generated by "${path.basename(__filename)}"
package cli
import "github.com/evanw/esbuild/pkg/api"
var validEngines = map[string]api.EngineName{
${jsTableValidEnginesMap(engines.filter(x => x !== 'es'))}
}
`)

0 comments on commit 698898f

Please sign in to comment.