-
Notifications
You must be signed in to change notification settings - Fork 122
feat: support output.inlineScripts #874
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e597157
feat: support output.inlineScripts
gaoachao 57b7b22
Update .changeset/afraid-meals-attend.md
gaoachao 1b3a058
Update packages/rspeedy/core/src/config/output/index.ts
gaoachao c88998c
Update docs
gaoachao 260ba28
Update docs
gaoachao a56169a
Adopt environment.config
gaoachao 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,21 @@ | ||
| --- | ||
| "@lynx-js/template-webpack-plugin": patch | ||
| "@lynx-js/react-rsbuild-plugin": patch | ||
| "@lynx-js/rspeedy": patch | ||
| --- | ||
|
|
||
| Support `output.inlineScripts`, which controls whether to inline scripts into Lynx bundle (`.lynx.bundle`). | ||
|
|
||
| Only background thread scripts can remain non-inlined, whereas the main thread script is always inlined. | ||
|
|
||
| example: | ||
|
|
||
| ```js | ||
| import { defineConfig } from '@lynx-js/rspeedy'; | ||
|
|
||
| export default defineConfig({ | ||
| output: { | ||
| inlineScripts: false, | ||
| }, | ||
| }); | ||
| ``` |
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
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
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
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
3 changes: 3 additions & 0 deletions
3
packages/webpack/template-webpack-plugin/test/cases/inline-scripts/external/foo.js
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,3 @@ | ||
| export function foo() { | ||
| return 42; | ||
| } |
46 changes: 46 additions & 0 deletions
46
packages/webpack/template-webpack-plugin/test/cases/inline-scripts/external/index.js
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,46 @@ | ||
| /* | ||
| // Copyright 2024 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
| */ | ||
| /// <reference types="vitest/globals" /> | ||
|
|
||
| import { existsSync } from 'node:fs'; | ||
| import { readFile } from 'node:fs/promises'; | ||
| import { resolve } from 'node:path'; | ||
|
|
||
| it('should have correct chunk content', async () => { | ||
| const { foo } = await import( | ||
| /* webpackChunkName: 'foo:main-thread' */ | ||
| './foo.js' | ||
| ); | ||
| expect(foo()).toBe(42); | ||
|
|
||
| const fooBackground = await import( | ||
| /* webpackChunkName: 'foo:background' */ | ||
| './foo.js' | ||
| ); | ||
| expect(fooBackground.foo()).toBe(42); | ||
| }); | ||
|
|
||
| it('manifest only contains /app-service.js', async () => { | ||
| const tasmJSONPath = resolve(__dirname, '.rspeedy/async/foo/tasm.json'); | ||
| expect(existsSync(tasmJSONPath)).toBeTruthy(); | ||
|
|
||
| const content = await readFile(tasmJSONPath, 'utf-8'); | ||
| const { sourceContent, manifest } = JSON.parse(content); | ||
| const output = resolve(__dirname, 'foo:background.rspack.bundle.js'); | ||
| expect(existsSync(output)); | ||
|
|
||
| const outputContent = await readFile(output, 'utf-8'); | ||
| expect(outputContent).toContain(['function', 'foo()'].join(' ')); | ||
|
|
||
| expect(sourceContent).toHaveProperty('appType', 'DynamicComponent'); | ||
|
|
||
| expect(manifest).not.toHaveProperty('/foo:background.rspack.bundle.js'); | ||
| expect(manifest).toHaveProperty('/app-service.js'); | ||
|
|
||
| expect(manifest['/app-service.js']).toContain( | ||
| `lynx.requireModule('/foo:background.rspack.bundle.js',globDynamicComponentEntry?globDynamicComponentEntry:'__Card__')`, | ||
| ); | ||
| }); |
33 changes: 33 additions & 0 deletions
33
packages/webpack/template-webpack-plugin/test/cases/inline-scripts/external/rspack.config.js
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,33 @@ | ||
| import { LynxEncodePlugin, LynxTemplatePlugin } from '../../../../src'; | ||
|
|
||
| /** @type {import('@rspack/core').Configuration} */ | ||
| export default { | ||
| devtool: false, | ||
| mode: 'development', | ||
| plugins: [ | ||
| new LynxEncodePlugin({ | ||
| inlineScripts: false, | ||
| }), | ||
| new LynxTemplatePlugin({ | ||
| ...LynxTemplatePlugin.defaultOptions, | ||
| intermediate: '.rspeedy/main', | ||
| }), | ||
| /** | ||
| * @param {import('@rspack/core').Compiler} compiler - Rspack Compiler | ||
| */ | ||
| (compiler) => { | ||
| compiler.hooks.thisCompilation.tap('test', (compilation) => { | ||
| const hooks = LynxTemplatePlugin.getLynxTemplatePluginHooks( | ||
| compilation, | ||
| ); | ||
| hooks.asyncChunkName.tap( | ||
| 'test', | ||
| chunkName => | ||
| chunkName | ||
| .replace(':main-thread', '') | ||
| .replace(':background', ''), | ||
| ); | ||
| }); | ||
| }, | ||
| ], | ||
| }; |
3 changes: 3 additions & 0 deletions
3
packages/webpack/template-webpack-plugin/test/cases/inline-scripts/inline/foo.js
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,3 @@ | ||
| export function foo() { | ||
| return 42; | ||
| } |
37 changes: 37 additions & 0 deletions
37
packages/webpack/template-webpack-plugin/test/cases/inline-scripts/inline/index.js
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,37 @@ | ||
| /* | ||
| // Copyright 2024 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
| */ | ||
| /// <reference types="vitest/globals" /> | ||
|
|
||
| import { existsSync } from 'node:fs'; | ||
| import { readFile } from 'node:fs/promises'; | ||
| import { resolve } from 'node:path'; | ||
|
|
||
| it('should have correct chunk content', async () => { | ||
| const { foo } = await import( | ||
| /* webpackChunkName: 'foo:main-thread' */ | ||
| './foo.js' | ||
| ); | ||
| expect(foo()).toBe(42); | ||
|
|
||
| const fooBackground = await import( | ||
| /* webpackChunkName: 'foo:background' */ | ||
| './foo.js' | ||
| ); | ||
| expect(fooBackground.foo()).toBe(42); | ||
| }); | ||
|
|
||
| it('should generate correct foo template', async () => { | ||
| const tasmJSONPath = resolve(__dirname, '.rspeedy/async/foo/tasm.json'); | ||
| expect(existsSync(tasmJSONPath)).toBeTruthy(); | ||
|
|
||
| const content = await readFile(tasmJSONPath, 'utf-8'); | ||
| const { sourceContent, manifest } = JSON.parse(content); | ||
| expect(sourceContent).toHaveProperty('appType', 'DynamicComponent'); | ||
| expect(manifest).toHaveProperty( | ||
| '/foo:background.rspack.bundle.js', | ||
| expect.stringContaining('function foo()'), | ||
| ); | ||
| }); |
33 changes: 33 additions & 0 deletions
33
packages/webpack/template-webpack-plugin/test/cases/inline-scripts/inline/rspack.config.js
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,33 @@ | ||
| import { LynxEncodePlugin, LynxTemplatePlugin } from '../../../../src'; | ||
|
|
||
| /** @type {import('@rspack/core').Configuration} */ | ||
| export default { | ||
| devtool: false, | ||
| mode: 'development', | ||
| plugins: [ | ||
| new LynxEncodePlugin({ | ||
| inlineScripts: true, | ||
| }), | ||
| new LynxTemplatePlugin({ | ||
| ...LynxTemplatePlugin.defaultOptions, | ||
| intermediate: '.rspeedy/main', | ||
| }), | ||
| /** | ||
| * @param {import('@rspack/core').Compiler} compiler - Rspack Compiler | ||
| */ | ||
| (compiler) => { | ||
| compiler.hooks.thisCompilation.tap('test', (compilation) => { | ||
| const hooks = LynxTemplatePlugin.getLynxTemplatePluginHooks( | ||
| compilation, | ||
| ); | ||
| hooks.asyncChunkName.tap( | ||
| 'test', | ||
| chunkName => | ||
| chunkName | ||
| .replace(':main-thread', '') | ||
| .replace(':background', ''), | ||
| ); | ||
| }); | ||
| }, | ||
| ], | ||
| }; |
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.