From 04991013333aa59d1cb03c0ba98bb17215a4dab8 Mon Sep 17 00:00:00 2001 From: ambroseus Date: Sat, 14 Mar 2020 20:44:49 +0200 Subject: [PATCH] added tests with fixture build-withConfig --- src/index.ts | 2 +- .../build-withConfig/errors/ErrorDev.js | 7 -- .../build-withConfig/errors/ErrorProd.js | 14 ---- .../build-withConfig/errors/codes.json | 4 - test/fixtures/build-withConfig/src/foo.ts | 2 +- test/fixtures/build-withConfig/src/index.ts | 13 ++- .../tsdx.config.closure-advanced.js | 12 +++ .../tsdx.config.closure-simple.js | 7 ++ test/fixtures/build-withConfig/tsdx.config.js | 22 ------ .../tests/tsdx-build-closure-compiler.test.js | 79 +++++++++++++++++++ 10 files changed, 105 insertions(+), 57 deletions(-) delete mode 100644 test/fixtures/build-withConfig/errors/ErrorDev.js delete mode 100644 test/fixtures/build-withConfig/errors/ErrorProd.js delete mode 100644 test/fixtures/build-withConfig/errors/codes.json create mode 100644 test/fixtures/build-withConfig/tsdx.config.closure-advanced.js create mode 100644 test/fixtures/build-withConfig/tsdx.config.closure-simple.js delete mode 100644 test/fixtures/build-withConfig/tsdx.config.js create mode 100644 test/tests/tsdx-build-closure-compiler.test.js diff --git a/src/index.ts b/src/index.ts index c35267c35..c719c05af 100755 --- a/src/index.ts +++ b/src/index.ts @@ -382,7 +382,7 @@ prog .example('build --transpileOnly') .option( '--closureCompiler', - 'EXPERIMENTAL: Use closure compiler to minify production bundle' + 'EXPERIMENTAL: Use closure compiler to minify production bundle instead of terser' ) .example('build --env production --closureCompiler') .option( diff --git a/test/fixtures/build-withConfig/errors/ErrorDev.js b/test/fixtures/build-withConfig/errors/ErrorDev.js deleted file mode 100644 index fb93f9bf9..000000000 --- a/test/fixtures/build-withConfig/errors/ErrorDev.js +++ /dev/null @@ -1,7 +0,0 @@ -function ErrorDev(message) { - const error = new Error(message); - error.name = 'Invariant Violation'; - return error; -} - -export default ErrorDev; diff --git a/test/fixtures/build-withConfig/errors/ErrorProd.js b/test/fixtures/build-withConfig/errors/ErrorProd.js deleted file mode 100644 index 31ecff2fc..000000000 --- a/test/fixtures/build-withConfig/errors/ErrorProd.js +++ /dev/null @@ -1,14 +0,0 @@ -function ErrorProd(code) { - // TODO: replace this URL with yours - let url = 'https://reactjs.org/docs/error-decoder.html?invariant=' + code; - for (let i = 1; i < arguments.length; i++) { - url += '&args[]=' + encodeURIComponent(arguments[i]); - } - return new Error( - `Minified BuildWithconfig error #${code}; visit ${url} for the full message or ` + - 'use the non-minified dev environment for full errors and additional ' + - 'helpful warnings. ' - ); -} - -export default ErrorProd; diff --git a/test/fixtures/build-withConfig/errors/codes.json b/test/fixtures/build-withConfig/errors/codes.json deleted file mode 100644 index 9bf149d4c..000000000 --- a/test/fixtures/build-withConfig/errors/codes.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "0": "this is an old error that shouldn't be overwritten", - "1": "error occurred! o no" -} diff --git a/test/fixtures/build-withConfig/src/foo.ts b/test/fixtures/build-withConfig/src/foo.ts index 0abb9556d..8e60b3eab 100644 --- a/test/fixtures/build-withConfig/src/foo.ts +++ b/test/fixtures/build-withConfig/src/foo.ts @@ -1 +1 @@ -export const foo = () => 'bar'; +export const split = (str: string) => str.split(''); diff --git a/test/fixtures/build-withConfig/src/index.ts b/test/fixtures/build-withConfig/src/index.ts index 3a23a7132..90a62171f 100644 --- a/test/fixtures/build-withConfig/src/index.ts +++ b/test/fixtures/build-withConfig/src/index.ts @@ -1,12 +1,9 @@ -import invariant from 'tiny-invariant'; -import warning from 'tiny-warning'; -invariant(true, 'error occurred! o no'); -warning(false, 'warning - water is wet'); -export { foo } from './foo'; +import { split } from './foo'; export const sum = (a: number, b: number) => { - if ('development' === process.env.NODE_ENV) { - console.log('fuck'); - } return a + b; }; + +const bar = split('bar'); + +console.log(`${split('bar').join('')} ${sum(bar.length, -3)}`); diff --git a/test/fixtures/build-withConfig/tsdx.config.closure-advanced.js b/test/fixtures/build-withConfig/tsdx.config.closure-advanced.js new file mode 100644 index 000000000..59c86e9f3 --- /dev/null +++ b/test/fixtures/build-withConfig/tsdx.config.closure-advanced.js @@ -0,0 +1,12 @@ +const fs = require('fs-extra'); + +module.exports = { + rollup(config, options) { + const plugins = config.plugins.map(plugin => plugin.name); + fs.writeJSON('./plugins.json', plugins); + return config; + }, + closureCompilerOptions: { + compilation_level: 'ADVANCED_OPTIMIZATIONS', + }, +}; diff --git a/test/fixtures/build-withConfig/tsdx.config.closure-simple.js b/test/fixtures/build-withConfig/tsdx.config.closure-simple.js new file mode 100644 index 000000000..70b8801be --- /dev/null +++ b/test/fixtures/build-withConfig/tsdx.config.closure-simple.js @@ -0,0 +1,7 @@ +module.exports = { + /* SIMPLE_OPTIMIZATIONS by default + closureCompilerOptions: { + compilation_level: 'SIMPLE_OPTIMIZATIONS', + }, + */ +}; diff --git a/test/fixtures/build-withConfig/tsdx.config.js b/test/fixtures/build-withConfig/tsdx.config.js deleted file mode 100644 index ba3c055ed..000000000 --- a/test/fixtures/build-withConfig/tsdx.config.js +++ /dev/null @@ -1,22 +0,0 @@ -const postcss = require('rollup-plugin-postcss'); -const autoprefixer = require('autoprefixer'); -const cssnano = require('cssnano'); - -module.exports = { - rollup(config, options) { - config.plugins.push( - postcss({ - plugins: [ - autoprefixer(), - cssnano({ - preset: 'default', - }), - ], - inject: false, - // only write out CSS for the first bundle (avoids pointless extra files): - extract: !!options.writeMeta, - }) - ); - return config; - }, -}; diff --git a/test/tests/tsdx-build-closure-compiler.test.js b/test/tests/tsdx-build-closure-compiler.test.js new file mode 100644 index 000000000..f81fda9e6 --- /dev/null +++ b/test/tests/tsdx-build-closure-compiler.test.js @@ -0,0 +1,79 @@ +/** + * @jest-environment node + */ + +const shell = require('shelljs'); +const util = require('../fixtures/util'); + +shell.config.silent = false; + +const stageName = 'stage-build-closure-compiler'; + +describe('tsdx build with closure compiler', () => { + beforeAll(() => { + util.teardownStage(stageName); + }); + + it('should compile files with default options', () => { + util.setupStageWithFixture(stageName, 'build-withConfig'); + shell.mv('-f', 'tsdx.config.closure-simple.js', 'tsdx.config.js'); + + let output = shell.exec( + 'node ../dist/index.js build --env production --closureCompiler' + ); + expect(output.code).toBe(0); + + expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withconfig.cjs.development.js') + ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withconfig.cjs.production.min.js') + ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withconfig.esm.production.min.js') + ).toBeTruthy(); + + expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy(); + + output = shell.exec('node dist/index.js'); + expect(output.code).toBe(0); + expect(/bar 0/.test(output.stdout)).toBeTruthy(); + }); + + it('should compile files with advanced options', () => { + util.setupStageWithFixture(stageName, 'build-withConfig'); + shell.mv('-f', 'tsdx.config.closure-advanced.js', 'tsdx.config.js'); + + let output = shell.exec( + 'node ../dist/index.js build --env production --closureCompiler' + ); + expect(output.code).toBe(0); + + // ensure we use closure-compiler instead of terser + const plugins = require(`../../${stageName}/plugins.json`); + expect(plugins.includes('closure-compiler')).toBeTruthy(); + expect(plugins.includes('terser')).toBeFalsy(); + + expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withconfig.cjs.development.js') + ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withconfig.cjs.production.min.js') + ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withconfig.esm.production.min.js') + ).toBeTruthy(); + + expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy(); + + output = shell.exec('node dist/index.js'); + expect(output.code).toBe(0); + expect(/bar 0/.test(output.stdout)).toBeTruthy(); + }); + + afterEach(() => { + util.teardownStage(stageName); + }); +});