Skip to content

Commit

Permalink
fix #130: handle "sourcemap: true" in build api
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed May 22, 2020
1 parent 0017aa9 commit 75d0b1f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
9 changes: 5 additions & 4 deletions npm/esbuild/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ function esbuildSpawn({ flags, stdio }) {
throw new Error(`Unsupported platform: ${process.platform} ${os.arch()}`);
}

function pushCommonFlags(flags, options, defaults) {
function pushCommonFlags(flags, options) {
flags.push(`--error-limit=${options.errorLimit || 0}`);

if (options.sourcemap) flags.push(`--sourcemap=${options.sourcemap === true ? defaults.sourcemap : options.sourcemap}`);
if (options.sourcefile) flags.push(`--sourcefile=${options.sourcefile}`);
if (options.target) flags.push(`--target=${options.target}`);

Expand All @@ -70,8 +69,9 @@ exports.build = options => {
return new Promise((resolve, reject) => {
const stdio = options.stdio;
const flags = [];
pushCommonFlags(flags, options, { sourcemap: true });
pushCommonFlags(flags, options);

if (options.sourcemap) flags.push(`--sourcemap${options.sourcemap === true ? '' : `=${options.sourcemap}`}`);
if (options.name) flags.push(`--name=${options.name}`);
if (options.bundle) flags.push('--bundle');
if (options.outfile) flags.push(`--outfile=${options.outfile}`);
Expand Down Expand Up @@ -268,7 +268,8 @@ exports.startService = () => {
const loader = options.loader || 'js';
const name = `input.${loader}`;
const flags = ['build', `/${name}`, file, '--', name, '--outfile=/output.js'];
pushCommonFlags(flags, options, { sourcemap: 'external' });
pushCommonFlags(flags, options);
if (options.sourcemap) flags.push(`--sourcemap${options.sourcemap === true ? '=external' : `=${options.sourcemap}`}`);
if (options.loader) flags.push(`--loader:.${loader}=${options.loader}`);
const response = await sendRequest(flags);

Expand Down
38 changes: 33 additions & 5 deletions scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,44 @@ const fs = require('fs')
const repoDir = path.dirname(__dirname)
const testDir = path.join(repoDir, 'scripts', '.js-api-tests')

let tests = {
async build({ esbuild }) {
const input = path.join(testDir, 'input.js')
const output = path.join(testDir, 'output.js')
let buildTests = {
async es6_to_cjs({ esbuild }) {
const input = path.join(testDir, '1-in.js')
const output = path.join(testDir, '1-out.js')
await util.promisify(fs.writeFile)(input, 'export default 123')
await esbuild.build({ entryPoints: [input], bundle: true, outfile: output, format: 'cjs' })
const result = require(output)
assert.strictEqual(result.default, 123)
assert.strictEqual(result.__esModule, true)
},

async sourceMap({ esbuild }) {
const input = path.join(testDir, '2-in.js')
const output = path.join(testDir, '2-out.js')
await util.promisify(fs.writeFile)(input, 'exports.foo = 123')
await esbuild.build({ entryPoints: [input], outfile: output, sourcemap: true })
const result = require(output)
assert.strictEqual(result.foo, 123)
const resultMap = await util.promisify(fs.readFile)(output + '.map', 'utf8')
const json = JSON.parse(resultMap)
assert.strictEqual(json.version, 3)
},

async sourceMapInline({ esbuild }) {
const input = path.join(testDir, '3-in.js')
const output = path.join(testDir, '3-out.js')
await util.promisify(fs.writeFile)(input, 'exports.foo = 123')
await esbuild.build({ entryPoints: [input], outfile: output, sourcemap: 'inline' })
const result = require(output)
assert.strictEqual(result.foo, 123)
const outputFile = await util.promisify(fs.readFile)(output, 'utf8')
const match = /\/\/# sourceMappingURL=data:application\/json;base64,(.*)/.exec(outputFile)
const json = JSON.parse(Buffer.from(match[1], 'base64').toString())
assert.strictEqual(json.version, 3)
},
}

let transformTests = {
async jsx({ service }) {
const { js } = await service.transform(`console.log(<div/>)`, { loader: 'jsx' })
assert.strictEqual(js, `console.log(React.createElement("div", null));\n`)
Expand Down Expand Up @@ -111,7 +138,8 @@ async function main() {
if (e.errors) console.error(e.errors.map(x => x.text).join('\n'))
return false
})
const allTestsPassed = (await Promise.all(Object.entries(tests).map(runTest))).every(success => success)
const tests = [...Object.entries(buildTests), ...Object.entries(transformTests)]
const allTestsPassed = (await Promise.all(tests.map(runTest))).every(success => success)

// Clean up test output
service.stop()
Expand Down

0 comments on commit 75d0b1f

Please sign in to comment.