forked from external-repos/squoosh
Compare commits
1 Commits
dev
...
no-inline-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94157a3ef3 |
@@ -11,10 +11,23 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import { promisify } from 'util';
|
import { promisify } from 'util';
|
||||||
|
import { promises as fsp, readFileSync } from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { posix } from 'path';
|
import { posix } from 'path';
|
||||||
import glob from 'glob';
|
import glob from 'glob';
|
||||||
|
import postcss from 'postcss';
|
||||||
|
import postCSSNested from 'postcss-nested';
|
||||||
|
import postCSSUrl from 'postcss-url';
|
||||||
|
import postCSSModules from 'postcss-modules';
|
||||||
|
import postCSSSimpleVars from 'postcss-simple-vars';
|
||||||
|
import cssNano from 'cssnano';
|
||||||
|
import {
|
||||||
|
parse as parsePath,
|
||||||
|
resolve as resolvePath,
|
||||||
|
dirname,
|
||||||
|
normalize as nomalizePath,
|
||||||
|
sep as pathSep,
|
||||||
|
} from 'path';
|
||||||
|
|
||||||
const globP = promisify(glob);
|
const globP = promisify(glob);
|
||||||
|
|
||||||
@@ -30,26 +43,72 @@ export default function initialCssPlugin() {
|
|||||||
async load(id) {
|
async load(id) {
|
||||||
if (id !== initialCssModule) return;
|
if (id !== initialCssModule) return;
|
||||||
|
|
||||||
const matches = await globP('shared/prerendered-app/**/*.css', {
|
const matches = (
|
||||||
nodir: true,
|
await globP('shared/prerendered-app/**/*.css', {
|
||||||
cwd: path.join(process.cwd(), 'src'),
|
nodir: true,
|
||||||
});
|
cwd: path.join(process.cwd(), 'src'),
|
||||||
|
absolute: true,
|
||||||
|
})
|
||||||
|
).map((cssPath) =>
|
||||||
|
// glob() returns windows paths with a forward slash. Normalise it:
|
||||||
|
path.normalize(cssPath),
|
||||||
|
);
|
||||||
|
|
||||||
// Sort the matches so the parentmost items appear first.
|
// Sort the matches so the parentmost items appear first.
|
||||||
// This is a bit of a hack, but it means the util stuff appears in the cascade first.
|
// This is a bit of a hack, but it means the util stuff appears in the cascade first.
|
||||||
const sortedMatches = matches
|
const sortedMatches = matches
|
||||||
.map((match) => path.normalize(match).split(path.sep))
|
.map((match) => path.normalize(match).split(path.sep))
|
||||||
.sort((a, b) => a.length - b.length)
|
.sort((a, b) => a.length - b.length)
|
||||||
.map((match) => posix.join(...match));
|
.map((match) => '/' + posix.join(...match));
|
||||||
|
|
||||||
const imports = sortedMatches
|
const cssSources = await Promise.all(
|
||||||
.map((id, i) => `import css${i} from 'css:${id}';\n`)
|
sortedMatches.map(async (path) => {
|
||||||
.join('');
|
this.addWatchFile(path);
|
||||||
|
const file = await fsp.readFile(path);
|
||||||
|
|
||||||
return (
|
const cssResult = await postcss([
|
||||||
imports +
|
postCSSNested,
|
||||||
`export default ${sortedMatches.map((_, i) => `css${i}`).join(' + ')};`
|
postCSSSimpleVars(),
|
||||||
|
postCSSModules({
|
||||||
|
root: '',
|
||||||
|
}),
|
||||||
|
postCSSUrl({
|
||||||
|
url: ({ relativePath, url }) => {
|
||||||
|
if (/^((https?|data):|#)/.test(url)) return url;
|
||||||
|
const parsedPath = parsePath(relativePath);
|
||||||
|
const source = readFileSync(
|
||||||
|
resolvePath(dirname(path), relativePath),
|
||||||
|
);
|
||||||
|
const fileId = this.emitFile({
|
||||||
|
type: 'asset',
|
||||||
|
name: parsedPath.base,
|
||||||
|
source,
|
||||||
|
});
|
||||||
|
const hash = createHash('md5');
|
||||||
|
hash.update(source);
|
||||||
|
const md5 = hash.digest('hex');
|
||||||
|
hashToId.set(md5, fileId);
|
||||||
|
return `/fake/path/to/asset/${md5}/`;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
cssNano,
|
||||||
|
]).process(file, {
|
||||||
|
from: path,
|
||||||
|
});
|
||||||
|
|
||||||
|
return cssResult.css;
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const css = cssSources.join('\n');
|
||||||
|
|
||||||
|
const fileId = this.emitFile({
|
||||||
|
type: 'asset',
|
||||||
|
source: css,
|
||||||
|
name: 'initial.css',
|
||||||
|
});
|
||||||
|
|
||||||
|
return `export default import.meta.ROLLUP_FILE_URL_${fileId};`;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,11 +64,7 @@ const Index: FunctionalComponent<Props> = () => (
|
|||||||
<style
|
<style
|
||||||
dangerouslySetInnerHTML={{ __html: escapeStyleScriptContent(baseCss) }}
|
dangerouslySetInnerHTML={{ __html: escapeStyleScriptContent(baseCss) }}
|
||||||
/>
|
/>
|
||||||
<style
|
<link rel="stylesheet" href={initialCss} />
|
||||||
dangerouslySetInnerHTML={{
|
|
||||||
__html: escapeStyleScriptContent(initialCss),
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="app">
|
<div id="app">
|
||||||
|
|||||||
Reference in New Issue
Block a user