Skip to content
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

fix: transform without sourcesContent #4362

Closed
wants to merge 1 commit into from

Conversation

wmzy
Copy link
Contributor

@wmzy wmzy commented Jul 23, 2021

Description

If we use a plugin with transform like:

import MagicString from 'magic-string';
import type { Plugin } from 'vite';

export default function testTransformPlugin(): Plugin {
  return {
    name: 'testTransform',
    async transform(code, id) {
      const magicString = new MagicString(code);
      magicString.prepend('/* todo */');

      return {
        code: magicString.toString(),
        map: magicString.generateMap(),
      };
    },
  };
}

The vite will be crashed when browser load a script like:

import 'react';

This is because the sourcemap of react has a virtual path:

{
  "version": 3,
  "sources": ["../object-assign/index.js", "../react/cjs/react.development.js", "../react/index.js", "dep:react"],
...
}

This PR has fixed the crash. But there is also a warn log.

Solution

I found that the pluginContainer.transform has not lost the sourcesContent which causes the injectSourcesContent to be called. And I fixed this.

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@aleclarson
Copy link
Member

Can you provide a git repository that reproduces the bug that this PR is supposed to fix?

@wmzy
Copy link
Contributor Author

wmzy commented Jul 23, 2021

@aleclarson just create by vite:

npm init vite@latest test-sourcemap -- --template react-ts

and replace vit.config.ts with:

import { defineConfig } from 'vite';
import MagicString from 'magic-string';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [{
    name: 'testTransform',
    async transform(code, id) {
      const magicString = new MagicString(code);
      magicString.prepend('/* todo */');

      return {
        code: magicString.toString(),
        map: magicString.generateMap(),
      };
    },
  }]
});

replace man.tsx with:

import 'react';

and then run:

vite

to start

@aleclarson
Copy link
Member

@wmzy That does not reproduce the dep:react in sources scenario.

@wmzy
Copy link
Contributor Author

wmzy commented Jul 29, 2021

@aleclarson
Copy link
Member

@wmzy I'd prefer a git repository, or better instructions on how to reproduce manually.

@wmzy
Copy link
Contributor Author

wmzy commented Jul 30, 2021

@aleclarson https://github.com/wmzy/test-sourcemap

This repository may be removed someday, please use the zip file if so.

@wmzy
Copy link
Contributor Author

wmzy commented Oct 28, 2021

@aleclarson @patak-js Could you help to review and merge this pr?

The warn is noise and incorrect:
image

@aleclarson
Copy link
Member

#5587 will fix this, I think

@patak-dev
Copy link
Member

@wmzy is this still an issue with Vite 2.8?

@wmzy wmzy closed this Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants