solid-refresh
Advanced tools
Comparing version 0.4.0 to 0.4.1
120
babel.js
@@ -19,5 +19,3 @@ 'use strict'; | ||
enumerable: true, | ||
get: function () { | ||
return e[k]; | ||
} | ||
get: function () { return e[k]; } | ||
}); | ||
@@ -27,3 +25,3 @@ } | ||
} | ||
n['default'] = e; | ||
n["default"] = e; | ||
return Object.freeze(n); | ||
@@ -58,2 +56,5 @@ } | ||
} | ||
if (bundler === 'webpack5') { | ||
return t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier('import'), t__namespace.identifier('meta')), t__namespace.identifier('webpackHot')); | ||
} | ||
return t__namespace.memberExpression(t__namespace.identifier("module"), t__namespace.identifier("hot")); | ||
@@ -81,4 +82,4 @@ } | ||
function createSignatureValue(node) { | ||
const code = generator__default['default'](node); | ||
const result = crypto__default['default'].createHash('sha256').update(code.code).digest('base64'); | ||
const code = generator__default["default"](node); | ||
const result = crypto__default["default"].createHash('sha256').update(code.code).digest('base64'); | ||
return result; | ||
@@ -196,2 +197,99 @@ } | ||
} | ||
const SOURCE_MODULE = 'solid-js/web'; | ||
function isValidSpecifier(specifier, keyword) { | ||
return ((t__namespace.isIdentifier(specifier.imported) && specifier.imported.name === keyword) | ||
|| (t__namespace.isStringLiteral(specifier.imported) && specifier.imported.value === keyword)); | ||
} | ||
function captureValidIdentifiers(path) { | ||
const validIdentifiers = new Set(); | ||
path.traverse({ | ||
ImportDeclaration(p) { | ||
if (p.node.source.value === SOURCE_MODULE) { | ||
for (let i = 0, len = p.node.specifiers.length; i < len; i += 1) { | ||
const specifier = p.node.specifiers[i]; | ||
if (t__namespace.isImportSpecifier(specifier) | ||
&& (isValidSpecifier(specifier, 'render') | ||
|| isValidSpecifier(specifier, 'hydrate'))) { | ||
validIdentifiers.add(specifier.local); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
return validIdentifiers; | ||
} | ||
function captureValidNamespaces(path) { | ||
const validNamespaces = new Set(); | ||
path.traverse({ | ||
ImportDeclaration(p) { | ||
if (p.node.source.value === SOURCE_MODULE) { | ||
for (let i = 0, len = p.node.specifiers.length; i < len; i += 1) { | ||
const specifier = p.node.specifiers[i]; | ||
if (t__namespace.isImportNamespaceSpecifier(specifier)) { | ||
validNamespaces.add(specifier.local); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
return validNamespaces; | ||
} | ||
function isValidCallee(path, { callee }, validIdentifiers, validNamespaces) { | ||
if (t__namespace.isIdentifier(callee)) { | ||
const binding = path.scope.getBinding(callee.name); | ||
return binding && validIdentifiers.has(binding.identifier); | ||
} | ||
if (t__namespace.isMemberExpression(callee) | ||
&& !callee.computed | ||
&& t__namespace.isIdentifier(callee.object) | ||
&& t__namespace.isIdentifier(callee.property)) { | ||
const binding = path.scope.getBinding(callee.object.name); | ||
return (binding | ||
&& validNamespaces.has(binding.identifier) | ||
&& (callee.property.name === 'render' || callee.property.name === 'hydrate')); | ||
} | ||
return false; | ||
} | ||
function checkValidRenderCall(path) { | ||
let currentPath = path.parentPath; | ||
while (currentPath) { | ||
if (t__namespace.isProgram(currentPath.node)) { | ||
return true; | ||
} | ||
if (!t__namespace.isStatement(currentPath.node)) { | ||
return false; | ||
} | ||
currentPath = currentPath.parentPath; | ||
} | ||
return false; | ||
} | ||
function fixRenderCalls(path, opts) { | ||
const validIdentifiers = captureValidIdentifiers(path); | ||
const validNamespaces = captureValidNamespaces(path); | ||
path.traverse({ | ||
ExpressionStatement(p) { | ||
if (t__namespace.isCallExpression(p.node.expression) | ||
&& checkValidRenderCall(p) | ||
&& isValidCallee(p, p.node.expression, validIdentifiers, validNamespaces)) { | ||
// Replace with variable declaration | ||
const id = p.scope.generateUidIdentifier("cleanup"); | ||
p.replaceWith(t__namespace.variableDeclaration('const', [ | ||
t__namespace.variableDeclarator(id, p.node.expression), | ||
])); | ||
const pathToHot = getHotIdentifier(opts.bundler); | ||
p.insertAfter(t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier('dispose')), [id])))); | ||
p.skip(); | ||
} | ||
}, | ||
}); | ||
} | ||
function getHMRDecline(opts, pathToHot) { | ||
if (isESMHMR(opts.bundler)) { | ||
return (t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), [])))); | ||
} | ||
if (opts.bundler === 'webpack5') { | ||
return (t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), [])))); | ||
} | ||
return (t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.conditionalExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), []), t__namespace.callExpression(t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier("window"), t__namespace.identifier("location")), t__namespace.identifier("reload")), []))))); | ||
} | ||
function solidRefreshPlugin() { | ||
@@ -211,2 +309,4 @@ return { | ||
Program(path, { file, opts, processed, granular }) { | ||
var _a; | ||
let shouldReload = false; | ||
const comments = file.ast.comments; | ||
@@ -226,6 +326,5 @@ if (comments) { | ||
processed.value = true; | ||
shouldReload = true; | ||
const pathToHot = getHotIdentifier(opts.bundler); | ||
path.pushContainer('body', isESMHMR(opts.bundler) | ||
? (t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), [])))) | ||
: (t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.conditionalExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), []), t__namespace.callExpression(t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier("window"), t__namespace.identifier("location")), t__namespace.identifier("reload")), [])))))); | ||
path.pushContainer('body', getHMRDecline(opts, pathToHot)); | ||
return; | ||
@@ -235,2 +334,5 @@ } | ||
} | ||
if (!shouldReload && ((_a = opts.fixRender) !== null && _a !== void 0 ? _a : true)) { | ||
fixRenderCalls(path, opts); | ||
} | ||
}, | ||
@@ -237,0 +339,0 @@ ExportNamedDeclaration(path, state) { |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"homepage": "https://github.com/solidjs/solid-refresh#readme", | ||
@@ -28,25 +28,23 @@ "repository": { | ||
"build": "rollup -c", | ||
"test": "jest", | ||
"test": "vitest", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.16.0", | ||
"@rollup/plugin-node-resolve": "13.0.0", | ||
"@rollup/plugin-typescript": "^8.3.0", | ||
"@types/babel__core": "^7.1.16", | ||
"@types/jest": "^27.0.3", | ||
"jest": "^27.4.3", | ||
"rollup": "^2.52.1", | ||
"solid-js": "^1.3.0", | ||
"ts-jest": "^27.1.0", | ||
"typescript": "^4.5.2" | ||
"@babel/core": "^7.18.5", | ||
"@rollup/plugin-node-resolve": "13.3.0", | ||
"@rollup/plugin-typescript": "^8.3.3", | ||
"@types/babel__core": "^7.1.19", | ||
"rollup": "^2.75.7", | ||
"solid-js": "^1.4.4", | ||
"typescript": "^4.7.4", | ||
"vitest": "^0.15.1" | ||
}, | ||
"peerDependencies": { | ||
"solid-js": "^1.3.0" | ||
"solid-js": "^1.3" | ||
}, | ||
"dependencies": { | ||
"@babel/generator": "^7.16.0", | ||
"@babel/helper-module-imports": "^7.16.0", | ||
"@babel/types": "^7.16.0" | ||
"@babel/generator": "^7.18.2", | ||
"@babel/helper-module-imports": "^7.16.7", | ||
"@babel/types": "^7.18.4" | ||
} | ||
} |
<p> | ||
<img width="100%" src="https://raw.githubusercontent.com/solidjs/solid-refresh/master/banner.png" alt="Solid Refresh"> | ||
<img width="100%" src="https://assets.solidjs.com/banner?project=Refresh&type=core" alt="Solid Refresh"> | ||
</p> | ||
@@ -21,3 +21,3 @@ | ||
* Webpack | ||
* Webpack (for strict ESM, use option `bundler: "webpack5"`) | ||
* Parcel | ||
@@ -64,2 +64,14 @@ * Nollup | ||
If you're using strict ESM: | ||
```json | ||
{ | ||
"env": { | ||
"development": { | ||
"plugins": ["solid-refresh/babel", { "bundler": "webpack5" }] | ||
} | ||
} | ||
} | ||
``` | ||
### Nollup | ||
@@ -123,2 +135,8 @@ | ||
## Automatic Render Cleanup | ||
The plugin automatically handles cleanups for unhandled `render` and `hydrate` calls from `solid-js/web`. | ||
You can disable this feature entirely through the option `"fixRender": false`. | ||
## Pragma | ||
@@ -125,0 +143,0 @@ |
34025
8
654
163
Updated@babel/generator@^7.18.2
Updated@babel/types@^7.18.4