Skip to content

Commit

Permalink
Remove temp-file dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoferbaxter committed Jan 8, 2020
1 parent f1f2994 commit e430c7e
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 55 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@
"acorn-walk": "7.0.0",
"google-closure-compiler": "20200101.0.0",
"magic-string": "0.25.5",
"temp-write": "4.0.0"
"uuid": "3.3.3"
},
"devDependencies": {
"@types/acorn": "4.0.5",
"@types/estree": "0.0.41",
"@types/node": "12.12.24",
"@types/temp-write": "3.3.0",
"@types/uuid": "3.4.6",
"ava": "2.4.0",
"builtins": "3.0.0",
"c8": "7.0.0",
Expand Down
13 changes: 8 additions & 5 deletions src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
* limitations under the License.
*/

import { sync } from 'temp-write';
import { writeTempFile } from './temp-file';

const DEBUG_ENABLED = false;

/* c8 ignore next 9 */
export const logSource = (preamble: string, source: string, code?: string) => {
/* c8 ignore next 12 */
export const logSource = async (preamble: string, source: string, code?: string): Promise<void> => {
if (DEBUG_ENABLED) {
const sourceLocation: string = await writeTempFile(source);
const codeLocation: string = code ? await writeTempFile(code) : '';

console.log(preamble);
console.log(sync(source));
console.log(sourceLocation);
if (code) {
console.log(sync(code));
console.log(codeLocation);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const renderChunk = async (
outputOptions: OutputOptions,
): Promise<{ code: string; map: SourceMapInput } | void> => {
const code = await preCompilation(sourceCode, outputOptions, transforms);
const [compileOptions, mapFile] = options(
const [compileOptions, mapFile] = await options(
requestedCompileOptions,
outputOptions,
code,
Expand Down
18 changes: 9 additions & 9 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Transform } from './types';
import { ModuleFormat, OutputOptions } from 'rollup';
import { CompileOptions } from 'google-closure-compiler';
import { sync } from 'temp-write';
import { writeTempFile } from './temp-file';
import { log } from './debug';

export const ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_UNSPECIFIED =
Expand Down Expand Up @@ -54,11 +54,11 @@ function validateCompileOptions(compileOptions: CompileOptions): void {
* @param options
* @return derived CompileOptions for Closure Compiler
*/
export const defaults = (
export const defaults = async (
options: OutputOptions,
providedExterns: Array<string>,
transformers: Array<Transform> | null,
): CompileOptions => {
): Promise<CompileOptions> => {
// Defaults for Rollup Projects are slightly different than Closure Compiler defaults.
// - Users of Rollup tend to transpile their code before handing it to a minifier,
// so no transpile is default.
Expand All @@ -69,7 +69,7 @@ export const defaults = (
for (const transform of transformers || []) {
const extern = transform.extern(options);
if (extern !== null) {
transformerExterns.push(sync(extern));
transformerExterns.push(await writeTempFile(extern));
}
}

Expand All @@ -91,13 +91,13 @@ export const defaults = (
* @param code
* @param transforms
*/
export default function(
export default async function(
incomingCompileOptions: CompileOptions,
outputOptions: OutputOptions,
code: string,
transforms: Array<Transform> | null,
): [CompileOptions, string] {
const mapFile: string = sync('');
): Promise<[CompileOptions, string]> {
const mapFile: string = await writeTempFile('');
const compileOptions: CompileOptions = { ...incomingCompileOptions };
let externs: Array<string> = [];

Expand All @@ -119,9 +119,9 @@ export default function(
}

const options = {
...defaults(outputOptions, externs, transforms),
...(await defaults(outputOptions, externs, transforms)),
...compileOptions,
js: sync(code),
js: await writeTempFile(code),
create_source_map: mapFile,
};

Expand Down
28 changes: 28 additions & 0 deletions src/temp-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { join, dirname } from 'path';
import { tmpdir } from 'os';
import { v4 } from 'uuid';
import { promises } from 'fs';

export async function writeTempFile(content: string): Promise<string> {
const path: string = join(tmpdir(), v4(), '');
await promises.mkdir(dirname(path), { recursive: true });
await promises.writeFile(path, content, 'utf-8');

return path;
}
8 changes: 4 additions & 4 deletions src/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function preCompilation(
): Promise<string> {
// Each transform has a 'preCompilation' step that must complete before passing
// the resulting code to Closure Compiler.
logSource('before preCompilation handlers', code);
await logSource('before preCompilation handlers', code);
for (const transform of transforms) {
transform.outputOptions = outputOptions;
const result = await transform.preCompilation(code);
Expand All @@ -70,7 +70,7 @@ export async function preCompilation(
}
}

logSource('after preCompilation handlers', code);
await logSource('after preCompilation handlers', code);
return code;
}

Expand All @@ -83,7 +83,7 @@ export async function preCompilation(
export async function postCompilation(code: string, transforms: Array<Transform>): Promise<string> {
// Following successful Closure Compiler compilation, each transform needs an opportunity
// to clean up work is performed in preCompilation via postCompilation.
logSource('before postCompilation handlers', code);
await logSource('before postCompilation handlers', code);
for (const transform of transforms) {
const result = await transform.postCompilation(code);
if (result && result.code) {
Expand All @@ -92,6 +92,6 @@ export async function postCompilation(code: string, transforms: Array<Transform>
}
}

logSource('after postCompilation handlers', code);
await logSource('after postCompilation handlers', code);
return code;
}
8 changes: 4 additions & 4 deletions test/closure-config/prefer-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import test from 'ava';
import options from '../../transpile-tests/options';
import { generator } from '../generator';

test('platform unspecified is respected', t => {
const typical = options({}, 'let x = 1;', []);
test('platform unspecified is respected', async t => {
const typical = await options({}, 'let x = 1;', []);

t.is(typical[0].platform, undefined);
});

test('platform javascript is respected', t => {
const javascriptPlatform = options(
test('platform javascript is respected', async t => {
const javascriptPlatform = await options(
{
platform: 'javascript',
},
Expand Down
11 changes: 6 additions & 5 deletions test/closure-config/rollup-config-externs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ const IifeTransform = class {
test('when rollup configuration specifies externs, extern is leveraged', async t => {
t.plan(3);

const compilerOptionsExterns = compile({
const compiled = await compile({
externs: PROVIDED_EXTERN,
}, {
format: 'iife',
name: 'wrapper',
}, 'var x = 1;', [new IifeTransform()])[0].externs;
}, 'var x = 1;', [new IifeTransform()]);
const externs = compiled[0].externs;

t.is(compilerOptionsExterns.length, 2);
t.true(compilerOptionsExterns.includes(PROVIDED_EXTERN));
t.is(externs.length, 2);
t.true(externs.includes(PROVIDED_EXTERN));

// While we can use the path for the provided extern, we need to inspect the content of
// the other extern to ensure it is the generated extern.
// Externs are passed as filepaths to Closure Compiler.
const fileContent = await fsPromises.readFile(compilerOptionsExterns.filter(path => path !== PROVIDED_EXTERN)[0], 'utf8');
const fileContent = await fsPromises.readFile(externs.filter(path => path !== PROVIDED_EXTERN)[0], 'utf8');
t.true(fileContent === IIFE_TRANSFORM_EXTERN_CONTENT);
});
6 changes: 3 additions & 3 deletions test/closure-config/rollup-config-to-flags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import test from 'ava';
import { defaults } from '../../transpile-tests/options';

test.beforeEach(t => {
test.beforeEach(async t => {
t.context = {
default: defaults({}, [], null),
esOutput: defaults({
default: await defaults({}, [], null),
esOutput: await defaults({
format: 'es',
}, [], null),
};
Expand Down
8 changes: 4 additions & 4 deletions test/closure-config/warning-level.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import test from 'ava';
import compile, {ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_UNSPECIFIED, ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_INVALID} from '../../transpile-tests/options';

test('with no language out set, and warnings set to verbose... an error is returned', t => {
test('with no language out set, and warnings set to verbose... an error is returned', async t => {
try {
compile({
await compile({
warning_level: 'VERBOSE',
}, {
format: 'es',
Expand All @@ -31,9 +31,9 @@ test('with no language out set, and warnings set to verbose... an error is retur
}
});

test('with language out set to no_transpile, and warnings set to verbose... an error is returned', t => {
test('with language out set to no_transpile, and warnings set to verbose... an error is returned', async t => {
try {
compile({
await compile({
warning_level: 'VERBOSE',
language_out: 'NO_TRANSPILE',
}, {
Expand Down
2 changes: 1 addition & 1 deletion test/export-cjs/export-cjs-extern.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('generate extern for cjs export pattern', async t => {
};

const transforms = createTransforms({});
const options = defaults(outputOptions, [], transforms);
const options = await defaults(outputOptions, [], transforms);

for (const externFilePath of options.externs) {
const fileContent = await fsPromises.readFile(externFilePath, 'utf8');
Expand Down
2 changes: 1 addition & 1 deletion test/iife/iife-wrapped-safely.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test('generate extern for iife name', async t => {
};

const transforms = createTransforms({});
const options = defaults(outputOptions, [], transforms);
const options = await defaults(outputOptions, [], transforms);

for (const externFilePath of options.externs) {
const fileContent = await fsPromises.readFile(externFilePath, 'utf8');
Expand Down
25 changes: 8 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,13 @@
dependencies:
"@types/node" "*"

"@types/uuid@3.4.6":
version "3.4.6"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-3.4.6.tgz#d2c4c48eb85a757bf2927f75f939942d521e3016"
integrity sha512-cCdlC/1kGEZdEglzOieLDYBxHsvEOIg7kp/2FYyVR9Pxakq+Qf/inL3RKQ+PA8gOlI/NnL+fXmQH12nwcGzsHw==
dependencies:
"@types/node" "*"

acorn-dynamic-import@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/acorn-dynamic-import/-/acorn-dynamic-import-4.0.0.tgz#482210140582a36b83c3e342e1cfebcaa9240948"
Expand Down Expand Up @@ -3990,22 +3997,6 @@ teeny-request@^3.11.3:
node-fetch "^2.2.0"
uuid "^3.3.2"

temp-dir@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/temp-dir/-/temp-dir-1.0.0.tgz#0a7c0ea26d3a39afa7e0ebea9c1fc0bc4daa011d"
integrity sha1-CnwOom06Oa+n4OvqnB/AvE2qAR0=

temp-write@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/temp-write/-/temp-write-4.0.0.tgz#cd2e0825fc826ae72d201dc26eef3bf7e6fc9320"
integrity sha512-HIeWmj77uOOHb0QX7siN3OtwV3CTntquin6TNVg6SHOqCP3hYKmox90eeFOGaY1MqJ9WYDDjkyZrW6qS5AWpbw==
dependencies:
graceful-fs "^4.1.15"
is-stream "^2.0.0"
make-dir "^3.0.0"
temp-dir "^1.0.0"
uuid "^3.3.2"

term-size@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/term-size/-/term-size-1.2.0.tgz#458b83887f288fc56d6fffbfad262e26638efa69"
Expand Down Expand Up @@ -4208,7 +4199,7 @@ util-deprecate@~1.0.1:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=

uuid@^3.3.2:
uuid@3.3.3, uuid@^3.3.2:
version "3.3.3"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.3.tgz#4568f0216e78760ee1dbf3a4d2cf53e224112866"
integrity sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ==
Expand Down

0 comments on commit e430c7e

Please sign in to comment.