-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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 #11257 - Apply "files" npm whitelist at build time #11404
Issue #11257 - Apply "files" npm whitelist at build time #11404
Conversation
I'm thinking that we also probably want to fail the build if there is a |
Thanks for the advice @gaearon, I can do a file check after |
We use files like Does this make sense? |
Awesome @gaearon.
It won't be exposed in npm packages as it's not whitelisted in package.json. |
Hmm. Good point, I haven't though about this. |
52a4d3b
to
af7f44f
Compare
Hi @gaearon, please review the updated code as per our discussion above. |
Can you rebase now? |
af7f44f
to
9a0189b
Compare
Done, please review. Thanks @gaearon. |
e2d1516
to
507357f
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.
Nits.
scripts/rollup/packaging.js
Outdated
const packageJson = resolve(`${from}/package.json`); | ||
const whitelistedFiles = fs.existsSync(packageJson) | ||
? require(packageJson).files | ||
: null; |
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.
Maybe just fall back to []
? Then you don't have to check for existence later.
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.
Agree. Also need to check require(packageJson).files
is not undefined
, as files entry is missing in some package.json(e.g. react-call-return).
Updated to
const whitelistedFiles = (fs.existsSync(packageJson) && require(packageJson).files) || [];
And deleted the existance check below.
if (whitelistedFiles && whitelistedFiles.length > 0) {}
if (whitelistedFiles.length > 0) {}
scripts/rollup/packaging.js
Outdated
} | ||
}); | ||
if (whitelistedFiles && whitelistedFiles.length > 0) { | ||
let asyncCopyProxies = []; |
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.
Useless assignment: it's reassigned right afterwards. Let's combine?
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.
Agree, done.
scripts/rollup/packaging.js
Outdated
if (whitelistedFiles && whitelistedFiles.length > 0) { | ||
let asyncCopyProxies = []; | ||
asyncCopyProxies = npmRootFiles.reduce((proxies, file) => { | ||
if ( |
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.
A comment here would help understand what this does and why
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.
Comments added
scripts/rollup/packaging.js
Outdated
return proxies; | ||
}, asyncCopyProxies); | ||
return Promise.all([ | ||
...asyncCopyProxies, |
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.
Maybe promisesForForwardingModules
?
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.
hehe, sorry, didn't realise I typed these promises as proxies.
Variables renamed.
b4ddd28
to
cf2656b
Compare
Thanks @gaearon. All updated, please see my comments. |
Shouldn't we also fail if an entry point is not in |
Good point, the problem is some package like
I'm going to check the existence of Another defect l noticed, wildcard patterns( Working on these two issues now, talk soon. |
This actually sounds like a problem we should fix in that package. 😉 |
I fixed |
cf2656b
to
d445df9
Compare
No problem, thanks for the fix. |
scripts/rollup/packaging.js
Outdated
`'files' field is missing from package.json in package ${packageName}` | ||
); | ||
process.exit(1); | ||
} else { |
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.
Since this is a hard exit, can you remove the else
and just leave it after this code unindented?
879231a
to
8b33c91
Compare
- Terminate the build if 'files' field is missing from package.json - Terminate the build if any entry point doesn’t have equivalent in ./npm folder - Handle patterns in 'files' field
9a988d9
to
703fb32
Compare
Yes, makes sense, please review, thanks @gaearon. |
Seems like file permissions were changed accidentally?
|
scripts/rollup/packaging.js
Outdated
fs.mkdirSync(to); | ||
let existingDirCache = {}; | ||
// looping through entries(single file / directory / pattern) in `files` | ||
const whitelistedFiles = whitelist.reduce((list, pattern) => { |
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.
This logic is a bit complicated. Is there any module we can use for this? Or maybe there's some simpler way to write it? At least let's extract it to a separate function.
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.
Thanks @gaearon, some modules may help with bits and pieces, but I'm a little hesitant including any as new dev dependencies, considering we already use things like ncp
and glob
(which these modules are also build upon).
The other reason I did't use a prebuild module is I don't want to encapsulate tasks together. For example, glob
composes a whiteListedFiles list during the copy process, which is used later for checking all entry points are whitelisted. If a module doing things like copy with glob
, we'll have to run glob
again separately to get the list.
node-mkdirp can help creating nested directories. Do you think I can add it as dev dependency?
I will extract it to a separate function, make the code flow more intuitive.
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.
node-mkdirp seems fine. Why didn't we need it before?
Thanks @gaearon, |
This still seems complicated. My biggest issue is we're essentially reimplementing npm's I think it would make more sense if we used What do you think? |
Sorry, I'm a little lost here. Do you mind explaining the idea using |
I was thinking that maybe we need to change the build step to be:
This way we simulate what would actually happen during npm publish.
That's true. But I added a suite of tests that run on bundles in |
Here we'll still copy everything inside
I think npm pack and unpack is pretty handy to prepare the build packages, but not much of help validating the packages, as they just silently skip any missing files. So all the validation need to be handled in the test suite. I'm happy to give it a try. |
Yes, but this is enough because our test suite covers all public API (or at least it should—if not, we should fix the test suite), and >92% tests run on bundles. |
We decided to go with #11750 instead. |
Compare files/folders in package/npm to "files" entries in package.json, only copy whitelisted files/folders to build.