Skip to content

Commit

Permalink
build: compatible with ESM import
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Apr 5, 2022
1 parent 6bdd3bf commit 922ac6d
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test:coverage": "jest --clearCache && jest --runInBand --coverage=true",
"changelog": "pnpm -r --filter ./packages run changelog",
"test:watch": "jest -w",
"type-check": "pnpm -r --parallel --filter ./packages/vite-plugin-checker exec -- tsc --noEmit -p tsconfig.build.json",
"type-check": "pnpm -r --parallel --filter ./packages/vite-plugin-checker exec -- tsc --noEmit",
"ci:publish": "zx scripts/publish.mjs"
},
"husky": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import strip from 'strip-ansi'
import invariant from 'tiny-invariant'
import { createServer, ViteDevServer, CustomPayload } from 'vite'
import { Checker } from 'vite-plugin-checker'
import { Checker } from 'vite-plugin-checker/lib/Checker'

import { expectStdoutNotContains, expectStderrContains, sleep, testDir } from '../testUtils'

Expand Down
14 changes: 7 additions & 7 deletions packages/vite-plugin-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
"!lib/checkers/vueTsc/typescript-vue-tsc"
],
"scripts": {
"watch:node": "tsc -p tsconfig.build.json --watch",
"dev": "run-p watch:node",
"dev": "tsup --watch",
"clean": "rimraf lib",
"build": "tsc -p tsconfig.build.json",
"build:test": "tsc -p tsconfig.test.json",
"build": "tsup",
"build:test": "tsup --sourcemap inline",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package vite-plugin-checker",
"release": "zx ../../scripts/release.mjs"
},
Expand Down Expand Up @@ -57,12 +56,13 @@
"@types/eslint": "^7.2.14",
"@types/lodash.debounce": "^4.0.6",
"@types/lodash.pick": "^4.4.6",
"@volar/vue-typescript": "^0.33.0",
"esbuild": "^0.14.27",
"npm-run-all": "^4.1.5",
"optionator": "^0.9.1",
"vls": "^0.7.6",
"vue-tsc": "0.33.5",
"tsup": "^5.12.3",
"typescript": "~4.5.5",
"@volar/vue-typescript": "^0.33.0"
"vls": "^0.7.6",
"vue-tsc": "0.33.5"
}
}
5 changes: 0 additions & 5 deletions packages/vite-plugin-checker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ import {
UserPluginConfig,
} from './types'

export * from './types'
export * from './codeFrame'
export * from './worker'
export { Checker } from './Checker'

const sharedConfigKeys: (keyof SharedConfig)[] = ['enableBuild', 'overlay']
const buildInCheckerKeys: BuildInCheckerNames[] = ['typescript', 'vueTsc', 'vls', 'eslint']

Expand Down
9 changes: 0 additions & 9 deletions packages/vite-plugin-checker/tsconfig.build.json

This file was deleted.

5 changes: 5 additions & 0 deletions packages/vite-plugin-checker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./",
"moduleResolution": "node"
},

"include": ["src", "__tests__"],
"exclude": ["src/@runtime"]
}
10 changes: 0 additions & 10 deletions packages/vite-plugin-checker/tsconfig.test.json

This file was deleted.

62 changes: 62 additions & 0 deletions packages/vite-plugin-checker/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import chalk from 'chalk'
import { Plugin } from 'esbuild'
import { relative, join } from 'path'
import { defineConfig } from 'tsup'

export default defineConfig({
entry: ['src'],
outDir: 'lib',
splitting: false,
bundle: false,
sourcemap: true,
clean: false,
target: 'node12',
platform: 'node',
dts: true,
esbuildPlugins: [createPatchEsbuildDistPlugin()],
})

// copied from https://github.com/vitejs/vite/blob/5d6ea8efc36bfdcd8b70afa8e82026ad1ccc0a77/scripts/patchEsbuildDist.ts, with a slight modification :)
function createPatchEsbuildDistPlugin(): Plugin {
return {
name: 'patch-esbuild-dist',
setup(build) {
build.onEnd((result) => {
const targetFile = result.outputFiles!.filter((f) => {
return relative(__dirname, f.path) === join('lib', 'main.js')
})[0]

if (!targetFile) {
console.error(chalk.red(`did not find targetFile`))
process.exit(1)
}

const modifiedCode = patchEsbuildDist(targetFile.text, 'Plugin')

if (modifiedCode) {
Object.defineProperty(targetFile, 'text', { value: modifiedCode })
}
})
},
}
}

function patchEsbuildDist(_code: string, varName: string) {
let code = _code
const moduleExportsLine = `module.exports = __toCommonJS(main_exports);`

if (code.includes(moduleExportsLine)) {
// overwrite for cjs require('...')() usage
code = code.replace(
moduleExportsLine,
`module.exports = ${varName};
${varName}['default'] = ${varName};`
)

console.log(chalk.bold(`patched with overwrite for cjs require('...')()`))
return code
} else {
console.error(chalk.red(`post-esbuild bundling patch failed`))
process.exit(1)
}
}
Loading

0 comments on commit 922ac6d

Please sign in to comment.