Skip to content

Commit 9ba01d1

Browse files
committed
abs-paths: js api and tests
1 parent ca196c9 commit 9ba01d1

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

lib/shared/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ function pushCommonFlags(flags: string[], options: CommonOptions, keys: OptionKe
174174
let keepNames = getFlag(options, keys, 'keepNames', mustBeBoolean)
175175
let platform = getFlag(options, keys, 'platform', mustBeString)
176176
let tsconfigRaw = getFlag(options, keys, 'tsconfigRaw', mustBeStringOrObject)
177+
let absPaths = getFlag(options, keys, 'absPaths', mustBeArrayOfStrings)
177178

178179
if (legalComments) flags.push(`--legal-comments=${legalComments}`)
179180
if (sourceRoot !== void 0) flags.push(`--source-root=${sourceRoot}`)
@@ -194,6 +195,7 @@ function pushCommonFlags(flags: string[], options: CommonOptions, keys: OptionKe
194195
if (ignoreAnnotations) flags.push(`--ignore-annotations`)
195196
if (drop) for (let what of drop) flags.push(`--drop:${validateStringValue(what, 'drop')}`)
196197
if (dropLabels) flags.push(`--drop-labels=${validateAndJoinStringArray(dropLabels, 'drop label')}`)
198+
if (absPaths) flags.push(`--abs-paths=${validateAndJoinStringArray(absPaths, 'abs paths')}`)
197199
if (mangleProps) flags.push(`--mangle-props=${jsRegExpToGoRegExp(mangleProps)}`)
198200
if (reserveProps) flags.push(`--reserve-props=${jsRegExpToGoRegExp(reserveProps)}`)
199201
if (mangleQuoted !== void 0) flags.push(`--mangle-quoted=${mangleQuoted}`)

lib/shared/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type Loader = 'base64' | 'binary' | 'copy' | 'css' | 'dataurl' | 'default
44
export type LogLevel = 'verbose' | 'debug' | 'info' | 'warning' | 'error' | 'silent'
55
export type Charset = 'ascii' | 'utf8'
66
export type Drop = 'console' | 'debugger'
7+
export type AbsPaths = 'code' | 'log' | 'metafile'
78

89
interface CommonOptions {
910
/** Documentation: https://esbuild.github.io/api/#sourcemap */
@@ -75,6 +76,8 @@ interface CommonOptions {
7576
/** Documentation: https://esbuild.github.io/api/#keep-names */
7677
keepNames?: boolean
7778

79+
/** Documentation: https://esbuild.github.io/api/#abs-paths */
80+
absPaths?: AbsPaths[]
7881
/** Documentation: https://esbuild.github.io/api/#color */
7982
color?: boolean
8083
/** Documentation: https://esbuild.github.io/api/#log-level */

scripts/js-api-tests.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,108 @@ let buildTests = {
143143
assert.strictEqual(require(bOut).y, true)
144144
},
145145

146+
async absPathsCodeTest({ esbuild, testDir }) {
147+
let srcDir = path.join(testDir, 'src');
148+
let outfile = path.join(testDir, 'out', 'result.js');
149+
let entry = path.join(srcDir, 'entry.js');
150+
fs.mkdirSync(srcDir, { recursive: true });
151+
fs.writeFileSync(entry, `x = typeof y == "null"`);
152+
153+
const { metafile, warnings, outputFiles } = await esbuild.build({
154+
entryPoints: [entry],
155+
outfile,
156+
bundle: true,
157+
write: false,
158+
metafile: true,
159+
format: 'cjs',
160+
logLevel: 'silent',
161+
absPaths: ['code'],
162+
})
163+
const cwd = process.cwd()
164+
const makePath = absPath => path.relative(cwd, absPath).split(path.sep).join('/')
165+
166+
assert.strictEqual(outputFiles.length, 1)
167+
assert.deepStrictEqual(outputFiles[0].path, outfile)
168+
assert.deepStrictEqual(outputFiles[0].text, `// ${entry}\nx = typeof y == "null";\n`)
169+
170+
assert.deepStrictEqual(Object.keys(metafile.inputs), [makePath(entry)])
171+
assert.deepStrictEqual(Object.keys(metafile.outputs), [makePath(outfile)])
172+
assert.strictEqual(metafile.inputs[makePath(entry)].imports.length, 0)
173+
assert.strictEqual(metafile.outputs[makePath(outfile)].entryPoint, makePath(entry))
174+
175+
assert.strictEqual(warnings.length, 1)
176+
assert.strictEqual(warnings[0].text, 'The "typeof" operator will never evaluate to "null"')
177+
assert.strictEqual(warnings[0].location.file, makePath(entry))
178+
},
179+
180+
async absPathsLogTest({ esbuild, testDir }) {
181+
let srcDir = path.join(testDir, 'src');
182+
let outfile = path.join(testDir, 'out', 'result.js');
183+
let entry = path.join(srcDir, 'entry.js');
184+
fs.mkdirSync(srcDir, { recursive: true });
185+
fs.writeFileSync(entry, `x = typeof y == "null"`);
186+
187+
const { metafile, warnings, outputFiles } = await esbuild.build({
188+
entryPoints: [entry],
189+
outfile,
190+
bundle: true,
191+
write: false,
192+
metafile: true,
193+
format: 'cjs',
194+
logLevel: 'silent',
195+
absPaths: ['log'],
196+
})
197+
const cwd = process.cwd()
198+
const makePath = absPath => path.relative(cwd, absPath).split(path.sep).join('/')
199+
200+
assert.strictEqual(outputFiles.length, 1)
201+
assert.deepStrictEqual(outputFiles[0].path, outfile)
202+
assert.deepStrictEqual(outputFiles[0].text, `// ${makePath(entry)}\nx = typeof y == "null";\n`)
203+
204+
assert.deepStrictEqual(Object.keys(metafile.inputs), [makePath(entry)])
205+
assert.deepStrictEqual(Object.keys(metafile.outputs), [makePath(outfile)])
206+
assert.strictEqual(metafile.inputs[makePath(entry)].imports.length, 0)
207+
assert.strictEqual(metafile.outputs[makePath(outfile)].entryPoint, makePath(entry))
208+
209+
assert.strictEqual(warnings.length, 1)
210+
assert.strictEqual(warnings[0].text, 'The "typeof" operator will never evaluate to "null"')
211+
assert.strictEqual(warnings[0].location.file, entry)
212+
},
213+
214+
async absPathsMetafileTest({ esbuild, testDir }) {
215+
let srcDir = path.join(testDir, 'src');
216+
let outfile = path.join(testDir, 'out', 'result.js');
217+
let entry = path.join(srcDir, 'entry.js');
218+
fs.mkdirSync(srcDir, { recursive: true });
219+
fs.writeFileSync(entry, `x = typeof y == "null"`);
220+
221+
const { metafile, warnings, outputFiles } = await esbuild.build({
222+
entryPoints: [entry],
223+
outfile,
224+
bundle: true,
225+
write: false,
226+
metafile: true,
227+
format: 'cjs',
228+
logLevel: 'silent',
229+
absPaths: ['metafile'],
230+
})
231+
const cwd = process.cwd()
232+
const makePath = absPath => path.relative(cwd, absPath).split(path.sep).join('/')
233+
234+
assert.strictEqual(outputFiles.length, 1)
235+
assert.deepStrictEqual(outputFiles[0].path, outfile)
236+
assert.deepStrictEqual(outputFiles[0].text, `// ${makePath(entry)}\nx = typeof y == "null";\n`)
237+
238+
assert.deepStrictEqual(Object.keys(metafile.inputs), [entry])
239+
assert.deepStrictEqual(Object.keys(metafile.outputs), [outfile])
240+
assert.strictEqual(metafile.inputs[entry].imports.length, 0)
241+
assert.strictEqual(metafile.outputs[outfile].entryPoint, entry)
242+
243+
assert.strictEqual(warnings.length, 1)
244+
assert.strictEqual(warnings[0].text, 'The "typeof" operator will never evaluate to "null"')
245+
assert.strictEqual(warnings[0].location.file, makePath(entry))
246+
},
247+
146248
async aliasValidity({ esbuild }) {
147249
const valid = async alias => {
148250
const result = await esbuild.build({

0 commit comments

Comments
 (0)