Skip to content

Commit

Permalink
migrate vue microfrontend to rsbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Sep 20, 2024
1 parent 75cada0 commit 9acbfe2
Show file tree
Hide file tree
Showing 31 changed files with 555 additions and 776 deletions.
8 changes: 8 additions & 0 deletions generators/client/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ const command = {
],
scope: 'storage',
},
clientBundler: {
cli: {
type: String,
hide: true,
},
choices: ['webpack', 'rsbuild', 'vite'],
scope: 'storage',
},
microfrontend: {
description: 'Enable microfrontend support',
cli: {
Expand Down
2 changes: 1 addition & 1 deletion generators/client/files-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const files = {
templates: ['README.md.jhi.client', '.prettierignore.jhi.client'],
},
clientRootTemplatesBlock({
condition: generator => generator.microfrontend && (generator.clientFrameworkVue || generator.clientFrameworkReact),
condition: generator => generator.microfrontend && generator.clientFrameworkReact,
templates: ['webpack/webpack.microfrontend.js.jhi'],
}),
{
Expand Down
4 changes: 2 additions & 2 deletions generators/client/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ export default class JHipsterClientGenerator extends BaseApplicationGenerator {
source.addWebpackConfig({
config: `${conditional}require('./webpack.microfrontend')(config, options, targetOptions)`,
});
} else if (application.clientFrameworkVue || application.clientFrameworkReact) {
} else if (application.clientFrameworkReact) {
source.addWebpackConfig({ config: "require('./webpack.microfrontend')({ serve: options.env.WEBPACK_SERVE })" });
} else {
} else if (!application.clientFrameworkVue) {
throw new Error(`Client framework ${application.clientFramework} doesn't support microfrontends`);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ task webapp_test(type: NpmTask) {
.withPathSensitivity(PathSensitivity.RELATIVE)
<%_ if (microfrontend) { _%>
def webpackDevFiles = fileTree("<%= CLIENT_WEBPACK_DIR %>")
webpackDevFiles.exclude("webpack.prod.js")
inputs.files(webpackDevFiles)
.withPropertyName("webpack-dir")
inputs.files('rsbuild.config.ts', 'rsbuild.module-federation.config.ts')
.withPropertyName("rsbuild")
.withPathSensitivity(PathSensitivity.RELATIVE)
<%_ } else { _%>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generator - javascript:rsbuild with defaults options should call source snapshot 1`] = `{}`;

exports[`generator - javascript:rsbuild with defaults options should match files snapshot 1`] = `
{
".yo-rc.json": {
"stateCleared": "modified",
},
"package.json": {
"stateCleared": "modified",
},
"rsbuild.config.ts.jhi": {
"stateCleared": "modified",
},
}
`;
26 changes: 26 additions & 0 deletions generators/javascript/generators/rsbuild/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { JHipsterCommandDefinition } from '../../../../lib/command/types.js';

const command = {
configs: {},
import: [],
} as const satisfies JHipsterCommandDefinition;

export default command;
53 changes: 53 additions & 0 deletions generators/javascript/generators/rsbuild/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { basename, dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { before, describe, expect, it } from 'esmocha';

import { shouldSupportFeatures, testBlueprintSupport } from '../../../../test/support/tests.js';
import { defaultHelpers as helpers, result } from '../../../../lib/testing/index.js';
import Generator from './index.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const generator = `${basename(resolve(__dirname, '../../'))}:${basename(__dirname)}`;

describe(`generator - ${generator}`, () => {
shouldSupportFeatures(Generator);
describe('blueprint support', () => testBlueprintSupport(generator));

describe('with defaults options', () => {
before(async () => {
await helpers.runJHipster(generator).withMockedJHipsterGenerators().withMockedSource().withSharedApplication({}).withJHipsterConfig();
});

it('should match files snapshot', () => {
expect(result.getStateSnapshot()).toMatchSnapshot();
});

it('should call source snapshot', () => {
expect(result.sourceCallsArg).toMatchSnapshot();
});

it('should compose with generators', () => {
expect(result.composedMockedGenerators).toMatchInlineSnapshot(`[]`);
});
});
});
129 changes: 129 additions & 0 deletions generators/javascript/generators/rsbuild/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import BaseGenerator from '../../../../generators/base-application/index.js';

export default class RspackGenerator extends BaseGenerator {
constructor(args, options, features) {
super(args, options, { queueCommandTasks: true, ...features });
}

async beforeQueue() {
if (!this.fromBlueprint) {
await this.composeWithBlueprints();
}

if (!this.delegateToBlueprint) {
await this.dependsOnBootstrapApplication();
}
}

get preparing() {
return this.asPreparingTaskGroup({});
}

get [BaseGenerator.PREPARING]() {
return this.delegateTasksToBlueprint(() => this.preparing);
}

get postPreparing() {
return this.asPostPreparingTaskGroup({});
}

get [BaseGenerator.POST_PREPARING]() {
return this.delegateTasksToBlueprint(() => this.postPreparing);
}

get preparingEachEntity() {
return this.asPreparingEachEntityTaskGroup({});
}

get [BaseGenerator.PREPARING_EACH_ENTITY]() {
return this.delegateTasksToBlueprint(() => this.preparingEachEntity);
}

get preparingEachEntityField() {
return this.asPreparingEachEntityFieldTaskGroup({});
}

get [BaseGenerator.PREPARING_EACH_ENTITY_FIELD]() {
return this.delegateTasksToBlueprint(() => this.preparingEachEntityField);
}

get preparingEachEntityRelationship() {
return this.asPreparingEachEntityRelationshipTaskGroup({});
}

get [BaseGenerator.PREPARING_EACH_ENTITY_RELATIONSHIP]() {
return this.delegateTasksToBlueprint(() => this.preparingEachEntityRelationship);
}

get postPreparingEachEntity() {
return this.asPostPreparingEachEntityTaskGroup({});
}

get [BaseGenerator.POST_PREPARING_EACH_ENTITY]() {
return this.delegateTasksToBlueprint(() => this.postPreparingEachEntity);
}

get default() {
return this.asDefaultTaskGroup({});
}

get [BaseGenerator.DEFAULT]() {
return this.delegateTasksToBlueprint(() => this.default);
}

get writing() {
return this.asWritingTaskGroup({
async writeFiles({ application }) {
await this.writeFiles({
blocks: [{ templates: ['rsbuild.config.ts.jhi'] }],
context: application,
});
},
});
}

get [BaseGenerator.WRITING]() {
return this.delegateTasksToBlueprint(() => this.writing);
}

get postWriting() {
return this.asPostWritingTaskGroup({
addScripts({ application }) {
this.packageJson.merge({
devDependencies: {
'@rsbuild/core': 'latest',
},
scripts: {
start: 'rsbuild dev',
build: 'rsbuild build',
'webapp:build:dev': `${application.clientPackageManager} run build -- --mode=development`,
'webapp:build:prod': `${application.clientPackageManager} run build -- --mode=production`,
'webapp:dev': `${application.clientPackageManager} run start`,
},
});
},
});
}

get [BaseGenerator.POST_WRITING]() {
return this.delegateTasksToBlueprint(() => this.postWriting);
}
}
20 changes: 20 additions & 0 deletions generators/javascript/generators/rsbuild/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { default } from './generator.js';
export { default as command } from './command.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<%#
Copyright 2013-2024 the original author or authors from the JHipster project.
This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
<&_
// Register sections and max allowed fragments, 0 for unlimited.
fragments.registerSections({
importsSection: 0,
pluginsSection: 0,
configSection: 0,
});
_&>
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { defineConfig } from '@rsbuild/core';
<&- fragments.importsSection() -&>

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default defineConfig({
root: path.join(__dirname, '<%- this.relativeDir(clientRootDir, clientSrcDir) %>'),
output: {
cleanDistPath: true,
distPath: {
root: path.join(__dirname, './<%= this.relativeDir(clientRootDir, clientDistDir) %>'),
},
},
html: {
template: './index.html',
scriptLoading: 'defer',
tags: [
{
tag: 'base',
attrs: { href: '/' },
},
],
},
plugins: [
<&- fragments.pluginsSection() -&>
],
<&- fragments.configSection() -&>
});
2 changes: 1 addition & 1 deletion generators/maven/generators/frontend-plugin/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class FrontendPluginGenerator extends BaseApplicationGenerator {
} else if (clientFrameworkVue) {
checksumIncludedFiles.push('.postcssrc.js', 'tsconfig.app.json');
if (microfrontend) {
checksumIncludedFiles.push('webpack/*.*');
checksumIncludedFiles.push('rsbuild.config.ts', 'rsbuild.module-federation.config.ts');
} else {
checksumIncludedFiles.push('vite.config.ts');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,15 @@ public class SecurityConfiguration {
<%_ if (!skipClient) { _%>
.requestMatchers(mvc.pattern("/index.html"), mvc.pattern("/*.js"), mvc.pattern("/*.txt"), mvc.pattern("/*.json"), mvc.pattern("/*.map"), mvc.pattern("/*.css")).permitAll()
.requestMatchers(mvc.pattern("/*.ico"), mvc.pattern("/*.png"), mvc.pattern("/*.svg"), mvc.pattern("/*.webapp")).permitAll()
<%_ if (clientFrameworkVue) { _%>
<%_ if (clientBundlerRsbuild) { _%>
.requestMatchers(mvc.pattern("/static/**")).permitAll()
<%_ } else if (clientBundlerVite) { _%>
.requestMatchers(mvc.pattern("/assets/**")).permitAll()
<%_ if (microfrontend) { _%>
.requestMatchers(mvc.pattern("/app/**")).permitAll()
.requestMatchers(mvc.pattern("/i18n/**")).permitAll()
<%_ } _%>
<%_ } else { _%>
.requestMatchers(mvc.pattern("/app/**")).permitAll()
.requestMatchers(mvc.pattern("/i18n/**")).permitAll()
<%_ } _%>
.requestMatchers(mvc.pattern("/content/**")).permitAll()
<%_ } _%>
.requestMatchers(mvc.pattern("/swagger-ui/**")).permitAll()
<%_ } _%>
<%_ if (authenticationTypeJwt) { _%>
Expand All @@ -227,7 +225,11 @@ public class SecurityConfiguration {
<%_ if (applicationTypeGateway) { _%>
<%_ if (microfrontend) { _%>
// microfrontend resources are loaded by webpack without authentication, they need to be public
<%_ if (clientBundlerRsbuild) { _%>
.requestMatchers(mvc.pattern("/services/*/static/**")).permitAll()
<%_ } _%>
.requestMatchers(mvc.pattern("/services/*/*.js")).permitAll()
.requestMatchers(mvc.pattern("/services/*/content/*.js")).permitAll()
.requestMatchers(mvc.pattern("/services/*/*.txt")).permitAll()
.requestMatchers(mvc.pattern("/services/*/*.json")).permitAll()
.requestMatchers(mvc.pattern("/services/*/*.js.map")).permitAll()
Expand Down
Loading

0 comments on commit 9acbfe2

Please sign in to comment.