-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add typescript plugin, add tests * update docs and templates * Update plugin.js
- Loading branch information
1 parent
2b3670f
commit d86e495
Showing
18 changed files
with
225 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
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
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,73 @@ | ||
const plugin = require('../plugin.js'); | ||
const {EventEmitter} = require('events'); | ||
|
||
jest.mock('execa'); | ||
const execa = require('execa'); | ||
|
||
// NOTE(fks): This is skipped due to refactoring happening in https://github.com/pikapkg/snowpack/pull/1203 | ||
// Once that PR is merged, this should be safe to unskip | ||
describe.skip('plugin-run-script', () => { | ||
const DEFAULT_OPTIONS = { | ||
cmd: 'CMD', | ||
watch: '$1 --additional-test-watch-options', | ||
}; | ||
let execaResult, execaFn; | ||
|
||
beforeEach(() => { | ||
execa.command.mockClear(); | ||
execaResult = {stderr: new EventEmitter(), stdout: new EventEmitter()}; | ||
execaFn = jest.fn(() => execaResult); | ||
execa.command = execaFn; | ||
}); | ||
|
||
test('returns the execa command promise', async () => { | ||
const p = plugin(null, DEFAULT_OPTIONS); | ||
const result = await p.run({isDev: false, log: jest.fn}); | ||
expect(result).toEqual(result); | ||
}); | ||
test('calls the given "cmd" command when isDev=false', async () => { | ||
const p = plugin(null, {cmd: 'CMD'}); | ||
await p.run({isDev: false, log: jest.fn}); | ||
expect(execaFn.mock.calls[0][0]).toMatch('CMD'); | ||
}); | ||
test('calls the given "watch" command when isDev=true', async () => { | ||
const p = plugin(null, {cmd: 'CMD', watch: '$1 --additional-test-watch-options'}); | ||
await p.run({isDev: true, log: jest.fn}); | ||
expect(execaFn.mock.calls[0][0]).toMatch('cmd --additional-test-watch-options'); | ||
}); | ||
test('handles command output in "stream" mode', async () => { | ||
const logFn = jest.fn(); | ||
const p = plugin(null, {...DEFAULT_OPTIONS, output: 'stream'}); | ||
await p.run({isDev: false, log: logFn}); | ||
execaResult.stdout.emit('data', Buffer.from('STDOUT_TEST_MESSAGE')); | ||
execaResult.stderr.emit('data', Buffer.from('STDERR_TEST_MESSAGE')); | ||
expect(logFn.mock.calls).toEqual([ | ||
['CONSOLE_INFO', {id: 'CMD', msg: 'STDOUT_TEST_MESSAGE'}], | ||
['CONSOLE_INFO', {id: 'CMD', msg: 'STDERR_TEST_MESSAGE'}], | ||
]); | ||
}); | ||
test('handles command output in "dashboard" mode', async () => { | ||
const logFn = jest.fn(); | ||
const p = plugin(null, {...DEFAULT_OPTIONS, output: 'dashboard'}); | ||
await p.run({isDev: false, log: logFn}); | ||
execaResult.stdout.emit('data', Buffer.from('STDOUT_TEST_MESSAGE')); | ||
execaResult.stderr.emit('data', Buffer.from('STDERR_TEST_MESSAGE')); | ||
expect(logFn.mock.calls).toEqual([ | ||
['WORKER_MSG', {level: 'log', msg: 'STDOUT_TEST_MESSAGE'}], | ||
['WORKER_MSG', {level: 'log', msg: 'STDERR_TEST_MESSAGE'}], | ||
]); | ||
}); | ||
test('handles clear character output in "dashboard" mode', async () => { | ||
const logFn = jest.fn(); | ||
const p = plugin(null, {...DEFAULT_OPTIONS, output: 'dashboard'}); | ||
await p.run({isDev: false, log: logFn}); | ||
execaResult.stderr.emit('data', Buffer.from('\u001bcTEST_CLEAR_MESSAGE')); | ||
execaResult.stderr.emit('data', Buffer.from('\x1BcTEST_CLEAR_MESSAGE')); | ||
expect(logFn.mock.calls).toEqual([ | ||
['WORKER_RESET', {}], | ||
['WORKER_MSG', {level: 'log', msg: 'TEST_CLEAR_MESSAGE'}], | ||
['WORKER_RESET', {}], | ||
['WORKER_MSG', {level: 'log', msg: 'TEST_CLEAR_MESSAGE'}], | ||
]); | ||
}); | ||
}); |
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,25 @@ | ||
# @snowpack/plugin-typescript | ||
|
||
This plugin adds TypeScript type checking to any Snowpack project. | ||
|
||
When developing or building your site with Snowpack, this plugin will run TypeScript's `tsc` CLI in your project and pipe the output through Snowpack. Works with all version of TypeScript, as long as TypeScript is installed separately in your project. | ||
|
||
|
||
|
||
## Usage | ||
|
||
```bash | ||
npm i @snowpack/plugin-typescript typescript | ||
``` | ||
|
||
Then add the plugin to your Snowpack config: | ||
|
||
```js | ||
// snowpack.config.js | ||
|
||
module.exports = { | ||
plugins: [ | ||
'@snowpack/plugin-typescript' | ||
], | ||
}; | ||
``` |
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,22 @@ | ||
{ | ||
"version": "1.0.0", | ||
"name": "@snowpack/plugin-typescript", | ||
"main": "plugin.js", | ||
"license": "MIT", | ||
"homepage": "https://github.com/pikapkg/snowpack/tree/master/plugins/plugin-typescript#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/pikapkg/snowpack.git", | ||
"directory": "plugins/plugin-typescript" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "*" | ||
}, | ||
"dependencies": { | ||
"execa": "^4.0.3", | ||
"npm-run-path": "^4.0.1" | ||
} | ||
} |
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,32 @@ | ||
const execa = require('execa'); | ||
const npmRunPath = require('npm-run-path'); | ||
const cwd = process.cwd(); | ||
|
||
function typescriptPlugin() { | ||
return { | ||
name: '@snowpack/plugin-typescript', | ||
async run({isDev, log}) { | ||
const workerPromise = execa.command(`tsc --noEmit ${isDev ? '--watch' : ''}`, { | ||
env: npmRunPath.env(), | ||
extendEnv: true, | ||
windowsHide: false, | ||
cwd, | ||
}); | ||
const {stdout, stderr} = workerPromise; | ||
function dataListener(chunk) { | ||
let stdOutput = chunk.toString(); | ||
// In --watch mode, handle the "clear" character | ||
if (stdOutput.includes('\u001bc') || stdOutput.includes('\x1Bc')) { | ||
log('WORKER_RESET', {}); | ||
stdOutput = stdOutput.replace(/\x1Bc/, '').replace(/\u001bc/, ''); | ||
} | ||
log('WORKER_MSG', {level: 'log', msg: stdOutput}); | ||
} | ||
stdout && stdout.on('data', dataListener); | ||
stderr && stderr.on('data', dataListener); | ||
return workerPromise; | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = typescriptPlugin; |
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,56 @@ | ||
const plugin = require('../plugin.js'); | ||
const {EventEmitter} = require('events'); | ||
|
||
jest.mock('execa'); | ||
const execa = require('execa'); | ||
|
||
describe('plugin-typescript', () => { | ||
let execaResult, execaFn; | ||
|
||
beforeEach(() => { | ||
execa.command.mockClear(); | ||
execaResult = {stderr: new EventEmitter(), stdout: new EventEmitter()}; | ||
execaFn = jest.fn(() => execaResult); | ||
execa.command = execaFn; | ||
}); | ||
|
||
test('returns the execa command promise', async () => { | ||
const p = plugin(); | ||
const result = await p.run({isDev: false, log: jest.fn}); | ||
expect(result).toEqual(result); | ||
}); | ||
test('calls "tsc" when isDev=false', async () => { | ||
const p = plugin(); | ||
await p.run({isDev: false, log: jest.fn}); | ||
expect(execaFn.mock.calls[0][0]).toMatch('tsc'); | ||
}); | ||
test('calls "tsc --watch" when isDev=true', async () => { | ||
const p = plugin(); | ||
await p.run({isDev: true, log: jest.fn}); | ||
expect(execaFn.mock.calls[0][0]).toMatch('tsc --watch'); | ||
}); | ||
test('handles tsc output', async () => { | ||
const logFn = jest.fn(); | ||
const p = plugin(); | ||
await p.run({isDev: false, log: logFn}); | ||
execaResult.stdout.emit('data', Buffer.from('STDOUT_TEST_MESSAGE')); | ||
execaResult.stderr.emit('data', Buffer.from('STDERR_TEST_MESSAGE')); | ||
expect(logFn.mock.calls).toEqual([ | ||
['WORKER_MSG', {level: 'log', msg: 'STDOUT_TEST_MESSAGE'}], | ||
['WORKER_MSG', {level: 'log', msg: 'STDERR_TEST_MESSAGE'}], | ||
]); | ||
}); | ||
test('handles tsc clear character messages', async () => { | ||
const logFn = jest.fn(); | ||
const p = plugin(); | ||
await p.run({isDev: false, log: logFn}); | ||
execaResult.stderr.emit('data', Buffer.from('\u001bcTEST_CLEAR_MESSAGE')); | ||
execaResult.stderr.emit('data', Buffer.from('\x1BcTEST_CLEAR_MESSAGE')); | ||
expect(logFn.mock.calls).toEqual([ | ||
['WORKER_RESET', {}], | ||
['WORKER_MSG', {level: 'log', msg: 'TEST_CLEAR_MESSAGE'}], | ||
['WORKER_RESET', {}], | ||
['WORKER_MSG', {level: 'log', msg: 'TEST_CLEAR_MESSAGE'}], | ||
]); | ||
}); | ||
}); |
d86e495
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: