Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

'__sfc_main' has already been declared. #28

Closed
@jaxonly

Description

@jaxonly

a minimal reproduction

https://github.com/JaxXu/sugartake2try

intro

 ERROR  Failed to compile with 1 error                                                                                                                                                          7:28:01 PM

 error  in ./src/js/App.vue?vue&type=script&lang=js&

Syntax Error: Identifier '__sfc_main' has already been declared. (19:6)

  17 |   Old
  18 | }, __sfc_main.components);
> 19 | const __sfc_main = __sfc_main;
     |       ^
  20 | export default __sfc_main;
  21 |

This bug recurs when placing the Vue2ScriptSetupPlugin plugin in front of the VueLoaderPlugin, Maybe some additional documentation is needed

But the above case is not a percentage recurrence, you remove the /* unlock comment */ from App.vue and the error will not be reported. Is this also a bug? As far as I can see, when transformScriptSetup is processed, the problem is that the ParsedSFC object does not contain scriptSetup but script

error

===============        transformScriptSetup.sfc           ==================
{
  id: '/Users/xuyihao/Documents/GitHub.nosync/webpack5/packages/dev/src/js/App.vue?vue&type=script&lang=js&',
  template: {
    components: Set(2) { 'New', 'Old' },
    identifiers: Set(2) { 'readersNumber', 'add' }
  },
  scriptSetup: {
    start: 0,
    end: 0,
    contentStart: 0,
    contentEnd: 0,
    content: '',
    attrs: {},
    found: false,
    ast: Node {
      type: 'Program',
      start: 0,
      end: 0,
      loc: [SourceLocation],
      sourceType: 'module',
      interpreter: null,
      body: [],
      directives: []
    }
  },
  script: {
    start: 273,
    end: 644,
    contentStart: 282,
    contentEnd: 635,
    content: '\n' +
      "import New from './components/new';\n" +
      "import Old from './components/old'; // let readersNumber = $ref(0);\n" +
      '//\n' +
      '// const add = () => {\n' +
      '//     readersNumber += 1;\n' +
      '// }\n' +
      '\n' +
      'const __sfc_main = {};\n' +
      '\n' +
      '__sfc_main.setup = (__props, __ctx) => {\n' +
      '  return {};\n' +
      '};\n' +
      '\n' +
      '__sfc_main.components = Object.assign({\n' +
      '  New,\n' +
      '  Old\n' +
      '}, __sfc_main.components);\n' +
      'export default __sfc_main;\n',
    attrs: {},
    found: true,
    ast: Node {
      type: 'Program',
      start: 0,
      end: 353,
      loc: [SourceLocation],
      sourceType: 'module',
      interpreter: null,
      body: [Array],
      directives: []
    }
  },
  parserOptions: { sourceType: 'module', plugins: [] },
  extraDeclarations: []
}

===============          generate(ast).code         ==================

import New from './components/new';
import Old from './components/old'; // let readersNumber = $ref(0);
//
// const add = () => {
//     readersNumber += 1;
// }

const __sfc_main = {};

__sfc_main.setup = (__props, __ctx) => {
  return {};
};

__sfc_main.components = Object.assign({
  New,
  Old
}, __sfc_main.components);
const __sfc_main = __sfc_main;
export default __sfc_main;

normal

===============        transformScriptSetup.sfc           ==================
{
  id: '/Users/xuyihao/Documents/GitHub.nosync/webpack5/packages/dev/src/js/App.vue?vue&type=script&lang=js&',
  template: {
    components: Set(2) { 'New', 'Old' },
    identifiers: Set(2) { 'readersNumber', 'add' }
  },
  scriptSetup: {
    start: 273,
    end: 460,
    contentStart: 287,
    contentEnd: 451,
    content: '\n' +
      "import New from './components/new';\n" +
      "import Old from './components/old';\n" +
      '\n' +
      '// let readersNumber = $ref(0);\n' +
      '//\n' +
      '// const add = () => {\n' +
      '//     readersNumber += 1;\n' +
      '// }\n',
    attrs: { setup: '' },
    found: true,
    ast: Node {
      type: 'Program',
      start: 0,
      end: 164,
      loc: [SourceLocation],
      sourceType: 'module',
      interpreter: null,
      body: [Array],
      directives: []
    }
  },
  script: {
    start: 0,
    end: 0,
    contentStart: 0,
    contentEnd: 0,
    content: '',
    attrs: {},
    found: false,
    ast: Node {
      type: 'Program',
      start: 0,
      end: 0,
      loc: [SourceLocation],
      sourceType: 'module',
      interpreter: null,
      body: [],
      directives: []
    }
  },
  parserOptions: { sourceType: 'module', plugins: [] },
  extraDeclarations: []
}

===============          generate(ast).code         ==================

import New from './components/new';
import Old from './components/old'; // let readersNumber = $ref(0);
//
// const add = () => {
//     readersNumber += 1;
// }

const __sfc_main = {};

__sfc_main.setup = (__props, __ctx) => {
  return {};
};

__sfc_main.components = Object.assign({
  New,
  Old
}, __sfc_main.components);
export default __sfc_main;
============

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions