Skip to content

Commit e430c7e

Browse files
Remove temp-file dependency
1 parent f1f2994 commit e430c7e

13 files changed

+79
-55
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
"acorn-walk": "7.0.0",
4040
"google-closure-compiler": "20200101.0.0",
4141
"magic-string": "0.25.5",
42-
"temp-write": "4.0.0"
42+
"uuid": "3.3.3"
4343
},
4444
"devDependencies": {
4545
"@types/acorn": "4.0.5",
4646
"@types/estree": "0.0.41",
4747
"@types/node": "12.12.24",
4848
"@types/temp-write": "3.3.0",
49+
"@types/uuid": "3.4.6",
4950
"ava": "2.4.0",
5051
"builtins": "3.0.0",
5152
"c8": "7.0.0",

src/debug.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
const DEBUG_ENABLED = false;
2020

21-
/* c8 ignore next 9 */
22-
export const logSource = (preamble: string, source: string, code?: string) => {
21+
/* c8 ignore next 12 */
22+
export const logSource = async (preamble: string, source: string, code?: string): Promise<void> => {
2323
if (DEBUG_ENABLED) {
24+
const sourceLocation: string = await writeTempFile(source);
25+
const codeLocation: string = code ? await writeTempFile(code) : '';
26+
2427
console.log(preamble);
25-
console.log(sync(source));
28+
console.log(sourceLocation);
2629
if (code) {
27-
console.log(sync(code));
30+
console.log(codeLocation);
2831
}
2932
}
3033
};

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const renderChunk = async (
4444
outputOptions: OutputOptions,
4545
): Promise<{ code: string; map: SourceMapInput } | void> => {
4646
const code = await preCompilation(sourceCode, outputOptions, transforms);
47-
const [compileOptions, mapFile] = options(
47+
const [compileOptions, mapFile] = await options(
4848
requestedCompileOptions,
4949
outputOptions,
5050
code,

src/options.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { Transform } from './types';
1818
import { ModuleFormat, OutputOptions } from 'rollup';
1919
import { CompileOptions } from 'google-closure-compiler';
20-
import { sync } from 'temp-write';
20+
import { writeTempFile } from './temp-file';
2121
import { log } from './debug';
2222

2323
export const ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_UNSPECIFIED =
@@ -54,11 +54,11 @@ function validateCompileOptions(compileOptions: CompileOptions): void {
5454
* @param options
5555
* @return derived CompileOptions for Closure Compiler
5656
*/
57-
export const defaults = (
57+
export const defaults = async (
5858
options: OutputOptions,
5959
providedExterns: Array<string>,
6060
transformers: Array<Transform> | null,
61-
): CompileOptions => {
61+
): Promise<CompileOptions> => {
6262
// Defaults for Rollup Projects are slightly different than Closure Compiler defaults.
6363
// - Users of Rollup tend to transpile their code before handing it to a minifier,
6464
// so no transpile is default.
@@ -69,7 +69,7 @@ export const defaults = (
6969
for (const transform of transformers || []) {
7070
const extern = transform.extern(options);
7171
if (extern !== null) {
72-
transformerExterns.push(sync(extern));
72+
transformerExterns.push(await writeTempFile(extern));
7373
}
7474
}
7575

@@ -91,13 +91,13 @@ export const defaults = (
9191
* @param code
9292
* @param transforms
9393
*/
94-
export default function(
94+
export default async function(
9595
incomingCompileOptions: CompileOptions,
9696
outputOptions: OutputOptions,
9797
code: string,
9898
transforms: Array<Transform> | null,
99-
): [CompileOptions, string] {
100-
const mapFile: string = sync('');
99+
): Promise<[CompileOptions, string]> {
100+
const mapFile: string = await writeTempFile('');
101101
const compileOptions: CompileOptions = { ...incomingCompileOptions };
102102
let externs: Array<string> = [];
103103

@@ -119,9 +119,9 @@ export default function(
119119
}
120120

121121
const options = {
122-
...defaults(outputOptions, externs, transforms),
122+
...(await defaults(outputOptions, externs, transforms)),
123123
...compileOptions,
124-
js: sync(code),
124+
js: await writeTempFile(code),
125125
create_source_map: mapFile,
126126
};
127127

src/temp-file.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { join, dirname } from 'path';
18+
import { tmpdir } from 'os';
19+
import { v4 } from 'uuid';
20+
import { promises } from 'fs';
21+
22+
export async function writeTempFile(content: string): Promise<string> {
23+
const path: string = join(tmpdir(), v4(), '');
24+
await promises.mkdir(dirname(path), { recursive: true });
25+
await promises.writeFile(path, content, 'utf-8');
26+
27+
return path;
28+
}

src/transforms.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function preCompilation(
6060
): Promise<string> {
6161
// Each transform has a 'preCompilation' step that must complete before passing
6262
// the resulting code to Closure Compiler.
63-
logSource('before preCompilation handlers', code);
63+
await logSource('before preCompilation handlers', code);
6464
for (const transform of transforms) {
6565
transform.outputOptions = outputOptions;
6666
const result = await transform.preCompilation(code);
@@ -70,7 +70,7 @@ export async function preCompilation(
7070
}
7171
}
7272

73-
logSource('after preCompilation handlers', code);
73+
await logSource('after preCompilation handlers', code);
7474
return code;
7575
}
7676

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

95-
logSource('after postCompilation handlers', code);
95+
await logSource('after postCompilation handlers', code);
9696
return code;
9797
}

test/closure-config/prefer-config.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import test from 'ava';
1818
import options from '../../transpile-tests/options';
1919
import { generator } from '../generator';
2020

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

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

27-
test('platform javascript is respected', t => {
28-
const javascriptPlatform = options(
27+
test('platform javascript is respected', async t => {
28+
const javascriptPlatform = await options(
2929
{
3030
platform: 'javascript',
3131
},

test/closure-config/rollup-config-externs.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,20 @@ const IifeTransform = class {
3636
test('when rollup configuration specifies externs, extern is leveraged', async t => {
3737
t.plan(3);
3838

39-
const compilerOptionsExterns = compile({
39+
const compiled = await compile({
4040
externs: PROVIDED_EXTERN,
4141
}, {
4242
format: 'iife',
4343
name: 'wrapper',
44-
}, 'var x = 1;', [new IifeTransform()])[0].externs;
44+
}, 'var x = 1;', [new IifeTransform()]);
45+
const externs = compiled[0].externs;
4546

46-
t.is(compilerOptionsExterns.length, 2);
47-
t.true(compilerOptionsExterns.includes(PROVIDED_EXTERN));
47+
t.is(externs.length, 2);
48+
t.true(externs.includes(PROVIDED_EXTERN));
4849

4950
// While we can use the path for the provided extern, we need to inspect the content of
5051
// the other extern to ensure it is the generated extern.
5152
// Externs are passed as filepaths to Closure Compiler.
52-
const fileContent = await fsPromises.readFile(compilerOptionsExterns.filter(path => path !== PROVIDED_EXTERN)[0], 'utf8');
53+
const fileContent = await fsPromises.readFile(externs.filter(path => path !== PROVIDED_EXTERN)[0], 'utf8');
5354
t.true(fileContent === IIFE_TRANSFORM_EXTERN_CONTENT);
5455
});

test/closure-config/rollup-config-to-flags.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
import test from 'ava';
1818
import { defaults } from '../../transpile-tests/options';
1919

20-
test.beforeEach(t => {
20+
test.beforeEach(async t => {
2121
t.context = {
22-
default: defaults({}, [], null),
23-
esOutput: defaults({
22+
default: await defaults({}, [], null),
23+
esOutput: await defaults({
2424
format: 'es',
2525
}, [], null),
2626
};

test/closure-config/warning-level.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import test from 'ava';
1818
import compile, {ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_UNSPECIFIED, ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_INVALID} from '../../transpile-tests/options';
1919

20-
test('with no language out set, and warnings set to verbose... an error is returned', t => {
20+
test('with no language out set, and warnings set to verbose... an error is returned', async t => {
2121
try {
22-
compile({
22+
await compile({
2323
warning_level: 'VERBOSE',
2424
}, {
2525
format: 'es',
@@ -31,9 +31,9 @@ test('with no language out set, and warnings set to verbose... an error is retur
3131
}
3232
});
3333

34-
test('with language out set to no_transpile, and warnings set to verbose... an error is returned', t => {
34+
test('with language out set to no_transpile, and warnings set to verbose... an error is returned', async t => {
3535
try {
36-
compile({
36+
await compile({
3737
warning_level: 'VERBOSE',
3838
language_out: 'NO_TRANSPILE',
3939
}, {

0 commit comments

Comments
 (0)