Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroseus committed Mar 20, 2020
1 parent f46c04c commit 11c5862
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 127 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ export interface TsdxOptions {
// Only transpile, do not type check (makes compilation faster)
transpileOnly?: boolean;
// EXPERIMENTAL: Use closure compiler to minify production bundle instead of terser
// use closureCompilerOptions section from tsdx.config.js for advanced options
closureCompiler?: boolean;
}
```
Expand Down
3 changes: 1 addition & 2 deletions src/createBuildConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ let tsdxConfig = {
};

if (fs.existsSync(paths.appConfig)) {
const customConfig = require(paths.appConfig);
tsdxConfig = { ...tsdxConfig, ...customConfig };
tsdxConfig = require(paths.appConfig);
}

export async function createBuildConfigs(
Expand Down
15 changes: 4 additions & 11 deletions src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { safeVariableName, safePackageName, external } from './utils';
import { paths } from './constants';
import { RollupOptions } from 'rollup';
import { terser } from 'rollup-plugin-terser';
import compiler from '@ampproject/rollup-plugin-closure-compiler';
import closureCompiler from '@ampproject/rollup-plugin-closure-compiler';
import { DEFAULT_EXTENSIONS } from '@babel/core';
// import babel from 'rollup-plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
Expand All @@ -12,7 +12,6 @@ import resolve from '@rollup/plugin-node-resolve';
import sourceMaps from 'rollup-plugin-sourcemaps';
import typescript from 'rollup-plugin-typescript2';
import ts from 'typescript';
import * as fs from 'fs-extra';

import { extractErrors } from './errors/extractErrors';
import { babelPluginTsdx } from './babelPluginTsdx';
Expand All @@ -36,14 +35,6 @@ export async function createRollupConfig(
const shouldMinify =
opts.minify !== undefined ? opts.minify : opts.env === 'production';

let closureCompilerOptions: any;

if (await fs.pathExists(paths.appConfig)) {
// use closureCompilerOptions section from tsdx.config.js
const tsdxConfig = require(paths.appConfig);
closureCompilerOptions = tsdxConfig.closureCompilerOptions;
}

const outputName = [
`${paths.appDist}/${safePackageName(opts.name)}`,
opts.format,
Expand Down Expand Up @@ -206,7 +197,9 @@ export async function createRollupConfig(
// }),
shouldMinify &&
(opts.closureCompiler
? compiler(closureCompilerOptions)
? closureCompiler({
compilation_level: 'SIMPLE_OPTIMIZATIONS',
})
: terser({
sourcemap: true,
output: { comments: false },
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export interface TsdxOptions extends SharedOpts {
// Only transpile, do not type check (makes compilation faster)
transpileOnly?: boolean;
// EXPERIMENTAL: Use closure compiler to minify production bundle instead of terser
// use closureCompilerOptions section from tsdx.config.js for advanced options
closureCompiler?: boolean;
}

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/build-withConfig/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { split } from './foo';

export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('fuck');
}
return a + b;
};

Expand Down
18 changes: 16 additions & 2 deletions test/fixtures/build-withConfig/tsdx.config.closure-advanced.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
const closureCompiler = require('@ampproject/rollup-plugin-closure-compiler');

const closureCompilerPlugin = closureCompiler({
compilation_level: 'ADVANCED_OPTIMIZATIONS',
});

module.exports = {
closureCompilerOptions: {
compilation_level: 'ADVANCED_OPTIMIZATIONS',
rollup(config) {
config.plugins = config.plugins.map(plugin => {
// override closure compiler plugin's default config
if (plugin && plugin.name === closureCompilerPlugin.name) {
return closureCompilerPlugin;
}
return plugin;
});

return config;
},
};
6 changes: 0 additions & 6 deletions test/fixtures/build-withConfig/tsdx.config.closure-simple.js

This file was deleted.

36 changes: 24 additions & 12 deletions test/tests/tsdx-build-closure-compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ const util = require('../fixtures/util');

shell.config.silent = false;

const grep = (pattern, file) => {
const output = shell.grep(pattern, file);
const grepOutput = output.stdout.replace(`\n`, ``);
return grepOutput.length > 0;
};

const stageName = 'stage-build-closure-compiler';

describe('tsdx build with closure compiler', () => {
describe('tsdx build with closure compiler default options', () => {
beforeAll(() => {
util.teardownStage(stageName);
});

it('should minify bundle 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'
Expand All @@ -36,11 +41,15 @@ describe('tsdx build with closure compiler', () => {

expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy();

// only closure compiler minifies bundle to `signature="bar 0"`
output = shell.grep('bar 0', [
'dist/build-withconfig.esm.production.min.js',
]);
expect(/bar 0/.test(output.stdout)).toBeTruthy();
const lib = require(`../../${stageName}/dist`);
expect(lib.signature).toBe('bar 0');

expect(
grep(
'exports.signature=signature',
'dist/build-withconfig.cjs.production.min.js'
)
).toBeTruthy();
});

it('should minify bundle with advanced options', () => {
Expand All @@ -65,11 +74,14 @@ describe('tsdx build with closure compiler', () => {

expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy();

// only closure compiler minifies bundle to `signature="bar 0"`
output = shell.grep('bar 0', [
'dist/build-withconfig.esm.production.min.js',
]);
expect(/bar 0/.test(output.stdout)).toBeTruthy();
const lib = require(`../../${stageName}/dist`);
expect(lib.signature).toBe('bar 0');

// closure compiler with advanced optimization level
// minifies bundle with `signature="bar 0"`
expect(
grep('bar 0', 'dist/build-withconfig.cjs.production.min.js')
).toBeTruthy();
});

afterEach(() => {
Expand Down
Loading

0 comments on commit 11c5862

Please sign in to comment.