Skip to content

Commit

Permalink
fix import proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Feb 24, 2021
1 parent 92b5659 commit f48f7c4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion snowpack/src/build/file-builder.ts
Expand Up @@ -319,8 +319,9 @@ export class FileBuilder {
return this.resolvedOutput[type].map;
}

async getProxy(url: string, type: string) {
async getProxy(_url: string, type: string) {
const code = this.resolvedOutput[type].code;
const url = path.posix.join(this.config.buildOptions.baseUrl, _url);
return await wrapImportProxy({url, code, hmr: this.isHMR, config: this.config});
}

Expand Down
6 changes: 5 additions & 1 deletion snowpack/src/build/file-urls.ts
Expand Up @@ -74,7 +74,11 @@ export function getUrlsForFile(fileLoc: string, config: SnowpackConfig): string[
if (!mountEntryResult) {
const builtEntrypointUrls = getBuiltFileUrls(fileLoc, config);
return builtEntrypointUrls.map((u) =>
path.posix.join(config.buildOptions.metaUrlPath, 'link', slash(path.relative(config.root, u))),
path.posix.join(
config.buildOptions.metaUrlPath,
'link',
slash(path.relative(config.root, u)),
),
);
}
const [mountKey, mountEntry] = mountEntryResult;
Expand Down
3 changes: 2 additions & 1 deletion snowpack/src/commands/build.ts
Expand Up @@ -181,6 +181,7 @@ export async function build(commandOptions: CommandOptions): Promise<SnowpackBui

let optimizedImportMap: undefined | ImportMap;
if (!config.buildOptions.watch) {
logger.info(colors.yellow('! optimizing dependencies...'));
const packagesStart = performance.now();
const installDest = path.join(buildDirectoryLoc, config.buildOptions.metaUrlPath, 'pkg');
const installResult = await installOptimizedDependencies(
Expand All @@ -190,7 +191,7 @@ export async function build(commandOptions: CommandOptions): Promise<SnowpackBui
);
const packagesEnd = performance.now();
logger.info(
`${colors.green('✔')} packages optimized. ${colors.dim(
`${colors.green('✔')} dependencies ready. ${colors.dim(
`[${((packagesEnd - packagesStart) / 1000).toFixed(2)}s]`,
)}`,
);
Expand Down
6 changes: 6 additions & 0 deletions test/build/base-url/base-url.test.js
Expand Up @@ -24,6 +24,12 @@ describe('buildOptions.baseUrl', () => {
expect($('script').attr('src').startsWith('/static/')).toBe(true);
});

it('import proxies works', () => {
expect(files['/_dist_/logo.png.proxy.js']).toEqual(
expect.stringContaining(`export default "/static/_dist_/logo.png";`),
);
});

it('import.meta.env works', () => {
// env is present in index.js
expect(files['/index.js']).toEqual(
Expand Down
5 changes: 4 additions & 1 deletion test/build/base-url/src/index.js
@@ -1,10 +1,13 @@
// src/index.js - Test import URLs from the "/_dist_/" directory

import {flatten} from 'array-flatten';
import logo from './logo.png';

export default function doNothing() {
// I do nothing 🎉
}

// Triggers a snowpack meta import URL
console.log(import.meta.env)
console.log(import.meta.env)
// Test import proxies
console.log(logo)
Binary file added test/build/base-url/src/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f48f7c4

Please sign in to comment.