MCPcopy Index your code
hub / github.com/FredKSchott/snowpack / optimize

Function optimize

plugins/plugin-optimize/plugin.js:110–181  ·  view source on GitHub ↗
({buildDirectory})

Source from the content-addressed store, hash-verified

108 return {
109 name: '@snowpack/plugin-optimize',
110 async optimize({buildDirectory}) {
111 // 0. setup
112 await init;
113 let generatedFiles = {};
114
115 // 1. index files
116 const allFiles = glob
117 .sync('**/*', {
118 cwd: buildDirectory,
119 ignore: [`${config.buildOptions.metaUrlPath}/*`],
120 nodir: true,
121 })
122 .map((file) => path.join(buildDirectory, file)); // resolve to root dir
123
124 // 2. scan imports
125 const manifest = await scanHTML(
126 allFiles.filter((f) => path.extname(f) === '.html'),
127 buildDirectory,
128 );
129 let preloadCSS = false; // only bother preloading CSS if option is enabled AND there are .css.proxy.js files
130 if (options.preloadCSS) {
131 for (f in manifest) {
132 if (
133 manifest[f].js &&
134 manifest[f].js.findIndex((js) => js.endsWith('.css.proxy.js')) !== -1
135 ) {
136 preloadCSS = true;
137 break;
138 }
139 }
140 }
141
142 // 3. optimize all files in parallel
143 const parallelWorkQueue = new PQueue({concurrency: CONCURRENT_WORKERS});
144 allFiles
145 .filter(
146 (file) => (preloadCSS ? !file.endsWith('.css.proxy.js') : true), // if preloading CSS, don’t optimize .css.proxy.js files
147 )
148 .forEach((file) => {
149 parallelWorkQueue.add(() =>
150 optimizeFile({
151 file,
152 preloadCSS,
153 rootDir: buildDirectory,
154 target: options.target,
155 }).catch((err) => {
156 log(`Error: ${file} ${err.toString()}`, 'error');
157 }),
158 );
159 });
160 await parallelWorkQueue.onIdle();
161
162 // 5. build CSS file
163 if (preloadCSS) {
164 const combinedCSS = buildImportCSS(manifest, options.minifyCSS);
165 const outputCSS = path.join(buildDirectory, options.preloadCSSFileName);
166 await mkdirp(path.dirname(outputCSS));
167 fs.writeFileSync(outputCSS, combinedCSS, 'utf-8');

Callers

nothing calls this directly

Calls 6

scanHTMLFunction · 0.85
optimizeFileFunction · 0.85
buildImportCSSFunction · 0.85
formatManifestFunction · 0.85
addMethod · 0.80
logFunction · 0.70

Tested by

no test coverage detected