Skip to content

Commit 2748d5d

Browse files
committed
Updates involving webpack
1 parent 2eed60c commit 2748d5d

File tree

5 files changed

+74
-41
lines changed

5 files changed

+74
-41
lines changed

MIGRATING.md

+49-24
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ been using the feature, you can take the following steps:
3939
4040
## Webpack
4141
42-
Starting with `v4.0.0` of this tool, Webpack is no longer required and is disabled by default for new applications. This can simplify development and result in shorter build times.
42+
Starting with `v4.0.0` of this tool, webpack is no longer required and is disabled by default for new applications. This can simplify development and result in shorter build times.
4343
44-
You may still wish to use Webpack if you need some of the features it provides, e.g., the ability to use loaders, asset modules, module replacement, dynamic imports, etc.
44+
You may still wish to use webpack if you need some of the features it provides, e.g., the ability to use loaders, asset modules, module replacement, dynamic imports, etc.
4545
46-
To migrate away from using Webpack, make the following changes in your `./compute-js` directory:
46+
To migrate away from using webpack, make the following changes in your `./compute-js` directory:
4747
48-
* First, check your `webpack.config.js` file to make sure you aren't actually depending on any custom Webpack features. When you're ready, continue to the next step.
48+
* First, check your `webpack.config.js` file to make sure you aren't actually depending on any custom webpack features. When you're ready, continue to the next step.
4949
* Delete `webpack.config.js`.
5050
* Modify `static-publish.rc.js`:
5151
* Change the line `module.exports = {` to `const config = {`
@@ -65,27 +65,52 @@ To migrate away from using Webpack, make the following changes in your `./comput
6565
}
6666
```
6767
68-
If you aren't moving away from Webpack just yet, check that your `webpack.config.js` is up-to-date:
68+
If you aren't moving away from webpack just yet, check that your `webpack.config.js` is up-to-date. Refer
69+
to the [default `webpack.config.js` in this package](./resources/webpack.config.js) and add your changes,
70+
or modify your configuration file using the following steps:
6971
70-
* Starting `v3.0.0`, we depend on `v1.0.0` of the `js-compute` library, which provides namespaced exports for Fastly
71-
features. To use them, you'll need to add a new `externals` array to the bottom if it doesn't exist already, with
72-
the following entry:
72+
* To make the resulting bundle easier to debug, it is recommended to set the `devtool` value to `false`.
7373
74-
```javascript
75-
module.exports = {
76-
/* ... other config ... */
77-
externals: [
78-
({request,}, callback) => {
79-
if (/^fastly:.*$/.test(request)) {
80-
return callback(null, 'commonjs ' + request);
81-
}
82-
callback();
83-
}
74+
* The JavaScript SDK automatically adds the named condition `fastly` when resolving dependency packages.
75+
To match the behavior when bundling with webpack, set `resolve.conditionsNames` to the following:
76+
```
77+
resolve: {
78+
conditionNames: [
79+
'fastly',
80+
'...',
81+
],
8482
],
85-
}
86-
```
83+
```
84+
85+
* Starting `v3.0.0`, we depend on `v1.0.0` of the `js-compute` library, which provides namespaced exports for Fastly
86+
features. To use them, you'll need to make the following changes to `webpack.config.js`:
87+
88+
* Set the `target` value to `false`.
89+
90+
* The `output` section should look like this:
91+
```
92+
output: {
93+
filename: "index.js",
94+
path: path.resolve(__dirname, "bin"),
95+
chunkFormat: 'commonjs',
96+
library: {
97+
type: 'commonjs',
98+
},
99+
},
100+
```
101+
102+
* Add a new `externals` array to the bottom if it doesn't exist already. Add the following entry:
103+
104+
```javascript
105+
module.exports = {
106+
/* ... other config ... */
107+
externals: [
108+
/^fastly:.*$/,
109+
],
110+
}
111+
```
87112
88-
* Starting `v3.0.0`, we no longer use Webpack static assets to include the contents of static files, and instead [use the
113+
* Starting `v3.0.0`, we no longer use webpack static assets to include the contents of static files, and instead [use the
89114
`includeBytes` function](https://js-compute-reference-docs.edgecompute.app/docs/fastly:experimental/includeBytes)
90115
to enable more performant loading, as well as a more size-efficient Wasm binary. As a result, the following code can
91116
safely be removed from the `module.rules` array.
@@ -113,7 +138,7 @@ If you aren't moving away from Webpack just yet, check that your `webpack.config
113138
},
114139
```
115140

116-
If you need Webpack for a new project you are scaffolding with this site, specify the `--webpack` command-line option
141+
If you need webpack for a new project you are scaffolding with this site, specify the `--webpack` command-line option
117142
when you scaffold your application, e.g.:
118143

119144
```
@@ -217,7 +242,7 @@ See [static-publish.rc.js config file](./README.md#static-publish-rc) for a deta
217242
build = "npm run build"
218243
```
219244

220-
* If you're using Webpack, then the `scripts` section of `package.json` of your `compute-js` directory should contain
245+
* If you're using webpack, then the `scripts` section of `package.json` of your `compute-js` directory should contain
221246
the following items (along with any other scripts):
222247
```json
223248
{
@@ -226,7 +251,7 @@ See [static-publish.rc.js config file](./README.md#static-publish-rc) for a deta
226251
}
227252
```
228253

229-
* If you're not using Webpack, then the `scripts` section of `package.json` of your `compute-js` directory should
254+
* If you're not using webpack, then the `scripts` section of `package.json` of your `compute-js` directory should
230255
contain the following items (along with any other scripts):
231256
```json
232257
{

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ npm run deploy
6464
- Content and metadata are available to your application, accessible by files' pre-package file paths.
6565
- Brotli and Gzip compression.
6666
- Support for `If-None-Match` and `If-Modified-Since` request headers.
67-
- Optionally use Webpack as a module bundler.
67+
- Optionally use webpack as a module bundler.
6868
- Selectively serve files from Fastly's [KV Store](#kv-store), or embedded into your Wasm module.
6969
- Supports loading JavaScript files as code into your Compute application.
7070
- Presets for several static site generators.
@@ -583,10 +583,10 @@ publishing event.
583583

584584
See the definition of `ContentAssetMetadataMapEntry` in the [`types/content-assets` file](./src/types/content-assets.ts) for more details.
585585

586-
### Using Webpack
586+
### Using webpack
587587

588-
As of v4, Webpack is no longer required, and is no longer part of the default scaffolded application.
589-
If you wish to use some features of Webpack, you may include Webpack in your generated application by specifying
588+
As of v4, webpack is no longer required, and is no longer part of the default scaffolded application.
589+
If you wish to use some features of webpack, you may include webpack in your generated application by specifying
590590
`--webpack` at the command line.
591591

592592
## Migrating

resources/webpack.config.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,40 @@ const path = require("path");
22

33
module.exports = {
44
entry: "./src/index.js",
5+
target: false,
6+
devtool: false,
57
optimization: {
68
minimize: true
79
},
8-
target: "webworker",
910
output: {
1011
filename: "index.js",
1112
path: path.resolve(__dirname, "bin"),
12-
libraryTarget: "this",
13+
chunkFormat: 'commonjs',
14+
library: {
15+
type: 'commonjs',
16+
},
1317
},
1418
module: {
1519
// Loaders go here.
1620
// e.g., ts-loader for TypeScript
1721
// rules: [
1822
// ],
1923
},
24+
resolve: {
25+
conditionNames: [
26+
'fastly',
27+
'...',
28+
],
29+
},
2030
plugins: [
21-
// Polyfills go here.
22-
// Used for, e.g., any cross-platform WHATWG,
23-
// or core nodejs modules needed for your application.
31+
// Webpack Plugins and Polyfills go here
32+
// e.g., cross-platform WHATWG or core Node.js modules needed for your application.
33+
// new webpack.ProvidePlugin({
34+
// }),
2435
],
2536
externals: [
26-
({request,}, callback) => {
27-
if (/^fastly:.*$/.test(request)) {
28-
return callback(null, 'commonjs ' + request);
29-
}
30-
callback();
31-
}
37+
// Allow webpack to handle 'fastly:*' namespaced module imports by treating
38+
// them as modules rather than trying to process them as URLs
39+
/^fastly:.*$/,
3240
],
3341
};

src/cli/commands/init-app.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ export function initApp(commandLineValues: CommandLineOptions) {
492492
console.log('KV Store Name :', kvStoreName ?? '(None)');
493493
console.log('');
494494
if (useWebpack) {
495-
console.log('Creating project with Webpack.');
495+
console.log('Creating project with webpack.');
496496
console.log('');
497497
}
498498

src/cli/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const optionDefinitions: OptionDefinition[] = [
2222
{ name: 'suppress-framework-warnings', type: Boolean },
2323
{ name: 'output', alias: 'o', type: String, defaultValue: './compute-js', },
2424

25-
// Whether the scaffolded project should use Webpack to bundle assets.
25+
// Whether the scaffolded project should use webpack to bundle assets.
2626
{ name: 'webpack', type: Boolean, defaultValue: false },
2727

2828
// The 'root' directory for the publishing.

0 commit comments

Comments
 (0)