π£οΈ Webpack improvements #1
Replies: 17 comments
-
+1 for this, one of the painful parts of the build for me is that any time the template is updated I have to rewrite portions of the new template. Currently:
If the config was extendable so that it easier to update that would be awesome. |
Beta Was this translation helpful? Give feedback.
This comment was marked as disruptive content.
This comment was marked as disruptive content.
-
I personally was thinking of something a lot more strait forward in the sense of requiring a lot let work from us. |
Beta Was this translation helpful? Give feedback.
-
@farfromrefug an example is in the PR: NativeScript/nativescript-cli#5381 We could probably call into hooks with that. I would go for explicit inclusion: plugins can export a function users need to call from their webpack configs: const { baseVueConfig } = require('@nativescript/webpack')
const setupSomePlugin = require('someplugin/setup') // naming can be a convention we decide
module.exports = env => {
// allows making changes to env prior to calling the base config
const config = baseVueConfig(env)
// apply rules from the plugin by calling it's exported setup function
// it's free to do any modifications to the config
// optionally - it could be called prior to calling the base config, since these would be plain functions
setupSomePlugin(env, config);
// allows making changes/additions to the base config here
return config;
} Magically including webpack configs could lead to hard to debug/track down build issues in my opinion. |
Beta Was this translation helpful? Give feedback.
-
I dont want to do it magically. I am thinking more in the lines of doing it in the right order: |
Beta Was this translation helpful? Give feedback.
This comment was marked as disruptive content.
This comment was marked as disruptive content.
This comment was marked as disruptive content.
This comment was marked as disruptive content.
-
@NathanaelA I agree with you on most things |
Beta Was this translation helpful? Give feedback.
-
@NathanaelA That is one of my goals as well - however the CLI is already fairly coupled to webpack, and decoupling it is out of scope for this project. I would love to revisit it at a later point though, especially now that the bundler space is very much improving and alive (vite, esbuild, parcel etc.).
That's just twisting my words. That's not what I suggested...
I feel like managing the flavor specific configuration in the As for the example usage you are suggesting - it's beyond what webpack api's provide, introducing a new "DSL" and complexity is not what we should be focusing on, but we can definitely iterate on the idea! Auto-discovery of webpack configs can be much simpler, and easy to opt-out. Plugins could ship a configuration file in a standard location, with the standard webpack syntax: // the file-name is debatable, since non-ns plugins could potentially include a webpack config if not packaged correctly
// node_modules/nativescript-plugin-example/webpack.config.js
// the only deviation from a standard webpack config would be the second parameter
module.exports = (env, config) => {
// the plugin is free to make changes to the config
config.plugins.push(new CopyWebpackPlugin({ /* ... */ }))
} The project configuration would then explicitly call a const { baseVueConfig, applyPluginConfigs } = require('@nativescript/webpack')
module.exports = env => {
// allows making changes to env prior to calling the base config
const config = baseVueConfig(env)
// apply rules from all plugins that have customizations
applyPluginConfigs(env, config)
// optionally, the user can be specific by passing the plugin names
applyPluginConfigs(env, config, {
// this syntax would even allow passing parameters to plugins
'nativescript-plugin-example': {
someOption: true,
maxSomething: 200
}
})
// allows making changes/additions to the base config here
return config;
} The syntax and API is up for discussion, there are missing scenarios that we can solve by providing different methods:
This approach would get us closer to best-of-both-worlds. |
Beta Was this translation helpful? Give feedback.
This comment was marked as disruptive content.
This comment was marked as disruptive content.
-
The details aren't clear yet - as we are still moving things around and figuring stuff out, but the progress on the PR looks like it will be able to solve all the issues you mentioned, as we opted to use webpack-chain which gives us the flexibility to modify anything in the config. A very early example of it is something like: tap into the existing // set up ts support in vue files
config.module
.rule('ts')
.use('ts-loader')
.loader('ts-loader')
.tap((options) => {
return {
...options,
appendTsSuffixTo: [/\.vue$/],
};
}); How the |
Beta Was this translation helpful? Give feedback.
This comment was marked as disruptive content.
This comment was marked as disruptive content.
-
@NathanaelA Yes, it is in the PR, but far from something useful - we only just started: We set up jest for testing, here's what the vue stuff looks so far (this isn't the actual config, rather a human-readable version from webpack-chain using It builds on top of |
Beta Was this translation helpful? Give feedback.
-
Very curious about webpack-chain. Looks nifty; might do the trick. Iβve felt very limited by straight up extension of Webpack configs (how weβre doing it right now). Hacking around with plugins.find() and plugins.splice() has been very messy. And defining extra WebpackDefinePlugin definitions wasnβt fun, either. Worst of all has been trying to roll back CopyWebpackPlugin because it is invoked with a new (and mutually exclusive) API in the base config, yet thereβs no way to inherit it and swap it out without instantiating an instance of CopyWebpackPlugin (which inevitably causes a crash if youβve deliberately installed the older version of the plugin, due to the incompatible construction args). I couldnβt think of an elegant answer to the frictions I encountered (though a few user- and dev-unfriendly options came to mind), so Iβm very interested to see what options webpack-chain opens up through its builder pattern. Good find, @rigor789! |
Beta Was this translation helpful? Give feedback.
-
I suggest we take a look at https://github.com/GoogleChromeLabs/worker-plugin. The current worker-loader implementation is based on https://github.com/webpack-contrib/worker-loader Mainly we don't need stuff like |
Beta Was this translation helpful? Give feedback.
-
@edusperoni seems like Webpack 5 supports workers out of the box, with a slightly different syntax: const worker = new Worker(new URL('./worker.js', import.meta.url)); I've tested the worker-plugin and it doesn't work with webpack5, and the built-in way is mentioned in their docs:
This still needs testing - or perhaps if the worker-plugin releases a new version that just converts the syntax to the webpack syntax, that would be pretty nice! |
Beta Was this translation helpful? Give feedback.
-
Closing in favor of the pending RFC: #2 Please continue discussion under the RFC! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@nativescript/webpack
Webpack configs are currently handled by having separate config templates for different project flavors. These templates are copied in the project's root folder in an npm postinstall hook (unless a config already exists). The correct config is determined based on installed dependencies.
Making changes to the official webpack configs is difficult as the changes usually have to be repeated for each template. After an update, users need to manually update their configs, or alternatively delete their existing ones and install
@nativescript/webpack
again in order to re-generate the config. This is generally not an issue, but many times projects need to customize the configs, making the update process even more difficult.Project Overview
Create a new package in the workspace
packages/webpack5
- it would still be published as@nativescript/webpack
. The reason is to be able to start from scratch and rebuild it from the ground up with maintenance and better usage in mind - and allow to maintain current configs without interference.Simplify maintenance by standardizing custom loader implementations, and their location in a
loaders
folder (or where it makes sense).Design Goals
baseConfig
baseConfig
(chainable)The
baseConfig
would generally be a regular webpack config that exports a functionA flavor specific config would extend the
baseConfig
:Users would have a
webpack.config.js
that extends from the flavor config:Beta Was this translation helpful? Give feedback.
All reactions