-
Notifications
You must be signed in to change notification settings - Fork 34
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
issue-132 - dedupe insertStyle in output files #139
Conversation
bd21f66
to
c581ccc
Compare
Well done sir! (👏) Good work !!
|
In order to test this scenario I updated In addition I also added a new test: Note Since we are basically testing strings, for the two tests involved in this PR, I opted out to use snapshot testing. Tip I think we can migrate all tests to snapshots system, |
7f3f8e5
to
634f13b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updates look good minus a couple of call outs I listed:
* @see insertStyle.ts for additional information | ||
* Let rollup handle import by processing insertStyle as a module | ||
*/ | ||
imports = `import ${insertFnName} from '${__dirname}/insertStyle.js';\n`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Path here should be rollup-plugin-sass/dist/insertStyle
instead (since files generated here are consumed on the user-land side).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this is not feasible:
if we use rollup-plugin-sass/dist/insertStyle
as path rollup
won't be able to resolve it.
This because rollup
doesn't resolve imports with node by default, to do so you need node-resolve
plugin.
Since the import is not resolved, rollup
will not include the insertStyle code in the bundle.
On the contrary using __dirname
will fill the import with an absolute path that rollup
can resolve correctly.
__dirname
points to dist
folder which also contain insertStyle
so there should be no problem using it.
(I already tested this in my app and is working)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~~Right though, to your first point, shouldn't we assume that anyone that wants modules from node_modules/
to be included in their app bundles, would add additional configuration for said modules to be included? (something like @rollup/plugin-node-resolve
) ~~ - Read this too early in the morning - I get what you're saying about node-resolve
- Is there a way we can get the relative path to the file instead? - Revealing the users system path in artifacts is a code smell and reveals information that could be used against the authors.
To your second point, you are correct, however, doing this causes the users system path to be revealed in their artifacts when adding external: [/\/insertStyle\.js$/]
to a rollup config - see test file (in PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly the complete file system path is not inlined when using the plugin via node_modules
.
I did a test on my work app (using [email protected]
):
Anyway I'll try to use relative path. Maybe something could be achieved via path.relative
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~~Ok, gotcha, no need though, seems the full path only gets rendered when external: [/\/insertStyle\.js$/]
is set (in the app's rollup config, which wouldn't make sense if insertStyle
is required for an app, in this case). ~~
I would say let's setup an 'examples/using-preserve-modules' app example which we could use to validate the behavior, and allow others to do the same, this way we can ensure that everything will work as expected (opening a ticket and PR for this shortly).
Disregard comments above - Just ran an additional test and indeed when when insertStyle
isn't excluded via external: [/\/insertStyle\.js$/]
module path doesn't include the (OS) absolute path - Moving ahead with merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I must have left the external
setting during some tryouts I've done earlier 😅.
I'll see if I can remove it when migrating to snapshots.
Thank you for the good work @marcalexiei 👍 . |
Thanks for the work on this plugin!
Description
This PR aims to remove the duplication of
insertStyle
function in each chunk / bundle produced byrollup
__$insertStyle
function is inserted only once per bundle #132Proposed solution
When
insert
option is set totrue
,instead of adding the code of
insertStyle
on the top of each file via theintro
hook,the
insertStyle
function is now included via animport
which points tosrc/insertStyle.ts
file.Note
Using the
import
,rollup
will take care to include the function code only once per bundle (or output only one file ifpreserveModules
function is used).Important
Since
rollup
process ESModules theinsertStyle
file should not be compiled usingCommonJS
modules but usingES6
modules.Because of this I added 2
tsconfig.build
files to produce the two different outputs.Tip
When migrating this package to ESM these two files can be safely removed.