Skip to content

Commit

Permalink
Merge pull request #30 from vite-plugin/v0.8.1
Browse files Browse the repository at this point in the history
v0.8.1
  • Loading branch information
yejimeiming committed Jul 12, 2023
2 parents ef781ec + bcdf176 commit 647fe26
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,10 @@
## 0.8.1 (2023-07-12)

- dfd8742 feat: generate sourcemap #28
- 4257aa6 chore: remove `build.test.ts`
- 23b7cdf fix: correct esbuild loader
- ef781ec Merge pull request #29 from vite-plugin/v0.8.0

## 0.8.0 (2023-06-24)

- bf2f306 fix: dynamic-require typo #28, closes [#28](https://github.com/vite-plugin/vite-plugin-commonjs/issues/28)
Expand Down
7 changes: 4 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-commonjs",
"version": "0.8.0",
"version": "0.8.1",
"description": "A pure JavaScript implementation of CommonJs",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -28,7 +28,8 @@
"dependencies": {
"acorn": "^8.8.2",
"fast-glob": "^3.2.12",
"vite-plugin-dynamic-import": "^1.4.0"
"magic-string": "^0.30.1",
"vite-plugin-dynamic-import": "^1.5.0"
},
"devDependencies": {
"@types/node": "^18.16.2",
Expand All @@ -47,4 +48,4 @@
"files": [
"dist"
]
}
}
24 changes: 17 additions & 7 deletions src/index.ts
@@ -1,14 +1,17 @@
import fs from 'node:fs'
import path from 'node:path'
import type { Plugin, ResolvedConfig } from 'vite'
import type { SourceMapInput } from 'rollup'
import type { Loader } from 'esbuild'
import { parse as parseAst } from 'acorn'
import MagicString from 'magic-string'
import {
DEFAULT_EXTENSIONS,
KNOWN_SFC_EXTENSIONS,
KNOWN_ASSET_TYPES,
KNOWN_CSS_TYPES,
} from 'vite-plugin-utils/constant'
import { MagicString, cleanUrl } from 'vite-plugin-utils/function'
import { cleanUrl } from 'vite-plugin-utils/function'
import { analyzer } from './analyze'
import { generateImport } from './generate-import'
import { generateExport } from './generate-export'
Expand Down Expand Up @@ -76,16 +79,19 @@ export default function commonjs(options: Options = {}): Plugin {
return
}

const contents = await transformCommonjs({
const result = await transformCommonjs({
options,
code,
id,
extensions,
dynaimcRequire,
})

if (contents != null) {
return { contents }
if (result != null) {
return {
contents: result.code,
loader: id.slice(id.lastIndexOf('.') + 1) as Loader,
}
}
})
},
Expand Down Expand Up @@ -115,7 +121,7 @@ async function transformCommonjs({
id: string,
extensions: string[],
dynaimcRequire: DynaimcRequire,
}) {
}): Promise<{ code: string; map: SourceMapInput; } | null | undefined> {
if (!(extensions.includes(path.extname(id)) || extensions.includes(path.extname(cleanUrl(id))))) return
if (!isCommonjs(code)) return

Expand Down Expand Up @@ -214,6 +220,10 @@ async function transformCommonjs({
}
}

const str = ms.toString()
return str !== code ? str : null
if (ms.hasChanged()) {
return {
code: ms.toString(),
map: ms.generateMap({ hires: true, source: id }),
}
}
}
Empty file removed test/build.test.ts
Empty file.

0 comments on commit 647fe26

Please sign in to comment.