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

add file loader #135

Merged
merged 10 commits into from
May 29, 2020
Merged

add file loader #135

merged 10 commits into from
May 29, 2020

Conversation

viankakrisna
Copy link
Contributor

No description provided.

@viankakrisna viankakrisna changed the title Add url loader add url loader May 23, 2020
PrettyPath: prettyPath,
Contents: contents,
}

// Get the file extension
extension := ""
if lastDot := strings.LastIndexByte(path, '.'); lastDot >= 0 {
extension = path[lastDot:]
if lastDot := strings.LastIndexByte(sourcePath, '.'); lastDot >= 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this line is probably could be simplified with https://golang.org/pkg/path/#Ext thinking @evanw any reason we don't just use it?

Copy link
Owner

Choose a reason for hiding this comment

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

Sounds good to me. I probably just wasn't aware of it existing.

t.Fatalf("%s != %s", a, b)
stringA := fmt.Sprintf("%v", a)
stringB := fmt.Sprintf("%v", b)
t.Fatalf(diff.Diff(stringA, stringB))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i've added this change because it's hard to diff any whitespace differences in tests when it happens

Copy link
Owner

@evanw evanw left a comment

Choose a reason for hiding this comment

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

Thanks for giving this a shot. It looks like this is intended to address #14. Is that right?

I left some comments below.

@@ -46,7 +46,7 @@ Options:
--jsx-factory=... What to use instead of React.createElement
--jsx-fragment=... What to use instead of React.Fragment
--loader:X=L Use loader L to load file extension X, where L is
one of: js, jsx, ts, tsx, json, text, base64, dataurl
one of: js, jsx, ts, tsx, json, text, base64, url, dataurl
Copy link
Owner

Choose a reason for hiding this comment

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

This feature seems similar to the Webpack file-loader plugin, so I think people will understand the name file better than url. There is also a Rollup url plugin which does something different (it does what dataurl does in esbuild), so that may also be confusing for people coming from Rollup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's true! url-loader seems to have more specific feature in webpack world https://webpack.js.org/loaders/url-loader/ we could use url to automatically switch between dataurl and file in the future i guess. i'll rename this to file

PrettyPath: prettyPath,
Contents: contents,
}

// Get the file extension
extension := ""
if lastDot := strings.LastIndexByte(path, '.'); lastDot >= 0 {
extension = path[lastDot:]
if lastDot := strings.LastIndexByte(sourcePath, '.'); lastDot >= 0 {
Copy link
Owner

Choose a reason for hiding this comment

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

Sounds good to me. I probably just wasn't aware of it existing.

if targetFolder == "" {
targetFolder = path.Dir(bundleOptions.AbsOutputFile)
}
defer ioutil.WriteFile(path.Join(targetFolder, url), []byte(source.Contents), 0644)
Copy link
Owner

Choose a reason for hiding this comment

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

The bundler isn't supposed to write files itself. The bundler returns all of the files to be written to the caller. This makes the bundler easier to unit test and also makes it easy for the API to return results without modifying the file system (see for example #139).

Doing things this way also defeats future tree shaking optimizations, which may end up removing this module entirely. In that case the file should not be emitted in the output directory since it's unreferenced.

Instead of writing to the file system, the contents of the file should be stashed somewhere associated with the file, probably just on the parseResult or ast. Then the bundler should return the file to the caller, probably as an extra BundleResult entry. But that requires generalizing BundleResult to handle non-JavaScript result files.

Copy link
Contributor Author

@viankakrisna viankakrisna May 27, 2020

Choose a reason for hiding this comment

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

thank you! yeah this is a part where i'm not really sure. i'll try generalizing BundleResult to handle non-JavaScript result files

@viankakrisna viankakrisna changed the title add url loader add file loader May 27, 2020
Copy link
Owner

@evanw evanw left a comment

Choose a reason for hiding this comment

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

Cool, looks good to me!

@@ -635,6 +637,17 @@ func run(fs fs.FS, args argsObject) {
}
args.logInfo(fmt.Sprintf("Wrote to %s (%s)", path, toSize(len(item.JsContents))))

// Write out the additional files
for _, file := range item.AdditionalFiles {
Copy link
Owner

Choose a reason for hiding this comment

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

The contents of AdditionalFiles end up being written out twice because they are both written out here and duplicated to become normal files in this code elsewhere:

		for _, bundle := range group {
			for _, additionalFile := range bundle.AdditionalFiles {
				if additionalFile.Path != "" {
					results = append(results, BundleResult{additionalFile.Path, []byte(additionalFile.Contents), "", []byte(""), []AdditionalFile{}})
				}
			}
		}

That's ok, I can fix it after landing this.

stringA := fmt.Sprintf("%v", a)
stringB := fmt.Sprintf("%v", b)
if strings.Contains(stringA, "\n") {
t.Fatal(diff.Diff(stringA, stringB))
Copy link
Owner

Choose a reason for hiding this comment

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

I understand why you did this, but this is going to make it harder to update tests since I've been copying and pasting the output of the tests to accept the new output, and this interleaves the old and new output. Luckily it's easy to disable by just commenting out a few lines, so I don't think it's a big problem.

Really I should figure out some system of doing snapshot-style testing so that test output can be updated automatically...

@evanw evanw merged commit 12ff76f into evanw:master May 29, 2020
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.

2 participants