From 74a190516bbe8da39400aab9f6ac8b2a2883e3ae Mon Sep 17 00:00:00 2001 From: Gary Borton Date: Thu, 4 Oct 2018 12:08:11 -0700 Subject: [PATCH] [assets] Adds support for publicPath to enable serving assets from different locations. --- packages/metro-config/src/defaults/index.js | 1 + packages/metro/src/Assets.js | 3 ++- packages/metro/src/DeltaBundler/Transformer.js | 1 + packages/metro/src/JSTransformer/worker.js | 2 ++ packages/metro/src/assetTransformer.js | 1 + 5 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/metro-config/src/defaults/index.js b/packages/metro-config/src/defaults/index.js index 6c32cc04d6..a87563602f 100644 --- a/packages/metro-config/src/defaults/index.js +++ b/packages/metro-config/src/defaults/index.js @@ -79,6 +79,7 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({ postMinifyProcess: x => x, transformVariants: {default: {}}, workerPath: 'metro/src/DeltaBundler/Worker', + publicPath: '/assets', }, cacheStores: [ new FileStore({ diff --git a/packages/metro/src/Assets.js b/packages/metro/src/Assets.js index 5ab85a91cf..2aa89ad89f 100644 --- a/packages/metro/src/Assets.js +++ b/packages/metro/src/Assets.js @@ -177,8 +177,9 @@ async function getAssetData( localPath: string, assetDataPlugins: $ReadOnlyArray, platform: ?string = null, + publicPath: ?string = '/assets', ): Promise { - let assetUrlPath = path.join('/assets', path.dirname(localPath)); + let assetUrlPath = `${publicPath}/${path.dirname(localPath)}`; // On Windows, change backslashes to slashes to get proper URL path from file path. if (path.sep === '\\') { diff --git a/packages/metro/src/DeltaBundler/Transformer.js b/packages/metro/src/DeltaBundler/Transformer.js index 4ca88f0a6c..e1aa9200e9 100644 --- a/packages/metro/src/DeltaBundler/Transformer.js +++ b/packages/metro/src/DeltaBundler/Transformer.js @@ -48,6 +48,7 @@ class Transformer { enableBabelRCLookup: this._config.transformer.enableBabelRCLookup, minifierPath: this._config.transformer.minifierPath, optimizationSizeLimit: this._config.transformer.optimizationSizeLimit, + publicPath: this._config.transformer.publicPath, }, }; diff --git a/packages/metro/src/JSTransformer/worker.js b/packages/metro/src/JSTransformer/worker.js index aaf386319e..82bbcaf9b4 100644 --- a/packages/metro/src/JSTransformer/worker.js +++ b/packages/metro/src/JSTransformer/worker.js @@ -84,6 +84,7 @@ export type JsTransformerConfig = {| +enableBabelRCLookup: boolean, +minifierPath: string, +optimizationSizeLimit: number, + +publicPath: string, |}; export type CustomTransformOptions = {[string]: mixed, __proto__: null}; @@ -179,6 +180,7 @@ class JsTransformer { // is used by other tooling, and this would affect it. inlineRequires: false, projectRoot: this._projectRoot, + publicPath: this._config.publicPath, }, plugins: [], src: sourceCode, diff --git a/packages/metro/src/assetTransformer.js b/packages/metro/src/assetTransformer.js index 6892670ce3..bf5fd15f2a 100644 --- a/packages/metro/src/assetTransformer.js +++ b/packages/metro/src/assetTransformer.js @@ -36,6 +36,7 @@ async function transform( filename, assetDataPlugins, options.platform, + options.publicPath, ); return {