Skip to content

Commit

Permalink
chore: support new rollup 0.48 sourcemap option
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Aug 27, 2017
1 parent cbfeec1 commit c582327
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 26 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"gulp-jasmine": "2.4.2",
"gulp-util": "3.0.8",
"jasmine-core": "2.8.0",
"rollup": "0.45.2",
"q": "1.5.0",
"rollup": "0.48.2",
"run-sequence": "2.1.0",
"tmp": "0.0.33"
}
Expand Down
36 changes: 30 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,18 @@ const prettier = require('prettier');

const NAME = 'rollup-plugin-prettier';

/**
* Check if `sourcemap` option is enable or not.
*
* @param {Object} opts Options.
* @return {boolean} `true` if sourcemap is enabled, `false` otherwise.
*/
function hasSourceMap(opts) {
return !!(opts.sourcemap || opts.sourceMap);
}

module.exports = (options = {}) => {
let sourceMap;
let _sourcemap = false;

return {
/**
Expand All @@ -47,24 +57,38 @@ module.exports = (options = {}) => {
* @return {void}
*/
options(opts = {}) {
sourceMap = !!opts.sourceMap;
// Get the global `sourcemap` option on given object.
// Should support:
// - `sourcemap` (lowercase) option which is the name with rollup >= 0.48.0,
// - `sourceMap` (camelcase) option which is the (deprecated) name with rollup < 0.48.0.
const globalSourcemap = hasSourceMap(opts);

// Since rollup 0.48, sourcemap option can be set on the `output` object.
const output = opts.output || {};
const outputSourceMap = Array.isArray(output) ? output.some(hasSourceMap) : hasSourceMap(output);

// Enable or disable `sourcemap` generation.
_sourcemap = globalSourcemap || outputSourceMap;
},

/**
* Function called by `rollup` before generating final bundle.
*
* @param {string} source Souce code of the final bundle.
* @param {Object} oo Output option.
* @return {Object} The result containing a `code` property and, if a enabled, a `map` property.
*/
transformBundle(source) {
transformBundle(source, oo) {
const output = prettier.format(source, options);

// No need to do more.
if (!sourceMap) {
// Should we generate sourcemap?
// The sourcemap option may be a boolean or any truthy value (such as a `string`).
// Note that this option should be false by default as it may take a (very) long time.
if (!_sourcemap) {
return {code: output};
}

console.log(`[${NAME}] Source-map is enabled, computing diff is required`);
console.log(`[${NAME}] Sourcemap is enabled, computing diff is required`);
console.log(`[${NAME}] This may take a moment (depends on the size of your bundle)`);

const magicString = new MagicString(source);
Expand Down
44 changes: 35 additions & 9 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const plugin = require('../dist/index.js');

describe('rollup-plugin-prettier', () => {
beforeEach(() => {
spyOn(console, 'log');
spyOn(console, 'log').and.callThrough();
});

it('should have a name', () => {
Expand All @@ -50,18 +50,47 @@ describe('rollup-plugin-prettier', () => {
);
});

it('should run esformatter with source map', () => {
it('should run prettier with sourceMap (camelcase with rollup < 0.48)', () => {
const instance = plugin();

instance.options({
sourceMap: true,
});

console.log.and.stub();

const code = 'var foo=0;var test="hello world";';
const result = instance.transformBundle(code);

expect(console.log).toHaveBeenCalledWith(
'[rollup-plugin-prettier] Sourcemap is enabled, computing diff is required'
);

expect(console.log).toHaveBeenCalledWith(
'[rollup-plugin-prettier] This may take a moment (depends on the size of your bundle)'
);

expect(result.map).toBeDefined();
expect(result.code).toBe(
'var foo = 0;\n' +
'var test = "hello world";\n'
);
});

it('should run prettier with sourcemap (lowercase with rollup >= 0.48)', () => {
const instance = plugin();

instance.options({
sourcemap: true,
});

console.log.and.stub();

const code = 'var foo=0;var test="hello world";';
const result = instance.transformBundle(code);

expect(console.log).toHaveBeenCalledWith(
'[rollup-plugin-prettier] Source-map is enabled, computing diff is required'
'[rollup-plugin-prettier] Sourcemap is enabled, computing diff is required'
);

expect(console.log).toHaveBeenCalledWith(
Expand All @@ -83,13 +112,12 @@ describe('rollup-plugin-prettier', () => {
const instance = plugin(options);

instance.options({
sourceMap: true,
sourceMap: false,
});

const code = 'var foo=0;var test="hello world";';
const result = instance.transformBundle(code);

expect(result.map).toBeDefined();
expect(result.code).toBe(
`var foo = 0;\n` +
`var test = 'hello world';\n`
Expand All @@ -100,13 +128,12 @@ describe('rollup-plugin-prettier', () => {
const instance = plugin();

instance.options({
sourceMap: true,
sourceMap: false,
});

const code = 'var foo = 0;\nvar test = "hello world";';
const result = instance.transformBundle(code);

expect(result.map).toBeDefined();
expect(result.code).toBe(
'var foo = 0;\n' +
'var test = "hello world";\n'
Expand All @@ -117,13 +144,12 @@ describe('rollup-plugin-prettier', () => {
const instance = plugin();

instance.options({
sourceMap: true,
sourceMap: false,
});

const code = 'var foo = 0;var test = "hello world";';
const result = instance.transformBundle(code);

expect(result.map).toBeDefined();
expect(result.code).toBe(
'var foo = 0;\n' +
'var test = "hello world";\n'
Expand Down
102 changes: 92 additions & 10 deletions test/it/it.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ const fs = require('fs');
const path = require('path');
const rollup = require('rollup');
const tmp = require('tmp');
const Q = require('q');
const prettier = require('../../dist/index.js');

describe('rollup-plugin-prettier', () => {
let tmpDir;

beforeEach(() => {
spyOn(console, 'log').and.callThrough();
});

beforeEach(() => {
tmpDir = tmp.dirSync({
unsafeCleanup: true,
Expand All @@ -44,28 +49,29 @@ describe('rollup-plugin-prettier', () => {
});

it('should run prettier on final bundle', (done) => {
const bundleOutput = path.join(tmpDir.name, 'bundle.js');
const rollupConfig = {
entry: path.join(__dirname, 'fixtures', 'bundle.js'),
dest: bundleOutput,
sourceMap: false,
format: 'es',
const output = path.join(tmpDir.name, 'bundle.js');
const config = {
input: path.join(__dirname, 'fixtures', 'bundle.js'),
output: {
file: output,
format: 'es',
},

plugins: [
prettier(),
],
};

rollup.rollup(rollupConfig)
.then((bundle) => bundle.write(rollupConfig))
rollup.rollup(config)
.then((bundle) => bundle.write(config.output))
.then(() => {
fs.readFile(bundleOutput, 'utf8', (err, data) => {
fs.readFile(output, 'utf8', (err, data) => {
if (err) {
done.fail(err);
}

const content = data.toString();

// Should be formatted.
expect(content).toBeDefined();
expect(content).toContain(
'function sum(array) {\n' +
Expand All @@ -77,4 +83,80 @@ describe('rollup-plugin-prettier', () => {
});
});
});

it('should run prettier on final bundle with sourcemap set in output option', (done) => {
const output = path.join(tmpDir.name, 'bundle.js');
const config = {
input: path.join(__dirname, 'fixtures', 'bundle.js'),

output: {
file: output,
format: 'es',
sourcemap: 'inline',
},

plugins: [
prettier(),
],
};

console.log.and.stub();

rollup.rollup(config)
.then((bundle) => bundle.write(config.output))
.then(() => {
fs.readFile(output, 'utf8', (err, data) => {
if (err) {
done.fail(err);
return;
}

const content = data.toString();
expect(content).toContain('//# sourceMappingURL');
expect(console.log).toHaveBeenCalledWith('[rollup-plugin-prettier] Sourcemap is enabled, computing diff is required');
done();
});
})
.catch((err) => {
done.fail(err);
});
});

it('should run prettier on final bundle with sourcemap set in output array option', (done) => {
const output = path.join(tmpDir.name, 'bundle.js');
const config = {
input: path.join(__dirname, 'fixtures', 'bundle.js'),

output: [
{file: output, format: 'es', sourcemap: 'inline'},
],

plugins: [
prettier(),
],
};

console.log.and.stub();

rollup.rollup(config)
.then((bundle) => (
Q.all(config.output.map((out) => bundle.write(out)))
))
.then(() => {
fs.readFile(output, 'utf8', (err, data) => {
if (err) {
done.fail(err);
return;
}

const content = data.toString();
expect(content).toContain('//# sourceMappingURL');
expect(console.log).toHaveBeenCalledWith('[rollup-plugin-prettier] Sourcemap is enabled, computing diff is required');
done();
});
})
.catch((err) => {
done.fail(err);
});
});
});

0 comments on commit c582327

Please sign in to comment.