Skip to content

Commit

Permalink
File builder url refactor (#1454)
Browse files Browse the repository at this point in the history
* Updated FileBuilder to use URLs instead of paths

* Updated scan-imports.ts

* Added missing coma

* Removed unused line
  • Loading branch information
high1 authored Nov 12, 2020
1 parent e8c7868 commit 213091b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require('fs');
const path = require('path');
const url = require('url');

const cwd = process.cwd();
const isTS = fs.existsSync(path.join(cwd, 'tsconfig.json'));
const isTS = fs.existsSync(url.pathToFileURL(path.join(process.cwd(), 'tsconfig.json')));

module.exports = {
mount: {
Expand Down
9 changes: 5 additions & 4 deletions snowpack/src/build/build-pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import url from 'url';
import {validatePluginLoadResult} from '../config';
import {logger} from '../logger';
import {
Expand Down Expand Up @@ -106,7 +107,7 @@ async function runPipelineLoadStep(

return {
[srcExt]: {
code: await readFile(srcPath),
code: await readFile(url.pathToFileURL(srcPath)),
},
};
}
Expand Down Expand Up @@ -246,13 +247,13 @@ export async function runPipelineCleanupStep({plugins}: SnowpackConfig) {

/** Core Snowpack file pipeline builder */
export async function buildFile(
srcPath: string,
srcURL: URL,
buildFileOptions: BuildFileOptions,
): Promise<SnowpackBuildMap> {
// Pass 1: Find the first plugin to load this file, and return the result
const loadResult = await runPipelineLoadStep(srcPath, buildFileOptions);
const loadResult = await runPipelineLoadStep(url.fileURLToPath(srcURL), buildFileOptions);
// Pass 2: Pass that result through every plugin transform() method.
const transformResult = await runPipelineTransformStep(loadResult, srcPath, buildFileOptions);
const transformResult = await runPipelineTransformStep(loadResult, url.fileURLToPath(srcURL), buildFileOptions);
// Return the final build result.
return transformResult;
}
31 changes: 15 additions & 16 deletions snowpack/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function getIsHmrEnabled(config: SnowpackConfig) {
}

function handleFileError(err: Error, builder: FileBuilder) {
logger.error(`✘ ${builder.filepath}\n ${err.stack ? err.stack : err.message}`);
logger.error(`✘ ${builder.fileURL}\n ${err.stack ? err.stack : err.message}`);
process.exit(1);
}

Expand Down Expand Up @@ -95,26 +95,26 @@ class FileBuilder {
filesToResolve: Record<string, SnowpackSourceFile> = {};
filesToProxy: string[] = [];

readonly filepath: string;
readonly fileURL: URL;
readonly mountEntry: MountEntry;
readonly outDir: string;
readonly config: SnowpackConfig;
readonly lockfile: ImportMap | null;

constructor({
filepath,
fileURL,
mountEntry,
outDir,
config,
lockfile,
}: {
filepath: string;
fileURL: URL;
mountEntry: MountEntry;
outDir: string;
config: SnowpackConfig;
lockfile: ImportMap | null;
}) {
this.filepath = filepath;
this.fileURL = fileURL;
this.mountEntry = mountEntry;
this.outDir = outDir;
this.config = config;
Expand All @@ -124,10 +124,10 @@ class FileBuilder {
async buildFile() {
this.filesToResolve = {};
const isSSR = this.config.experiments.ssr;
const srcExt = path.extname(this.filepath);
const srcExt = path.extname(url.fileURLToPath(this.fileURL));
const fileOutput = this.mountEntry.static
? {[srcExt]: {code: await readFile(this.filepath)}}
: await buildFile(this.filepath, {
? {[srcExt]: {code: await readFile(this.fileURL)}}
: await buildFile(this.fileURL, {
plugins: this.config.plugins,
isDev: false,
isSSR,
Expand All @@ -140,7 +140,7 @@ class FileBuilder {
if (!code) {
continue;
}
const outFilename = replaceExt(path.basename(this.filepath), srcExt, fileExt);
const outFilename = replaceExt(path.basename(url.fileURLToPath(this.fileURL)), srcExt, fileExt);
const outLoc = path.join(this.outDir, outFilename);
const sourceMappingURL = outFilename + '.map';
if (this.mountEntry.resolve && typeof code === 'string') {
Expand All @@ -151,7 +151,7 @@ class FileBuilder {
baseExt: fileExt,
expandedExt: fileExt,
contents: code,
locOnDisk: this.filepath,
locOnDisk: url.fileURLToPath(this.fileURL),
};
break;
}
Expand All @@ -168,7 +168,7 @@ class FileBuilder {
baseExt: fileExt,
expandedExt: fileExt,
contents: code,
locOnDisk: this.filepath,
locOnDisk: url.fileURLToPath(this.fileURL),
};
break;
}
Expand All @@ -186,7 +186,7 @@ class FileBuilder {
baseExt: fileExt,
expandedExt: fileExt,
contents: code,
locOnDisk: this.filepath,
locOnDisk: url.fileURLToPath(this.fileURL),
};
break;
}
Expand Down Expand Up @@ -392,7 +392,7 @@ export async function command(commandOptions: CommandOptions) {
!installedFileLoc.endsWith('import-map.json') &&
path.extname(installedFileLoc) !== '.js'
) {
const proxiedCode = await readFile(installedFileLoc);
const proxiedCode = await readFile(url.pathToFileURL(installedFileLoc));
const importProxyFileLoc = installedFileLoc + '.proxy.js';
const proxiedUrl = installedFileLoc.substr(buildDirectoryLoc.length).replace(/\\/g, '/');
const proxyCode = await wrapImportProxy({
Expand Down Expand Up @@ -422,7 +422,7 @@ export async function command(commandOptions: CommandOptions) {
const finalDestLoc = path.join(buildDirectoryLoc, finalUrl);
const outDir = path.dirname(finalDestLoc);
const buildPipelineFile = new FileBuilder({
filepath: fileLoc,
fileURL: url.pathToFileURL(fileLoc),
mountEntry,
outDir,
config,
Expand Down Expand Up @@ -530,9 +530,8 @@ export async function command(commandOptions: CommandOptions) {
const finalUrl = getUrlForFileMount({fileLoc, mountKey, mountEntry, config})!;
const finalDest = path.join(buildDirectoryLoc, finalUrl);
const outDir = path.dirname(finalDest);

const changedPipelineFile = new FileBuilder({
filepath: fileLoc,
fileURL: url.pathToFileURL(fileLoc),
mountEntry,
outDir,
config,
Expand Down
5 changes: 3 additions & 2 deletions snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
return existingBuilderPromise;
}
const fileBuilderPromise = (async () => {
const builtFileOutput = await _buildFile(fileLoc, {
const builtFileOutput = await _buildFile(url.pathToFileURL(fileLoc), {
plugins: config.plugins,
isDev: true,
isSSR,
Expand Down Expand Up @@ -942,7 +942,8 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
}

// 2. Load the file from disk. We'll need it to check the cold cache or build from scratch.
const fileContents = await readFile(fileLoc);
const fileContents = await readFile(url.pathToFileURL(fileLoc));

// 3. Send static files directly, since they were already build & resolved at install time.
if (!isProxyModule && isStatic) {
// If no resolution needed, just send the file directly.
Expand Down
4 changes: 3 additions & 1 deletion snowpack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {validate, ValidatorResult} from 'jsonschema';
import os from 'os';
import path from 'path';
import yargs from 'yargs-parser';
import url from 'url';

import {logger} from './logger';
import {esbuildPlugin} from './plugins/plugin-esbuild';
import {
Expand Down Expand Up @@ -314,7 +316,7 @@ function loadPlugins(
plugin.load = async (options: PluginLoadOptions) => {
const result = await build({
...options,
contents: await readFile(options.filePath),
contents: await readFile(url.pathToFileURL(options.filePath)),
}).catch((err) => {
logger.error(
`[${plugin.name}] There was a problem running this older plugin. Please update the plugin to the latest version.`,
Expand Down
3 changes: 2 additions & 1 deletion snowpack/src/scan-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {InstallTarget} from 'esinstall';
import glob from 'glob';
import path from 'path';
import stripComments from 'strip-comments';
import url from 'url';
import {logger} from './logger';
import {SnowpackConfig, SnowpackSourceFile} from './types/snowpack';
import {
Expand Down Expand Up @@ -259,7 +260,7 @@ export async function scanImports(
baseExt,
expandedExt,
locOnDisk: filePath,
contents: await readFile(filePath),
contents: await readFile(url.pathToFileURL(filePath)),
};
});
const loadedFiles: (SnowpackSourceFile | null)[] = await Promise.all(
Expand Down
2 changes: 1 addition & 1 deletion snowpack/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const CSS_REGEX = /@import\s*['"](.*?)['"];/gs;
export const SVELTE_VUE_REGEX = /(<script[^>]*>)(.*?)<\/script>/gims;

/** Read file from disk; return a string if it’s a code file */
export async function readFile(filepath: string): Promise<string | Buffer> {
export async function readFile(filepath: URL): Promise<string | Buffer> {
const data = await fs.promises.readFile(filepath);
const isBinary = await isBinaryFile(data);
return isBinary ? data : data.toString('utf-8');
Expand Down

1 comment on commit 213091b

@vercel
Copy link

@vercel vercel bot commented on 213091b Nov 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.