Skip to content

Commit

Permalink
refactor snowpack internals
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Feb 22, 2021
1 parent c0501f7 commit 2c21a9b
Show file tree
Hide file tree
Showing 59 changed files with 1,956 additions and 2,382 deletions.
4 changes: 2 additions & 2 deletions esinstall/src/index.ts
Expand Up @@ -306,7 +306,7 @@ ${colors.dim(
input: installEntrypoints,
context: userDefinedRollup.context,
external: (id) => external.some((packageName) => isImportOfPackage(id, packageName)),
treeshake: {moduleSideEffects: 'no-external'},
treeshake: {moduleSideEffects: true},
plugins: [
rollupPluginAlias({
entries: [
Expand Down Expand Up @@ -346,7 +346,7 @@ ${colors.dim(
rollupPluginReplace(generateEnvReplacements(env)),
rollupPluginCommonjs({
extensions: ['.js', '.cjs'],
esmExternals: externalEsm,
esmExternals: (id) => externalEsm.some((packageName) => isImportOfPackage(id, packageName)),
requireReturnsDefault: 'auto',
} as RollupCommonJSOptions),
rollupPluginWrapInstallTargets(!!isTreeshake, autoDetectNamedExports, installTargets, logger),
Expand Down
35 changes: 20 additions & 15 deletions plugins/plugin-svelte/plugin.js
Expand Up @@ -13,22 +13,27 @@ module.exports = function plugin(snowpackConfig, pluginOptions = {}) {
const isDev = process.env.NODE_ENV !== 'production';
const useSourceMaps =
snowpackConfig.buildOptions.sourcemap || snowpackConfig.buildOptions.sourceMaps;
// Old Snowpack versions wouldn't build dependencies. Starting in v3.1, Snowpack's build pipeline
// is run on all files, including npm package files. The rollup plugin is no longer needed.
const needsRollupPlugin = typeof snowpackConfig.buildOptions.resolveProxyImports === 'undefined';

// Support importing Svelte files when you install dependencies.
const packageOptions = snowpackConfig.packageOptions || snowpackConfig.installOptions;
if (packageOptions.source === 'local') {
packageOptions.rollup = packageOptions.rollup || {};
packageOptions.rollup.plugins = packageOptions.rollup.plugins || [];
packageOptions.rollup.plugins.push(
svelteRollupPlugin({
include: /\.svelte$/,
compilerOptions: {dev: isDev},
// Snowpack wraps JS-imported CSS in a JS wrapper, so use
// Svelte's own first-class `emitCss: false` here.
// TODO: Remove once Snowpack adds first-class CSS import support in deps.
emitCss: false,
}),
);
if (needsRollupPlugin) {
packageOptions.rollup = packageOptions.rollup || {};
packageOptions.rollup.plugins = packageOptions.rollup.plugins || [];
packageOptions.rollup.plugins.push(
svelteRollupPlugin({
include: /\.svelte$/,
compilerOptions: {dev: isDev},
// Snowpack wraps JS-imported CSS in a JS wrapper, so use
// Svelte's own first-class `emitCss: false` here.
// TODO: Remove once Snowpack adds first-class CSS import support in deps.
emitCss: false,
}),
);
}
// Support importing sharable Svelte components.
packageOptions.packageLookupFields.push('svelte');
}
Expand Down Expand Up @@ -97,7 +102,7 @@ module.exports = function plugin(snowpackConfig, pluginOptions = {}) {
'svelte-hmr/runtime/hot-api-esm.js',
'svelte-hmr/runtime/proxy-adapter-dom.js',
],
async load({filePath, isHmrEnabled, isSSR}) {
async load({filePath, isHmrEnabled, isSSR, isPackage}) {
let codeToCompile = await fs.promises.readFile(filePath, 'utf-8');
// PRE-PROCESS
if (preprocessOptions !== false) {
Expand All @@ -110,9 +115,9 @@ module.exports = function plugin(snowpackConfig, pluginOptions = {}) {

const finalCompileOptions = {
generate: isSSR ? 'ssr' : 'dom',
css: false,
css: isPackage ? true : false,
...compilerOptions, // Note(drew) should take precedence over generate above
dev: isDev,
dev: isHmrEnabled || isDev,
outputFilename: filePath,
filename: filePath,
};
Expand Down
1 change: 0 additions & 1 deletion plugins/plugin-svelte/test/plugin.test.js
Expand Up @@ -15,7 +15,6 @@ describe('@snowpack/plugin-svelte (mocked)', () => {
buildOptions: {sourcemap: false},
packageOptions: {
source: 'local',
rollup: {plugins: []},
packageLookupFields: [],
},
};
Expand Down
4 changes: 4 additions & 0 deletions plugins/web-test-runner-plugin/plugin.js
Expand Up @@ -19,6 +19,10 @@ To Resolve:
packageOptions: {external: ['/__web-dev-server__web-socket.js']},
devOptions: {open: 'none', output: 'stream', hmr: false},
});
// npm packages should be installed/prepared ahead of time.
console.log('[snowpack] preparing npm packages...');
await snowpack.preparePackages({config, lockfile: null});
console.log('[snowpack] starting server...');
fileWatcher.add(Object.keys(config.mount));
server = await snowpack.startServer({
config,
Expand Down
27 changes: 27 additions & 0 deletions snowpack/LICENSE
Expand Up @@ -19,3 +19,30 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


"""

This license applies to parts of the src/dev.ts file originating from the
https://github.com/lukejacksonn/servor repository:

MIT License
Copyright (c) 2019 Luke Jackson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 0 additions & 1 deletion snowpack/package.json
Expand Up @@ -56,7 +56,6 @@
"devDependencies": {
"@types/cheerio": "0.22.22",
"bufferutil": "^4.0.2",
"cacache": "^15.0.0",
"cachedir": "^2.3.0",
"cheerio": "1.0.0-rc.3",
"chokidar": "^3.4.0",
Expand Down
24 changes: 11 additions & 13 deletions snowpack/src/build/build-import-proxy.ts
Expand Up @@ -6,8 +6,8 @@ import {SnowpackConfig} from '../types';
import {appendHtmlToHead, hasExtension, HMR_CLIENT_CODE, HMR_OVERLAY_CODE} from '../util';
import {generateSRI} from './import-sri';

const SRI_CLIENT_HMR_SNOWPACK = generateSRI(Buffer.from(HMR_CLIENT_CODE));
const SRI_ERROR_HMR_SNOWPACK = generateSRI(Buffer.from(HMR_OVERLAY_CODE));
export const SRI_CLIENT_HMR_SNOWPACK = generateSRI(Buffer.from(HMR_CLIENT_CODE));
export const SRI_ERROR_HMR_SNOWPACK = generateSRI(Buffer.from(HMR_OVERLAY_CODE));

const importMetaRegex = /import\s*\.\s*meta/;

Expand Down Expand Up @@ -235,18 +235,16 @@ export async function wrapImportProxy({
hmr: boolean;
config: SnowpackConfig;
}) {
if (typeof code === 'string') {
if (hasExtension(url, '.json')) {
return generateJsonImportProxy({code, hmr, config});
}
if (hasExtension(url, '.json')) {
return generateJsonImportProxy({code: code.toString(), hmr, config});
}

if (hasExtension(url, '.css')) {
// if proxying a CSS file, remove its source map (the path no longer applies)
const sanitized = code.replace(/\/\*#\s*sourceMappingURL=[^/]+\//gm, '');
return hasExtension(url, '.module.css')
? generateCssModuleImportProxy({url, code: sanitized, hmr, config})
: generateCssImportProxy({code: sanitized, hmr, config});
}
if (hasExtension(url, '.css')) {
// if proxying a CSS file, remove its source map (the path no longer applies)
const sanitized = code.toString().replace(/\/\*#\s*sourceMappingURL=[^/]+\//gm, '');
return hasExtension(url, '.module.css')
? generateCssModuleImportProxy({url, code: sanitized, hmr, config})
: generateCssImportProxy({code: sanitized, hmr, config});
}

return generateDefaultImportProxy(url);
Expand Down
42 changes: 12 additions & 30 deletions snowpack/src/build/build-pipeline.ts
@@ -1,42 +1,19 @@
import path from 'path';
import {RawSourceMap, SourceMapConsumer, SourceMapGenerator} from 'source-map';
import url from 'url';
import {validatePluginLoadResult} from '../config';
import {logger} from '../logger';
import {SnowpackBuildMap, SnowpackConfig, SnowpackPlugin, PluginTransformResult} from '../types';
import {getExtension, readFile, removeExtension, replaceExtension} from '../util';
import {SourceMapConsumer, SourceMapGenerator, RawSourceMap} from 'source-map';
import {PluginTransformResult, SnowpackBuildMap, SnowpackConfig} from '../types';
import {getExtension, readFile, removeExtension} from '../util';

export interface BuildFileOptions {
isDev: boolean;
isSSR: boolean;
isPackage: boolean;
isHmrEnabled: boolean;
config: SnowpackConfig;
}

export function getInputsFromOutput(fileLoc: string, plugins: SnowpackPlugin[]) {
const srcFile = removeExtension(fileLoc, '.map'); // if this is a .map file, try loading source

const potentialInputs = new Set([srcFile]);
for (const plugin of plugins) {
if (!plugin.resolve) {
continue;
}
const isHubExt = plugin.resolve.output.length > 1;
const matchedOutputExt = plugin.resolve.output.find((ext) => srcFile.endsWith(ext));
if (!matchedOutputExt) {
continue;
}
plugin.resolve.input.forEach((inputExt) =>
potentialInputs.add(
isHubExt
? removeExtension(srcFile, matchedOutputExt)
: replaceExtension(srcFile, matchedOutputExt, inputExt),
),
);
}
return Array.from(potentialInputs);
}

/**
* Build Plugin First Pass: If a plugin defines a
* `resolve` object, check it against the current
Expand All @@ -48,7 +25,7 @@ export function getInputsFromOutput(fileLoc: string, plugins: SnowpackPlugin[])
*/
async function runPipelineLoadStep(
srcPath: string,
{isDev, isSSR, isHmrEnabled, config}: BuildFileOptions,
{isDev, isSSR, isPackage, isHmrEnabled, config}: BuildFileOptions,
): Promise<SnowpackBuildMap> {
const srcExt = getExtension(srcPath);
for (const step of config.plugins) {
Expand All @@ -66,6 +43,7 @@ async function runPipelineLoadStep(
filePath: srcPath,
isDev,
isSSR,
isPackage,
isHmrEnabled,
});
logger.debug(`✔ load() success [${debugPath}]`, {name: step.name});
Expand Down Expand Up @@ -143,7 +121,7 @@ async function composeSourceMaps(
async function runPipelineTransformStep(
output: SnowpackBuildMap,
srcPath: string,
{isDev, isHmrEnabled, isSSR, config}: BuildFileOptions,
{isDev, isHmrEnabled, isPackage, isSSR, config}: BuildFileOptions,
): Promise<SnowpackBuildMap> {
const rootFilePath = removeExtension(srcPath, getExtension(srcPath));
const rootFileName = path.basename(rootFilePath);
Expand All @@ -163,6 +141,7 @@ async function runPipelineTransformStep(
const result = await step.transform({
contents: code,
isDev,
isPackage,
fileExt: destExt,
id: filePath,
// @ts-ignore: Deprecated
Expand Down Expand Up @@ -206,7 +185,10 @@ async function runPipelineTransformStep(
return output;
}

export async function runPipelineOptimizeStep(buildDirectory: string, {config}: BuildFileOptions) {
export async function runPipelineOptimizeStep(
buildDirectory: string,
{config}: {config: SnowpackConfig},
) {
for (const step of config.plugins) {
if (!step.optimize) {
continue;
Expand Down

0 comments on commit 2c21a9b

Please sign in to comment.