-
Notifications
You must be signed in to change notification settings - Fork 413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(webpack): add cacheProvider for Linaria v3 #889
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export interface ICache { | ||
get: (key: string) => Promise<string>; | ||
set: (key: string, value: string) => Promise<void>; | ||
} | ||
|
||
// memory cache, which is the default cache implementation in Linaria | ||
|
||
class MemoryCache implements ICache { | ||
private cache: Map<string, string> = new Map(); | ||
|
||
public get(key: string): Promise<string> { | ||
return Promise.resolve(this.cache.get(key) ?? ''); | ||
} | ||
|
||
public set(key: string, value: string): Promise<void> { | ||
this.cache.set(key, value); | ||
return Promise.resolve(); | ||
} | ||
} | ||
|
||
export const memoryCache = new MemoryCache(); | ||
|
||
/** | ||
* return cache instance from `options.cacheProvider` | ||
* @param cacheProvider string | ICache | undefined | ||
* @returns ICache instance | ||
*/ | ||
export const getCacheInstance = async ( | ||
cacheProvider: string | ICache | undefined | ||
): Promise<ICache> => { | ||
if (!cacheProvider) { | ||
return memoryCache; | ||
} | ||
if (typeof cacheProvider === 'string') { | ||
return require(cacheProvider); | ||
} | ||
if ( | ||
typeof cacheProvider === 'object' && | ||
'get' in cacheProvider && | ||
'set' in cacheProvider | ||
) { | ||
return cacheProvider; | ||
} | ||
throw new Error(`Invalid cache provider: ${cacheProvider}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
const cssLookup = new Map<string, string>(); | ||
import loaderUtils from 'loader-utils'; | ||
import { getCacheInstance } from './cache'; | ||
|
||
export const addFile = (id: string, content: string) => { | ||
cssLookup.set(id, content); | ||
}; | ||
type LoaderContext = Parameters<typeof loaderUtils.getOptions>[0]; | ||
|
||
export default function outputCssLoader(this: { resourcePath: string }) { | ||
return cssLookup.get(this.resourcePath) ?? ''; | ||
export default function outputCssLoader(this: LoaderContext) { | ||
this.async(); | ||
const { cacheProvider } = loaderUtils.getOptions(this) || {}; | ||
getCacheInstance(cacheProvider) | ||
.then((cacheInstance) => cacheInstance.get(this.resourcePath)) | ||
.then((result) => this.callback(null, result)) | ||
.catch((err: Error) => this.callback(err)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export interface ICache { | ||
get: (key: string) => Promise<string>; | ||
set: (key: string, value: string) => Promise<void>; | ||
} | ||
|
||
// memory cache, which is the default cache implementation in Linaria | ||
|
||
class MemoryCache implements ICache { | ||
private cache: Map<string, string> = new Map(); | ||
|
||
public get(key: string): Promise<string> { | ||
return Promise.resolve(this.cache.get(key) ?? ''); | ||
} | ||
|
||
public set(key: string, value: string): Promise<void> { | ||
this.cache.set(key, value); | ||
return Promise.resolve(); | ||
} | ||
} | ||
|
||
export const memoryCache = new MemoryCache(); | ||
|
||
/** | ||
* return cache instance from `options.cacheProvider` | ||
* @param cacheProvider string | ICache | undefined | ||
* @returns ICache instance | ||
*/ | ||
export const getCacheInstance = async ( | ||
cacheProvider: string | ICache | undefined | ||
): Promise<ICache> => { | ||
if (!cacheProvider) { | ||
return memoryCache; | ||
} | ||
if (typeof cacheProvider === 'string') { | ||
return require(cacheProvider); | ||
} | ||
if ( | ||
typeof cacheProvider === 'object' && | ||
'get' in cacheProvider && | ||
'set' in cacheProvider | ||
) { | ||
return cacheProvider; | ||
} | ||
throw new Error(`Invalid cache provider: ${cacheProvider}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,9 @@ import path from 'path'; | |
import loaderUtils from 'loader-utils'; | ||
import enhancedResolve from 'enhanced-resolve'; | ||
import type { RawSourceMap } from 'source-map'; | ||
import { EvalCache, Module, transform } from '@linaria/babel-preset'; | ||
import { EvalCache, Module, Result, transform } from '@linaria/babel-preset'; | ||
import { debug, notify } from '@linaria/logger'; | ||
import { addFile } from './outputCssLoader'; | ||
import { getCacheInstance } from './cache'; | ||
|
||
const outputCssLoader = require.resolve('./outputCssLoader'); | ||
|
||
|
@@ -19,6 +19,9 @@ export default function webpack5Loader( | |
content: string, | ||
inputSourceMap: RawSourceMap | null | ||
) { | ||
// tell Webpack this loader is async | ||
this.async(); | ||
|
||
debug('loader', this.resourcePath); | ||
|
||
EvalCache.clearForFile(this.resourcePath); | ||
|
@@ -32,6 +35,7 @@ export default function webpack5Loader( | |
sourceMap = undefined, | ||
preprocessor = undefined, | ||
extension = '.linaria.css', | ||
cacheProvider, | ||
resolveOptions = {}, | ||
...rest | ||
} = this.getOptions() || {}; | ||
|
@@ -80,7 +84,7 @@ export default function webpack5Loader( | |
}); | ||
} | ||
|
||
let result; | ||
let result: Result; | ||
|
||
const originalResolveFilename = Module._resolveFilename; | ||
|
||
|
@@ -130,16 +134,21 @@ export default function webpack5Loader( | |
}); | ||
} | ||
|
||
addFile(this.resourcePath, cssText); | ||
|
||
const request = `${outputFileName}!=!${outputCssLoader}!${this.resourcePath}`; | ||
const stringifiedRequest = loaderUtils.stringifyRequest(this, request); | ||
|
||
this.callback( | ||
null, | ||
`${result.code}\n\nrequire(${stringifiedRequest});`, | ||
result.sourceMap ?? undefined | ||
); | ||
getCacheInstance(cacheProvider) | ||
.then((cacheInstance) => cacheInstance.set(this.resourcePath, cssText)) | ||
.then(() => { | ||
const request = `${outputFileName}!=!${outputCssLoader}?cacheProvider=${encodeURIComponent( | ||
cacheProvider ?? '' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also fixed this line in #890 |
||
)}!${this.resourcePath}`; | ||
const stringifiedRequest = loaderUtils.stringifyRequest(this, request); | ||
|
||
return this.callback( | ||
null, | ||
`${result.code}\n\nrequire(${stringifiedRequest});`, | ||
result.sourceMap ?? undefined | ||
); | ||
}) | ||
.catch((err: Error) => this.callback(err)); | ||
return; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
const cssLookup = new Map<string, string>(); | ||
import webpack from 'webpack'; | ||
import { getCacheInstance, ICache } from './cache'; | ||
|
||
export const addFile = (id: string, content: string) => { | ||
cssLookup.set(id, content); | ||
}; | ||
|
||
export default function outputCssLoader(this: { resourcePath: string }) { | ||
return cssLookup.get(this.resourcePath) ?? ''; | ||
export default function outputCssLoader( | ||
this: webpack.LoaderContext<{ cacheProvider: string | ICache | undefined }> | ||
) { | ||
this.async(); | ||
const { cacheProvider } = this.getOptions(); | ||
getCacheInstance(cacheProvider) | ||
.then((cacheInstance) => cacheInstance.get(this.resourcePath)) | ||
.then((result) => this.callback(null, result)) | ||
.catch((err: Error) => this.callback(err)); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also fixed this line in #890