diff --git a/.changeset/curly-flies-sell.md b/.changeset/curly-flies-sell.md new file mode 100644 index 00000000..f3252a64 --- /dev/null +++ b/.changeset/curly-flies-sell.md @@ -0,0 +1,5 @@ +--- +'@prefresh/vite': patch +--- + +Add filename to the vite options as well diff --git a/.changeset/eight-coats-explode.md b/.changeset/eight-coats-explode.md new file mode 100644 index 00000000..857e197d --- /dev/null +++ b/.changeset/eight-coats-explode.md @@ -0,0 +1,5 @@ +--- +'@prefresh/babel-plugin': patch +--- + +Ensure we accoutn for the filename when hashing diff --git a/packages/babel/src/index.mjs b/packages/babel/src/index.mjs index f99b7210..95033621 100644 --- a/packages/babel/src/index.mjs +++ b/packages/babel/src/index.mjs @@ -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; } @@ -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; @@ -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 + ), }) ); } @@ -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 diff --git a/packages/vite/src/index.js b/packages/vite/src/index.js index 6f57aab0..d7164596 100644 --- a/packages/vite/src/index.js +++ b/packages/vite/src/index.js @@ -110,6 +110,7 @@ const transform = (code, path, plugins) => }, ast: false, sourceMaps: true, + filename: path, sourceFileName: path, configFile: false, babelrc: false,