Skip to content

Commit

Permalink
Clean duplicated licenses
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Martins committed Nov 17, 2020
1 parent 57f0faa commit e61b280
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"karma-chai": "^0.1.0",
"karma-mocha": "^1.3.0",
"lit-analyzer": "^1.1.9",
"magic-string": "^0.25.7",
"mocha": "^7.1.1",
"prettier": "^2.0.4",
"rimraf": "^3.0.2",
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import filesize from 'rollup-plugin-filesize';
import { terser } from 'rollup-plugin-terser';
import resolve from 'rollup-plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import cleanLicenses from './utils/rollup-clean-licenses';

export default {
input: 'src/login-button.js',
Expand All @@ -41,6 +42,7 @@ export default {
},
},
}),
cleanLicenses(),
filesize({
showBrotliSize: true,
}),
Expand Down
34 changes: 34 additions & 0 deletions utils/rollup-clean-licenses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import MagicString from 'magic-string';
/**
* Rollup plugin that removes all license banners of source files.
* This is necessary to avoid having the license comment repeated in the output.
*/
let polymerLicense = null;

export default function () {
return {
name: 'clean-licenses',
transform(code) {
const newContent = new MagicString(code);
const regex = new RegExp(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm);
[...code.matchAll(regex)].forEach((match) => {
if (match[0].indexOf('http://polymer.github.io/LICENSE.txt') > -1) {
if (!polymerLicense) [polymerLicense] = match;
newContent.overwrite(match.index, match.index + match[0].length, '\n\n');
}
});
return {
code: newContent.toString(),
map: newContent.generateMap({ hires: true }),
};
},
renderChunk(code) {
const newContent = new MagicString(code);
if (polymerLicense) newContent.prependLeft(0, polymerLicense);
return {
code: newContent.toString(),
map: newContent.generateMap({ hires: true }),
};
},
};
}

0 comments on commit e61b280

Please sign in to comment.