Skip to content

Commit

Permalink
Merge pull request #25 from vite-plugin/v0.7.1
Browse files Browse the repository at this point in the history
V0.7.1
  • Loading branch information
caoxiemeihao committed May 14, 2023
2 parents 2e1aa35 + 301ae43 commit b4e7476
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
## 0.7.1 (2023-05-14)

- ddfbfeb fix: bump vite-plugin-dynamic-import to 1.4.0 for `pnpm`
- 356c22e chore: cleanup
- 4e4f807 docs: update

## 0.7.0 (2023-04-30)

- e9eb5f0 refactor(test): integrate vitest 🌱
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -60,7 +60,7 @@ export interface Options {
commonjs({
filter(id) {
// `node_modules` is exclude by default, so we need to include it explicitly
// https://github.com/vite-plugin/vite-plugin-commonjs/blob/v0.7.0/src/index.ts#L123-L125
// https://github.com/vite-plugin/vite-plugin-commonjs/blob/v0.7.0/src/index.ts#L125-L127
if (id.includes('node_modules/xxx')) {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Expand Up @@ -60,7 +60,7 @@ export interface Options {
commonjs({
filter(id) {
// 默认会排除 `node_modules`,所以必须显式的包含它explicitly
// https://github.com/vite-plugin/vite-plugin-commonjs/blob/v0.7.0/src/index.ts#L123-L125
// https://github.com/vite-plugin/vite-plugin-commonjs/blob/v0.7.0/src/index.ts#L125-L127
if (id.includes('node_modules/xxx')) {
return true
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-commonjs",
"version": "0.7.0",
"version": "0.7.1",
"description": "A pure JavaScript implementation of CommonJs",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand All @@ -23,12 +23,12 @@
"build": "vite build",
"test": "vitest run",
"types": "tsc",
"prepublishOnly": "npm run test && npm run build"
"prepublishOnly": "npm run build && npm run test"
},
"dependencies": {
"acorn": "^8.8.2",
"fast-glob": "^3.2.12",
"vite-plugin-dynamic-import": "^1.3.2"
"vite-plugin-dynamic-import": "^1.4.0"
},
"devDependencies": {
"@types/node": "^18.16.2",
Expand All @@ -47,4 +47,4 @@
"files": [
"dist"
]
}
}
3 changes: 2 additions & 1 deletion src/analyze.ts
Expand Up @@ -74,7 +74,8 @@ export function analyzer(ast: AcornNode, code: string, id: string): Analyzed {
},
AssignmentExpression(node) {
if (node.left.type !== 'MemberExpression') return
if (!(node.left.object.type === 'Identifier' && ['module', 'exports'].includes(node.left.object.name))) return
// only `module.exports`, `exports.xxx`
if (!['module', 'exports'].includes(node.left.object.name)) return

analyzed.exports.push({
node,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -67,7 +67,7 @@ export default function commonjs(options: Options = {}): Plugin {
_config.optimizeDeps.esbuildOptions ??= {}
_config.optimizeDeps.esbuildOptions.plugins ??= []
_config.optimizeDeps.esbuildOptions.plugins.push({
name: 'vite-plugin-dynamic-import:pre-bundle',
name: 'vite-plugin-commonjs:pre-bundle',
setup(build) {
build.onLoad({ filter: /.*/ }, async ({ path: id }) => {
let code: string
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/vite.config.ts
Expand Up @@ -11,7 +11,7 @@ export default defineConfig({
name: 'vite-plugin-commonjs-test',
transform(code, id) {
if (/\/src\//.test(id)) {
// Write transformed code to output/
// write transformed code to dist/
const filename = id.replace('src', 'dist')
const dirname = path.dirname(filename)
if (!fs.existsSync(dirname)) fs.mkdirSync(dirname)
Expand Down
8 changes: 5 additions & 3 deletions test/serve.test.ts
Expand Up @@ -16,19 +16,21 @@ import fastGlob from 'fast-glob'

const root = path.join(__dirname, 'fixtures')
let server: ViteDevServer | null = null
const PORT = 4000
let port = 4000

beforeAll(async () => {
fs.rmSync(path.join(root, 'dist'), { recursive: true, force: true })
server = await createServer({ configFile: path.join(root, 'vite.config.ts') })
await server.listen(PORT)
await server.listen(port)
// @ts-ignore
port = server.httpServer?.address().port
})

describe('vite serve', async () => {
it('__snapshots__', async () => {
const files = fastGlob.sync('__snapshots__/**/*', { cwd: root })
for (const file of files) {
const response = await (await fetch(`http://localhost:${PORT}/${file.replace('__snapshots__', 'src')}`)).text()
const response = await (await fetch(`http://localhost:${port}/${file.replace('__snapshots__', 'src')}`)).text()
const distFile = fs.readFileSync(path.join(root, file.replace('__snapshots__', 'dist')), 'utf8')
const snapFile = fs.readFileSync(path.join(root, file), 'utf8')

Expand Down
1 change: 1 addition & 0 deletions vite.config.ts
Expand Up @@ -11,6 +11,7 @@ export default defineConfig({
build: {
minify: false,
emptyOutDir: !isdev,
target: 'node14',
lib: {
entry: 'src/index.ts',
formats: ['cjs', 'es'],
Expand Down

0 comments on commit b4e7476

Please sign in to comment.