Skip to content

Commit

Permalink
feat: sourcemap is now enable by default
Browse files Browse the repository at this point in the history
To avoid incompatibility with other rollup plugins and / or rollup
itself, sourcemap is now enabled by default.
Note that if sourcemap is disabled globally, it will be automatically
disabled. For example:

```javascript
const license = require('rollup-plugin-license');

module.exports = {
  input: './index.js',
  output: {
    file: './dist/bundle.js',
  },

  // Disable sourcemap globally.
  sourcemap: false,

  plugins: [
    license({
      banner: `Copyright <%= moment().format('YYYY') %>`,
    }),
  ],
};
```
  • Loading branch information
mjeanroy committed Aug 21, 2017
1 parent edf4edb commit f2fd89d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ module.exports = (options = {}) => {
}

// Rollup >= 0.48 replace `sourceMap` with `sourcemap`.
if (opts.sourceMap || opts.sourcemap) {
plugin.enableSourceMap();
// If `sourcemap` is disabled globally, disable it on the plugin.
if (opts.sourceMap === false || opts.sourcemap === false) {
plugin.disableSourceMap();
}
},

Expand Down
6 changes: 3 additions & 3 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class LicensePlugin {
this.name = 'rollup-plugin-license';

this._options = options;
this._sourceMap = false;
this._sourceMap = true;
this._cwd = process.cwd();
this._dependencies = {};
this._pkg = require(path.join(this._cwd, 'package.json'));
Expand All @@ -65,8 +65,8 @@ class LicensePlugin {
*
* @return {void}
*/
enableSourceMap() {
this._sourceMap = true;
disableSourceMap() {
this._sourceMap = false;
}

/**
Expand Down
36 changes: 24 additions & 12 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,59 +45,71 @@ describe('rollup-plugin-license', () => {
});

it('should enable sourceMap if options.sourceMap is true', () => {
spyOn(LicensePlugin.prototype, 'enableSourceMap').and.callThrough();
spyOn(LicensePlugin.prototype, 'disableSourceMap').and.callThrough();

const instance = plugin();
const result = instance.options({
sourceMap: true,
});

expect(result).not.toBeDefined();
expect(LicensePlugin.prototype.enableSourceMap).toHaveBeenCalledWith();
expect(LicensePlugin.prototype.disableSourceMap).not.toHaveBeenCalled();
});

it('should enable sourceMap if options.sourcemap (lowercase) is true', () => {
spyOn(LicensePlugin.prototype, 'enableSourceMap').and.callThrough();
spyOn(LicensePlugin.prototype, 'disableSourceMap').and.callThrough();

const instance = plugin();
const result = instance.options({
sourcemap: true,
});

expect(result).not.toBeDefined();
expect(LicensePlugin.prototype.enableSourceMap).toHaveBeenCalledWith();
expect(LicensePlugin.prototype.disableSourceMap).not.toHaveBeenCalled();
});

it('should not enable sourceMap if options.sourceMap is false', () => {
spyOn(LicensePlugin.prototype, 'enableSourceMap').and.callThrough();
spyOn(LicensePlugin.prototype, 'disableSourceMap').and.callThrough();

const instance = plugin();
const result = instance.options({
sourceMap: false,
});

expect(result).not.toBeDefined();
expect(LicensePlugin.prototype.enableSourceMap).not.toHaveBeenCalled();
expect(LicensePlugin.prototype.disableSourceMap).toHaveBeenCalledWith();
});

it('should not enable sourceMap by default', () => {
spyOn(LicensePlugin.prototype, 'enableSourceMap').and.callThrough();
it('should not enable sourceMap if options.sourcemap (lowercase) is false', () => {
spyOn(LicensePlugin.prototype, 'disableSourceMap').and.callThrough();

const instance = plugin();
const result = instance.options({
sourcemap: false,
});

expect(result).not.toBeDefined();
expect(LicensePlugin.prototype.disableSourceMap).toHaveBeenCalledWith();
});

it('should enable sourceMap by default', () => {
spyOn(LicensePlugin.prototype, 'disableSourceMap').and.callThrough();

const instance = plugin();
const result = instance.options({});

expect(result).not.toBeDefined();
expect(LicensePlugin.prototype.enableSourceMap).not.toHaveBeenCalled();
expect(LicensePlugin.prototype.disableSourceMap).not.toHaveBeenCalled();
});

it('should not enable sourceMap if options is not set', () => {
spyOn(LicensePlugin.prototype, 'enableSourceMap').and.callThrough();
it('should enable sourceMap if options is not set', () => {
spyOn(LicensePlugin.prototype, 'disableSourceMap').and.callThrough();

const instance = plugin();
const result = instance.options();

expect(result).not.toBeDefined();
expect(LicensePlugin.prototype.enableSourceMap).not.toHaveBeenCalled();
expect(LicensePlugin.prototype.disableSourceMap).not.toHaveBeenCalled();
});

it('should prepend banner when bundle is transformed', () => {
Expand Down
27 changes: 11 additions & 16 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ describe('LicensePlugin', () => {
const plugin = new LicensePlugin();
expect(plugin._cwd).toBeDefined();
expect(plugin._pkg).toBeDefined();
expect(plugin._sourceMap).toBe(false);
expect(plugin._sourceMap).toBe(true);
expect(plugin._dependencies).toEqual({});
});

it('should enable source map', () => {
it('should disable source map', () => {
const plugin = new LicensePlugin();
expect(plugin._sourceMap).toBe(false);

plugin.enableSourceMap();
expect(plugin._sourceMap).toBe(true);

plugin.disableSourceMap();
expect(plugin._sourceMap).toBe(false);
});

it('should load pkg', () => {
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('LicensePlugin', () => {
const result = instance.prependBanner(code);

expect(result).toBeDefined();
expect(result.map).not.toBeDefined();
expect(result.map).toBeDefined();
expect(result.code).toEqual(
`/**\n` +
` * Test banner.\n` +
Expand All @@ -365,7 +365,7 @@ describe('LicensePlugin', () => {
const result = instance.prependBanner(code);

expect(result).toBeDefined();
expect(result.map).not.toBeDefined();
expect(result.map).toBeDefined();
expect(result.code).toEqual(
`/**\n` +
` * Test banner.\n` +
Expand Down Expand Up @@ -394,7 +394,7 @@ describe('LicensePlugin', () => {

expect(fs.readFileSync).toHaveBeenCalledWith(jasmine.any(String), encoding);
expect(result).toBeDefined();
expect(result.map).not.toBeDefined();
expect(result.map).toBeDefined();
expect(result.code).toEqual(
`/**\n` +
` * Test banner.\n` +
Expand All @@ -406,20 +406,20 @@ describe('LicensePlugin', () => {
);
});

it('should prepend banner to bundle with source map', () => {
it('should prepend banner to bundle without source map', () => {
const instance = new LicensePlugin({
banner: {
file: path.join(__dirname, 'fixtures', 'banner.js'),
},
});

instance.enableSourceMap();
instance.disableSourceMap();

const code = 'var foo = 0;';
const result = instance.prependBanner(code);

expect(result).toBeDefined();
expect(result.map).toBeDefined();
expect(result.map).not.toBeDefined();
expect(result.code).toEqual(
`/**\n` +
` * Test banner.\n` +
Expand All @@ -433,7 +433,6 @@ describe('LicensePlugin', () => {

it('should not prepend default banner to bundle', () => {
const instance = new LicensePlugin();
instance.enableSourceMap();

const code = 'var foo = 0;';
const result = instance.prependBanner(code);
Expand All @@ -451,8 +450,6 @@ describe('LicensePlugin', () => {
},
});

instance.enableSourceMap();

const code = 'var foo = 0;';
const result = instance.prependBanner(code);

Expand Down Expand Up @@ -492,8 +489,6 @@ describe('LicensePlugin', () => {
},
});

instance.enableSourceMap();

const code = 'var foo = 0;';
const result = instance.prependBanner(code);

Expand Down

0 comments on commit f2fd89d

Please sign in to comment.