Skip to content

Commit 48208bb

Browse files
Improve test coverage for imports slightly
1 parent 6a64b6b commit 48208bb

7 files changed

Lines changed: 107 additions & 80 deletions

File tree

src/transformers/imports.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,23 @@ window['${DYNAMIC_IMPORT_REPLACEMENT}'] = ${DYNAMIC_IMPORT_REPLACEMENT};`;
8484
walk.simple(program, {
8585
async ImportDeclaration(node: ImportDeclaration) {
8686
const name = literalName(self.context, node.source);
87-
const range: Range = node.range ? [node.range[0], node.range[1]] : [0, 0];
88-
self.importedExternalsSyntax[name] = code.slice(range[0], range[1]);
89-
source.remove(range[0], range[1]);
87+
const range: Range = node.range as Range;
88+
self.importedExternalsSyntax[name] = code.slice(...range);
89+
source.remove(...range);
9090

9191
self.importedExternalsLocalNames = self.importedExternalsLocalNames.concat(
9292
importLocalNames(self.context, node),
9393
);
9494
},
9595
Import(node: RangedImport) {
96+
const [start, end] = node.range;
9697
self.dynamicImportPresent = true;
9798
// Rename the `import` method to something we can put in externs.
9899
// CC doesn't understand dynamic import yet.
99100
source.overwrite(
100-
node.range[0],
101-
node.range[1],
102-
code
103-
.substring(node.range[0], node.range[1])
104-
.replace(DYNAMIC_IMPORT_KEYWORD, DYNAMIC_IMPORT_REPLACEMENT),
101+
start,
102+
end,
103+
code.substring(start, end).replace(DYNAMIC_IMPORT_KEYWORD, DYNAMIC_IMPORT_REPLACEMENT),
105104
);
106105
},
107106
});
@@ -128,8 +127,8 @@ window['${DYNAMIC_IMPORT_REPLACEMENT}'] = ${DYNAMIC_IMPORT_REPLACEMENT};`;
128127
walk.simple(program, {
129128
Identifier(node: Identifier) {
130129
if (node.name === DYNAMIC_IMPORT_REPLACEMENT) {
131-
const range: Range = node.range ? [node.range[0], node.range[1]] : [0, 0];
132-
source.overwrite(range[0], range[1], DYNAMIC_IMPORT_KEYWORD);
130+
const [start, end] = node.range as Range;
131+
source.overwrite(start, end, DYNAMIC_IMPORT_KEYWORD);
133132
}
134133
},
135134
});

src/types.ts

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

17-
import * as path from 'path';
18-
import {
19-
OutputOptions,
20-
TransformSourceDescription,
21-
PluginContext,
22-
InputOptions,
23-
InputOption,
24-
} from 'rollup';
17+
import { OutputOptions, TransformSourceDescription, PluginContext, InputOptions } from 'rollup';
2518
const dynamicImport = require('acorn-dynamic-import');
2619

2720
// @see https://github.com/estree/estree/blob/master/es2015.md#imports
@@ -65,9 +58,6 @@ export interface ExportDetails {
6558
range: Range;
6659
source: string | null;
6760
}
68-
// export interface ExportDetailsMapping {
69-
// [key: string]: ExportDetails;
70-
// }
7161

7262
export type TransformMethod = (code: string) => Promise<TransformSourceDescription>;
7363
export interface TransformInterface {
@@ -96,25 +86,27 @@ export class Transform implements TransformInterface {
9686
code,
9787
};
9888
}
89+
9990
public async postCompilation(code: string): Promise<TransformSourceDescription> {
10091
return {
10192
code,
10293
};
10394
}
10495

105-
protected isEntryPoint(id: string) {
106-
const inputs = (input: InputOption): Array<string> => {
107-
if (typeof input === 'string') {
108-
return [input];
109-
} else if (typeof input === 'object') {
110-
return Object.values(input);
111-
} else {
112-
return input;
113-
}
114-
};
96+
// TODO (KB): Is this needed?
97+
// protected isEntryPoint(id: string) {
98+
// const inputs = (input: InputOption): Array<string> => {
99+
// if (typeof input === 'string') {
100+
// return [input];
101+
// } else if (typeof input === 'object') {
102+
// return Object.values(input);
103+
// } else {
104+
// return input;
105+
// }
106+
// };
115107

116-
return inputs(this.inputOptions.input)
117-
.map(input => path.resolve(input))
118-
.includes(id);
119-
}
108+
// return inputs(this.inputOptions.input)
109+
// .map(input => path.resolve(input))
110+
// .includes(id);
111+
// }
120112
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2019 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 '../generator';
19+
20+
test.serial('export * is unsupported', async t => {
21+
try {
22+
await compile('export-all', 'all-external', false, {default:{}}, 'default', 'esm');
23+
24+
t.fail('expected error, but passed');
25+
} catch(e) {
26+
t.is(e.message, 'Rollup Plugin Closure Compiler does not support export all syntax for externals.');
27+
}
28+
});
29+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './external.js';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './external.js';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const export1 = 1;
2+
export function export2() {
3+
return 2;
4+
}

test/generator.js

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -50,71 +50,71 @@ const fixtureLocation = (category, name, format, optionsKey, minified = false) =
5050
: `${name}.js`
5151
}`;
5252

53-
function generate(shouldFail, category, name, codeSplit, formats, closureFlags) {
54-
const targetLength = longest(formats);
55-
const optionLength = longest(Object.keys(closureFlags));
53+
async function compile(category, name, codeSplit, closureFlags, optionKey, format) {
54+
const bundle = await rollup.rollup({
55+
input: fixtureLocation(category, name, format, optionKey, false),
56+
plugins: [compiler(closureFlags[optionKey])],
57+
external: ['lodash', './external.js', './external-default.js'],
58+
experimentalCodeSplitting: codeSplit,
59+
onwarn: _ => null,
60+
});
5661

57-
async function compile(optionKey, format) {
58-
const bundle = await rollup.rollup({
59-
input: fixtureLocation(category, name, format, optionKey, false),
60-
plugins: [compiler(closureFlags[optionKey])],
61-
external: ['lodash', './external.js', './external-default.js'],
62-
experimentalCodeSplitting: codeSplit,
63-
onwarn: _ => null,
64-
});
62+
const bundles = await bundle.generate({
63+
format,
64+
sourcemap: true,
65+
});
6566

66-
const bundles = await bundle.generate({
67-
format,
68-
sourcemap: true,
69-
});
70-
71-
const output = [];
72-
if (bundles.output) {
73-
for (file in bundles.output) {
74-
const minified = await fs.promises.readFile(
75-
path.join(
76-
fixtureLocation(
77-
category,
78-
path.parse(bundles.output[file].fileName).name,
79-
format,
80-
optionKey,
81-
true,
82-
),
83-
),
84-
'utf8',
85-
);
86-
output.push({
87-
minified,
88-
code: bundles.output[file].code,
89-
});
90-
}
91-
} else {
67+
const output = [];
68+
if (bundles.output) {
69+
for (file in bundles.output) {
9270
const minified = await fs.promises.readFile(
9371
path.join(
94-
fixtureLocation(category, path.parse(bundles.fileName).name, format, optionKey, true),
72+
fixtureLocation(
73+
category,
74+
path.parse(bundles.output[file].fileName).name,
75+
format,
76+
optionKey,
77+
true,
78+
),
9579
),
9680
'utf8',
9781
);
9882
output.push({
9983
minified,
100-
code: bundles.code,
84+
code: bundles.output[file].code,
10185
});
10286
}
103-
104-
return output;
87+
} else {
88+
const minified = await fs.promises.readFile(
89+
path.join(
90+
fixtureLocation(category, path.parse(bundles.fileName).name, format, optionKey, true),
91+
),
92+
'utf8',
93+
);
94+
output.push({
95+
minified,
96+
code: bundles.code,
97+
});
10598
}
10699

100+
return output;
101+
}
102+
103+
function generate(shouldFail, category, name, codeSplit, formats, closureFlags) {
104+
const targetLength = longest(formats);
105+
const optionLength = longest(Object.keys(closureFlags));
106+
107107
for (const format of formats) {
108-
for(const optionKey of Object.keys(closureFlags)) {
108+
for (const optionKey of Object.keys(closureFlags)) {
109109
const method = shouldFail ? test.serial.failing : test.serial;
110110
method(
111111
`${name}${format.padEnd(targetLength)}${optionKey.padEnd(optionLength)}`,
112112
async t => {
113-
const output = await compile(optionKey, format);
113+
const output = await compile(category, name, codeSplit, closureFlags, optionKey, format);
114114

115115
t.plan(output.length);
116116
for (result of output) {
117-
t.is(result.code, result.minified)
117+
t.is(result.code, result.minified);
118118
}
119119
},
120120
);
@@ -150,4 +150,5 @@ module.exports = {
150150
ESM_OUTPUT,
151151
generator,
152152
failureGenerator,
153-
};
153+
compile,
154+
};

0 commit comments

Comments
 (0)