Skip to content

Commit

Permalink
fix: use hash as virtualModule key
Browse files Browse the repository at this point in the history
  • Loading branch information
10Derozan committed Sep 19, 2023
1 parent 4c08ba9 commit 7138e32
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
12 changes: 1 addition & 11 deletions packages/solutions/module-tools/src/builder/esbuild/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,6 @@ export const adapterPlugin = (compiler: ICompiler): Plugin => {
};
}

const { originalFilePath, query } = resolvePathAndQuery(args.path);

// external css virtual module
if (query.css_virtual) {
return {
path: originalFilePath,
pluginData: query,
external: true,
};
}

const { externals, sideEffects: userSideEffects } = config;
const regExternal = externals.filter(
(item): item is RegExp => !isString(item),
Expand Down Expand Up @@ -202,6 +191,7 @@ export const adapterPlugin = (compiler: ICompiler): Plugin => {
: compiler.node_resolve(id, dir, kind);
};

const { originalFilePath, query } = resolvePathAndQuery(args.path);
const isExternal = getIsExternal(originalFilePath);
const dir =
args.resolveDir ?? (args.importer ? dirname(args.importer) : root);
Expand Down
19 changes: 16 additions & 3 deletions packages/solutions/module-tools/src/builder/feature/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
* This plugin is used to redirectImport only in unbundle mode
* Taking from https://github.com/ice-lab/icepkg/blob/main/packages/pkg/src/plugins/transform/alias.ts
*/
import { isAbsolute, resolve, relative, join, dirname, extname } from 'path';
import {
isAbsolute,
resolve,
relative,
join,
dirname,
extname,
basename,
} from 'path';
import { js } from '@ast-grep/napi';
import MagicString from 'magic-string';
import { createMatchPath, loadConfig, MatchPath } from 'tsconfig-paths';
Expand Down Expand Up @@ -85,8 +93,13 @@ async function redirectImport(

if (query.css_virtual) {
// css module
const contents = compiler.virtualModule.get(originalFilePath)!;
const base = query.basename as string;
const replacedName = basename(
originalFilePath,
extname(originalFilePath),
).replace('.', '_');
const base = `${replacedName}.css`;
const key = query.hash as string;
const contents = compiler.virtualModule.get(key)!;
const fileName = join(outputDir, base);
compiler.emitAsset(fileName, {
type: 'asset',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ export const css = {
name,
hooks(compiler: ICompiler) {
compiler.hooks.load.tapPromise({ name }, async args => {
if (args.pluginData?.css_virtual) {
const contents = compiler.virtualModule.get(args.path)!;
return {
contents,
loader: 'css',
};
}
if (isStyleExt(args.path)) {
if (args.pluginData?.css_virtual) {
const key = args.pluginData.hash as string;
const contents = compiler.virtualModule.get(key)!;
return {
contents,
loader: 'css',
};
}
return {
contents: readFileSync(args.path),
loader: 'css',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { basename, extname } from 'path';
import postcss from 'postcss';
import { ICompiler } from '../../../types';
import { getHash } from '../../../utils';
import { getHash, normalizeSlashes } from '../../../utils';
import { postcssUrlPlugin } from './postcssUrlPlugin';

const cssLangs = `\\.(css|less|sass|scss)($|\\?)`;
Expand Down Expand Up @@ -67,15 +66,11 @@ export const postcssTransformer = async (
...processOptions,
});
if (Object.values(modules).length) {
// use hash to to set virtual module key
const hash = getHash(code, 'utf-8').substring(0, 8);
const replacedName = basename(entryPath, extname(entryPath)).replace(
'.',
'_',
);
const base = `${replacedName}.css`;
compilation.virtualModule.set(hash, code);
code = `import "${hash}?css_virtual&basename=${base}";export default ${JSON.stringify(
// add hash query for same path, let esbuild cache invalid
const normalizedPath = normalizeSlashes(entryPath);
const key = getHash(code, 'utf-8').slice(0, 5);
compilation.virtualModule.set(key, code);
code = `import "${normalizedPath}?css_virtual&hash=${key}";export default ${JSON.stringify(
modules,
)}`;
loader = 'js';
Expand Down

0 comments on commit 7138e32

Please sign in to comment.