vite-plugin-babel-dev
Advanced tools
Comparing version 1.0.1 to 1.0.2
import { TransformOptions } from '@babel/core'; | ||
import { Plugin } from 'vite'; | ||
export interface BabelDevOptions { | ||
import { Plugin as Plugin$1 } from 'vite'; | ||
import { Plugin } from 'esbuild'; | ||
/** | ||
* Original: https://github.com/nativew/esbuild-plugin-babel | ||
* Copied, because there was a problem with `type: "module"` in `package.json` | ||
*/ | ||
interface ESBuildPluginBabelOptions { | ||
filter?: RegExp; | ||
namespace?: string; | ||
config?: TransformOptions; | ||
} | ||
declare const esbuildPluginBabel: (options?: ESBuildPluginBabelOptions) => Plugin; | ||
interface BabelDevOptions { | ||
filter?: RegExp; | ||
babelConfig?: TransformOptions; | ||
} | ||
declare const babelDevPlugin: ({ babelConfig, filter }?: BabelDevOptions) => Plugin; | ||
export default babelDevPlugin; | ||
export * from './esbuildBabel'; | ||
declare const babelDevPlugin: ({ babelConfig, filter }?: BabelDevOptions) => Plugin$1; | ||
export { BabelDevOptions, ESBuildPluginBabelOptions, babelDevPlugin as default, esbuildPluginBabel }; |
@@ -1,40 +0,85 @@ | ||
import babel from '@babel/core'; | ||
import { esbuildPluginBabel } from './esbuildBabel'; | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var babel = require('@babel/core'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var babel__default = /*#__PURE__*/_interopDefaultLegacy(babel); | ||
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs); | ||
var path__default = /*#__PURE__*/_interopDefaultLegacy(path); | ||
const esbuildPluginBabel = (options = {}) => ({ | ||
name: "babel", | ||
setup(build) { | ||
const { filter = /.*/, namespace = "", config = {} } = options; | ||
const transformContents = ({ args, contents }) => { | ||
const babelOptions = babel__default["default"].loadOptions({ | ||
...config, | ||
filename: args.path, | ||
caller: { | ||
name: "esbuild-plugin-babel", | ||
supportsStaticESM: true | ||
} | ||
}); | ||
if (!babelOptions) | ||
return { contents }; | ||
if (babelOptions.sourceMaps) { | ||
const filename = path__default["default"].relative(process.cwd(), args.path); | ||
babelOptions.sourceFileName = filename; | ||
} | ||
return new Promise((resolve, reject) => { | ||
babel__default["default"].transform(contents, babelOptions, (error, result) => { | ||
error ? reject(error) : resolve({ contents: result?.code ?? "" }); | ||
}); | ||
}); | ||
}; | ||
build.onLoad({ filter, namespace }, async (args) => { | ||
const contents = await fs__default["default"].promises.readFile(args.path, "utf8"); | ||
return transformContents({ args, contents }); | ||
}); | ||
} | ||
}); | ||
const DEFAULT_FILTER = /\.jsx?$/; | ||
const babelDevPlugin = ({ babelConfig = {}, filter = DEFAULT_FILTER } = {}) => { | ||
return { | ||
name: 'babel-dev-plugin', | ||
enforce: 'pre', | ||
apply: 'serve', | ||
config() { | ||
return { | ||
optimizeDeps: { | ||
esbuildOptions: { | ||
plugins: [ | ||
esbuildPluginBabel({ | ||
filter, | ||
config: { | ||
...babelConfig, | ||
}, | ||
}), | ||
], | ||
}, | ||
}, | ||
}; | ||
}, | ||
transform(code, id) { | ||
const shouldTransform = filter.test(id); | ||
if (!shouldTransform) | ||
return; | ||
const { code: output, map } = babel.transformSync(code, { | ||
...babelConfig, | ||
}) ?? {}; | ||
return { | ||
code: output ?? '', | ||
map, | ||
}; | ||
}, | ||
}; | ||
return { | ||
name: "babel-dev-plugin", | ||
enforce: "pre", | ||
apply: "serve", | ||
config() { | ||
return { | ||
optimizeDeps: { | ||
esbuildOptions: { | ||
plugins: [ | ||
esbuildPluginBabel({ | ||
filter, | ||
config: { | ||
...babelConfig | ||
} | ||
}) | ||
] | ||
} | ||
} | ||
}; | ||
}, | ||
transform(code, id) { | ||
const shouldTransform = filter.test(id); | ||
if (!shouldTransform) | ||
return; | ||
const { code: output, map } = babel__default["default"].transformSync(code, { | ||
...babelConfig | ||
}) ?? {}; | ||
return { | ||
code: output ?? "", | ||
map | ||
}; | ||
} | ||
}; | ||
}; | ||
export default babelDevPlugin; | ||
export * from './esbuildBabel'; | ||
exports["default"] = babelDevPlugin; | ||
exports.esbuildPluginBabel = esbuildPluginBabel; |
{ | ||
"name": "vite-plugin-babel-dev", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Runs babel during dev serve in Vite", | ||
@@ -12,4 +12,5 @@ "main": "dist/index.js", | ||
"clean": "rm -rf dist", | ||
"build": "tsc", | ||
"prepare": "npm run clean && npm run build" | ||
"dev": "rollup -c --watch", | ||
"build": "rollup -c", | ||
"prepare": "npm run build" | ||
}, | ||
@@ -35,2 +36,5 @@ "repository": { | ||
"@types/node": "^17.0.8", | ||
"rollup": "^2.63.0", | ||
"rollup-plugin-dts": "^4.1.0", | ||
"rollup-plugin-esbuild": "^4.8.2", | ||
"typescript": "^4.5.4", | ||
@@ -37,0 +41,0 @@ "vite": "^2.7.10" |
7714
97
8
5