huozhi / rollup-preserve-directives

This is a rollup plugin that helps preserve shebang and string directives.
https://npmjs.com/package/rollup-preserve-directives
34 stars 4 forks source link

Unexpected token #9

Closed himself65 closed 1 year ago

himself65 commented 1 year ago
[commonjs--resolver] Unexpected token (1:8) in /Users/himself65/Code/AFFiNE/node_modules/prop-types/factoryWithThrowingShims.js
file: /Users/himself65/Code/AFFiNE/node_modules/prop-types/index.js:1:8
1: ght (c) 2013-present, Facebook, Inc.
           ^
2:  *
3:  * This source code is licensed under the MIT license found in the
ERR! SyntaxError: Unexpected token (1:8) in /Users/himself65/Code/AFFiNE/node_modules/prop-types/factoryWithThrowingShims.js
ERR!     at pp$4.raise (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:21497:13)
ERR!     at pp$9.unexpected (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18705:8)
ERR!     at pp$9.semicolon (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18682:66)
ERR!     at pp$8.parseExpressionStatement (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:19171:8)
ERR!     at pp$8.parseStatement (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18898:24)
ERR!     at pp$8.parseTopLevel (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18762:21)
ERR!     at Parser.parse (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18534:15)
ERR!     at Function.parse (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18584:35)
ERR!     at Graph.contextParse (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:25629:38)
ERR!     at tryParse (file:///Users/himself65/Code/AFFiNE/node_modules/vite/dist/node/chunks/dep-df561101.js:7346:12)
ERR!     at analyzeTopLevelStatements (file:///Users/himself65/Code/AFFiNE/node_modules/vite/dist/node/chunks/dep-df561101.js:7365:15)
ERR!     at Object.transformAndCheckExports (file:///Users/himself65/Code/AFFiNE/node_modules/vite/dist/node/chunks/dep-df561101.js:9395:68)
ERR!     at Object.transform (file:///Users/himself65/Code/AFFiNE/node_modules/vite/dist/node/chunks/dep-df561101.js:9586:41)
ERR!     at file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:25419:40
ERR!  SyntaxError: Unexpected token (1:8) in /Users/himself65/Code/AFFiNE/node_modules/prop-types/factoryWithThrowingShims.js
ERR!     at pp$4.raise (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:21497:13)
ERR!     at pp$9.unexpected (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18705:8)
ERR!     at pp$9.semicolon (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18682:66)
ERR!     at pp$8.parseExpressionStatement (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:19171:8)
ERR!     at pp$8.parseStatement (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18898:24)
ERR!     at pp$8.parseTopLevel (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18762:21)
ERR!     at Parser.parse (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18534:15)
ERR!     at Function.parse (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:18584:35)
ERR!     at Graph.contextParse (file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:25629:38)
ERR!     at tryParse (file:///Users/himself65/Code/AFFiNE/node_modules/vite/dist/node/chunks/dep-df561101.js:7346:12)
ERR!     at analyzeTopLevelStatements (file:///Users/himself65/Code/AFFiNE/node_modules/vite/dist/node/chunks/dep-df561101.js:7365:15)
ERR!     at Object.transformAndCheckExports (file:///Users/himself65/Code/AFFiNE/node_modules/vite/dist/node/chunks/dep-df561101.js:9395:68)
ERR!     at Object.transform (file:///Users/himself65/Code/AFFiNE/node_modules/vite/dist/node/chunks/dep-df561101.js:9586:41)
ERR!     at file:///Users/himself65/Code/AFFiNE/node_modules/rollup/dist/es/shared/node-entry.js:25419:40 {
ERR!   pos: 8,
ERR!   loc: {
ERR!     column: 8,
ERR!     file: '/Users/himself65/Code/AFFiNE/node_modules/prop-types/factoryWithThrowingShims.js',
ERR!     line: 1
ERR!   },
ERR!   raisedAt: 12,
ERR!   frame: '1: ght (c) 2013-present, Facebook, Inc.\n' +
ERR!     '           ^\n' +
ERR!     '2:  *\n' +
ERR!     '3:  * This source code is licensed under the MIT license found in the',
ERR!   id: '/Users/himself65/Code/AFFiNE/node_modules/prop-types/index.js',
ERR!   hook: 'resolveId',
ERR!   code: 'PLUGIN_ERROR',
ERR!   plugin: 'commonjs--resolver',
ERR!   watchFiles: [
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/iframe.html',
ERR!     '/virtual:/@storybook/builder-vite/vite-app.js',
ERR!     '\x00vite/modulepreload-polyfill',
ERR!     '/virtual:/@storybook/builder-vite/setup-addons.js',
ERR!     '/virtual:/@storybook/builder-vite/storybook-stories.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/react/dist/config.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-links/dist/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-essentials/dist/docs/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-essentials/dist/actions/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-essentials/dist/backgrounds/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-essentials/dist/measure/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-essentials/dist/outline/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-essentials/dist/highlight/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-interactions/dist/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/.storybook/preview.tsx',
ERR!     '\x00vite/preload-helper',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-docs/dist/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-links/dist/chunk-VJY7NXNQ.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-links/dist/chunk-DXNAW7Q2.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-links/dist/chunk-JT3VIYBO.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-actions/dist/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-measure/dist/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-backgrounds/dist/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-outline/dist/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-highlight/dist/preview.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-interactions/node_modules/jest-mock/build/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-interactions/dist/chunk-JRLSWQMA.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/global/dist/index.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/instrumenter/dist/index.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/prop-types/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/html-tags/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/escodegen/escodegen.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/ts-dedent/esm/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/react/node_modules/acorn-jsx/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/lodash/isPlainObject.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/lodash/isFunction.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/lodash/isString.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/react-element-to-jsx-string/dist/esm/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/react/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/react/dist/chunk-JWY6Y6NU.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/docs-tools/dist/index.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/react/node_modules/acorn/dist/acorn.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/react/node_modules/acorn-walk/dist/walk.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-docs/dist/DocsRenderer-3PUGWF3O.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/csf/dist/index.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-actions/dist/chunk-AY7I2SME.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-actions/dist/chunk-VWCVBQ22.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-highlight/dist/chunk-33ALZPRS.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-backgrounds/dist/chunk-GRJZJKJ4.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-outline/dist/chunk-2DMOCDBJ.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/html-tags/html-tags.json',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/prop-types/factoryWithThrowingShims.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/tiny-invariant/dist/esm/tiny-invariant.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-measure/dist/chunk-YIXVQKZ5.mjs',
ERR!     '\x00commonjsHelpers.js',
ERR!     '\x00/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-interactions/node_modules/jest-mock/build/index.js?commonjs-exports',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/introduction.stories.mdx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/workspace-list.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/workspace-avatar.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/switch.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/share-menu.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/page-list.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/page-detail-skeleton.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/onboarding-modal.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/notification-center.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/import-page.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/image-preview-modal.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/datepicker.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/core.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/card.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/breadcrumbs.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/app-sidebar.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/src/stories/affine-banner.stories.tsx',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/escodegen/package.json',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/estraverse/estraverse.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/esutils/lib/utils.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/source-map/source-map.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/lodash/_baseGetTag.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/lodash/_getPrototype.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/lodash/isObjectLike.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/react/node_modules/acorn-jsx/xhtml.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/lodash/isObject.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/lodash/isArray.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/react/cjs/react.production.min.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/react-dom-shim/dist/react-18.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@base2/pretty-print-object/dist/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/react-element-to-jsx-string/node_modules/react-is/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/is-plain-object/dist/is-plain-object.mjs',
ERR!     '\x00/Users/himself65/Code/AFFiNE/node_modules/html-tags/html-tags.json?commonjs-proxy',
ERR!     '\x00react/jsx-runtime',
ERR!     '/Users/himself65/Code/AFFiNE/apps/storybook/package.json',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-essentials/dist/docs/mdx-react-shim.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/blocks/dist/index.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/lodash/mapValues.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/doctrine/lib/doctrine.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@storybook/addon-docs/dist/chunk-S4VUQJ4A.mjs',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/uuid/dist/esm-browser/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/ses/index.js',
ERR!     '/Users/himself65/Code/AFFiNE/node_modules/next-themes/dist/index.module.js',
ERR!     ... 4 more items
ERR!   ]
ERR! }
transforming (223) ../../node_modules/lodash/_arrayMap.js
/fonts/inter/Inter-VariableFont_slnt,wght.ttf referenced in /Users/himself65/Code/AFFiNE/packages/component/src/theme/global.css didn't resolve at build time, it will remain unchanged to be resolved at runtime

/fonts/source-code-pro/SourceCodePro-VariableFont_wght.ttf referenced in /Users/himself65/Code/AFFiNE/packages/component/src/theme/global.css didn't resolve at build time, it will remain unchanged to be resolved at runtime

/fonts/source-code-pro/SourceCodePro-Italic-VariableFont_wght.ttf referenced in /Users/himself65/Code/AFFiNE/packages/component/src/theme/global.css didn't resolve at build time, it will remain unchanged to be resolved at runtime
huozhi commented 1 year ago

Hi, I couldn't tell much from the error trace here. Do you have a reproduction for this or can you provide more info about how you bumped to this? Thanks

himself65 commented 1 year ago

I will do that later

SukkaW commented 1 year ago

I have reproduced the issue @himself65 @huozhi: https://github.com/SukkaW/rollup-plugin-swc-preserve-directives/commit/e05281fb400d87d82cb7d684b8e048a3a9376ad7

I will look into this.


Update: I have found the root cause of the issue.

When parsing the module, swc ignores the leading comments (/** Copyright (c)), and starts calculating span at the position of "use strict". But the magic string operates at the start of the file.

See also: swc playground:

image
SukkaW commented 1 year ago

@huozhi I highly recommend dropping swc and pushing https://github.com/huozhi/rollup-plugin-swc-preserve-directives/pull/4 forward. swc's JS Parse API is literally not usable at this moment.

huozhi commented 1 year ago

Sounds we should file an issue to swc to let swc fix it

SukkaW commented 1 year ago

@huozhi

Although https://github.com/swc-project/swc/issues/7845 has been closed, I suspect https://github.com/swc-project/swc/issues/7842 is also related to this.