Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: typescript support #60

Merged
merged 2 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@
"@dword-design/functions": "^4.0.0",
"ast-to-literal": "^0.0.5",
"fs-extra": "^10.0.0",
"ts-ast-to-literal": "^1.0.0",
"vue-template-compiler": "^2.6.11"
},
"devDependencies": {
"@dword-design/base": "^8.0.0",
"@nuxt/typescript-build": "^2.1.0",
"depcheck-package-name": "^2.0.0",
"nuxt": "^2.15.3",
"output-files": "^2.0.0",
"with-local-tmp-dir": "^4.0.0"
},
"peerDependencies": {
"typescript": "~4.2"
},
"engines": {
"node": ">=12"
},
Expand Down
55 changes: 36 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,49 @@ const predefinedProperties = {
export default function () {
const extractMeta = filename => {
const fileContent = readFileSync(filename, 'utf8')
let data = {}

const scriptContent =
const Component =
P.extname(filename) === '.vue'
? (() => {
const vueTemplateCompiler = require('vue-template-compiler')

const Component = vueTemplateCompiler.parseComponent(fileContent)

return Component.script?.content
return vueTemplateCompiler.parseComponent(fileContent)
})()
: fileContent
let data = {}
: { script: { content: fileContent, lang: 'js' } }

const scriptContent = Component.script?.content
if (scriptContent) {
const ast = babel.parseSync(scriptContent, {
filename,
...(this.options.build.babel |> pick(['configFile', 'babelrc'])),
...(!this.options.build.babel.configFile &&
!this.options.build.babel.babelrc && {
extends: '@nuxt/babel-preset-app',
}),
})
traverse(ast, {
ExportDefaultDeclaration: path => {
data = path.node.declaration |> astToLiteral
},
})
if (Component.script.lang === 'ts') {
const ts = require('typescript')

const tsAstToLiteral = require('ts-ast-to-literal')

const rootNode = ts.createSourceFile(
'x.ts',
scriptContent,
ts.ScriptTarget.Latest
)
ts.forEachChild(rootNode, node => {
if (node.kind === ts.SyntaxKind.ExportAssignment) {
data = node.expression |> tsAstToLiteral
}
})
} else {
const ast = babel.parseSync(scriptContent, {
filename,
...(this.options.build.babel |> pick(['configFile', 'babelrc'])),
...(!this.options.build.babel.configFile &&
!this.options.build.babel.babelrc && {
extends: '@nuxt/babel-preset-app',
}),
})
traverse(ast, {
ExportDefaultDeclaration: path => {
data = path.node.declaration |> astToLiteral
},
})
}
}

return {
Expand Down
48 changes: 48 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,52 @@ export default {
`,
},
},
typescript: {
config: {
buildModules: [packageName`@nuxt/typescript-build`],
modules: ['~/../src', '~/modules/module'],
},
files: {
'modules/module.js': endent`
export default function () {
this.extendRoutes(routes =>
expect(routes[0].meta.foo).toEqual(true)
)
}
`,
'pages/index.vue': endent`
<template>
<div />
</template>

<script lang="ts">
const foo: number = 1
export default {
foo: true,
}
</script>

`,
'tsconfig.json': JSON.stringify({
compilerOptions: {
allowJs: true,
baseUrl: '.',
esModuleInterop: true,
lib: ['ESNext', 'ESNext.AsyncIterable', 'DOM'],
module: 'ESNext',
moduleResolution: 'Node',
noEmit: true,
paths: {
'@/*': ['./*'],
'~/*': ['./*'],
},
sourceMap: true,
strict: true,
target: 'ES2018',
types: ['@types/node', '@nuxt/types'],
},
exclude: ['node_modules'],
}),
},
},
} |> mapValues(runTest)
Loading