Skip to content

Commit

Permalink
feat: conversion of EntryFileName and ChunkFileName (#86)
Browse files Browse the repository at this point in the history
* feat: conversion of EntryFileName and ChunkFileName

* docs: update rule description

* fix: type Output

* test: add use case

* chore: clear importer
  • Loading branch information
konpeki622 committed Jan 26, 2022
1 parent 76d9469 commit d23a0ed
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ Options:
* 如果 `entry` 类型是 `string``entry` -> `build.rollupOptions.input`
* 如果 `entry` 类型是 `object` ,则将 object 中的每条属性配置到 `build.rollupOptions.input`
* 如果 `entry` 类型是 `function` ,则将 function 的运行结果配置到 `build.rollupOptions.input`
* ✅ W02: outDir 配置
* ✅ W02: output 配置
* `output.path` -> `build.outDir`
* `output.filename` -> `build.rollupOptions.output.entryFileNames`
* `output.chunkFilename` -> `build.rollupOptions.output.chunkFileNames`
* ✅ W03: `resolve.alias` 配置
* 添加默认 alias 配置

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ Legend of annotations:
* if `entry` is `string` type: `entry` -> `build.rollupOptions.input`
* if `entry` is `object` type: the properties of `entry` will be converted set to `build.rollupOptions.input`
* if `entry` is `function` type: execute result of `entry` will be set to `build.rollupOptions.input`
* ✅ W02: `outDir` options
* ✅ W02: `output` options
* `output.path` -> `build.outDir`
* `output.filename` -> `build.rollupOptions.output.entryFileNames`
* `output.chunkFilename` -> `build.rollupOptions.output.chunkFileNames`
* ✅ W03: `resolve.alias` options
* add alias options by default
```javascript
Expand Down
3 changes: 2 additions & 1 deletion src/config/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Config, DevServer } from './config'
import type { Config, DevServer } from './config'

export interface Output {
path?: string;
filename?: string;
publicPath?: string;
chunkFilename?: string
}

export interface Rule {
Expand Down
35 changes: 28 additions & 7 deletions src/transform/transformWebpack.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import path from 'path'
import { parseWebpackConfig } from '../config/parse'
import { RawValue, ViteConfig } from '../config/vite'
import { TransformContext } from './context'
import { initViteConfig, Transformer, transformImporters } from './transformer'
import type { ViteConfig } from '../config/vite';
import { RawValue } from '../config/vite'
import type { TransformContext } from './context'
import type { Transformer } from './transformer';
import { initViteConfig, transformImporters } from './transformer'
import { DEFAULT_VUE_VERSION } from '../constants/constants'
import { Entry } from '../config/webpack'
import type { Entry } from '../config/webpack'
import { isObject } from '../utils/common'
import { recordConver } from '../utils/report'
import { AstParsingResult } from '../ast-parse/astParse'
import type { AstParsingResult } from '../ast-parse/astParse'
import { getVueVersion } from '../utils/version'
import { ServerOptions } from 'vite'
import type { ServerOptions } from 'vite'
import { relativePathFormat } from '../utils/file'
import type { OutputOptions } from 'rollup'

// convert webpack.config.js => vite.config.js
export class WebpackTransformer implements Transformer {
Expand Down Expand Up @@ -72,7 +75,25 @@ export class WebpackTransformer implements Transformer {
const relativePath = relativePathFormat(rootDir, webpackConfig.output.path)
config.build.outDir = new RawValue(`path.resolve(__dirname, '${relativePath}')`)
}
recordConver({ num: 'W02', feat: 'outDir options' })
if (webpackConfig.output?.filename) {
const output: OutputOptions = {
entryFileNames: webpackConfig.output.filename
}
if (!config.build.rollupOptions.output) {
config.build.rollupOptions.output = {}
}
Object.assign(config.build.rollupOptions.output, output)
}
if (webpackConfig.output?.chunkFilename) {
const output: OutputOptions = {
chunkFileNames: webpackConfig.output.chunkFilename
}
if (!config.build.rollupOptions.output) {
config.build.rollupOptions.output = {}
}
Object.assign(config.build.rollupOptions.output, output)
}
recordConver({ num: 'W02', feat: 'output options' })
// convert alias
const defaultAlias = []
const alias = {
Expand Down
17 changes: 17 additions & 0 deletions tests/transformWebpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,21 @@ describe('WebpackTransformer', () => {
}
})
})

test('transform webpack.config.js', async () => {
const srcPath: string = path.resolve('tests/testdata/transform-webpack/webpack.config.js')
const destPath: string = path.resolve('tests/out-transform-webpack/webpack.config.js')
fs.copyFileSync(srcPath, destPath)

const transformer: WebpackTransformer = new WebpackTransformer()
const viteConfig: ViteConfig = await transformer.transform(path.resolve('tests/out-transform-webpack'))
expect(viteConfig.build.rollupOptions.input).toEqual('./main.js')
expect(viteConfig.build.rollupOptions.output.entryFileNames).toEqual('bundle.js')
expect(viteConfig.resolve.alias).toMatchObject([
{
find: '@',
replacement: new RawValue(`path.resolve(__dirname,'src')`)
}
])
})
})

0 comments on commit d23a0ed

Please sign in to comment.