Skip to content

Commit

Permalink
renderChunk to follow all transformBundle tests, type definition
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Aug 14, 2018
1 parent 120fd27 commit d7184f2
Show file tree
Hide file tree
Showing 26 changed files with 285 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Chunk.ts
Expand Up @@ -16,7 +16,7 @@ import {
GlobalsOption,
OutputOptions,
RawSourceMap,
RenderChunk,
RenderedChunk,
RenderedModule
} from './rollup/types';
import { Addons } from './utils/addons';
Expand Down Expand Up @@ -1033,7 +1033,7 @@ export default class Chunk {
this.id = outName;
}

render(options: OutputOptions, addons: Addons, outputChunk: RenderChunk) {
render(options: OutputOptions, addons: Addons, outputChunk: RenderedChunk) {
timeStart('render format', 3);

if (!this.renderedSource)
Expand Down
16 changes: 14 additions & 2 deletions src/rollup/types.d.ts
Expand Up @@ -136,6 +136,17 @@ export type TransformChunkHook = (
| void
| null;

export type RenderChunkHook = (
this: PluginContext,
code: string,
chunk: RenderedChunk,
options: OutputOptions
) =>
| Promise<{ code: string; map: RawSourceMap } | void>
| { code: string; map: RawSourceMap }
| void
| null;

export type ResolveDynamicImportHook = (
this: PluginContext,
specifier: string | ESTree.Node,
Expand Down Expand Up @@ -166,6 +177,7 @@ export interface Plugin {
// TODO: deprecate
transformBundle?: TransformChunkHook;
transformChunk?: TransformChunkHook;
renderChunk?: RenderChunkHook;
buildStart?: (this: PluginContext, options: InputOptions) => Promise<void> | void;
buildEnd?: (this: PluginContext, err?: any) => Promise<void> | void;
// TODO: deprecate
Expand Down Expand Up @@ -343,7 +355,7 @@ export interface RenderedModule {
originalLength: number;
}

export interface RenderChunk {
export interface RenderedChunk {
fileName: string;
isEntry: boolean;
imports: string[];
Expand All @@ -353,7 +365,7 @@ export interface RenderChunk {
};
}

export interface OutputChunk extends RenderChunk {
export interface OutputChunk extends RenderedChunk {
code: string;
map?: SourceMap;
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/renderChunk.ts
@@ -1,7 +1,7 @@
import { decode } from 'sourcemap-codec';
import Chunk from '../Chunk';
import Graph from '../Graph';
import { OutputOptions, Plugin, RawSourceMap, RenderChunk } from '../rollup/types';
import { OutputOptions, Plugin, RawSourceMap, RenderedChunk } from '../rollup/types';
import error from './error';

export default function renderChunk({
Expand All @@ -14,7 +14,7 @@ export default function renderChunk({
}: {
graph: Graph;
chunk: Chunk;
renderChunk: RenderChunk;
renderChunk: RenderedChunk;
code: string;
sourcemapChain: RawSourceMap[];
options: OutputOptions;
Expand Down
17 changes: 17 additions & 0 deletions test/form/samples/render-chunk-plugin/_config.js
@@ -0,0 +1,17 @@
module.exports = {
description: 'allows plugins to hook render chunk',
options: {
plugins: [
{
renderChunk(code) {
return '/* first plugin */';
}
},
{
renderChunk(code) {
return code + '\n/* second plugin */';
}
}
]
}
};
2 changes: 2 additions & 0 deletions test/form/samples/render-chunk-plugin/_expected/amd.js
@@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */
2 changes: 2 additions & 0 deletions test/form/samples/render-chunk-plugin/_expected/cjs.js
@@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */
2 changes: 2 additions & 0 deletions test/form/samples/render-chunk-plugin/_expected/es.js
@@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */
2 changes: 2 additions & 0 deletions test/form/samples/render-chunk-plugin/_expected/iife.js
@@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */
2 changes: 2 additions & 0 deletions test/form/samples/render-chunk-plugin/_expected/system.js
@@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */
2 changes: 2 additions & 0 deletions test/form/samples/render-chunk-plugin/_expected/umd.js
@@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */
1 change: 1 addition & 0 deletions test/form/samples/render-chunk-plugin/main.js
@@ -0,0 +1 @@
console.log( 1 + 1 );
@@ -0,0 +1,25 @@
module.exports = {
description: 'throws error only with first plugin renderChunk',
options: {
plugins: [
{
name: 'plugin1',
renderChunk() {
throw Error('Something happened 1');
}
},
{
name: 'plugin2',
renderChunk() {
throw Error('Something happened 2');
}
}
]
},
generateError: {
code: 'PLUGIN_ERROR',
plugin: 'plugin1',
hook: 'renderChunk',
message: `Something happened 1`
}
};
@@ -0,0 +1 @@
console.log(1);
22 changes: 22 additions & 0 deletions test/function/samples/render-chunk-async/_config.js
@@ -0,0 +1,22 @@
module.exports = {
description: 'bundle transformers can be asynchronous',
options: {
plugins: [
{
renderChunk(code) {
return Promise.resolve(code.replace('x', 1));
}
},
{
renderChunk(code) {
return code.replace('1', 2);
}
},
{
renderChunk(code) {
return Promise.resolve(code.replace('2', 3));
}
}
]
}
};
1 change: 1 addition & 0 deletions test/function/samples/render-chunk-async/main.js
@@ -0,0 +1 @@
assert.equal( x, 3 );
62 changes: 62 additions & 0 deletions test/sourcemaps/samples/names-transformed-render-chunk/_config.js
@@ -0,0 +1,62 @@
const assert = require('assert');
const uglify = require('uglify-js');
const MagicString = require('magic-string');
const getLocation = require('../../getLocation');
const SourceMapConsumer = require('source-map').SourceMapConsumer;

module.exports = {
description: 'names are recovered if transforms are used',
options: {
plugins: [
{
transform(code) {
const s = new MagicString(code);
const pattern = /mangleMe/g;
let match;

while ((match = pattern.exec(code))) {
s.overwrite(match.index, match.index + match[0].length, 'mangleMePlease', {
storeName: true,
contentOnly: false
});
}

return {
code: s.toString(),
map: s.generateMap({ hires: true })
};
},
renderChunk(code) {
return uglify.minify(code, {
sourceMap: {
filename: 'x'
}
});
}
}
]
},
test(code, map) {
const smc = new SourceMapConsumer(map);

let generatedLoc = getLocation(code, /\w+=["']this/.exec(code).index);
let originalLoc = smc.originalPositionFor(generatedLoc);

assert.deepEqual(originalLoc, {
source: '../a.js',
line: 1,
column: 4,
name: 'mangleMe'
});

generatedLoc = getLocation(code, /\w+=["']nor/.exec(code).index);
originalLoc = smc.originalPositionFor(generatedLoc);

assert.deepEqual(originalLoc, {
source: '../b.js',
line: 1,
column: 4,
name: 'mangleMe'
});
}
};
4 changes: 4 additions & 0 deletions test/sourcemaps/samples/names-transformed-render-chunk/a.js
@@ -0,0 +1,4 @@
var mangleMe = "this string should not be inlined".toLowerCase();
export default function () {
assert.equal( mangleMe, 1 );
}
4 changes: 4 additions & 0 deletions test/sourcemaps/samples/names-transformed-render-chunk/b.js
@@ -0,0 +1,4 @@
var mangleMe = "nor should this one".toLowerCase();
export default function () {
assert.equal( mangleMe, 2 );
}
@@ -0,0 +1,5 @@
import a from './a.js';
import b from './b.js';

a();
b();
36 changes: 36 additions & 0 deletions test/sourcemaps/samples/render-chunk-babili/_config.js
@@ -0,0 +1,36 @@
const babiliResults = require('./babili-results');
const assert = require('assert');
const getLocation = require('../../getLocation');
const SourceMapConsumer = require('source-map').SourceMapConsumer;

module.exports = {
description: 'generates valid sourcemap when source could not be determined',
options: {
plugins: [
{
renderChunk(code, chunk, options) {
const format = options.format;

return babiliResults[format];
}
}
],
output: { indent: false }
},
test(code, map) {
const smc = new SourceMapConsumer(map);

let generatedLoc = getLocation(code, code.indexOf('42'));
let originalLoc = smc.originalPositionFor(generatedLoc);

assert.ok(/main/.test(originalLoc.source));
assert.equal(originalLoc.line, 1);
assert.equal(originalLoc.column, 13);

generatedLoc = getLocation(code, code.indexOf('log'));
originalLoc = smc.originalPositionFor(generatedLoc);

assert.equal(originalLoc.line, 1);
assert.equal(originalLoc.column, 8);
}
};
5 changes: 5 additions & 0 deletions test/sourcemaps/samples/render-chunk-babili/babili-results.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/sourcemaps/samples/render-chunk-babili/main.js
@@ -0,0 +1 @@
console.log( 42 );
39 changes: 39 additions & 0 deletions test/sourcemaps/samples/render-chunk/_config.js
@@ -0,0 +1,39 @@
const uglify = require('uglify-js');
const assert = require('assert');
const getLocation = require('../../getLocation');
const SourceMapConsumer = require('source-map').SourceMapConsumer;

module.exports = {
description: 'preserves sourcemap chains when transforming',
options: {
plugins: [
{
renderChunk(code) {
const options = {
sourceMap: {
filename: 'x' // trigger sourcemap generation
}
};

return uglify.minify(code, options);
}
}
]
},
test(code, map) {
const smc = new SourceMapConsumer(map);

let generatedLoc = getLocation(code, code.indexOf('42'));
let originalLoc = smc.originalPositionFor(generatedLoc);

assert.ok(/main/.test(originalLoc.source));
assert.equal(originalLoc.line, 1);
assert.equal(originalLoc.column, 13);

generatedLoc = getLocation(code, code.indexOf('log'));
originalLoc = smc.originalPositionFor(generatedLoc);

assert.equal(originalLoc.line, 1);
assert.equal(originalLoc.column, 8);
}
};
1 change: 1 addition & 0 deletions test/sourcemaps/samples/render-chunk/main.js
@@ -0,0 +1 @@
console.log( 42 );
@@ -0,0 +1,30 @@
module.exports = {
description: 'preserves sourcemap chains when transforming',
options: {
plugins: [
{
name: 'fake plugin 1',
transform(code) {
return code;
}
},
{
name: 'fake plugin 2',
transform(code) {
return { code, map: null };
},
renderChunk(code) {
return { code, map: null };
}
}
]
},
warnings: [
{
code: `SOURCEMAP_BROKEN`,
plugin: 'fake plugin 1',
message: `Sourcemap is likely to be incorrect: a plugin ('fake plugin 1') was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#sourcemap-is-likely-to-be-incorrect`
}
]
};
@@ -0,0 +1 @@
console.log( 42 );

0 comments on commit d7184f2

Please sign in to comment.