|
| 1 | +/** |
| 2 | + * Copyright 2018 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 test from 'ava'; |
| 18 | +import compile from '../../transpile/options'; |
| 19 | +import path from 'path'; |
| 20 | +import fs from 'fs'; |
| 21 | +import util from 'util'; |
| 22 | +const readFile = util.promisify(fs.readFile); |
| 23 | + |
| 24 | +const PROVIDED_EXTERN = path.resolve('test', 'closure-config', 'fixtures', 'externs.js'); |
| 25 | +const IIFE_TRANSFORM_EXTERN_CONTENT = '/** @externs */ function wrapper(){}'; |
| 26 | + |
| 27 | +const IifeTransform = class { |
| 28 | + // Transforms have a public method `extern` that generates an extern |
| 29 | + // if one is needed for the transform. |
| 30 | + |
| 31 | + // This test ensures the externs created by transforms are passed to |
| 32 | + // closure compiler when the caller also passes externs. |
| 33 | + extern() { |
| 34 | + return IIFE_TRANSFORM_EXTERN_CONTENT; |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +test('when rollup configuration specifies externs, extern is leveraged', async t => { |
| 39 | + t.plan(3); |
| 40 | + |
| 41 | + const compilerOptionsExterns = compile({ |
| 42 | + externs: PROVIDED_EXTERN, |
| 43 | + }, { |
| 44 | + format: 'iife', |
| 45 | + name: 'wrapper', |
| 46 | + }, 'var x = 1;', [new IifeTransform()])[0].externs; |
| 47 | + |
| 48 | + t.is(compilerOptionsExterns.length, 2); |
| 49 | + t.true(compilerOptionsExterns.includes(PROVIDED_EXTERN)); |
| 50 | + |
| 51 | + // While we can use the path for the provided extern, we need to inspect the content of |
| 52 | + // the other extern to ensure it is the generated extern. |
| 53 | + // Externs are passed as filepaths to Closure Compiler. |
| 54 | + const fileContent = await readFile(compilerOptionsExterns.filter(path => path !== PROVIDED_EXTERN)[0], 'utf8'); |
| 55 | + t.true(fileContent === IIFE_TRANSFORM_EXTERN_CONTENT); |
| 56 | +}); |
0 commit comments