Skip to content

feat: add package_json_key setting #139

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all 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
25 changes: 14 additions & 11 deletions config/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = (webpackEnv, argv) => {
}

const isDevServerOnly = env.dev_server_only?.toLowerCase() === 'true';
const packageJsonKey = env.package_json_key || 'visyn';
const devtool = env.devtool?.toLowerCase() === 'false' ? false : (env.devtool || (isEnvDevelopment ? 'eval-source-map' : 'source-map'));
const isReactRefresh = isDevServer && isEnvDevelopment;

Expand All @@ -42,8 +43,8 @@ module.exports = (webpackEnv, argv) => {
// Always look for the phovea_registry.ts in the src folder for standalone repos.
const workspaceRegistryFile = path.join(workspacePath, 'src/phovea_registry.ts');

if (!appPkg.visyn) {
throw Error(`The package.json of ${appPkg.name} does not contain a 'visyn' entry.`);
if (!appPkg[packageJsonKey]) {
throw Error(`The package.json of ${appPkg.name} does not contain a '${packageJsonKey}' entry.`);
}

/**
Expand All @@ -68,15 +69,15 @@ module.exports = (webpackEnv, argv) => {
// If a visynWebpackOverride.js file exists in the default app, it will be used to override the visyn configuration.
const visynWebpackOverride = require(path.join(workspacePath, 'visynWebpackOverride.js'))({ env }) || {};
console.log('Using visynWebpackOverride.js file to override visyn configuration.');
Object.assign(appPkg.visyn, visynWebpackOverride);
Object.assign(appPkg[packageJsonKey], visynWebpackOverride);
} catch (e) {
// ignore if file does not exist
}

let {
// eslint-disable-next-line prefer-const
devServerProxy, entries, copyFiles, historyApiFallback,
} = appPkg.visyn;
bundleFolder = '', devServerProxy, entries, copyFiles, historyApiFallback,
} = appPkg[packageJsonKey];

if (isDevServerOnly) {
// If we do yarn start dev_server_only=true, we only want to start the dev server and not build the app (i.e. for proxy support).
Expand All @@ -89,6 +90,8 @@ module.exports = (webpackEnv, argv) => {
// Check if Tailwind config exists
const useTailwind = fs.existsSync(path.join(workspacePath, 'tailwind.config.js'));

const bundlesFolder = `bundles/${bundleFolder}`;

return defineConfig({
mode,
// Logging noise constrained to errors and warnings
Expand All @@ -108,7 +111,7 @@ module.exports = (webpackEnv, argv) => {
},
devServer: isEnvDevelopment
? {
static: path.resolve(workspacePath, 'bundles'),
static: path.resolve(workspacePath, bundlesFolder),
compress: true,
// Explicitly set hot to true and liveReload to false to ensure that hot is preferred over liveReload
hot: true,
Expand Down Expand Up @@ -142,7 +145,7 @@ module.exports = (webpackEnv, argv) => {
: undefined,
output: {
// The build folder.
path: path.join(workspacePath, 'bundles'),
path: path.join(workspacePath, bundlesFolder),
// Add /* filename */ comments to generated require()s in the output.
// TODO: rspack: pathinfo: isEnvDevelopment,
// There will be one main bundle, and one file per asynchronous chunk.
Expand All @@ -153,7 +156,7 @@ module.exports = (webpackEnv, argv) => {
// webpack uses `publicPath` to determine where the app is being served from.
// It requires a trailing slash, or the file assets will get an incorrect path.
// We inferred the "public path" (such as / or /my-project) from homepage.
publicPath: '/',
publicPath: bundleFolder ? `/${bundleFolder}/` : '/',
clean: !isDevServerOnly,
},
/*
Expand Down Expand Up @@ -397,12 +400,12 @@ module.exports = (webpackEnv, argv) => {
patterns: [
...(copyFiles?.map((file) => ({
from: path.join(workspacePath, file),
to: path.join(workspacePath, 'bundles', path.basename(file)),
to: path.join(workspacePath, bundlesFolder, path.basename(file)),
})) || []),
...[
fs.existsSync(workspaceMetaDataFile) && {
from: workspaceMetaDataFile,
to: path.join(workspacePath, 'bundles', 'phoveaMetaData.json'),
to: path.join(workspacePath, bundlesFolder, 'phoveaMetaData.json'),
// @ts-ignore TODO: check why https://webpack.js.org/plugins/copy-webpack-plugin/#transform is not in the typing.
transform: () => {
function resolveScreenshot(appDirectory) {
Expand Down Expand Up @@ -433,7 +436,7 @@ module.exports = (webpackEnv, argv) => {
// use package-lock json as buildInfo
fs.existsSync(workspaceBuildInfoFile) && {
from: workspaceBuildInfoFile,
to: path.join(workspacePath, 'bundles', 'buildInfo.json'),
to: path.join(workspacePath, bundlesFolder, 'buildInfo.json'),
},
].filter(Boolean),
],
Expand Down