diff --git a/README.md b/README.md index 00d0839c8..43b5a5a79 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ export const sum = (a: number, b: number) => { }; ``` -`tsdx build` will output an ES module file and 3 CommonJS files (dev, prod, and an entry file). If you want to specify a UMD build, you can do that as well. For brevity, let's examine the CommonJS output (comments added for emphasis): +`tsdx build` will output an 3 files for CommonJS and ES formats (dev, prod, and an entry file). If you want to specify a UMD build, you can do that as well. For brevity, let's examine the CommonJS output (comments added for emphasis): ```js // Entry File diff --git a/src/index.ts b/src/index.ts index 8bfb66824..6634ea4ce 100755 --- a/src/index.ts +++ b/src/index.ts @@ -95,6 +95,8 @@ function createBuildConfigs( createRollupConfig('cjs', 'development', { ...opts, input }), opts.format.includes('cjs') && createRollupConfig('cjs', 'production', { ...opts, input }), + opts.format.includes('es') && + createRollupConfig('es', 'development', { ...opts, input }), opts.format.includes('es') && createRollupConfig('es', 'production', { ...opts, input }), opts.format.includes('umd') && @@ -337,33 +339,29 @@ prog opts.name = opts.name || appPackageJson.name; opts.input = await getInputs(opts.entry, appPackageJson.source); const buildConfigs = createBuildConfigs(opts); + + let promise = Promise.resolve(); + + if (opts.format.length > 0) { + promise.then(() => util.promisify(mkdirp)(resolveApp('./dist'))); + } + if (opts.format.includes('cjs')) { - try { - await util.promisify(mkdirp)(resolveApp('./dist')); - const promise = fs - .writeFile( - resolveApp('./dist/index.js'), - ` - 'use strict' + logger( + promise.then(() => writeEntryFile('cjs')).catch(logError), + 'Creating CJS entry file' + ); + } - if (process.env.NODE_ENV === 'production') { - module.exports = require('./${safePackageName( - opts.name - )}.cjs.production.js') - } else { - module.exports = require('./${safePackageName( - opts.name - )}.cjs.development.js') - }` - ) - .catch(e => { - throw e; - }); - logger(promise, 'Creating entry file'); - } catch (e) { - logError(e); - } + if (opts.format.includes('es')) { + logger( + promise.then(() => writeEntryFile('es')).catch(logError), + 'Creating ES entry file' + ); } + + await promise; + try { const promise = asyncro .map( @@ -381,6 +379,22 @@ prog } catch (error) { logError(error); } + + function writeEntryFile(format: string) { + // prettier-ignore + const baseLine = ` module.exports = require('./${safePackageName(opts.name)}.${format}`; + return fs.writeFile( + resolveApp(`./dist/index${format !== 'cjs' ? `.${format}` : ''}.js`), + [ + `'use strict'`, + `if (process.env.NODE_ENV === 'production') {`, + `${baseLine}.production.js')`, + '} else {', + `${baseLine}.development.js')`, + '}', + ].join('\n') + ); + } }); prog diff --git a/test/tests/tsdx-build.test.js b/test/tests/tsdx-build.test.js index 3f56da165..fc931f9bb 100644 --- a/test/tests/tsdx-build.test.js +++ b/test/tests/tsdx-build.test.js @@ -21,12 +21,16 @@ describe('tsdx build', () => { const output = shell.exec('node ../dist/index.js build'); expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); + expect(shell.test('-f', 'dist/index.es.js')).toBeTruthy(); expect( shell.test('-f', 'dist/build-default.cjs.development.js') ).toBeTruthy(); expect( shell.test('-f', 'dist/build-default.cjs.production.js') ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-default.es.development.js') + ).toBeTruthy(); expect( shell.test('-f', 'dist/build-default.es.production.js') ).toBeTruthy();