Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[legacy-framework] Fix webpack 5 support (Webpack 5 now used by default) (patch) #2453

Merged
merged 13 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ bin
packages/generator/templates/**
.github/ISSUE_TEMPLATE/bug_report.md
nextjs/packages/next/compiled/**

// Because file from nextjs upstream isn't formatted properly
nextjs/packages/next/build/webpack-config.ts
3 changes: 3 additions & 0 deletions nextjs/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ packages/next-codemod/**/*.js
packages/next-codemod/**/*.d.ts
packages/next-env/**/*.d.ts
test-timings.json

// Because file from nextjs upstream isn't formatted properly
nextjs/packages/next/build/webpack-config.ts
29 changes: 29 additions & 0 deletions nextjs/packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ import WebpackConformancePlugin, {
} from './webpack/plugins/webpack-conformance-plugin'
import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin'

import fs from 'fs'
import {getProjectRoot} from '../server/lib/utils'

/* ------ Blitz.js ------- */
function doesDbModuleExist() {
const projectRoot = getProjectRoot()
return (
fs.existsSync(path.join(projectRoot, 'db/index.js')) ||
fs.existsSync(path.join(projectRoot, 'db/index.ts')) ||
fs.existsSync(path.join(projectRoot, 'db/index.tsx'))
)
}
/* ------ Blitz.js ------- */

type ExcludesFalse = <T>(x: T | false) => x is T

const devtoolRevertWarning = execOnce(
Expand Down Expand Up @@ -882,6 +896,9 @@ export default async function getBaseWebpackConfig(
entry: async () => {
return {
...(clientEntries ? clientEntries : {}),
...(isServer && doesDbModuleExist()
? { '../blitz/db': './db/index' }
: {}),
...entrypoints,
}
},
Expand Down Expand Up @@ -997,6 +1014,18 @@ export default async function getBaseWebpackConfig(
]
: defaultLoaders.babel,
},
...(isServer
? []
: [
{
issuer: /(mutations|queries)(?!.*\.client)/,
resource: /_resolvers/,
use: { loader: 'null-loader' },
},
// Because of packages/core/src/prisma-utils.ts
{ test: /[\\/]npm-which[\\/]/, use: { loader: 'null-loader' } },
{ test: /[\\/]cross-spawn[\\/]/, use: { loader: 'null-loader' } },
flybayer marked this conversation as resolved.
Show resolved Hide resolved
]),
].filter(Boolean),
},
plugins: [
Expand Down
2 changes: 1 addition & 1 deletion nextjs/packages/next/next-server/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const defaultConfig: NextConfig = {
serialWebpackBuild: false,
turboMode: false,
eslint: false,
reactRoot: Number(process.env.NEXT_PRIVATE_REACT_ROOT) > 0,
reactRoot: true,
enableBlurryPlaceholder: false,
disableOptimizedLoading: true,
gzipSize: true,
Expand Down
17 changes: 17 additions & 0 deletions nextjs/packages/next/server/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'fs'
import path from 'path'

export function printAndExit(message: string, code = 1) {
if (code === 0) {
console.log(message)
Expand All @@ -12,3 +15,17 @@ export function getNodeOptionsWithoutInspect() {
const NODE_INSPECT_RE = /--inspect(-brk)?(=\S+)?( |$)/
return (process.env.NODE_OPTIONS || '').replace(NODE_INSPECT_RE, '')
}

export function getProjectRoot() {
return path.dirname(getConfigSrcPath())
}

export function getConfigSrcPath() {
const tsPath = path.resolve(path.join(process.cwd(), 'blitz.config.ts'))
if (fs.existsSync(tsPath)) {
return tsPath
} else {
const jsPath = path.resolve(path.join(process.cwd(), 'blitz.config.js'))
return jsPath
}
}
1 change: 1 addition & 0 deletions packages/config/index-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
4 changes: 3 additions & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
"test": "jest --passWithNoTests",
"test:watch": "jest --watch"
},
"browser": "index-browser.js",
"main": "dist/blitzjs-config.cjs.js",
"module": "dist/blitzjs-config.esm.js",
"types": "dist/blitzjs-config.cjs.d.ts",
"files": [
"dist"
"dist",
"index-browser.js"
],
"keywords": [
"next.config.js",
Expand Down
4 changes: 4 additions & 0 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ export interface BlitzConfigNormalized extends BlitzConfig {
}

export function getProjectRoot() {
// TODO consolidate with nextjs/packages/next/server/lib/utils.ts
// IF THIS IS UPDATED, so does the one inside nextjs
return path.dirname(getConfigSrcPath())
}

export function getConfigSrcPath() {
// TODO consolidate with nextjs/packages/next/server/lib/utils.ts
// IF THIS IS UPDATED, so does the one inside nextjs
const tsPath = path.resolve(path.join(process.cwd(), "blitz.config.ts"))
if (existsSync(tsPath)) {
return tsPath
Expand Down
6 changes: 2 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"head.ts",
"image.ts",
"index.ts",
"server/index.ts",
"with-blitz.ts"
"server/index.ts"
]
},
"main": "dist/blitzjs-core.cjs.js",
Expand All @@ -31,8 +30,7 @@
"dynamic",
"head",
"image",
"server",
"with-blitz"
"server"
],
"dependencies": {
"@blitzjs/config": "0.36.4",
Expand Down
48 changes: 0 additions & 48 deletions packages/core/src/with-blitz.test.ts

This file was deleted.

113 changes: 0 additions & 113 deletions packages/core/src/with-blitz.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/core/with-blitz/package.json

This file was deleted.

6 changes: 2 additions & 4 deletions packages/server/src/stages/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ export const createStageConfig: Stage = ({config, processNewFile, processNewChil
cwd: config.src,
path: resolve(config.src, "next.config.js"),
contents: Buffer.from(`
const {withBlitz} = require('@blitzjs/core/with-blitz');
const config = require('../blitz.config.js');
module.exports = withBlitz(config);
module.exports = config;
`),
}),
)
Expand Down Expand Up @@ -74,10 +73,9 @@ module.exports = withBlitz(config);
})

file.contents = Buffer.from(`
const {withBlitz} = require('@blitzjs/core/with-blitz');
const vercelConfig = require('./next-vercel.config.js');
const config = require('../blitz.config.js');
module.exports = withBlitz({...config, ...vercelConfig});
module.exports = {...config, ...vercelConfig};
`)
}

Expand Down
25 changes: 23 additions & 2 deletions patches/@preconstruct+cli+2.0.7.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/node_modules/@preconstruct/cli/cli/dist/cli.cjs.dev.js b/node_modules/@preconstruct/cli/cli/dist/cli.cjs.dev.js
index 972d57b..e6bc64b 100644
index 972d57b..3baa4e6 100644
--- a/node_modules/@preconstruct/cli/cli/dist/cli.cjs.dev.js
+++ b/node_modules/@preconstruct/cli/cli/dist/cli.cjs.dev.js
@@ -166,7 +166,7 @@ function format(args, messageType, scope) {
Expand All @@ -11,7 +11,17 @@ index 972d57b..e6bc64b 100644
return fullPrefix + util.format("", ...args).split("\n").reduce((str, line) => {
const prefixed = `${str}\n${fullPrefix}`;
return line ? `${prefixed} ${line}` : prefixed;
@@ -1917,6 +1917,8 @@ let getRollupConfig = (pkg, entrypoints, aliases, type, reportTransformedFile) =
@@ -847,6 +847,9 @@ const isFieldValid = {
},

browser(entrypoint) {
+ if (entrypoint.directory.includes("packages/config")) {
+ return true
+ }
return equal(entrypoint.json.browser, validFields.browser(entrypoint));
}

@@ -1917,6 +1920,8 @@ let getRollupConfig = (pkg, entrypoints, aliases, type, reportTransformedFile) =
external.push(...builtInModules);
}

Expand All @@ -20,3 +30,14 @@ index 972d57b..e6bc64b 100644
let input = {};
entrypoints.forEach(entrypoint => {
input[path__default.relative(pkg.directory, path__default.join(entrypoint.directory, "dist", getNameForDistForEntrypoint(entrypoint)))] = entrypoint.source;
@@ -2129,6 +2134,10 @@ function getRollupConfigs(pkg, aliases) {
});
let hasBrowserField = pkg.entrypoints[0].json.browser !== undefined;

+ if (["@blitzjs/config"].includes(pkg.entrypoints[0].json.name)) {
+ hasBrowserField = false
+ }
+
if (hasBrowserField) {
configs.push({
config: getRollupConfig(pkg, pkg.entrypoints, aliases, "browser", () => {}),