File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed
packages/nextjs/src/utils Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Determine if the given source code represents a file written using ES6 modules.
3
+ *
4
+ * The regexes used are from https://github.com/component/is-module, which got them from
5
+ * https://github.com/formatjs/js-module-formats, which says it got them from
6
+ * https://github.com/ModuleLoader/es-module-loader, though the originals are now nowhere to be found.
7
+ *
8
+ * @param moduleSource The source code of the module
9
+ * @returns True if the module contains ESM-patterned code, false otherwise.
10
+ */
11
+ export function isESM ( moduleSource : string ) : boolean {
12
+ const importExportRegex =
13
+ / (?: ^ \s * | [ } { ( ) ; , \n ] \s * ) ( i m p o r t \s + [ ' " ] | ( i m p o r t | m o d u l e ) \s + [ ^ " ' ( ) \n ; ] + \s + f r o m \s + [ ' " ] | e x p o r t \s + ( \* | \{ | d e f a u l t | f u n c t i o n | v a r | c o n s t | l e t | [ _ $ a - z A - Z \xA0 - \uFFFF ] [ _ $ a - z A - Z 0 - 9 \xA0 - \uFFFF ] * ) ) / ;
14
+ const exportStarRegex = / (?: ^ \s * | [ } { ( ) ; , \n ] \s * ) ( e x p o r t \s * \* \s * f r o m \s * (?: ' ( [ ^ ' ] + ) ' | " ( [ ^ " ] + ) " ) ) / ;
15
+
16
+ return importExportRegex . test ( moduleSource ) || exportStarRegex . test ( moduleSource ) ;
17
+ }
You can’t perform that action at this time.
0 commit comments