Skip to content

Commit

Permalink
ember vite hmr plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Feb 1, 2024
0 parents commit cc8e79c
Show file tree
Hide file tree
Showing 28 changed files with 12,112 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# unconventional js
/blueprints/*/files/

# compiled output
/dist/
/declarations/

# misc
/coverage/
46 changes: 46 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

module.exports = {
root: true,
parser: '@babel/eslint-parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
babelOptions: {
root: __dirname,
},
},
plugins: ['ember', 'prettier'],
extends: [
'eslint:recommended',
'plugin:ember/recommended',
'plugin:prettier/recommended',
],
env: {
browser: true,
},
rules: {},
overrides: [
{
files: ['**/*.ts'],
},
// node files
{
files: [
'./.eslintrc.cjs',
'./.prettierrc.cjs',
'./.template-lintrc.cjs',
'./addon-main.cjs',
],
parserOptions: {
sourceType: 'script',
},
env: {
browser: false,
node: true,
},
plugins: ['n'],
extends: ['plugin:n/recommended'],
},
],
};
39 changes: 39 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: "https://registry.npmjs.org"
env:
NODE_AUTH_TOKEN: "${{ secrets.NPM_TOKEN }}"
- uses: pnpm/action-setup@v2
with:
version: latest
- run: pnpm i
- run: pnpm run build || echo "ignore failures for now"
- run: pnpm run test
- run: pnpm view "ember-vite-hmr@$(cat package.json | jq -r '.version')" || npm publish
if: github.ref == 'refs/heads/main'

27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
*
!tests
!tests/**/*
!declarations
!declarations/**/*
!lib
!lib/**/*
!src
!src/**/*
!types
!types/**/*
!.eslintignore
!.eslintrc.cjs
!.gitignore
!.prettierignore
!.prettierrc.cjs
!.template-lintrc.cjs
!addon-main.cjs
!babel.config.json
!CONTRIBUTING.md
!package.json
!pnpm-lock.yaml
!README.md
!rollup.config.mjs
!tsconfig.json
!tsconfig.lib.json
!webpack.js
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/ember-webpack-hot-reload.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# unconventional js
/blueprints/*/files/

# compiled output
/dist/
/declarations/

# misc
/coverage/
6 changes: 6 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
plugins: ['prettier-plugin-ember-template-tag'],
singleQuote: true,
};
5 changes: 5 additions & 0 deletions .template-lintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
extends: 'recommended',
};
25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# How To Contribute

## Linting

- `pnpm lint`
- `pnpm lint:fix`

## Building the addon

- `cd ember-webpack-hot-reload`
- `pnpm build`

## Running tests

- `cd test-app`
- `pnpm test` – Runs the test suite on the current Ember version
- `pnpm test:watch` – Runs the test suite in "watch mode"

## Running the test application

- `cd test-app`
- `pnpm start`
- Visit the test application at [http://localhost:4200](http://localhost:4200).

For more information on using ember-cli, visit [https://cli.emberjs.com/release/](https://cli.emberjs.com/release/).
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ember-vite-hmr

Ember Vite Hmr plugin.

this can hot reload

- helpers
- modifiers
- components + templates, fcct components and its deps
- routes/controllers/route-templates (although it refreshes the whole route and thus looses all state)

it cannot hot reload local properties that turn out to be helpers/modifiers/components.

## Installation

```
ember install ember-vite-hmr
```

## Usage

update your `vite.config.mjs` with

```js
import { hmr } from 'ember-vite-hmr';
...hmr
plugins: [
...hmr(resolver())
]
```

## Contributing

See the [Contributing](CONTRIBUTING.md) guide for details.

## License

This project is licensed under the [MIT License](LICENSE.md).
44 changes: 44 additions & 0 deletions addon-main.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const { addonV1Shim } = require('@embroider/addon-shim');
const path = require('path');
module.exports = addonV1Shim(__dirname);
const included = module.exports.included;

Object.assign(module.exports, {
_getBabelOptions() {
const parentOptions = this.parent && this.parent.options;
const appOptions = this.app && this.app.options;
const addonOptions = parentOptions || appOptions || {};

addonOptions.babel = addonOptions.babel || {};
addonOptions.babel.plugins = addonOptions.babel.plugins || [];
return addonOptions.babel;
},
included(...args) {
this._getBabelOptions().plugins.push([require.resolve('./dist/lib/babel-plugin.js'), { v: 4, appName: this.app.name }]);
this._super.included; // need to access this somehow? otherwise it fails later on...

let astPlugin = this._buildPlugin();
astPlugin.parallelBabel = {
requireFile: __filename,
buildUsing: '_buildPlugin',
params: {}
};
const compatAppBuilder = require.resolve('@embroider/compat/src/compat-app-builder', { paths: [process.cwd()] });
const CompatAppBuilder = require(compatAppBuilder).CompatAppBuilder;
const etcOptions = CompatAppBuilder.prototype.etcOptions;
CompatAppBuilder.prototype.etcOptions = function (...args) {
const opts = etcOptions.call(this, ...args);
opts.transforms.push(astPlugin);
return opts;
}
included.call(this, ...args);
},

_buildPlugin() {
const hotAstProcessor = require('./dist/lib/babel-plugin').hotAstProcessor;
return hotAstProcessor.transform.bind(hotAstProcessor);
},
})

19 changes: 19 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"plugins": [
[
"@babel/plugin-transform-typescript",
{ "allExtensions": true, "allowDeclareFields": true }
],
"@embroider/addon-dev/template-colocation-plugin",
"@babel/plugin-transform-class-static-block",
[
"babel-plugin-ember-template-compilation",
{
"targetFormat": "hbs",
"transforms": []
}
],
["@babel/plugin-proposal-decorators", { "version": "legacy" }],
"@babel/plugin-proposal-class-properties"
]
}
Loading

0 comments on commit cc8e79c

Please sign in to comment.