-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for import.meta.hot (#15)
- Loading branch information
Showing
16 changed files
with
362 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package-lock=false | ||
legacy-peer-deps=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
MIT License | ||
|
||
Copyright (c) [2021] [Michael Peyper] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
associated documentation files (the "Software"), to deal in the Software without restriction, | ||
including without limitation the rights to use, copy, modify, merge, publish, distribute, | ||
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial | ||
portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES | ||
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# babel-plugin-transform-vite-meta-hot | ||
|
||
<!-- prettier-ignore-start --> | ||
[![Build Status](https://img.shields.io/github/workflow/status/OpenSourceRaidGuild/babel-vite/validate?logo=github&style=flat-square)](https://github.com/OpenSourceRaidGuild/babel-vite/actions?query=workflow%3Avalidate) | ||
[![codecov](https://img.shields.io/codecov/c/github/OpenSourceRaidGuild/babel-vite.svg?style=flat-square)](https://codecov.io/gh/OpenSourceRaidGuild/babel-vite) | ||
[![version](https://img.shields.io/npm/v/babel-plugin-transform-vite-meta-hot.svg?style=flat-square)](https://www.npmjs.com/package/babel-plugin-transform-vite-meta-hot) | ||
[![downloads](https://img.shields.io/npm/dm/babel-plugin-transform-vite-meta-hot.svg?style=flat-square)](http://www.npmtrends.com/babel-plugin-transform-vite-meta-hot) | ||
[![MIT License](https://img.shields.io/npm/l/babel-plugin-transform-vite-meta-hot.svg?style=flat-square)](https://github.com/OpenSourceRaidGuild/babel-vite/blob/master/LICENSE.md) | ||
|
||
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) | ||
[![Code of Conduct](https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square)](https://github.com/OpenSourceRaidGuild/babel-vite/blob/master/CODE_OF_CONDUCT.md) | ||
[![Discord](https://img.shields.io/discord/808364903822917662.svg?color=7389D8&labelColor=6A7EC2&logo=discord&logoColor=ffffff&style=flat-square)](https://discord.gg/grS89HWeYh) | ||
|
||
[![Watch on GitHub](https://img.shields.io/github/watchers/OpenSourceRaidGuild/babel-vite.svg?style=social)](https://github.com/OpenSourceRaidGuild/babel-vite/watchers) | ||
[![Star on GitHub](https://img.shields.io/github/stars/OpenSourceRaidGuild/babel-vite.svg?style=social)](https://github.com/OpenSourceRaidGuild/babel-vite/stargazers) | ||
[![Tweet](https://img.shields.io/twitter/url/https/github.com/OpenSourceRaidGuild/babel-vite.svg?style=social)](https://twitter.com/intent/tweet?text=Check%20out%20babel-plugin-transform-vite-meta-hot%20by%20OpenSourceRaidGuild%20https%3A%2F%2Fgithub.com%2FOpenSourceRaidGuild%2Fbabel-vite%20%F0%9F%91%8D) | ||
<!-- prettier-ignore-end --> | ||
|
||
> Please note: this plugin is intended to provide an approximation of some of Vite specific | ||
> transformations when running the code in non-Vite environment, for example, running tests with a | ||
> NodeJS based test runner. | ||
> | ||
> **The functionality within these transformations should not be relied upon in production.** | ||
## Example | ||
|
||
**In** | ||
|
||
``` | ||
if (import.meta.hot) { | ||
import.meta.hot.accept(callback); | ||
} | ||
``` | ||
|
||
**Out** | ||
|
||
``` | ||
if (module.hot) { | ||
module.hot.accept(callback); | ||
} | ||
``` | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install --save-dev babel-plugin-transform-vite-meta-hot | ||
``` | ||
|
||
## Usage | ||
|
||
### With a configuration file (Recommended) | ||
|
||
```json | ||
{ | ||
"plugins": ["babel-plugin-transform-vite-meta-hot"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
babel --plugins babel-plugin-transform-vite-meta-hot script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require('@babel/core').transformSync('code', { | ||
plugins: ['babel-plugin-transform-vite-meta-hot'] | ||
}) | ||
``` |
44 changes: 44 additions & 0 deletions
44
packages/babel-plugin-transform-vite-meta-hot/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"name": "babel-plugin-transform-vite-meta-hot", | ||
"version": "0.0.0-semantically-released", | ||
"description": "babel plugin that emulates vite's import.meta.hot functionality", | ||
"main": "lib/index.js", | ||
"typings": "lib/index.d.ts", | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/OpenSourceRaidGuild/babel-vite.git" | ||
}, | ||
"keywords": [ | ||
"babel", | ||
"vite", | ||
"plugin", | ||
"import", | ||
"meta", | ||
"hot" | ||
], | ||
"author": "Kelvin Zhang <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/OpenSourceRaidGuild/babel-vite/issues" | ||
}, | ||
"homepage": "https://github.com/OpenSourceRaidGuild/babel-vite#readme", | ||
"scripts": { | ||
"build": "kcd-scripts build --out-dir lib", | ||
"test": "kcd-scripts test" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.13.8", | ||
"@types/babel-plugin-tester": "^9.0.1", | ||
"babel-plugin-tester": "^10.0.0", | ||
"kcd-scripts": "^12.0.0", | ||
"typescript": "^4.2.2" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.13.9", | ||
"@types/babel__core": "^7.1.12" | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
packages/babel-plugin-transform-vite-meta-hot/src/__tests__/__snapshots__/index.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`vite-meta-hot not import.meta lookup: not import.meta lookup 1`] = ` | ||
const x = import.meta() | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const x = import.meta() | ||
`; | ||
|
||
exports[`vite-meta-hot not replace import.meta.hot: not replace import.meta.hot 1`] = ` | ||
const x = module.hot | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const x = module.hot | ||
`; | ||
|
||
exports[`vite-meta-hot not replace key access: not replace key access 1`] = ` | ||
const key = "env"; const x = import.meta[key] | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const key = 'env' | ||
const x = import.meta[key] | ||
`; | ||
|
||
exports[`vite-meta-hot not replace string access: not replace string access 1`] = ` | ||
const x = import.meta["env"] | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const x = import.meta['env'] | ||
`; | ||
|
||
exports[`vite-meta-hot not replaceable: not replaceable 1`] = ` | ||
const x = import.meta.other | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const x = import.meta.other | ||
`; | ||
|
||
exports[`vite-meta-hot replace import.meta.hot: replace import.meta.hot 1`] = ` | ||
const x = import.meta.hot | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const x = module.hot | ||
`; | ||
|
||
exports[`vite-meta-hot replace key access: replace key access 1`] = ` | ||
const key = "hot"; const x = import.meta[key] | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const key = 'hot' | ||
const x = module.hot | ||
`; | ||
|
||
exports[`vite-meta-hot replace string access: replace string access 1`] = ` | ||
const x = import.meta["hot"] | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const x = module.hot | ||
`; |
18 changes: 18 additions & 0 deletions
18
packages/babel-plugin-transform-vite-meta-hot/src/__tests__/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pluginTester from 'babel-plugin-tester' | ||
import plugin from '..' | ||
|
||
pluginTester({ | ||
plugin, | ||
pluginName: 'vite-meta-hot', | ||
snapshot: true, | ||
tests: { | ||
'replace import.meta.hot': 'const x = import.meta.hot', | ||
'not replace import.meta.hot': 'const x = module.hot', | ||
'replace string access': 'const x = import.meta["hot"]', | ||
'not replace string access': 'const x = import.meta["env"]', | ||
'replace key access': 'const key = "hot"; const x = import.meta[key]', | ||
'not replace key access': 'const key = "env"; const x = import.meta[key]', | ||
'not replaceable': 'const x = import.meta.other', | ||
'not import.meta lookup': 'const x = import.meta()' | ||
} | ||
}) |
52 changes: 52 additions & 0 deletions
52
packages/babel-plugin-transform-vite-meta-hot/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type babelCore from '@babel/core' | ||
|
||
export default function viteMetaHotBabelPlugin({ | ||
template, | ||
types: t | ||
}: typeof babelCore): babelCore.PluginObj { | ||
return { | ||
name: 'vite-meta-hot', | ||
visitor: { | ||
MemberExpression(path) { | ||
const isMetaProperty = t.isMetaProperty(path.node.object) | ||
const isHotVar = t.isIdentifier(path.node.property) && path.node.property.name === 'hot' | ||
|
||
if (!isMetaProperty || !isHotVar) { | ||
return | ||
} | ||
|
||
path.replaceWith(template.expression.ast('module.hot')) | ||
}, | ||
StringLiteral(path) { | ||
const isMetaProperty = | ||
t.isMemberExpression(path.parentPath.node) && | ||
t.isMetaProperty(path.parentPath.node.object) | ||
const isHotVar = path.node.value === 'hot' | ||
|
||
if (!isMetaProperty || !isHotVar) { | ||
return | ||
} | ||
|
||
path.parentPath.replaceWith(template.expression.ast('module.hot')) | ||
}, | ||
Identifier(path) { | ||
if ( | ||
!t.isMemberExpression(path.parentPath.node) || | ||
!t.isMetaProperty(path.parentPath.node.object) | ||
) { | ||
return | ||
} | ||
|
||
const key = path.node.name | ||
/* @ts-expect-error outdated types */ | ||
// eslint-disable-next-line | ||
const keyValue = path.scope.getBinding(key)?.path.node.init?.value | ||
if (keyValue !== 'hot') { | ||
return | ||
} | ||
|
||
path.parentPath.replaceWith(template.expression.ast('module.hot')) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,10 @@ | |
"keywords": [ | ||
"babel", | ||
"vite", | ||
"preset" | ||
"preset", | ||
"env", | ||
"glob", | ||
"hot" | ||
], | ||
"author": "Michael Peyper <[email protected]>", | ||
"license": "MIT", | ||
|
@@ -38,6 +41,7 @@ | |
"@babel/runtime": "^7.13.9", | ||
"@types/babel__core": "^7.1.12", | ||
"babel-plugin-transform-vite-meta-env": "^0.0.0-semantically-released", | ||
"babel-plugin-transform-vite-meta-glob": "^0.0.0-semantically-released" | ||
"babel-plugin-transform-vite-meta-glob": "^0.0.0-semantically-released", | ||
"babel-plugin-transform-vite-meta-hot": "^0.0.0-semantically-released" | ||
} | ||
} |
Oops, something went wrong.