Skip to content

Commit d5a0f03

Browse files
committed
feat: WIP
1 parent 61d4e00 commit d5a0f03

File tree

6 files changed

+4027
-52
lines changed

6 files changed

+4027
-52
lines changed

examples/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { createApp } from 'vue';
2+

package.json

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"version": "1.0.0",
44
"description": "Tweak Vue components written in JSX in real time.",
55
"main": "dist/index.js",
6-
"scripts": {},
6+
"scripts": {
7+
"build": "tsc"
8+
},
79
"files": [
810
"dist"
911
],
@@ -24,13 +26,20 @@
2426
"homepage": "https://github.com/Amour1688/vue-jsx-hot-loader#readme",
2527
"dependencies": {
2628
"@babel/parser": "^7.0.0",
29+
"@babel/template": "^7.0.0",
2730
"@babel/traverse": "^7.0.0",
2831
"hash-sum": "^2.0.0",
2932
"loader-utils": "^2.0.0"
3033
},
3134
"devDependencies": {
32-
"@types/webpack": "^4.41.22",
35+
"@babel/core": "^7.12.10",
36+
"@types/loader-utils": "^2.0.1",
37+
"@vue/babel-plugin-jsx": "^1.0.0",
38+
"babel-loader": "^8.2.2",
39+
"jest": "^26.6.3",
3340
"typescript": "^4.0.3",
34-
"webpack": "^4.44.2"
41+
"vue": "^3.0.5",
42+
"webpack": "^4.44.2",
43+
"webpack-dev-server": "^3.11.1"
3544
}
3645
}

src/index.ts

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
1-
import webpack from 'webpack';
1+
import * as webpack from 'webpack';
2+
import * as hash from 'hash-sum';
3+
import * as path from 'path';
4+
import * as loaderUtils from 'loader-utils';
25
import * as parser from "@babel/parser";
36
import traverse from "@babel/traverse";
47

58
export default function loader(
69
this: webpack.loader.LoaderContext,
7-
source: string
8-
): string {
10+
source: string,
11+
sourceMap: string
12+
) {
913
const loaderContext = this;
10-
11-
return source;
14+
15+
loaderContext.cacheable?.();
16+
const webpackRemainingChain = loaderUtils.getRemainingRequest(loaderContext).split('!');
17+
const fullPath = webpackRemainingChain[webpackRemainingChain.length - 1];
18+
const filename = path.relative(process.cwd(), fullPath);
19+
const hotId = hash(fullPath);
20+
21+
loaderContext.callback(
22+
null,
23+
`${source}
24+
/* hot reload */
25+
if (module.hot) {
26+
script.__hmrId = "${hotId}"
27+
const api = __VUE_HMR_RUNTIME__
28+
module.hot.accept()
29+
if (!api.createRecord('${hotId}')) {
30+
api.reload('${hotId}', script)
31+
}
32+
}
33+
`,
34+
sourceMap
35+
);
1236
};

src/shim.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'hash-sum'

webpack.config.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const babelConfig = {
2+
plugins: [
3+
'@vue/babel-plugin-jsx'
4+
],
5+
}
6+
7+
module.exports = {
8+
mode: 'development',
9+
entry: {
10+
app: './examples/index.js',
11+
},
12+
module: {
13+
rules: [
14+
{
15+
test: /\.(js|jsx)$/,
16+
loader: 'babel-loader',
17+
options: babelConfig,
18+
}
19+
],
20+
},
21+
};

0 commit comments

Comments
 (0)