Skip to content

Commit

Permalink
ensure hashing works for contexts (#524)
Browse files Browse the repository at this point in the history
* ensure hashing works

* vite filename present
  • Loading branch information
JoviDeCroock authored Nov 10, 2023
1 parent beb2d69 commit 7e47061
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-flies-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@prefresh/vite': patch
---

Add filename to the vite options as well
5 changes: 5 additions & 0 deletions .changeset/eight-coats-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@prefresh/babel-plugin': patch
---

Ensure we accoutn for the filename when hashing
68 changes: 47 additions & 21 deletions packages/babel/src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ export default function (babel, opts = {}) {
case 'Identifier': {
const calleeSource = calleePath.getSource().split('(')[0];
const innerName = inferredName + '$' + calleeSource;
const foundInside = argsPath.some((argPath) => findInnerComponents(
innerName,
argPath,
callback
));
const foundInside = argsPath.some(argPath =>
findInnerComponents(innerName, argPath, callback)
);
if (!foundInside) {
return false;
}
Expand Down Expand Up @@ -487,13 +485,17 @@ export default function (babel, opts = {}) {
}
}

const getFirstParent = (parentPath) => {
if (t.isProgram(parentPath) || t.isFunctionDeclaration(parentPath) || t.isArrowFunctionExpression(parentPath)) return parentPath;
return getFirstParent(parentPath.parentPath)

}
const getFirstParent = parentPath => {
if (
t.isProgram(parentPath) ||
t.isFunctionDeclaration(parentPath) ||
t.isArrowFunctionExpression(parentPath)
)
return parentPath;
return getFirstParent(parentPath.parentPath);
};

const closestClosurePath = getFirstParent(path.parentPath)
const closestClosurePath = getFirstParent(path.parentPath);

const contexts = state.get('contexts');
let counter = (contexts.get(id) || -1) + 1;
Expand All @@ -502,34 +504,50 @@ export default function (babel, opts = {}) {
id = '_' + state.get('filehash') + id;
path.skip();
if (!t.isProgram(closestClosurePath)) {
const params = closestClosurePath.node.params
const params = closestClosurePath.node.params;
params.forEach(param => {
if (t.isIdentifier(param)) {
id += '_PARAM' + param.name
id += '_PARAM' + param.name;
}
})
});
}

// TODO: maybe wrap with JSON.stringify
if (path.node.arguments[0]) {
const [quasi, ...expressions] = id.split('_PARAM');
const first = t.templateElement({ raw: quasi, cooked: '' })
const expr = expressions.map(x => t.identifier(x.replace('}', '')))
const first = t.templateElement({ raw: quasi, cooked: '' });
const expr = expressions.map(x => t.identifier(x.replace('}', '')));
path.replaceWith(
createContextTemplate({
CREATECONTEXT: path.get('callee').node,
IDENT: t.templateLiteral([first, ...expressions.map(() => t.templateElement({ raw: '', cooked: '' }, true))], expr),
IDENT: t.templateLiteral(
[
first,
...expressions.map(() =>
t.templateElement({ raw: '', cooked: '' }, true)
),
],
expr
),
VALUE: t.clone(getFirstNonTsExpression(path.node.arguments[0])),
})
);
} else {
const [quasi, ...expressions] = id.split('_PARAM');
const first = t.templateElement({ raw: quasi, cooked: '' })
const expr = expressions.map(x => t.identifier(x.replace('}', '')))
const first = t.templateElement({ raw: quasi, cooked: '' });
const expr = expressions.map(x => t.identifier(x.replace('}', '')));
path.replaceWith(
emptyTemplate({
CREATECONTEXT: path.get('callee').node,
IDENT: t.templateLiteral([first, ...expressions.map(() => t.templateElement({ raw: '', cooked: '' }, true))], expr),
IDENT: t.templateLiteral(
[
first,
...expressions.map(() =>
t.templateElement({ raw: '', cooked: '' }, true)
),
],
expr
),
})
);
}
Expand Down Expand Up @@ -853,7 +871,15 @@ export default function (babel, opts = {}) {
},
Program: {
enter(path, state) {
state.set('filehash', hash(path.hub.file.opts.filename || 'unnamed'));
state.set(
'filehash',
hash(
path.hub.file.opts.filename ||
path.hub.file.opts.sourceFileName ||
path.hub.file.opts.generatorOpts?.sourceFileName ||
'unnamed'
)
);
state.set('contexts', new Map());
// This is a separate early visitor because we need to collect Hook calls
// and "const [foo, setFoo] = ..." signatures before the destructuring
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const transform = (code, path, plugins) =>
},
ast: false,
sourceMaps: true,
filename: path,
sourceFileName: path,
configFile: false,
babelrc: false,
Expand Down

0 comments on commit 7e47061

Please sign in to comment.