Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

[RFC] webpack-like tilde resolves in less #143

Closed
2 tasks done
crazyair opened this issue Aug 21, 2022 · 34 comments
Closed
2 tasks done

[RFC] webpack-like tilde resolves in less #143

crazyair opened this issue Aug 21, 2022 · 34 comments
Labels
enhancement New feature or request help wanted Extra attention is needed with-ant-design Questions about integrating ant-design with Plasmo

Comments

@crazyair
Copy link

crazyair commented Aug 21, 2022

What happened?

index.less content @import '~antd/dist/antd.less';

Version

Latest

What OS are you seeing the problem on?

MacOSX

What browsers are you seeing the problem on?

Microsoft Edge

Relevant log output

🔴 ERROR  | The @import path "\~antd/dist/antd.less" is using webpack specific syntax, which isn't supported by Parcel.

To @import files from node\_modules, use "antd/dist/antd.less"

But this project is OK
https://github.com/ant-design/parcel-antd/blob/master/src/style/index.less#L1

image

image

(OPTIONAL) Contact Details

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I checked the current issues for duplicate problems.
@crazyair crazyair added the bug Something isn't working label Aug 21, 2022
@javaswing
Copy link

You can try @import "antd/dist/antd.less";

@crazyair
Copy link
Author

crazyair commented Aug 21, 2022

You can try @import "antd/dist/antd.less";

xxxxx package insert @import "antd/dist/antd.less"

  "dependencies": {
    "xxxxx": "0.0.1"
  }

yarn dev

🔴 ERROR  | Inline JavaScript is not enabled. Is it set in your options?

@crazyair
Copy link
Author

parcel-bundler/parcel#907 (comment)
parcel is support~

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

@crazyair just tried this example, it seems to work for me: https://github.com/PlasmoHQ/examples/tree/main/with-ant-design

The tilde import means from the root of the current project for Plasmo (and latest Parcel), NOT from node_modules: https://docs.plasmo.com/workflows/faq#tilde-import-resolution

@louisgv louisgv changed the title [BUG] less not support ~ [QA] tilde resolves in less Aug 21, 2022
@crazyair
Copy link
Author

crazyair commented Aug 21, 2022

~ It is used in the external package,Not in this project.

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

can you give me the project you're trying to use? Or is it antd itself?

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

Also, if they use ~ internally, that project should really bundle their style instead of exporting raw less (i.e, you should use the css file as end-user). I don't see the benefit of using less that way - bundle is the same?

@crazyair
Copy link
Author

crazyair commented Aug 21, 2022

You can install @ant-design/pro-components in with-ant-design

popup.tsx

import { ProCard } from '@ant-design/pro-components';

<ProCard title="默认尺寸" extra="extra" tooltip="这是提示" style={{ maxWidth: 300 }}>
    <div>Card content</div>
    <div>Card content</div>
    <div>Card content</div>
</ProCard>
🔴 ERROR  | The @import path "\~antd/es/style/themes/index.less" is using webpack specific syntax, which isn't supported by Parcel.

To @import files from node\_modules, use "antd/es/style/themes/index.less"
···

@crazyair
Copy link
Author

demo: PlasmoHQ/examples#15

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

This is a "missing feature" within Parcel's less transformer: https://github.com/parcel-bundler/parcel/blob/v2/packages/transformers/less/src/LessTransformer.js#L115 OR more likely, inconsistency with parcel's definition of the ~ tilde path.

Some potential solutions:

  1. Fix the less transformer linked above to parse internal ~ from node_modules (do a package.json lookup to ensure it always resolve to the root of its specific module).
  2. Fix https://github.com/ant-design/pro-components/blob/master/packages/card/src/components/Actions/index.less and all of their component to NOT use ~, or if they do, replace them with non-tilde path on publish

@crazyair can you link me the doc for the tilde path import in less? Also, would you be down to investigate and implement a solution?

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

parcel-bundler/parcel#907 (comment) parcel is support~

NOTE: These examples you're referencing uses parcel@1.x (same with the antd example). As of 2.x, parcel has its own definition of ~ and thus broke this less feature.

@crazyair
Copy link
Author

crazyair commented Aug 21, 2022

  1. Fix the less transformer linked above to parse internal ~ from node_modules (do a package.json lookup to ensure it always resolve to the root of its specific module).

external project Deleting ~, an error will be reported

@import '~antd/dist/antd.less'; to @import 'antd/dist/antd.less';

🔴 ERROR  | Inline JavaScript is not enabled. Is it set in your options?

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

  1. Fix the less transformer linked above to parse internal ~ from node_modules (do a package.json lookup to ensure it always resolve to the root of its specific module).

external project Deleting ~, an error will be reported

@import '~antd/dist/antd.less'; to @import 'antd/dist/antd.less';

🔴 ERROR  | Inline JavaScript is not enabled. Is it set in your options?

Do you have the lessrc option in your project?

@crazyair
Copy link
Author

Do you have the lessrc option in your project?

The main project has, but the external project does not

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

Can you do a bundling of the external project into some plain component, and just import the output inside plasmo and use it that way then?

@crazyair
Copy link
Author

how?

@louisgv louisgv changed the title [QA] tilde resolves in less [QA] webpack-like tilde resolves in less Aug 21, 2022
@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

Create a sibling-level project to your extension project, compile them with webpack (or any bundler that has the behavior described), then import them into the plasmo project.

https://lesscss.org/features/ <- based on this, I confirm the tilde resolve you're referencing from antd-pro is not a thing in less, and is only a thing within antd's webpack configuration (and maybe the webpack ecosystem?).

antd pro less loader is here: https://github.com/ant-design/pro-components/blob/master/webpack.config.js#L108

Also, try using the specific component that's been compiled and see if it works for you: https://www.npmjs.com/package/@ant-design/pro-card

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

Also, don't use the es version in that bundle, use the cjs version

@crazyair
Copy link
Author

Also, don't use the es version in that bundle, use the cjs version

Why? We currently require the use of ES

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

Also, don't use the es version in that bundle, use the cjs version

Why? We currently require the use of ES

Why would that matters for a dependency? Their ES version requires custom resolver for those less path

@crazyair
Copy link
Author

Only one path is referenced, lib or es.

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

Only one path is referenced, lib or es.

you can import directly like @import 'antd-pro/dist/some-pro-comp';

@crazyair
Copy link
Author

I've tried everything but still haven't solved it 😭

@crazyair
Copy link
Author

Only one path is referenced, lib or es.

you can import directly like @import 'antd-pro/dist/some-pro-comp';

no dist,need change less variable

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

ls .\node_modules\@ant-design\pro-card\dist\

    Directory: P:\Projects\plasmo-test\with-antd\node_modules\@
ant-design\pro-card\dist

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2022-08-21    12:31          15777 card.css
-a---          2022-08-21    12:31         243527 card.js
-a---          2022-08-21    12:31            771 card.js.LICENSE.txt
-a---          2022-08-21    12:31            431 card.less
-a---          2022-08-21    12:31          15777 card.min.css
-a---          2022-08-21    12:31         243531 card.min.js
-a---          2022-08-21    12:31            771 card.min.js.LICENSE.txt

^ There's a dist directory if you installed the specific pro-card package

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

I've tried everything but still haven't solved it 😭

have you tried hacking the less transformer? Basically, make it align with the way webpack bundle less source as antd. Also I have no idea how other project uses antd-pro unless they have a similar webpack bundler OR postcss plugin to reconfigure those paths.

@crazyair
Copy link
Author

No problem with webpack or umi

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

@crazyair in Umi for it to work with latest antd-pro, you also need to hack up the less path on webpack config:

https://v3.umijs.org/guide/upgrade-antd-pro-to-umi-3#%E9%85%8D%E7%BD%AE%E5%B1%82%E8%BF%81%E7%A7%BB

So yeah for it to work with parcel, you will need to play around with the less transformer as well and maybe handle the antd special case, OR if antd managed to make their convention widespread enough, implement it as default

@crazyair
Copy link
Author

How is parcel configured and can it be built-in?

@louisgv
Copy link
Contributor

louisgv commented Aug 21, 2022

Yes, see this comment: #143 (comment)

@crazyair
Copy link
Author

Second, I've tried, but I can't

@louisgv louisgv changed the title [QA] webpack-like tilde resolves in less [RFC] webpack-like tilde resolves in less Aug 21, 2022
@louisgv louisgv added enhancement New feature or request help wanted Extra attention is needed and removed bug Something isn't working labels Aug 21, 2022
@xudaolong
Copy link

@crazyair I encountered the same problem, the solution is parcel-bundler/parcel#6623 (comment)

@crazyair
Copy link
Author

crazyair commented Sep 14, 2022

@crazyair I encountered the same problem, the solution is parcel-bundler/parcel#6623 (comment)

node_moudles ? no

@xudaolong
Copy link

@crazyair You can use a script similar to the one executed after postinstall.

const fs = require("fs-extra")

const previous = {
  same: [
    // ISSUE: https://github.com/PlasmoHQ/plasmo/issues/143
    [
      "node_modules_cover/less/default-options.js",
      "node_modules/.pnpm/less@4.1.3/node_modules/less/lib/less/default-options.js"
    ]
  ]
}

;(async () => {
  ;[...previous["same"]].map(([source, target]) => {
    fs.copy(source, target)
      .then(() => console.log(`${target}: load success`))
      .catch((err) => console.log(err))
    return null
  })
})()


@louisgv louisgv added the with-ant-design Questions about integrating ant-design with Plasmo label Sep 14, 2022
@PlasmoHQ PlasmoHQ locked and limited conversation to collaborators Sep 14, 2022
@louisgv louisgv converted this issue into discussion #208 Sep 14, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
enhancement New feature or request help wanted Extra attention is needed with-ant-design Questions about integrating ant-design with Plasmo
Projects
None yet
Development

No branches or pull requests

4 participants