-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
fix(build): ensure amd bundles request require to be injected
#20861
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 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
835e172
fix(build): make relative imports in amd/cjs relative to the current …
dschmidt 4eaac92
fix: ensure amd bundles request `require` to be injected
dschmidt e890074
Update packages/vite/src/node/plugins/completeAmdWrap.ts
dschmidt afbbbcf
Update packages/vite/src/node/plugins/completeAmdWrap.ts
dschmidt 806edb7
fix: detect reject in deps when it's wrapped in single quotes
dschmidt 6f6d3c4
test(amd): add amd playground
dschmidt 560154b
test: add assertions
sapphi-red 2a2b2e8
Merge branch 'main' into fix/amd-relative-imports
sapphi-red 87b6ff3
chore: fix lockfile
sapphi-red 74c9da4
chore: add more package.json fields
sapphi-red 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
42 changes: 42 additions & 0 deletions
42
packages/vite/src/node/__tests__/plugins/completeAmdWrap.spec.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,42 @@ | ||
| import { describe, expect, test } from 'vitest' | ||
| import { completeAmdWrapPlugin } from '../../plugins/completeAmdWrap' | ||
|
|
||
| async function createCompleteAmdWrapPluginRenderChunk() { | ||
| const instance = completeAmdWrapPlugin() | ||
|
|
||
| return async (code: string) => { | ||
| // @ts-expect-error transform.handler should exist | ||
| const result = await instance.renderChunk.call(instance, code, 'foo.ts', { | ||
| format: 'amd', | ||
| }) | ||
| return result?.code || result | ||
| } | ||
| } | ||
|
|
||
| describe('completeAmdWrapPlugin', async () => { | ||
| const renderChunk = await createCompleteAmdWrapPluginRenderChunk() | ||
|
|
||
| describe('adds require parameter', async () => { | ||
| test('without other dependencies', async () => { | ||
| expect( | ||
| await renderChunk('define((function() { } ))'), | ||
| ).toMatchInlineSnapshot(`"define(["require"], (function(require) { } ))"`) | ||
| }) | ||
|
|
||
| test('with other dependencies', async () => { | ||
| expect( | ||
| await renderChunk( | ||
| 'define(["vue", "vue-router"], function(vue, vueRouter) { } ))', | ||
| ), | ||
| ).toMatchInlineSnapshot( | ||
| `"define(["require", "vue", "vue-router"], (function(require, vue, vueRouter) { } ))"`, | ||
| ) | ||
| }) | ||
|
|
||
| test("only if require isn't injected already", async () => { | ||
| expect( | ||
| await renderChunk('define(["require"], function(require) { } ))'), | ||
| ).toMatchInlineSnapshot(`"define(["require"], (function(require) { } ))"`) | ||
| }) | ||
| }) | ||
| }) |
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,30 @@ | ||
| import type { Plugin } from '../plugin' | ||
|
|
||
| /** | ||
| * ensure amd bundles request `require` to be injected | ||
| */ | ||
| export function completeAmdWrapPlugin(): Plugin { | ||
| const AmdWrapRE = | ||
| /\bdefine\((?:\s*\[([^\]]*)\],)?\s*(?:\(\s*)?function\s*\(([^)]*)\)\s*\{/g | ||
|
|
||
| return { | ||
| name: 'vite:force-amd-wrap-require', | ||
| renderChunk(code, _chunk, opts) { | ||
| if (opts.format !== 'amd') return | ||
|
|
||
| return { | ||
| code: code.replace(AmdWrapRE, (_, deps, params) => { | ||
| if (deps && /"require"/.test(deps)) { | ||
| return `define([${deps}], (function(${params}) {` | ||
| } | ||
|
|
||
| const newDeps = deps ? `"require", ${deps}` : '"require"' | ||
| const newParams = params.trim() ? `require, ${params}` : 'require' | ||
|
|
||
| return `define([${newDeps}], (function(${newParams}) {` | ||
| }), | ||
| map: null, | ||
dschmidt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }, | ||
| } | ||
| } | ||
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.