-
Notifications
You must be signed in to change notification settings - Fork 20
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
Add rollup & multitarget support #11
base: master
Are you sure you want to change the base?
Changes from all commits
955eda8
bc353cc
cd8cd7a
14ef3b7
e972822
d12183c
23946d7
1959488
e8b5438
772df6b
9dfd66a
814429e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dist/ | ||
tests/fixture/ | ||
demo-dist/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#!/usr/bin/env node | ||
require('../dist/lib/cli.js') | ||
require('../dist/cli.js') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import json from '@rollup/plugin-json' | ||
import replace from '@rollup/plugin-replace' | ||
import typescript from 'rollup-plugin-typescript2' | ||
|
||
const pluginTypeScript = typescript({ | ||
useTsconfigDeclarationDir: true, | ||
tsconfig: 'tsconfig.compile.json' | ||
}) | ||
const pluginJson = json() | ||
const pluginCommonJs = commonjs() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
export default [ | ||
{ | ||
input: 'src/index.ts', | ||
output: { | ||
file: 'dist/index.js', | ||
format: 'cjs' | ||
}, | ||
plugins: [pluginJson, pluginCommonJs, pluginTypeScript] | ||
}, | ||
{ | ||
input: 'src/index.ts', | ||
output: { | ||
file: 'dist/index.mjs', | ||
format: 'es' | ||
}, | ||
plugins: [pluginJson, pluginTypeScript] | ||
}, | ||
{ | ||
input: 'src/index.ts', | ||
output: { | ||
file: 'dist/index.browser.mjs', | ||
format: 'es' | ||
}, | ||
plugins: [replace({ 'process.env.BROWSER': 'true' }), pluginJson, pluginTypeScript] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can replace |
||
}, | ||
{ | ||
input: 'src/cli.ts', | ||
output: { | ||
file: 'dist/cli.js', | ||
format: 'cjs' | ||
}, | ||
plugins: [pluginJson, pluginCommonJs, pluginTypeScript] | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import program from 'commander' | ||
import { convertFile } from './index.js' | ||
import inquirer from 'inquirer' | ||
import { version } from '../package.json' | ||
import { writeFileInfo } from './file' | ||
|
||
const { convertFile } = require('./index.js') | ||
|
||
function camelize (str: string) { | ||
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '') | ||
} | ||
|
@@ -21,7 +23,7 @@ function getCmdOptions (cmd: any) { | |
} | ||
|
||
program | ||
.version(require('../../package.json').version) | ||
.version(version) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This version is wrong on release. |
||
.usage('<command> [options]') | ||
|
||
program | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,78 @@ | ||
import path from 'path' | ||
import fs from 'fs' | ||
import { isVueFile, parseVueFile } from './utils' | ||
import { Vc2cOptions } from './options' | ||
import { log } from './debug' | ||
import { Vc2cOptions } from './options' | ||
import { isVueFile, parseVueFile } from './utils' | ||
import { isAbsolute, resolve } from 'path' | ||
import { existsSync, readFileSync, writeFileSync } from 'fs' | ||
|
||
export enum FileKind { | ||
VUE, | ||
TS | ||
} | ||
|
||
type FileInfo = { | ||
fsPath: string, | ||
kind: FileKind, | ||
content: string | ||
} | { | ||
fsPath: string, | ||
kind: FileKind, | ||
fileContent: string, | ||
start: number, | ||
end: number, | ||
export type VueFileInfo = FileInfo | ||
|
||
export interface VueSFCFileInfo extends FileInfo { | ||
fileContent: string | ||
start: number | ||
end: number | ||
} | ||
|
||
export interface FileInfo { | ||
fsPath: string | ||
kind: FileKind | ||
content: string | ||
} | ||
|
||
export function readVueSFCOrTsFile (filePath: string, options: Vc2cOptions): FileInfo { | ||
const fileFsPath = (path.isAbsolute(filePath)) ? filePath : path.resolve(options.root, filePath) | ||
const fileContent = fs.readFileSync(fileFsPath, { encoding: 'utf8' }) | ||
if (process.env.BROWSER) { | ||
throw new Error('unsupported') | ||
} | ||
const fileFsPath: string = (isAbsolute(filePath)) ? filePath : resolve(options.root, filePath) | ||
const fileContent = readFileSync(fileFsPath, { encoding: 'utf8' }) | ||
if (isVueFile(fileFsPath)) { | ||
const scriptContent = parseVueFile(options.vueTemplateCompiler, fileContent).script | ||
if (scriptContent) { | ||
log(`Readed Vue file: ${fileFsPath}`) | ||
if (scriptContent?.content != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does we need |
||
log(`Read Vue file: ${fileFsPath}`) | ||
return { | ||
fsPath: fileFsPath, | ||
kind: FileKind.VUE, | ||
start: scriptContent.start, | ||
end: scriptContent.end, | ||
content: scriptContent.content, | ||
start: scriptContent?.start ?? 0, | ||
end: scriptContent?.end ?? 0, | ||
content: scriptContent?.content ?? '', | ||
fileContent | ||
} | ||
} as VueSFCFileInfo | ||
} | ||
throw new Error('The Vue SFC don\'t have sciprt element.') | ||
throw new Error('The Vue SFC don\'t have script element.') | ||
} else { | ||
log(`Readed TS file: ${fileFsPath}`) | ||
log(`Read TS file: ${fileFsPath}`) | ||
return { | ||
fsPath: fileFsPath, | ||
kind: FileKind.TS, | ||
content: fileContent | ||
} | ||
} as VueFileInfo | ||
} | ||
} | ||
|
||
export function writeFileInfo (fileInfo: FileInfo, content: string) { | ||
export function writeFileInfo (fileInfo: VueFileInfo | VueSFCFileInfo, content: string) { | ||
if (process.env.BROWSER) { | ||
throw new Error('unsupported') | ||
} | ||
|
||
const { fsPath } = fileInfo | ||
if ('start' in fileInfo) { | ||
log(`Write Vue file: ${fileInfo.fsPath}`) | ||
const fileContent = `${fileInfo.fileContent.slice(0, fileInfo.start)}\n${content}${fileInfo.fileContent.slice(fileInfo.end)}` | ||
fs.writeFileSync(fileInfo.fsPath, fileContent, { encoding: 'utf8' }) | ||
const { fileContent, start, end } = fileInfo as unknown as VueSFCFileInfo | ||
log(`Write Vue file: ${fsPath}`) | ||
const newFileContent = `${fileContent.slice(0, start)}\n${content}${fileContent.slice(end)}` | ||
writeFileSync(fsPath, newFileContent, { encoding: 'utf8' }) | ||
} else { | ||
log(`Write TS file: ${fileInfo.fsPath}`) | ||
fs.writeFileSync(fileInfo.fsPath, content, { encoding: 'utf8' }) | ||
log(`Write TS file: ${fsPath}`) | ||
writeFileSync(fsPath, content, { encoding: 'utf8' }) | ||
} | ||
} | ||
|
||
export function existsFileSync (path: string) { | ||
return fs.existsSync(path) | ||
export function existsFileSync (path: string): boolean { | ||
if (process.env.BROWSER) { | ||
throw new Error('unsupported') | ||
} | ||
return existsSync(path) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,21 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import { Vc2cOptions } from './options' | ||
import path from 'path' | ||
import { log } from './debug' | ||
import prettier from 'prettier/standalone' | ||
import * as prettier from 'prettier/standalone' | ||
import prettierTypescriptParser from 'prettier/parser-typescript' | ||
import { existsFileSync } from './file' | ||
|
||
export function format (content: string, options: Vc2cOptions) { | ||
export function format (content: string, options: Vc2cOptions): string { | ||
const isNode = typeof window === 'undefined' | ||
if (!isNode) { | ||
if (process.env.BROWSER || !isNode) { | ||
Comment on lines
9
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need |
||
return prettier.format(content, { | ||
plugins: [prettierTypescriptParser], | ||
parser: 'typescript', | ||
semi: false, | ||
singleQuote: true | ||
}) | ||
} | ||
|
||
const eslintConfigPath = path.resolve(options.root, options.eslintConfigFile) | ||
const prettierFormat = require('prettier-eslint') | ||
const prettierEslintOpions = (existsFileSync(eslintConfigPath)) | ||
? { | ||
text: content, | ||
|
@@ -64,5 +61,6 @@ export function format (content: string, options: Vc2cOptions) { | |
} | ||
|
||
log('Format result code.....') | ||
return prettierFormat(prettierEslintOpions) | ||
const prettierEslint = require('prettier-eslint') | ||
return prettierEslint(prettierEslintOpions) | ||
} |
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.
We need to add
replace({ 'process.env.BROWSER': 'false' })
for outputs without browser.