diff --git a/package.json b/package.json index 474e355b..ae2cd3c5 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "@typescript-eslint/parser": "^5.8.1", "conventional-changelog-cli": "^2.2.2", "cross-env": "^7.0.3", - "esbuild": "0.14.3", + "esbuild": "^0.14.14", "eslint": "^8.5.0", "eslint-define-config": "^1.2.1", "eslint-plugin-node": "^11.1.0", diff --git a/packages/plugin-vue/package.json b/packages/plugin-vue/package.json index 046c84ce..02dee5e0 100644 --- a/packages/plugin-vue/package.json +++ b/packages/plugin-vue/package.json @@ -13,7 +13,8 @@ "dev-types": "tsc -p . -w --incremental --emitDeclarationOnly", "dev-watch": "esbuild src/index.ts --watch --bundle --platform=node --target=node12 --external:@vue/compiler-sfc --external:vue/compiler-sfc --external:vite --outfile=dist/index.js", "build": "rimraf dist && run-s build-bundle build-types", - "build-bundle": "esbuild src/index.ts --bundle --platform=node --target=node12 --external:@vue/compiler-sfc --external:vue/compiler-sfc --external:vite --outfile=dist/index.js", + "build-bundle": "esbuild src/index.ts --bundle --platform=node --target=node12 --external:@vue/compiler-sfc --external:vue/compiler-sfc --external:vite --outfile=dist/index.js & npm run patch-dist", + "patch-dist": "ts-node ../../scripts/patchEsbuildDist.ts dist/index.js vuePlugin", "build-types": "tsc -p . --emitDeclarationOnly --outDir temp && api-extractor run && rimraf temp", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package plugin-vue", "release": "ts-node ../../scripts/release.ts" diff --git a/packages/plugin-vue/src/index.ts b/packages/plugin-vue/src/index.ts index 78c0c27c..fb1bce3c 100644 --- a/packages/plugin-vue/src/index.ts +++ b/packages/plugin-vue/src/index.ts @@ -244,5 +244,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin { } // overwrite for cjs require('...')() usage -module.exports = vuePlugin -vuePlugin['default'] = vuePlugin +// The following lines are inserted by scripts/patchEsbuildDist.ts, +// this doesn't bundle correctly after esbuild 0.14.4 +// +// module.exports = vuePlugin +// vuePlugin['default'] = vuePlugin diff --git a/scripts/patchEsbuildDist.ts b/scripts/patchEsbuildDist.ts new file mode 100644 index 00000000..70ef846a --- /dev/null +++ b/scripts/patchEsbuildDist.ts @@ -0,0 +1,31 @@ +// esbuild 0.14.4 https://github.com/evanw/esbuild/blob/master/CHANGELOG.md#0144 introduced a +// change that breaks the "overwrite for cjs require('...')() usage" hack used in plugin-vue +// and plugin-react. For the moment, we can remove the extra exports code added in 0.14.4 to +// continue using it. + +import { bold, red } from 'picocolors' +import { readFileSync, writeFileSync } from 'fs' + +const indexPath = process.argv[2] +const varName = process.argv[3] + +let code = readFileSync(indexPath, 'utf-8') + +const moduleExportsLine = `module.exports = __toCommonJS(src_exports);` + +if (code.includes(moduleExportsLine)) { + // overwrite for cjs require('...')() usage + code = code.replace( + moduleExportsLine, + `module.exports = ${varName}; +${varName}['default'] = ${varName};` + ) + + writeFileSync(indexPath, code) + + console.log( + bold(`${indexPath} patched with overwrite for cjs require('...')()`) + ) +} else { + console.error(red(`${indexPath} post-esbuild bundling patch failed`)) +}