Skip to content

Commit

Permalink
Bundle Valid ES5 syntax checker in Rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Oct 4, 2017
1 parent 51c6127 commit dd9b482
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
16 changes: 3 additions & 13 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
// https://github.com/Turfjs/turf/pull/986
function typescriptExportsDefault() {
return {
name: 'typescript-default-export',
transformBundle(code) {
code = code.trim();
const name = code.match(/module.exports = (\w+)/)
if (name) code += `\nmodule.exports.default = ${name[1]};\n`;
return code;
}
}
}
import typescriptExport from './scripts/rollup-plugin-typescript-export';
import validES5 from './scripts/rollup-plugin-valid-es5';

export default {
input: 'index.js',
Expand All @@ -18,5 +8,5 @@ export default {
file: 'main.js',
format: 'cjs'
},
plugins: [typescriptExportsDefault()]
plugins: [typescriptExport(), validES5()]
};
12 changes: 12 additions & 0 deletions scripts/rollup-plugin-typescript-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://github.com/Turfjs/turf/pull/986
export default function () {
return {
name: 'typescript-export',
transformBundle(code) {
code = code.trim();
const name = code.match(/module.exports = (\w+)/);
if (name) code += `\nmodule.exports.default = ${name[1]};\n`;
return code;
}
};
}
24 changes: 24 additions & 0 deletions scripts/rollup-plugin-valid-es5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default function () {
return {
name: 'valid-es5',
transformBundle(code) {
removeComments(code).match(/[\w\=\>]+/g).forEach(word => {
switch (word) {
case 'const':
case 'let':
case '=>':
throw new Error(word + ' is not valid ES5 syntax');
}
});
return code;
}
};
}

function removeComments(code) {
// Remove comments block comments
code = code.replace(/\/\*\*[\w\s*\.@{}|<>,=()[\];\/-]+\*\//g, '');
// Remove inline comments
code = code.replace(/(\/\/[\s\w()\*\/\\]*)\n/g, '\n');
return code;
}

0 comments on commit dd9b482

Please sign in to comment.