Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

support multi-entrypoint bundling(webpack) #144

Merged
merged 3 commits into from
Jun 17, 2020

Conversation

W1U02
Copy link
Contributor

@W1U02 W1U02 commented Jun 16, 2020

  • add multiply entrypoint support for webpack-plugin
    • change bundled file name to [name].[hash].js and [name].[hash].css
    • use filename as entrypoint name, if there are duplicated name, throw an error
  • fix a bug caused by introducing buildOptions

@W1U02 W1U02 requested a review from a team as a code owner June 16, 2020 02:22
@FredKSchott
Copy link
Owner

Awesome! Thanks for contributing. @drwpow want to review this one?

@FredKSchott FredKSchott requested a review from drwpow June 16, 2020 05:50
@W1U02
Copy link
Contributor Author

W1U02 commented Jun 16, 2020

It still only work for SPA, but can support the case that have more than one local js file.

const jsOutputPattern = args.outputPattern.js || "js/bundle-[hash].js";
const cssOutputPattern = args.outputPattern.css || "css/style-[hash].css";
const jsOutputPattern = args.outputPattern.js || "js/[name].[hash].js";
const cssOutputPattern = args.outputPattern.css || "css/[name].[hash].css";
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Good improvement here

Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 should we use [contenthash] here instead of [hash] (hash based on file contents)? If you don’t have an opinion, we can leave it for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree.

[contenthash] should be better than [hash]. I leave it unmodified just because it use [hash] before.

I will make an update.

//Find all local script, use it as the entrypoint
const scripts = Array.from(dom.window.document.querySelectorAll("script"))
.filter(el => el.type.trim().toLowerCase() === "module")
.filter(el => !/^[a-zA-Z]+:\/\//.test(el.src));
Copy link
Contributor

Choose a reason for hiding this comment

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

This is easier for me to follow. Nice! 👍

const parsedPath = path.parse(src);
const name = parsedPath.name;
if (entries.name !== undefined) {
throw new Error(`Duplictate script with name ${name}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

sp: Duplicate

Copy link
Contributor

@drwpow drwpow Jun 16, 2020

Choose a reason for hiding this comment

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

I like the approach you did here of using the JS filename as the entry name. While collisions can probably be a little confusing here for users (“Duplicate script? Where? How do I fix that?”), I think this error message is as good as it can be. I’d love to add a little more info on how this works, maybe on https://www.snowpack.dev/plugins. Because this is an “official” plugin

const jsFile = assetFiles.find((d) => d.endsWith(".js"));
const cssFile = assetFiles.find((d) => d.endsWith(".css"));
script.removeAttribute("type");
script.src = path.posix.join(baseUrl, jsFile);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to this PR, just thinking out loud: I think this path.posix.join will probably print backslashes on Windows, which is not what we want. We should probably replace this with a web-friendly, forward-slash-only method in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

path.posix.join will force path to join with a /, both in windows and linux.

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

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

This looks great! Thanks for adding this!

@drwpow
Copy link
Contributor

drwpow commented Jun 16, 2020

@W1U02 the spelling error is the only change you might want to make; otherwise it’s amazing. Ready to merge whenever you’re ready!

change [hash] to [contenthash]
fix typo
@W1U02
Copy link
Contributor Author

W1U02 commented Jun 17, 2020

@drwpow thanks for the comments. I've make an update.

change [hash] to [contenthash]
fix typo duplictate to duplicate

@drwpow drwpow merged commit afa8930 into FredKSchott:master Jun 17, 2020
@drwpow
Copy link
Contributor

drwpow commented Jun 17, 2020

Awesome work, @W1U02! 🙏

@W1U02 W1U02 deleted the mult-entry-webpack branch June 18, 2020 01:49
@haikyuu
Copy link

haikyuu commented Jun 19, 2020

Can someone help me with buildOptions error. How can i fix it while waiting for the new release?

Where should i include the buildOptions object? I tried in a bunch of places but none worked

@drwpow
Copy link
Contributor

drwpow commented Jun 19, 2020

@haikyuu buildOptions goes in your snowpack.config.js file, which you can read more about here: https://www.snowpack.dev/#configuration

@haikyuu
Copy link

haikyuu commented Jun 20, 2020

Thank you @drwpow

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants