Skip to content

Commit 5a63335

Browse files
authored
Merge pull request #1 from Concatapult/master
Allow arbitrary strings in import statements
2 parents 3e4a5c3 + 11aadde commit 5a63335

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const importReg = /^import\s+((\* as )?\s*([\w_]+)\s*)?,?\s*(\{\s*([\w_,\s]*)\s\})?\s*(from\s+)?(['"][\._\-\w\/]+['"])/gm;
1+
const stringLiteral = `"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*'`;
2+
const importReg = new RegExp(`^import\\s+((\\* as )?\\s*([\\w_]+)\\s*)?,?\\s*(\\{\\s*([\\w_,\\s]*)\\s\\})?\\s*(from\\s+)?(${stringLiteral})`, 'gm');
23
const exportRef = /^export\s+(default)?\s*(const|let|var|class|function|interface)?\s*([\w_]*)/mg;
34

45
export default function transform(source: string) {

src/tests/transform_spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ describe('transform', () => {
66
const imp = "import 'Bar'";
77
assert.equal(transform(imp), "require('Bar')");
88

9+
const imp = "import 'arbitrary \\' string'";
10+
assert.equal(transform(imp), "require('arbitrary \\' string')");
11+
912
const imp1 = "import * as Foo from 'Bar'";
1013
assert.equal(transform(imp1), "const Foo = require('Bar')");
1114
});

0 commit comments

Comments
 (0)