Skip to content

Commit 17eb321

Browse files
authored
Fixes to make web frameworks work for plugin (#6000)
1 parent 4612ef2 commit 17eb321

File tree

6 files changed

+38
-32
lines changed

6 files changed

+38
-32
lines changed

firebase-vscode/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"allowSyntheticDefaultImports": true,
66
"resolveJsonModule": true,
77
"typeRoots": ["node_modules/@types", "../src/types"],
8-
"module": "ES2015",
8+
"module": "es2020",
99
"moduleResolution": "node",
1010
"target": "ES2020",
1111
"outDir": "dist",

firebase-vscode/webpack.prod.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
const { merge } = require("webpack-merge");
2+
const TerserPlugin = require("terser-webpack-plugin");
23
const common = require("./webpack.common.js");
34

45
module.exports = common.map(config => merge(config, {
5-
mode: "production"
6+
mode: "production",
7+
optimization: {
8+
minimize: true,
9+
minimizer: [new TerserPlugin({
10+
terserOptions: {
11+
keep_classnames: /AbortSignal/,
12+
keep_fnames: /AbortSignal/
13+
}
14+
}), '...']
15+
}
616
}));
7-

src/dynamicImport.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
const { pathToFileURL } = require("url");
22

3+
// If being compiled with webpack, use non webpack require for these calls.
4+
// (VSCode plugin uses webpack which by default replaces require calls
5+
// with its own require, which doesn't work on files)
6+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
7+
const requireFunc =
8+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
9+
// @ts-ignore prevent VSCE webpack from erroring on non_webpack_require
10+
// eslint-disable-next-line camelcase
11+
typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
12+
313
exports.dynamicImport = function(mod) {
414
if (mod.startsWith("file://")) return import(mod);
515
if (mod.startsWith("/")) return import(pathToFileURL(mod).toString());
616
try {
7-
const path = require.resolve(mod);
17+
const path = requireFunc.resolve(mod);
818
return import(pathToFileURL(path).toString());
919
} catch(e) {
1020
return Promise.reject(e);

src/frameworks/constants.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { readdirSync, statSync } from "fs";
2-
import { join } from "path";
3-
import { Framework, SupportLevel } from "./interfaces";
1+
import { SupportLevel } from "./interfaces";
42
import * as clc from "colorette";
53

64
export const NPM_COMMAND_TIMEOUT_MILLIES = 10_000;
@@ -47,28 +45,6 @@ export const ALLOWED_SSR_REGIONS = [
4745

4846
export const I18N_ROOT = "/";
4947

50-
export const WebFrameworks: Record<string, Framework> = Object.fromEntries(
51-
readdirSync(__dirname)
52-
.filter((path) => statSync(join(__dirname, path)).isDirectory())
53-
.map((path) => {
54-
// If not called by the CLI, (e.g., by the VS Code Extension)
55-
// __dirname won't refer to this folder and these files won't be available.
56-
// Instead it may find sibling folders that aren't modules, and this
57-
// require will throw.
58-
// Long term fix may be to bundle this instead of reading files at runtime
59-
// but for now, this prevents crashing.
60-
try {
61-
return [path, require(join(__dirname, path))];
62-
} catch (e) {
63-
return [];
64-
}
65-
})
66-
.filter(
67-
([, obj]) =>
68-
obj && obj.name && obj.discover && obj.build && obj.type !== undefined && obj.support
69-
)
70-
);
71-
7248
export function GET_DEFAULT_BUILD_TARGETS() {
7349
return Promise.resolve(["production", "development"]);
7450
}

src/frameworks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import {
3939
NODE_VERSION,
4040
SupportLevelWarnings,
4141
VALID_ENGINES,
42-
WebFrameworks,
4342
} from "./constants";
4443
import {
4544
BUILD_TARGET_PURPOSE,
@@ -54,6 +53,7 @@ import { ensureTargeted } from "../functions/ensureTargeted";
5453
import { isDeepStrictEqual } from "util";
5554
import { resolveProjectPath } from "../projectPath";
5655
import { logger } from "../logger";
56+
import { WebFrameworks } from "./frameworks";
5757

5858
export { WebFrameworks };
5959

src/frameworks/utils.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,22 @@ export function relativeRequire(dir: string, mod: "@nuxt/kit"): Promise<any>;
246246
*/
247247
export function relativeRequire(dir: string, mod: string) {
248248
try {
249-
const path = require.resolve(mod, { paths: [dir] });
249+
// If being compiled with webpack, use non webpack require for these calls.
250+
// (VSCode plugin uses webpack which by default replaces require calls
251+
// with its own require, which doesn't work on files)
252+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
253+
const requireFunc: typeof require =
254+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
255+
// @ts-ignore prevent VSCE webpack from erroring on non_webpack_require
256+
// eslint-disable-next-line camelcase
257+
typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
258+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
259+
// @ts-ignore prevent VSCE webpack from erroring on non_webpack_require
260+
const path = requireFunc.resolve(mod, { paths: [dir] });
250261
if (extname(path) === ".mjs") {
251262
return dynamicImport(pathToFileURL(path).toString());
252263
} else {
253-
return require(path);
264+
return requireFunc(path);
254265
}
255266
} catch (e) {
256267
const path = relative(process.cwd(), dir);

0 commit comments

Comments
 (0)