Skip to content

Commit

Permalink
feat(transformer): implements webpack transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Jun 17, 2021
1 parent 4b0ee62 commit 2050e12
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 34 deletions.
40 changes: 8 additions & 32 deletions src/transform/transformVuecli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parseVueCliConfig } from '../config/parse';
import Config from 'webpack-chain';
import merge from 'webpack-merge';
import { Transformer } from './transformer';
import { initViteConfig, Transformer, useJsx } from './transformer';
import { ViteConfig, RawValue } from '../config/vite';
import path from 'path';
import { TransformContext } from './context';
Expand All @@ -15,8 +15,8 @@ export class VueCliTransformer implements Transformer {

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

Expand All @@ -30,7 +30,7 @@ export class VueCliTransformer implements Transformer {

const css = vueConfig.css || {};

//Base public path
// base public path
config.base =
process.env.PUBLIC_URL || vueConfig.publicPath || vueConfig.baseUrl;

Expand All @@ -57,7 +57,7 @@ export class VueCliTransformer implements Transformer {
config.server.proxy = devServer.proxy;
}

//build options
// build options
config.build = config.build || {};
config.build.outDir = vueConfig.outputDir;
const cssCodeSplit = Boolean(css.extract);
Expand Down Expand Up @@ -89,8 +89,7 @@ export class VueCliTransformer implements Transformer {
return (originConfig.resolve && originConfig.resolve.alias) || {};
}
})();
const finalAlias = [];
finalAlias.push({ find: new RawValue('/^~/'), replacement: ''});
const defaultAlias = [];
const alias = {
'@':`${rootDir}/src`,
...aliasOfConfigureWebpackObjectMode,
Expand All @@ -99,39 +98,16 @@ export class VueCliTransformer implements Transformer {
}
Object.keys(alias).forEach((key) => {
const relativePath = path.relative(rootDir,alias[key]).replace(/\\/g,'/');
finalAlias.push({
defaultAlias.push({
find: key,
replacement: new RawValue(`path.resolve(__dirname,'${relativePath}')`),
});
});

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


config.resolve.extensions = [
'.mjs',
'.js',
'.ts',
'.jsx',
'.tsx',
'.json',
'.vue',
];
config.resolve.alias = defaultAlias;
return config;
}

public useJsx() : boolean {
try {
const jsx = require('babel-plugin-transform-vue-jsx');
if (jsx) {
return true;
}
} catch (error) {} //eslint-disable-line no-empty

return false;
}

public transformVue(context: TransformContext) : void {
const plugins: RawValue[] = [];
if (context.vueVersion === 2) {
Expand Down
43 changes: 42 additions & 1 deletion src/transform/transformWebpack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
import { parseWebpackConfig } from '../config/parse';
import { RawValue, ViteConfig } from '../config/vite';
import { TransformContext } from './context';
import { initViteConfig, Transformer, useJsx } from './transformer';
import path from 'path';
import { DEFAULT_VUE_VERSION } from '../constants/constants';

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

context : TransformContext = {
vueVersion: DEFAULT_VUE_VERSION,
jsx: useJsx(),
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
config.root = webpackConfig.entry
config.mode = webpackConfig.mode

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;

return null
}

}
36 changes: 35 additions & 1 deletion src/transform/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransformContext } from './context';
import { ViteConfig } from '../config/vite';
import { RawValue, ViteConfig } from '../config/vite';
/**
* general implementation for vue.config.js and webpack.config.js
*
Expand All @@ -10,3 +10,37 @@ export interface Transformer{
transform(rootDir: string): Promise<ViteConfig>;

}

export function initViteConfig() : ViteConfig {
const config : ViteConfig = {}

const defaultAlias = []
defaultAlias.push({ find: new RawValue('/^~/'), replacement: ''});
defaultAlias.push({ find: '', replacement: new RawValue('path.resolve(__dirname,\'src\')')});

config.resolve = {};
config.resolve.alias = defaultAlias;
config.resolve.extensions = [
'.mjs',
'.js',
'.ts',
'.jsx',
'.tsx',
'.json',
'.vue',
];

return config;

}

export function useJsx() : boolean {
try {
const jsx = require('babel-plugin-transform-vue-jsx');
if (jsx) {
return true;
}
} catch (error) {} //eslint-disable-line no-empty

return false;
}

0 comments on commit 2050e12

Please sign in to comment.