Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions e2e/cases/assets/import-json-raw-query/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { expect, rspackTest } from '@e2e/helper';

rspackTest(
'should import raw JSON with raw query',
async ({ page, buildPreview }) => {
await buildPreview();

expect(await page.evaluate('window.a')).toBe(
readFileSync(join(__dirname, './src/a.json'), 'utf-8'),
);
expect(await page.evaluate('window.b')).toBe(
readFileSync(join(__dirname, './src/b.json'), 'utf-8'),
);
},
);
4 changes: 4 additions & 0 deletions e2e/cases/assets/import-json-raw-query/src/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": "bar",
"age": 1
}
3 changes: 3 additions & 0 deletions e2e/cases/assets/import-json-raw-query/src/b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"list": [1, 2]
}
5 changes: 5 additions & 0 deletions e2e/cases/assets/import-json-raw-query/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import a from './a.json?raw';
import b from './b.json?raw';

window.a = a;
window.b = b;
36 changes: 36 additions & 0 deletions packages/compat/webpack/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down Expand Up @@ -631,6 +640,15 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when produ
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down Expand Up @@ -1009,6 +1027,15 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down Expand Up @@ -1375,6 +1402,15 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/configChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const CHAIN_ID = {
MJS: 'mjs',
/** Rule for fonts */
FONT: 'font',
/** Rule for JSON */
JSON: 'json',
/** Rule for images */
IMAGE: 'image',
/** Rule for media */
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ const chainStaticAssetRule = ({
.type('asset/source')
.resourceQuery(RAW_QUERY_REGEX);

// the asset will be inlined if fileSize < dataUrlCondition.maxSize
rule
.oneOf(`${assetType}-asset`)
.type('asset')
.parser({
// asset will be inlined if file size < maxSize
dataUrlCondition: {
maxSize,
},
Expand Down Expand Up @@ -144,6 +144,15 @@ export const pluginAsset = (): RsbuildPlugin => ({
// font
createAssetRule(CHAIN_ID.RULE.FONT, FONT_EXTENSIONS, emitAssets);

// JSON
// Rspack has built-in rule for JSON, so we only need to handle imports with query
// get raw content: "foo.json?raw"
const rule = chain.module.rule(CHAIN_ID.RULE.JSON).test(/\.json$/i);
rule
.oneOf('json-asset-raw')
.type('asset/source')
.resourceQuery(RAW_QUERY_REGEX);

// assets
const assetsFilename = getMergedFilename('assets');
chain.output.assetModuleFilename(assetsFilename);
Expand Down
36 changes: 36 additions & 0 deletions packages/core/tests/__snapshots__/asset.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ exports[`plugin-asset > should add image rules correctly 1`] = `
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
],
},
"output": {
Expand Down Expand Up @@ -269,6 +278,15 @@ exports[`plugin-asset > should add image rules correctly 2`] = `
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
],
},
"output": {
Expand Down Expand Up @@ -410,6 +428,15 @@ exports[`plugin-asset > should allow to use distPath.image to modify dist path 1
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
],
},
"output": {
Expand Down Expand Up @@ -551,6 +578,15 @@ exports[`plugin-asset > should allow to use filename.image to modify filename 1`
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
],
},
"output": {
Expand Down
9 changes: 9 additions & 0 deletions packages/core/tests/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@ exports[`should use Rspack as the default bundler > apply Rspack correctly 1`] =
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down
36 changes: 36 additions & 0 deletions packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down Expand Up @@ -884,6 +893,15 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down Expand Up @@ -1386,6 +1404,15 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down Expand Up @@ -1859,6 +1886,15 @@ exports[`tools.rspack > should match snapshot 1`] = `
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,15 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down Expand Up @@ -2219,6 +2228,15 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
],
"test": /\\\\\\.\\(\\?:woff\\|woff2\\|eot\\|ttf\\|otf\\|ttc\\)\\$/i,
},
{
"oneOf": [
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
],
"test": /\\\\\\.json\\$/i,
},
{
"dependency": "url",
"generator": {
Expand Down
Loading