From 906a8d9af86a01e5bc509c514d706f8e8f3513da Mon Sep 17 00:00:00 2001 From: Daniel K Date: Wed, 12 Jun 2019 20:51:26 +0200 Subject: [PATCH 1/5] Create ES dev bundle same as CJS --- README.md | 2 +- src/index.ts | 62 +++++++++++++++++++++-------------- test/tests/tsdx-build.test.js | 4 +++ 3 files changed, 43 insertions(+), 25 deletions(-) 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..7abb7d2ba 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(); From 58b7a91a2f958fcb496bc08c5583202628f02d2b Mon Sep 17 00:00:00 2001 From: Daniel K Date: Wed, 12 Jun 2019 21:04:36 +0200 Subject: [PATCH 2/5] Missing semis in index file --- src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7abb7d2ba..8e16d2d8e 100755 --- a/src/index.ts +++ b/src/index.ts @@ -386,11 +386,11 @@ prog return fs.writeFile( resolveApp(`./dist/index${format !== 'cjs' ? `.${format}` : ''}.js`), [ - 'use strict', + 'use strict;', `if (process.env.NODE_ENV === 'production') {`, - `${baseLine}.production.js`, + `${baseLine}.production.js;`, '} else {', - `${baseLine}.development.js`, + `${baseLine}.development.js;`, '}', ].join('\n') ); From f3112c894270e779c7c9a1bb59d4b00a6181d926 Mon Sep 17 00:00:00 2001 From: Daniel K Date: Wed, 12 Jun 2019 21:07:16 +0200 Subject: [PATCH 3/5] Not semis, just quotes :) --- src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8e16d2d8e..cafa6f1e6 100755 --- a/src/index.ts +++ b/src/index.ts @@ -386,11 +386,11 @@ prog return fs.writeFile( resolveApp(`./dist/index${format !== 'cjs' ? `.${format}` : ''}.js`), [ - 'use strict;', + `'use strict'`, `if (process.env.NODE_ENV === 'production') {`, - `${baseLine}.production.js;`, + `${baseLine}.production.js`, '} else {', - `${baseLine}.development.js;`, + `${baseLine}.development.js`, '}', ].join('\n') ); From 6e9d6af952764175a5aa99b23550d273471923e2 Mon Sep 17 00:00:00 2001 From: Daniel K Date: Wed, 12 Jun 2019 21:10:19 +0200 Subject: [PATCH 4/5] More quotes, darn it! --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index cafa6f1e6..808e91e86 100755 --- a/src/index.ts +++ b/src/index.ts @@ -388,9 +388,9 @@ prog [ `'use strict'`, `if (process.env.NODE_ENV === 'production') {`, - `${baseLine}.production.js`, + `${baseLine}.production.js')`, '} else {', - `${baseLine}.development.js`, + `${baseLine}.development.js')`, '}', ].join('\n') ); From 38a0265ee1ebeaaab87441ad2b37af6f70cf879f Mon Sep 17 00:00:00 2001 From: Daniel K Date: Wed, 12 Jun 2019 21:13:11 +0200 Subject: [PATCH 5/5] Extra dot now, grrrh --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 808e91e86..6634ea4ce 100755 --- a/src/index.ts +++ b/src/index.ts @@ -382,7 +382,7 @@ prog function writeEntryFile(format: string) { // prettier-ignore - const baseLine = ` module.exports = require('./${safePackageName(opts.name)}.${format}.`; + const baseLine = ` module.exports = require('./${safePackageName(opts.name)}.${format}`; return fs.writeFile( resolveApp(`./dist/index${format !== 'cjs' ? `.${format}` : ''}.js`), [