Skip to content

Commit e894fc8

Browse files
RunDevelopmentmAAdhaTTah
authored andcommitted
Inline regex source with gulp (#1537)
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.
1 parent 0030a4e commit e894fc8

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

components/prism-pug.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
'punctuation': /[.\-!=|]+/
156156
};
157157

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

160160
// Non exhaustive list of available filters and associated languages
161161
var filters = [

components/prism-pug.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/prism-pure.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
'ats',
5555
'dsp'
5656
];
57-
var inlineLanguageRe = '%< *-\\*- *{lang}\\d* *-\\*-[\\s\\S]+?%>';
57+
var inlineLanguageRe = /%< *-\*- *{lang}\d* *-\*-[\s\S]+?%>/.source;
5858

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

81-
}(Prism));
81+
}(Prism));

components/prism-textile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function(Prism) {
22
// We don't allow for pipes inside parentheses
33
// to not break table pattern |(. foo |). bar |
4-
var modifierRegex = '(?:\\([^|)]+\\)|\\[[^\\]]+\\]|\\{[^}]+\\})+';
4+
var modifierRegex = /(?:\([^|)]+\)|\[[^\]]+\]|\{[^}]+\})+/.source;
55
var modifierTokens = {
66
'css': {
77
pattern: /\{[^}]+\}/,
@@ -254,4 +254,4 @@
254254
Prism.languages.textile['phrase'].inside['table'].inside['acronym'] = nestedPatterns['acronym'];
255255
Prism.languages.textile['phrase'].inside['table'].inside['mark'] = nestedPatterns['mark'];
256256

257-
}(Prism));
257+
}(Prism));

gulpfile.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,27 @@ var gulp = require('gulp'),
3434
reject(err);
3535
}
3636
});
37-
});
37+
}),
38+
39+
inlineRegexSource = function () {
40+
return replace(
41+
/\/((?:[^\n\r[\\\/]|\\.|\[(?:[^\n\r\\\]]|\\.)*\])*)\/\.source\b/,
42+
function (m, source) {
43+
// escape backslashes
44+
source = source.replace(/\\/g, '\\\\');
45+
// escape single quotes
46+
source = source.replace(/'/g, "\\'");
47+
// unescape characters like \\n and \\t to \n and \t
48+
source = source.replace(/(^|[^\\])\\\\([nrt0])/g, '$1\\$2');
49+
// wrap source in single quotes
50+
return "'" + source + "'";
51+
}
52+
);
53+
};
3854

3955
gulp.task('components', function() {
4056
return gulp.src(paths.components)
57+
.pipe(inlineRegexSource())
4158
.pipe(uglify())
4259
.pipe(rename({ suffix: '.min' }))
4360
.pipe(gulp.dest('components'));
@@ -54,6 +71,7 @@ gulp.task('build', function() {
5471

5572
gulp.task('plugins', ['languages-plugins'], function() {
5673
return gulp.src(paths.plugins)
74+
.pipe(inlineRegexSource())
5775
.pipe(uglify())
5876
.pipe(rename({ suffix: '.min' }))
5977
.pipe(gulp.dest('plugins'));

0 commit comments

Comments
 (0)