Skip to content

Commit

Permalink
fix(rollup,vite): fallback resolver for external libs (#1210)
Browse files Browse the repository at this point in the history
* fix(rollup): don't parse external libraries

* fix(rollup,vite): fallback resolver for external libs (#1210)

* chore: changeset

---------

Co-authored-by: Anton Evzhakov <anton@evz.name>
  • Loading branch information
kcover and Anber committed Feb 15, 2023
1 parent 15cb230 commit 13f0b41
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/metal-carrots-fix.md
@@ -0,0 +1,6 @@
---
'@linaria/rollup': patch
'@linaria/vite': patch
---

Fallback resolver for external libraries when bundling with Rollup or Vite.
17 changes: 15 additions & 2 deletions packages/rollup/src/index.ts
Expand Up @@ -18,7 +18,7 @@ import type {
Result,
} from '@linaria/babel-preset';
import { createCustomDebug } from '@linaria/logger';
import { getFileIdx } from '@linaria/utils';
import { getFileIdx, syncResolve } from '@linaria/utils';
import type { Plugin as VitePlugin } from '@linaria/vite';
import vitePlugin from '@linaria/vite';

Expand Down Expand Up @@ -60,10 +60,23 @@ export default function linaria({

log('rollup-init', id);

const asyncResolve = async (what: string, importer: string) => {
const asyncResolve = async (
what: string,
importer: string,
stack: string[]
) => {
const resolved = await this.resolve(what, importer);
if (resolved) {
if (resolved.external) {
// If module is marked as external, Rollup will not resolve it,
// so we need to resolve it ourselves with default resolver
const resolvedId = syncResolve(what, importer, stack);
log('resolve', "✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
return resolvedId;
}

log('resolve', "✅ '%s'@'%s -> %O\n%s", what, importer, resolved);

// Vite adds param like `?v=667939b3` to cached modules
const resolvedId = resolved.id.split('?')[0];

Expand Down
16 changes: 14 additions & 2 deletions packages/vite/src/index.ts
Expand Up @@ -17,7 +17,7 @@ import {
} from '@linaria/babel-preset';
import type { PluginOptions, Preprocessor } from '@linaria/babel-preset';
import { createCustomDebug } from '@linaria/logger';
import { getFileIdx } from '@linaria/utils';
import { getFileIdx, syncResolve } from '@linaria/utils';

type VitePluginOptions = {
include?: FilterPattern;
Expand Down Expand Up @@ -102,9 +102,21 @@ export default function linaria({

log('rollup-init', id);

const asyncResolve = async (what: string, importer: string) => {
const asyncResolve = async (
what: string,
importer: string,
stack: string[]
) => {
const resolved = await this.resolve(what, importer);
if (resolved) {
if (resolved.external) {
// If module is marked as external, Rollup will not resolve it,
// so we need to resolve it ourselves with default resolver
const resolvedId = syncResolve(what, importer, stack);
log('resolve', "✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
return resolvedId;
}

log('resolve', "✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
// Vite adds param like `?v=667939b3` to cached modules
const resolvedId = resolved.id.split('?')[0];
Expand Down

0 comments on commit 13f0b41

Please sign in to comment.