-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
865 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
coverage |
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,22 @@ | ||
const fs = require('fs'); | ||
const scriptCompilers = require('./src/script-compilers'); | ||
|
||
module.exports = function plugin(snowpackConfig, pluginOptions) { | ||
const curPluginOptions = pluginOptions || {}; | ||
const {sourceMaps} = snowpackConfig.buildOptions; | ||
const tsconfigFilePath = curPluginOptions.tsconfig; | ||
|
||
return { | ||
name: '@snowpack/plugin-vue-tsx-jsx', | ||
resolve: { | ||
input: ['.tsx', '.jsx'], | ||
output: ['.js'], | ||
}, | ||
async load({filePath, fileExt}) { | ||
const content = fs.readFileSync(filePath, 'utf-8'); | ||
const lang = fileExt.slice(fileExt.lastIndexOf('.') + 1); | ||
const result = scriptCompilers.esbuildCompile(content, lang, tsconfigFilePath); | ||
return result; | ||
}, | ||
}; | ||
}; |
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,39 @@ | ||
const esbuild = require('esbuild'); | ||
|
||
const codeSnippetH = `import { Fragment } from '/web_modules/vue.js';`; | ||
|
||
// https://github.com/vitejs/vite/blob/master/src/client/vueJsxCompat.ts | ||
const codeSnippetVueJsxCompat = | ||
`import {createVNode, isVNode} from '/web_modules/vue.js'; | ||
const slice = Array.prototype.slice; | ||
export function jsx(tag, props = null, children = null) { | ||
if (arguments.length > 3 || isVNode(children)) { | ||
children = slice.call(arguments, 2); | ||
} | ||
return createVNode(tag, props, children); | ||
}`; | ||
|
||
/** | ||
* @param {string} content | ||
* @param {'jsx' | 'ts' | 'tsx'} lang | ||
* @param {string} [tsconfig] | ||
*/ | ||
const esbuildCompile = (content, lang, tsconfig) => { | ||
let result = ''; | ||
if (['jsx', 'tsx'].includes(lang)) { | ||
result += `${codeSnippetH}\n`; | ||
result += `${codeSnippetVueJsxCompat}\n`; | ||
} | ||
const {js} = esbuild.transformSync(content, { | ||
loader: lang, | ||
tsconfig, | ||
jsxFactory: tsconfig ? undefined : 'jsx', | ||
jsxFragment: tsconfig ? undefined : 'Fragment', | ||
}); | ||
result += `\n${js.trim()}\n`; | ||
return result.trim(); | ||
}; | ||
|
||
module.exports = { | ||
esbuildCompile, | ||
}; |
24 changes: 24 additions & 0 deletions
24
plugins/plugin-vue/test/__snapshots__/plugin-tsx-jsx.test.js.snap
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,24 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`plugin with jsx 1`] = ` | ||
"import { h, Fragment } from '/web_modules/vue.js'; | ||
import {defineComponent} from \\"vue\\"; | ||
export default defineComponent({ | ||
name: \\"JsxContent\\", | ||
setup() { | ||
return () => /* @__PURE__ */ h(Fragment, null, /* @__PURE__ */ h(\\"h1\\", null, \\"JsxContent\\")); | ||
} | ||
});" | ||
`; | ||
|
||
exports[`plugin with tsx 1`] = ` | ||
"import { h, Fragment } from '/web_modules/vue.js'; | ||
import {defineComponent} from \\"vue\\"; | ||
export default defineComponent({ | ||
name: \\"TsxContent\\", | ||
setup() { | ||
const name = \\"TsxContent\\"; | ||
return () => /* @__PURE__ */ h(Fragment, null, /* @__PURE__ */ h(\\"h1\\", null, \\"TsxContent\\")); | ||
} | ||
});" | ||
`; |
69 changes: 69 additions & 0 deletions
69
plugins/plugin-vue/test/__snapshots__/plugin-vue-ts-tsx-jsx.test.js.snap
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,69 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`plugin vue with jsx 1`] = ` | ||
Object { | ||
".css": Object { | ||
"code": "", | ||
"map": "", | ||
}, | ||
".js": Object { | ||
"code": "import { h, Fragment } from '/web_modules/vue.js'; | ||
import {defineComponent} from \\"vue\\"; | ||
export default defineComponent({ | ||
name: \\"VueContentJsx\\", | ||
setup() { | ||
return () => /* @__PURE__ */ h(Fragment, null, /* @__PURE__ */ h(\\"h1\\", null, \\"VueContentJsx\\")); | ||
} | ||
});", | ||
"map": "", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`plugin vue with ts 1`] = ` | ||
Object { | ||
".css": Object { | ||
"code": "", | ||
"map": "", | ||
}, | ||
".js": Object { | ||
"code": "import {defineComponent} from \\"vue\\"; | ||
const defaultExport = defineComponent({ | ||
name: \\"VueContentTs\\", | ||
setup() { | ||
const name = \\"VueContentTs\\"; | ||
} | ||
}); | ||
import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"/web_modules/vue.js\\" | ||
export function render(_ctx, _cache) { | ||
return (_openBlock(), _createBlock(\\"h1\\", null, \\"Vue Content Ts\\")) | ||
} | ||
defaultExport.render = render | ||
export default defaultExport", | ||
"map": "", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`plugin vue with tsx 1`] = ` | ||
Object { | ||
".css": Object { | ||
"code": "", | ||
"map": "", | ||
}, | ||
".js": Object { | ||
"code": "import { h, Fragment } from '/web_modules/vue.js'; | ||
import {defineComponent} from \\"vue\\"; | ||
export default defineComponent({ | ||
name: \\"VueContentTsx\\", | ||
setup() { | ||
const name = \\"VueContentTsx\\"; | ||
return () => /* @__PURE__ */ h(Fragment, null, /* @__PURE__ */ h(\\"h1\\", null, \\"VueContentTsx\\")); | ||
} | ||
});", | ||
"map": "", | ||
}, | ||
} | ||
`; |
Oops, something went wrong.