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

'__sfc_main' has already been declared. #28

Closed
jaxonly opened this issue Aug 27, 2021 · 0 comments
Closed

'__sfc_main' has already been declared. #28

jaxonly opened this issue Aug 27, 2021 · 0 comments

Comments

@jaxonly
Copy link

jaxonly commented Aug 27, 2021

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;
============
@antfu antfu closed this as completed in eb5507b Aug 27, 2021
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

No branches or pull requests

1 participant