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

Write mode #69

Closed
wants to merge 7 commits into from
Closed

Write mode #69

wants to merge 7 commits into from

Conversation

huv1k
Copy link
Contributor

@huv1k huv1k commented Aug 24, 2019

This PR introduces write mode to webpack-asset-relocator-loader, this enables emmit files with writing. In normal mode, it would be skipped, because the file doesn't exist. Some libraries need to write to file system during development to save files. For example Nexus is using to saving output SDL schema, what is a side effect of creating the schema with code first approach.

How it works

In write mode computePureStaticValue is skipped for fsWriteSymbols, these symbols are used for emitting new assets. This allows for creating new files or folders during runtime.

Problems

In Next.js sources are transpiled with webpack and output it's into .next folder. This folder is in a different location, so the current relative path to the context it's not working. This PR adds relativeToSource what adds __webpack_require__.ac what is a relative path from output to the source file. This simulates actual __dirname functionality.

Questions

Do we want to write a 2nd test suite just for relativeToSource?

@huv1k huv1k changed the title Write mode [WIP - Write mode Aug 24, 2019
@huv1k huv1k changed the title [WIP - Write mode [WIP] - Write mode Aug 24, 2019
@huv1k huv1k changed the title [WIP] - Write mode Write mode Aug 30, 2019
@codecov-io
Copy link

codecov-io commented Aug 30, 2019

Codecov Report

Merging #69 into master will increase coverage by 0.24%.
The diff coverage is 96.29%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #69      +/-   ##
==========================================
+ Coverage   76.01%   76.25%   +0.24%     
==========================================
  Files          10       10              
  Lines        1113     1133      +20     
==========================================
+ Hits          846      864      +18     
- Misses        267      269       +2
Impacted Files Coverage Δ
src/asset-relocator.js 83.89% <96.29%> (+0.5%) ⬆️
src/utils/merge-source-maps.js 0% <0%> (-4.66%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5ca1642...6765e1f. Read the comment docs.

@huv1k huv1k requested review from guybedford and styfle August 30, 2019 13:21
Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't say that I've reviewed all the implications of this - it's got a lot of interactions, but left one comment re the analysis trigger.

@@ -804,6 +838,11 @@ module.exports = async function (content, map) {
// if we have a direct pure static function,
// and that function has a [TRIGGER] symbol -> trigger asset emission from it
if (calleeValue && typeof calleeValue.value === 'function' && calleeValue.value[TRIGGER]) {
// Skip static analysis for write operations -> to replace for example __dirname
// It's not possible analyze not existing files
if(options.writeMode && parent && parent.callee && parent.callee.property && fsWriteSymbols[parent.callee.property.name]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you absolutely sure this code path ever gets hit? I would have thought it would have caught typeof caleeValue.value === 'symbol' given the use of FS_FN?

Note that an easy way to do the write detection in that case is just to have a FS_WRITE_FN symbol detected in the switch statement a few lines down.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It hits this path, I added a test to cover this use case. It's function, I tested it on different cases and it was always function.

@styfle styfle requested review from Timer and removed request for styfle August 30, 2019 14:11
Copy link
Member

@Timer Timer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need explicit __filename support too? It seems only __dirname was handled. Please see other review comments as well.

@@ -48,6 +48,8 @@ Any `.node` files included will also support binary relocation.
// defined that should not be emitted
existingAssetNames: []
wrapperCompatibility: false, // optional, default
writeMode: false // optional, default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"writeMode" makes the option sound like you're switching from "read mode" to "write mode".

Let's rename this to be writeSupport or relocateWrites.

@@ -578,7 +606,7 @@ module.exports = async function (content, map) {
const result = evaluate(expr, vars, computeBranches);
return result;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@@ -307,7 +335,7 @@ module.exports = async function (content, map) {
// injection to set __webpack_require__.ab
const options = getOptions(this);

injectPathHook(this._compilation, options.outputAssetBase);
injectPathHook(this._compilation, options.outputAssetBase, options.relativeToSource);
Copy link
Member

@Timer Timer Sep 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the behavior for this option -- if you set relativeToSource: true it seems to rewrite both reads and writes to be relative to source? This almost seems like it'd never be what you want.

Wouldn't typical behavior be (for this option) to want to rewrite reads but leave writes in their original location? I guess this wouldn't work for an app that reads-its-own-writes. 🤔

Also, can you please point me to a library that needs write support? I've brought it up a couple times, but I'd rather land this in Next.js without write support first.

@styfle
Copy link
Member

styfle commented May 27, 2021

Closing since I don't think this is relevant anymore. Feel free to re-open otherwise 🙂

@styfle styfle closed this May 27, 2021
@styfle styfle deleted the write-mode branch May 27, 2021 13:35
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

Successfully merging this pull request may close these issues.

5 participants