Skip to content

Commit 578d625

Browse files
committed
Pack for local deploy & address potential name collisions in express
1 parent c549105 commit 578d625

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"ssr"
2222
],
2323
"scripts": {
24-
"build": "rm -rf dist && tsc && ts-node ./tools/build.ts",
24+
"build": "rm -rf dist && tsc && ts-node ./tools/build.ts && npm pack .",
2525
"test": "echo \"Error: no test specified\" && exit 1",
2626
"e2e:build-next": "rm -rf e2e && npx create-next-app e2e --use-npm --ts",
2727
"e2e:build-nuxt": "rm -rf e2e && npx create-nuxt-app e2e --answers '$(cat ./tools/create-nuxt-answers.json)'",

src/frameworks/express/index.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
import { promises as fsPromises } from 'fs';
16-
import { dirname, join } from 'path';
16+
import { join } from 'path';
1717

1818
import { DeployConfig, PathFactory, exec, spawn } from '../../utils';
1919

@@ -72,7 +72,7 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
7272
let bootstrapScript = '';
7373
if (serverRenderMethod) {
7474
let stack = serverRenderMethod.slice();
75-
const entry = `./${packageJson.main || 'index.js'}`;
75+
const entry = packageJson.name;
7676
if (stack.shift() === 'require') {
7777
bootstrapScript += `const bootstrap = Promise.resolve(require('${entry}'))`;
7878
} else {
@@ -105,19 +105,11 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
105105
}
106106

107107
if (serverRenderMethod) {
108-
const npmPackResults = JSON.parse(await exec(`npm pack ${getProjectPath()} --dry-run --json`) as string);
109-
110-
await Promise.all(
111-
// TODO types
112-
npmPackResults.
113-
find(({ name }: any) => name === packageJson.name ).
114-
files.
115-
map(({ path }: any) =>
116-
mkdir(dirname(deployPath('functions', path)), { recursive: true }).then(() =>
117-
copyFile(getProjectPath(path), deployPath('functions', path))
118-
)
119-
)
120-
);
108+
const npmPackResults = JSON.parse(await exec(`npm pack ${getProjectPath()} --json`, { cwd: deployPath('functions')}) as string);
109+
const matchingPackResult = npmPackResults.find((it: any) => it.name === packageJson.name);
110+
const { filename } = matchingPackResult;
111+
packageJson.dependencies ||= {};
112+
packageJson.dependencies[packageJson.name] = `file:${filename}`;
121113
}
122114

123115
return { usingCloudFunctions: !!serverRenderMethod, framework: 'express', rewrites: [], redirects: [], headers: [], packageJson, bootstrapScript };

src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { join } from 'path';
15+
import { basename, join } from 'path';
1616
import { exit } from 'process';
1717
import { promises as fs } from 'fs';
1818

@@ -123,7 +123,14 @@ export const prepare = async (targetNames: string[], context: any, options: any)
123123
packageJson.main = 'server.js';
124124
delete packageJson.devDependencies;
125125
packageJson.dependencies ||= {};
126-
packageJson.dependencies['firebase-frameworks'] = FIREBASE_FRAMEWORKS_VERSION;
126+
if (FIREBASE_FRAMEWORKS_VERSION.startsWith('file:')) {
127+
const file = FIREBASE_FRAMEWORKS_VERSION.split(':')[1];
128+
const filename = basename(file);
129+
await copyFile(file, join(functionsDist, filename));
130+
packageJson.dependencies['firebase-frameworks'] = `file:${filename}`;
131+
} else {
132+
packageJson.dependencies['firebase-frameworks'] = FIREBASE_FRAMEWORKS_VERSION;
133+
}
127134
// TODO test these with semver, error if already set out of range
128135
packageJson.dependencies['firebase-admin'] ||= FIREBASE_ADMIN_VERSION;
129136
packageJson.dependencies['firebase-functions'] ||= FIREBASE_FUNCTIONS_VERSION;

tools/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const main = async () => {
4949
const { version } = require('../package.json');
5050
const npmList = JSON.parse(spawnSync('npm', ['list', '--json=true'], { encoding: 'utf8' }).stdout.toString());
5151
const from = ['__FIREBASE_FRAMEWORKS_VERSION__'];
52-
const to = [`file:${process.cwd()}`];
52+
const to = [`file:${process.cwd()}/firebase-frameworks-${version}.tgz`];
5353
for (const [dep, { version }] of Object.entries<Record<string, string>>(npmList.dependencies)) {
5454
from.push(`__${dep.toUpperCase().replace(/[^A-Z]/g, '_')}_VERSION__`);
5555
to.push(`^${version}`);

0 commit comments

Comments
 (0)