Skip to content

Commit

Permalink
Inline regex source with gulp (#1537)
Browse files Browse the repository at this point in the history
Let gulp inline the source of inline regular expressions without flags in minified files.

This means that gulp will replace `/regex/.source` with `'regex'`.

## Motivation

Most IDEs support syntax highlighting and checking of JS inline regular expressions.
This makes them easier to work with and less error-prone than having a string hold the expression. Inline regexes are also usually shorter than their string counterparts because
you don't have to escape backslashes. There are also some linters (e.g. ESLint) for JS
regular expression which (of course) won't work on strings.

This lets you enjoy the advantages of inline regexes without worrying about the additional
overhead of compiling another regular expression on the client side.
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Jan 23, 2019
1 parent 0030a4e commit e894fc8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion components/prism-pug.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
'punctuation': /[.\-!=|]+/
};

var filter_pattern = '(^([\\t ]*)):{{filter_name}}(?:(?:\\r?\\n|\\r(?!\\n))(?:\\2[\\t ]+.+|\\s*?(?=\\r?\\n|\\r)))+';
var filter_pattern = /(^([\t ]*)):{{filter_name}}(?:(?:\r?\n|\r(?!\n))(?:\2[\t ]+.+|\s*?(?=\r?\n|\r)))+/.source;

// Non exhaustive list of available filters and associated languages
var filters = [
Expand Down
2 changes: 1 addition & 1 deletion components/prism-pug.min.js

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

4 changes: 2 additions & 2 deletions components/prism-pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
'ats',
'dsp'
];
var inlineLanguageRe = '%< *-\\*- *{lang}\\d* *-\\*-[\\s\\S]+?%>';
var inlineLanguageRe = /%< *-\*- *{lang}\d* *-\*-[\s\S]+?%>/.source;

inlineLanguages.forEach(function (lang) {
var alias = lang;
Expand All @@ -78,4 +78,4 @@
Prism.languages.pure['inline-lang'].inside.rest = Prism.util.clone(Prism.languages.c);
}

}(Prism));
}(Prism));
4 changes: 2 additions & 2 deletions components/prism-textile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function(Prism) {
// We don't allow for pipes inside parentheses
// to not break table pattern |(. foo |). bar |
var modifierRegex = '(?:\\([^|)]+\\)|\\[[^\\]]+\\]|\\{[^}]+\\})+';
var modifierRegex = /(?:\([^|)]+\)|\[[^\]]+\]|\{[^}]+\})+/.source;
var modifierTokens = {
'css': {
pattern: /\{[^}]+\}/,
Expand Down Expand Up @@ -254,4 +254,4 @@
Prism.languages.textile['phrase'].inside['table'].inside['acronym'] = nestedPatterns['acronym'];
Prism.languages.textile['phrase'].inside['table'].inside['mark'] = nestedPatterns['mark'];

}(Prism));
}(Prism));
20 changes: 19 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,27 @@ var gulp = require('gulp'),
reject(err);
}
});
});
}),

inlineRegexSource = function () {
return replace(
/\/((?:[^\n\r[\\\/]|\\.|\[(?:[^\n\r\\\]]|\\.)*\])*)\/\.source\b/,
function (m, source) {
// escape backslashes
source = source.replace(/\\/g, '\\\\');
// escape single quotes
source = source.replace(/'/g, "\\'");
// unescape characters like \\n and \\t to \n and \t
source = source.replace(/(^|[^\\])\\\\([nrt0])/g, '$1\\$2');
// wrap source in single quotes
return "'" + source + "'";
}
);
};

gulp.task('components', function() {
return gulp.src(paths.components)
.pipe(inlineRegexSource())
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('components'));
Expand All @@ -54,6 +71,7 @@ gulp.task('build', function() {

gulp.task('plugins', ['languages-plugins'], function() {
return gulp.src(paths.plugins)
.pipe(inlineRegexSource())
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('plugins'));
Expand Down

0 comments on commit e894fc8

Please sign in to comment.