Skip to content

Commit

Permalink
feat(cli): cli support webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Jun 19, 2021
1 parent f90864a commit 054a823
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 46 deletions.
4 changes: 1 addition & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ module.exports = {
es2021: true,
node: true
},
extends: [
'standard'
],
extends: 'standard',
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
Expand Down
17 changes: 9 additions & 8 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,31 @@ export function run (): void {
const version = require('../../package.json').version
program
.version(version, '-v, --version', 'output the version number')
.option('--rootDir <path>', 'the directory of project to be transfered')
.option('-d --rootDir <path>', 'the directory of project to be transfered')
.option('-t --type <type>', 'the type of the project, use vue-cli or webpack')
.parse(process.argv)

const keys = ['rootDir']
const keys = ['rootDir', 'type']
const config: Config = {}
keys.forEach(function (k) {
if (Object.prototype.hasOwnProperty.call(program.opts(), k)) {
config[k] = program.opts()[k]
}
})
start(config.rootDir)
start(config)
}

export function start (rootDir: string): void {
export function start (config : Config): void {
console.log('******************* Webpack to Vite *******************')
console.log(`Project path: ${rootDir}`)
console.log(`Project path: ${config.rootDir}`)

if (!fs.existsSync(rootDir)) {
console.error(`Project path is not correct : ${rootDir}`)
if (!fs.existsSync(config.rootDir)) {
console.error(`Project path is not correct : ${config.rootDir}`)
return
}

const cwd = process.cwd()
rootDir = path.resolve(rootDir)
const rootDir = path.resolve(config.rootDir)

// TODO:how to deal with the index.html in the project,
// notice that this will not choose the root directory in non-vite projects
Expand Down
1 change: 1 addition & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface Config {
rootDir?: string;
type?: string;
}

export interface DevServer {
Expand Down
2 changes: 1 addition & 1 deletion src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface Resolve {

export interface WebpackConfig extends Config {
mode?: string;
entry?: string;
entry?: string | [] | {} ;
output?: Output;
module?: Module;
resolve?: Resolve;
Expand Down
72 changes: 38 additions & 34 deletions src/transform/transformWebpack.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
import { parseWebpackConfig } from '../config/parse';
import { RawValue, ViteConfig } from '../config/vite';
import { TransformContext } from './context';
import { initViteConfig, Transformer } from './transformer';
import path from 'path';
import { DEFAULT_VUE_VERSION } from '../constants/constants';
import { parseWebpackConfig } from '../config/parse'
import { RawValue, ViteConfig } from '../config/vite'
import { TransformContext } from './context'
import { initViteConfig, Transformer } from './transformer'
import path from 'path'
import { DEFAULT_VUE_VERSION } from '../constants/constants'

// convert webpack.config.js => vite.config.js
export class WebpackTransformer implements Transformer {

context : TransformContext = {
vueVersion: DEFAULT_VUE_VERSION,
config: initViteConfig(),
importList: [],
vueVersion: DEFAULT_VUE_VERSION,
config: initViteConfig(),
importList: []
}

public async transform (rootDir: string): Promise<ViteConfig> {
const webpackConfig = await parseWebpackConfig(path.resolve(rootDir, 'webpack.config.js'))
const config = this.context.config

// convert base config
// TODO: convert entry
// webpack may have multiple entry files, e.g.
// 1. one entry, with one entry file : e.g. entry: './app/index.js'
// 2. one entry, with multiple entry files: e.g. entry: ['./pc/index.js','./wap/index.js']
// 3. multiple entries e.g. entry: {
// wap: './pc/index.js',
// pc: './wap/index.js'
// }
config.mode = webpackConfig.mode

const defaultAlias = []

public async transform(rootDir: string): Promise<ViteConfig> {
const webpackConfig = await parseWebpackConfig(path.resolve(rootDir, 'webpack.config.js'))
const config = this.context.config;

// convert base config
config.root = webpackConfig.entry
config.mode = webpackConfig.mode
const alias = {
'@': `${rootDir}/src`
}
Object.keys(alias).forEach((key) => {
const relativePath = path.relative(rootDir, alias[key]).replace(/\\/g, '/')
defaultAlias.push({
find: key,
replacement: new RawValue(`path.resolve(__dirname,'${relativePath}')`)
})
})

const defaultAlias = [];

const alias = {
'@':`${rootDir}/src`,
}
Object.keys(alias).forEach((key) => {
const relativePath = path.relative(rootDir,alias[key]).replace(/\\/g,'/');
defaultAlias.push({
find: key,
replacement: new RawValue(`path.resolve(__dirname,'${relativePath}')`),
});
});
config.resolve = {}
config.resolve.alias = defaultAlias

config.resolve = {};
config.resolve.alias = defaultAlias;

return null
return null
}

}

0 comments on commit 054a823

Please sign in to comment.