Repository navigation
feat: No json as string in JS bundle #1469
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
retyui
wants to merge
4
commits into
main
Choose a base branch
from
feat/retyui/raw-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+316
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@callstack/repack": minor | ||
| --- | ||
|
|
||
| Add `jsonParse` option to `RepackPlugin`. It sets `module.generator.json.JSONParse`, so `jsonParse: false` bundles `.json` files as JS objects instead of `JSON.parse('...')` calls, which is faster in release builds on Hermes. It is off by default and needs Rspack 1.3.0+ or webpack 5.99.0+. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import type { Compiler as RspackCompiler } from '@rspack/core'; | ||
| import semver from 'semver'; | ||
| import type { Compiler as WebpackCompiler } from 'webpack'; | ||
| import { isRspackCompiler } from '../helpers/index.js'; | ||
|
|
||
| const MIN_RSPACK_VERSION = '1.3.0'; | ||
| const MIN_WEBPACK_VERSION = '5.99.0'; | ||
|
|
||
| function getBundlerSupport(compiler: RspackCompiler) { | ||
| const bundler = isRspackCompiler(compiler) | ||
| ? { | ||
| name: 'Rspack', | ||
| version: compiler.webpack.rspackVersion, | ||
| minVersion: MIN_RSPACK_VERSION, | ||
| } | ||
| : { | ||
| name: 'webpack', | ||
| version: (compiler as unknown as WebpackCompiler).webpack.version, | ||
| minVersion: MIN_WEBPACK_VERSION, | ||
| }; | ||
|
|
||
| return { | ||
| ...bundler, | ||
| supported: semver.gte( | ||
| semver.coerce(bundler.version) ?? '0.0.0', | ||
| bundler.minVersion | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| export class JsonParsePlugin { | ||
| constructor(private jsonParse: boolean) {} | ||
|
|
||
| apply(compiler: RspackCompiler): void; | ||
| apply(compiler: WebpackCompiler): void; | ||
|
|
||
| apply(__compiler: unknown) { | ||
| const compiler = __compiler as RspackCompiler; | ||
| const logger = compiler.getInfrastructureLogger('RepackJsonParsePlugin'); | ||
|
|
||
| const generator = compiler.options.module.generator ?? {}; | ||
| const jsonGenerator = generator.json ?? {}; | ||
|
|
||
| if (jsonGenerator.JSONParse !== undefined) { | ||
| return; | ||
| } | ||
|
|
||
| const bundler = getBundlerSupport(compiler); | ||
| if (!bundler.supported) { | ||
| logger.warn( | ||
| `The jsonParse option needs ${bundler.name} ${bundler.minVersion} or newer ` + | ||
| `(found ${bundler.version}). It will be ignored.` | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| compiler.options.module.generator = { | ||
| ...generator, | ||
| json: { ...jsonGenerator, JSONParse: this.jsonParse }, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/repack/src/plugins/__tests__/JsonParsePlugin.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import type { Compiler } from '@rspack/core'; | ||
| import { JsonParsePlugin } from '../JsonParsePlugin.js'; | ||
|
|
||
| function createCompiler( | ||
| bundler: { rspackVersion: string } | { version: string }, | ||
| generator?: Record<string, unknown> | ||
| ) { | ||
| const warn = jest.fn(); | ||
| const compiler = { | ||
| webpack: bundler, | ||
| options: { module: { generator } }, | ||
| getInfrastructureLogger: () => ({ warn }), | ||
| }; | ||
| return { compiler: compiler as unknown as Compiler, warn }; | ||
| } | ||
|
|
||
| describe('JsonParsePlugin', () => { | ||
| it('sets JSONParse on Rspack 1.3.0+', () => { | ||
| const { compiler, warn } = createCompiler({ rspackVersion: '1.3.0' }); | ||
|
|
||
| new JsonParsePlugin(false).apply(compiler); | ||
|
|
||
| expect(compiler.options.module.generator).toEqual({ | ||
| json: { JSONParse: false }, | ||
| }); | ||
| expect(warn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('sets JSONParse on webpack 5.99.0+', () => { | ||
| const { compiler } = createCompiler({ version: '5.99.0' }); | ||
|
|
||
| new JsonParsePlugin(false).apply(compiler); | ||
|
|
||
| expect(compiler.options.module.generator).toEqual({ | ||
| json: { JSONParse: false }, | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps other generator options', () => { | ||
| const { compiler } = createCompiler( | ||
| { rspackVersion: '2.0.0' }, | ||
| { asset: { dataUrl: {} } } | ||
| ); | ||
|
|
||
| new JsonParsePlugin(false).apply(compiler); | ||
|
|
||
| expect(compiler.options.module.generator).toEqual({ | ||
| asset: { dataUrl: {} }, | ||
| json: { JSONParse: false }, | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps JSONParse set by the user', () => { | ||
| const { compiler } = createCompiler( | ||
| { rspackVersion: '2.0.0' }, | ||
| { json: { JSONParse: true } } | ||
| ); | ||
|
|
||
| new JsonParsePlugin(false).apply(compiler); | ||
|
|
||
| expect(compiler.options.module.generator).toEqual({ | ||
| json: { JSONParse: true }, | ||
| }); | ||
| }); | ||
|
|
||
| it('warns and does nothing on Rspack older than 1.3.0', () => { | ||
| const { compiler, warn } = createCompiler({ rspackVersion: '1.2.8' }); | ||
|
|
||
| new JsonParsePlugin(false).apply(compiler); | ||
|
|
||
| expect(compiler.options.module.generator).toBeUndefined(); | ||
| expect(warn).toHaveBeenCalledWith(expect.stringContaining('Rspack 1.3.0')); | ||
| }); | ||
|
|
||
| it('warns and does nothing on webpack older than 5.99.0', () => { | ||
| const { compiler, warn } = createCompiler({ version: '5.98.0' }); | ||
|
|
||
| new JsonParsePlugin(false).apply(compiler); | ||
|
|
||
| expect(compiler.options.module.generator).toBeUndefined(); | ||
| expect(warn).toHaveBeenCalledWith( | ||
| expect.stringContaining('webpack 5.99.0') | ||
| ); | ||
| }); | ||
| }); |
51 changes: 51 additions & 0 deletions
51
packages/repack/src/plugins/__tests__/RepackPlugin.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type { Compiler } from '@rspack/core'; | ||
| import { RepackPlugin } from '../RepackPlugin.js'; | ||
|
|
||
| function mockPlugin() { | ||
| return jest.fn().mockImplementation(() => ({ apply() {} })); | ||
| } | ||
|
|
||
| jest.mock('../BabelPlugin.js', () => ({ BabelPlugin: mockPlugin() })); | ||
| jest.mock('../DevelopmentPlugin.js', () => ({ | ||
| DevelopmentPlugin: mockPlugin(), | ||
| })); | ||
| jest.mock('../LoggerPlugin.js', () => ({ LoggerPlugin: mockPlugin() })); | ||
| jest.mock('../NativeEntryPlugin/index.js', () => ({ | ||
| NativeEntryPlugin: mockPlugin(), | ||
| })); | ||
| jest.mock('../OutputPlugin/index.js', () => ({ OutputPlugin: mockPlugin() })); | ||
| jest.mock('../RepackTargetPlugin/index.js', () => ({ | ||
| RepackTargetPlugin: mockPlugin(), | ||
| })); | ||
| jest.mock('../SourceMapPlugin.js', () => ({ SourceMapPlugin: mockPlugin() })); | ||
|
|
||
| function createCompiler() { | ||
| return { | ||
| webpack: { | ||
| rspackVersion: '2.0.0', | ||
| DefinePlugin: jest.fn().mockImplementation(() => ({ apply() {} })), | ||
| }, | ||
| options: { name: 'ios', mode: 'production', context: '/', module: {} }, | ||
| getInfrastructureLogger: () => ({ warn: jest.fn() }), | ||
| } as unknown as Compiler; | ||
| } | ||
|
|
||
| describe('RepackPlugin jsonParse option', () => { | ||
| it('sets module.generator.json.JSONParse when jsonParse is set', () => { | ||
| const compiler = createCompiler(); | ||
|
|
||
| new RepackPlugin({ jsonParse: false }).apply(compiler); | ||
|
|
||
| expect(compiler.options.module.generator).toEqual({ | ||
| json: { JSONParse: false }, | ||
| }); | ||
| }); | ||
|
|
||
| it('leaves module.generator untouched when jsonParse is not set', () => { | ||
| const compiler = createCompiler(); | ||
|
|
||
| new RepackPlugin().apply(compiler); | ||
|
|
||
| expect(compiler.options.module.generator).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { createContext, runInContext } from 'node:vm'; | ||
| import { plugins } from '@callstack/repack'; | ||
| import type { Configuration } from '@rspack/core'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| compile, | ||
| createCompiler, | ||
| createVirtualModulePlugin, | ||
| } from '../helpers.js'; | ||
|
|
||
| const data = { greeting: 'Bonjour le monde', items: [1, 2, 3] }; | ||
|
|
||
| const virtualModules = { | ||
| './index.cjs': ` | ||
| globalThis.data = require('./data.json'); | ||
| globalThis.animation = require('./animation.lottie.json'); | ||
| `, | ||
| './data.json': JSON.stringify(data), | ||
| './animation.lottie.json': JSON.stringify({ layers: ['lottie-layer'] }), | ||
| }; | ||
|
|
||
| async function compileJson(config: Partial<Configuration>) { | ||
| const virtualPlugin = await createVirtualModulePlugin(virtualModules); | ||
| const compiler = await createCompiler({ | ||
| context: __dirname, | ||
| mode: 'none', | ||
| devtool: false, | ||
| entry: './index.cjs', | ||
| output: { path: '/out', filename: 'main.js', publicPath: '' }, | ||
| ...config, | ||
| plugins: [virtualPlugin, ...(config.plugins ?? [])], | ||
| } satisfies Configuration); | ||
|
|
||
| return compile(compiler); | ||
| } | ||
|
|
||
| function executeBundle(code: string) { | ||
| const context = createContext({}); | ||
| runInContext(code, context); | ||
| return context; | ||
| } | ||
|
|
||
| describe('JsonParsePlugin', () => { | ||
| it('keeps JSON.parse by default', async () => { | ||
| const { code } = await compileJson({}); | ||
|
|
||
| expect(code).toContain("JSON.parse('"); | ||
| expect(executeBundle(code).data).toEqual(data); | ||
| }); | ||
|
|
||
| it('bundles JSON as JS objects with jsonParse: false', async () => { | ||
| const { code } = await compileJson({ | ||
| plugins: [new plugins.JsonParsePlugin(false)], | ||
| }); | ||
|
|
||
| expect(code).not.toContain('JSON.parse('); | ||
| expect(executeBundle(code).data).toEqual(data); | ||
| }); | ||
|
|
||
| it("keeps the user's own .json rules", async () => { | ||
| const { code, volume } = await compileJson({ | ||
| module: { | ||
| rules: [{ test: /\.lottie\.json$/, type: 'asset/resource' }], | ||
| }, | ||
| plugins: [new plugins.JsonParsePlugin(false)], | ||
| }); | ||
|
|
||
| expect(code).not.toContain('lottie-layer'); | ||
| const emitted = Object.keys(volume.toJSON()).filter((file) => | ||
| file.endsWith('.json') | ||
| ); | ||
| expect(emitted).toHaveLength(1); | ||
| expect(executeBundle(code).data).toEqual(data); | ||
| }); | ||
|
|
||
| it("keeps the user's own JSONParse setting", async () => { | ||
| const { code } = await compileJson({ | ||
| module: { generator: { json: { JSONParse: true } } }, | ||
| plugins: [new plugins.JsonParsePlugin(false)], | ||
| }); | ||
|
|
||
| expect(code).toContain("JSON.parse('"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.