-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace smpltmpl dependency with internal function (#111)
smpltmpl depends on babel-code-frame, which, with its subdependencies adds about 10 unnecessary packages to the tree in production. Moreover, it interpolates the template by running it as code in a Node.js vm, which is not that simple, for a "simple" library. Replace it with a simple internal implementation.
- Loading branch information
Showing
6 changed files
with
82 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { readFileSync } from 'fs' | ||
|
||
export function template (tpl, data) { | ||
return tpl.replace(/\$\{([a-zA-Z0-9_-]*)}/g, (_, p1: string) => { | ||
if (!(p1 in data)) { | ||
throw new Error(`Missing value for "${p1}"`) | ||
} | ||
return String(data[p1]) | ||
}) | ||
} | ||
|
||
export function templateFromFile (file: string, data: object): string { | ||
const contents = readFileSync(file, 'utf8') | ||
return template(contents, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello ${value1}, ${value2} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* @adonisjs/ace | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import test from 'japa' | ||
import { join } from 'path' | ||
import { template, templateFromFile } from '../src/utils/template' | ||
|
||
test.group('template', () => { | ||
test('template: interpolate valid template', (assert) => { | ||
const result = template('${test} ${other}', { | ||
test: 123, | ||
other: 'hello', | ||
}) | ||
assert.strictEqual(result, '123 hello') | ||
}) | ||
|
||
test('template: error if a value is missing', (assert) => { | ||
assert.throws( | ||
() => template('${param}', {}), | ||
'Missing value for "param"' | ||
) | ||
}) | ||
|
||
test('templateFromFile: interpolate valid template', (assert) => { | ||
const result = templateFromFile(join(__dirname, 'fixtures/template1.txt'), { | ||
value1: 'World', | ||
value2: 42, | ||
}) | ||
|
||
assert.strictEqual(result, 'Hello World, 42') | ||
}) | ||
|
||
test('templateFromFile: error if a value is missing', (assert) => { | ||
assert.throws(() => templateFromFile(join(__dirname, 'fixtures/template1.txt'), { | ||
value1: 'World', | ||
}), 'Missing value for "value2"') | ||
}) | ||
|
||
test('templateFromFile: error if file is missing', (assert) => { | ||
assert.throws(() => templateFromFile(join(__dirname, 'fixtures/i-do-not-exist'), {})) | ||
}) | ||
}) |