Skip to content

Commit d42a85b

Browse files
feat: adding support for webpack externals (#115)
* feat: working on externals support * feat: working on externals support * feat: adding support for webpack externals externals rewrites the externalModule and makes its module.id equal to the dependency request. * chore: updating demos removing yarn links and adding externals configs for testing * refactor: removing unused plugin options removing multiple plugin options * build: clear previous build before compile * docs: removing unused options from docs * refactor: remove commented out code * docs: Updating readme with externals section * fix: normalizing externals config * v2.2.0-beta.0 * style: linting * refactor: rename demo site folders * style: linting
1 parent 5df5925 commit d42a85b

26 files changed

+663
-110
lines changed

README.md

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<img src="docs/webpack-external-import.png" width="40%" alt="webpack-external-import" />
2727
</p>
2828

29-
> This project has been proposed for implementation into the Webpack core (with some rewrites and refactors). Track the progress and share the issue for wider exposure if you are interested in seeing this become part of Webpack. I believe a system like this would offer great benefits for the JavaSciprt community. Fingers crossed! https://github.com/webpack/webpack/issues/10352
29+
> This project has been proposed for implementation into the Webpack core (with some rewrites and refactors). Track the progress and share the issue for wider exposure if you are interested in seeing this become part of Webpack. I believe a system like this would offer great benefits for the JavaSciprt community. Fingers crossed! https://github.com/webpack/webpack/issues/10352
3030
3131
```shell
3232
$ yarn add webpack-external-import
@@ -55,7 +55,7 @@ yarn add webpack-external-import
5555
Major rewrite which has taken the original concept and built it directly into webpack runtime.
5656
A big achievement in this release is **tree-shaking support**
5757

58-
If you want to read me about what this tool does.
58+
If you want to read me about what this tool does.
5959

6060
Read the following:
6161

@@ -203,8 +203,8 @@ const URLImportPlugin = require("webpack-external-import/webpack");
203203

204204
Pretend we have two separate apps that each have their _independent_ build. We want to share a module from one of our apps with the other.
205205

206-
To do this, you must add an `externalize` object to `package.json`.
207-
The `externalize` object tells the plugin to make the module accessible through a predictable name.
206+
To do this, you must add an `interleave` object to `package.json`.
207+
The `interleave` object tells the plugin to make the module accessible through a predictable name.
208208

209209
For example:
210210

@@ -229,12 +229,43 @@ __webpack_require__
229229
This ensures a easy way for other consumers, teams, engineers to look up what another project or team is willing
230230
to allow for interleaving
231231

232+
## Working with Webpack Externals
233+
234+
Its important to follow the instructions below if you are planning to use Webpack externals.
235+
This plugin must be installed on all builds - it is intended that the build creating providing external is built by this plugin.
236+
Externals work best in scenarios where the "host" app should supplying dependencies to an interleaved one.
237+
238+
**Providing Externals**
239+
To support webpack externals, you will need to use `provideExternals` to specify externals
240+
241+
**Note:** you must use `provideExternals` **instead** of the webpack `externals` option.
242+
243+
```js
244+
new URLImportPlugin({
245+
provideExternals: {
246+
react: "React"
247+
}
248+
});
249+
```
250+
251+
**Consuming Externals**
252+
To consume externals, you will need to use `useExternals` to inform webpack that the interleaved app should use the module specified by `provideExternals`
253+
254+
```js
255+
new URLImportPlugin({
256+
useExternals: {
257+
react: "React"
258+
}
259+
});
260+
```
261+
232262
## Full Example
233263

234-
WEBSITE-ONE
235-
app.js
264+
**WEBSITE-ONE**
236265

237266
```js
267+
// app.js
268+
238269
import React, { Component } from "react";
239270
import { ExternalComponent } from "webpack-external-import";
240271
import HelloWorld from "./components/goodbye-world";
@@ -299,17 +330,14 @@ Promise.all([
299330
});
300331
```
301332

302-
WEBSITE-TWO:
303-
package.json
333+
**WEBSITE-TWO**
304334

305335
```json
336+
// package.json
337+
306338
{
307339
"name": "website-two",
308340
"version": "0.0.0-development",
309-
"repository": {
310-
"type": "git",
311-
"url": "https://github.com/faceyspacey/remixx.git"
312-
},
313341
"author": "Zack Jackson <[email protected]> (https://github.com/ScriptedAlchemy)",
314342
"interleave": {
315343
"src/components/Title/index.js": "TitleComponentWithCSSFile",
@@ -322,47 +350,19 @@ package.json
322350
## API:
323351

324352
```js
325-
// Website Two - webpack.config.js
326-
327353
module.exports = {
328-
output: {
329-
publicPath
330-
},
331-
plugins: [
332-
new URLImportPlugin({
333-
manifestName: "website-two",
334-
fileName: "importManifest.js",
335-
basePath: ``,
336-
publicPath: `//localhost:3002/`,
337-
transformExtensions: /^(gz|map)$/i,
338-
writeToFileEmit: false,
339-
filter: null,
340-
debug: true,
341-
map: null,
342-
generate: null,
343-
sort: null
344-
})
345-
]
346-
};
347-
348-
// Website One webpack.config.js
349-
module.exports = {
350-
output: {
351-
publicPath
352-
},
353354
plugins: [
354355
new URLImportPlugin({
355356
manifestName: "website-one",
356357
fileName: "importManifest.js",
357358
basePath: ``,
358359
publicPath: `//localhost:3001/`,
359-
transformExtensions: /^(gz|map)$/i,
360360
writeToFileEmit: false,
361361
seed: null,
362362
filter: null,
363363
debug: true,
364-
map: null,
365-
generate: null
364+
useExternals: {},
365+
provideExternals: {}
366366
})
367367
]
368368
};
@@ -413,19 +413,20 @@ Default: `src`
413413

414414
Test resource path to see if plugin should apply transformations
415415

416-
### `options.generate`
416+
### `options.useExternals`
417417

418-
Type: `Function(Object, FileDescriptor): Object`<br>
419-
Default: `(seed, files) => files.reduce((manifest, {name, path}) => ({...manifest, [name]: path}), seed)`
418+
Type: `Object`<br>
419+
Default: `{}`
420420

421-
Create the manifest. It can return anything as long as it's serializable by `JSON.stringify`. [FileDescriptor typings](#filedescriptor)
421+
Informs the webpack treat the following dependencies as externals.
422+
Works the same way externals does.
422423

423-
### `options.serialize`
424+
### `options.provideExternals`
424425

425-
Type: `Function(Object): string`<br>
426-
Default: `(manifest) => JSON.stringify(manifest, null, 2)`
426+
Type: `Object`<br>
427+
Default: `{}`
427428

428-
Output manifest file in a different format then json (i.e., yaml).
429+
Informs webpack to provide the dependencies listed in the object to other apps using `useExternals`
429430

430431
### **ExternalComponent**
431432

manual/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"workspaces": [
44
"website1",
55
"website2",
6-
"website3"
6+
"website3",
7+
"website4"
78
],
89
"name": "external-import-demo",
910
"version": "0.0.0-development",

manual/webpack/webpackConfigFactory.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,9 @@ module.exports = (siteId, options) => {
3737
fileName: "importManifest.js",
3838
basePath: ``,
3939
publicPath: `//localhost:300${siteId}/`,
40-
transformExtensions: /^(gz|map)$/i,
4140
writeToFileEmit: false,
42-
seed: null,
4341
filter: null,
4442
debug: true,
45-
map: null,
46-
generate: null,
47-
sort: null
4843
}),
4944
new HtmlWebpackPlugin({
5045
template: templatePath,

manual/website2/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"author": "Zack Jackson <[email protected]> (https://github.com/ScriptedAlchemy)",
99
"license": "GPL-3.0-only",
1010
"scripts": {
11-
"manual:prod": "cd ../../ && yarn link && cd manual/website2 && yarn link webpack-external-import && cross-env NODE_ENV=production webpack && cp serve.json dist/serve.json && serve dist -l 3002",
12-
"manual:dev": "cd ../../ && yarn link && cd manual/website2 && yarn link webpack-external-import && cross-env NODE_ENV=development node ../node_modules/webpack-dev-server/bin/webpack-dev-server.js",
13-
"manual:debug": "cd ../../ && yarn link && cd manual/website2 && yarn link webpack-external-import && cross-env NODE_ENV=development node --inspect ../node_modules/webpack-dev-server/bin/webpack-dev-server.js"
11+
"manual:prod": "cd ../../ && cd manual/website2 && cross-env NODE_ENV=production webpack && cp serve.json dist/serve.json && serve dist -l 3002",
12+
"manual:dev": "cd ../../ && cd manual/website2 && cross-env NODE_ENV=development node node_modules/webpack-dev-server/bin/webpack-dev-server.js",
13+
"manual:debug": "cd ../../ && cd manual/website2 && cross-env NODE_ENV=production node --inspect node_modules/webpack-dev-server/bin/webpack-dev-server.js"
1414
},
1515
"interleave": {
1616
"src/components/Title/index.js": "TitleComponent",

manual/website3/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"author": "Zack Jackson <[email protected]> (https://github.com/ScriptedAlchemy)",
99
"license": "GPL-3.0-only",
1010
"scripts": {
11-
"manual:prod": "cd ../../ && yarn link && cd manual/website3 && yarn link webpack-external-import && cross-env NODE_ENV=production webpack && cp serve.json dist/serve.json && serve dist -l 3003",
12-
"manual:dev": "cd ../../ && yarn link && cd manual/website3 && yarn link webpack-external-import && cross-env NODE_ENV=development node ../node_modules/webpack-dev-server/bin/webpack-dev-server.js",
13-
"manual:debug": "cd ../../ && yarn link && cd manual/website3 && yarn link webpack-external-import && cross-env NODE_ENV=development node ../node_modules/webpack-dev-server/bin/webpack-dev-server.js"
11+
"manual:prod": "cd ../../ && cd manual/website3 && cross-env NODE_ENV=production webpack && cp serve.json dist/serve.json && serve dist -l 3003",
12+
"manual:dev": "cd ../../ && cd manual/website3 && cross-env NODE_ENV=development node node_modules/webpack-dev-server/bin/webpack-dev-server.js",
13+
"manual:debug": "cd ../../ && cd manual/website3 && cross-env NODE_ENV=development node node_modules/webpack-dev-server/bin/webpack-dev-server.js"
1414
},
1515
"interleave": {
1616
"src/components/Title/index.js": "TitleComponentWithCSSFile",

manual/website3/webpack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const webpack = require("webpack");
21
const ExtractCssChunks = require("extract-css-chunks-webpack-plugin");
32
const configFactory = require("../webpack/webpackConfigFactory");
43

manual/website4/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

manual/website4/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2+
3+
## Available Scripts
4+
5+
In the project directory, you can run:
6+
7+
### `yarn start`
8+
9+
Runs the app in the development mode.<br />
10+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11+
12+
The page will reload if you make edits.<br />
13+
You will also see any lint errors in the console.
14+
15+
### `yarn test`
16+
17+
Launches the test runner in the interactive watch mode.<br />
18+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19+
20+
### `yarn build`
21+
22+
Builds the app for production to the `build` folder.<br />
23+
It correctly bundles React in production mode and optimizes the build for the best performance.
24+
25+
The build is minified and the filenames include the hashes.<br />
26+
Your app is ready to be deployed!
27+
28+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29+
30+
### `yarn eject`
31+
32+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33+
34+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35+
36+
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37+
38+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39+
40+
## Learn More
41+
42+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43+
44+
To learn React, check out the [React documentation](https://reactjs.org/).
45+
46+
### Code Splitting
47+
48+
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49+
50+
### Analyzing the Bundle Size
51+
52+
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53+
54+
### Making a Progressive Web App
55+
56+
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57+
58+
### Advanced Configuration
59+
60+
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61+
62+
### Deployment
63+
64+
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65+
66+
### `yarn build` fails to minify
67+
68+
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify

manual/website4/package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "website4",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^4.2.4",
7+
"@testing-library/react": "^9.3.2",
8+
"@testing-library/user-event": "^7.1.2",
9+
"react": "^16.12.0",
10+
"react-dom": "^16.12.0",
11+
"react-scripts": "3.3.1"
12+
},
13+
"scripts": {
14+
"start": "react-scripts start",
15+
"build": "react-scripts build",
16+
"test": "react-scripts test",
17+
"eject": "react-scripts eject"
18+
},
19+
"eslintConfig": {
20+
"extends": "react-app"
21+
},
22+
"browserslist": {
23+
"production": [
24+
">0.2%",
25+
"not dead",
26+
"not op_mini all"
27+
],
28+
"development": [
29+
"last 1 chrome version",
30+
"last 1 firefox version",
31+
"last 1 safari version"
32+
]
33+
}
34+
}

manual/website4/public/favicon.ico

3.08 KB
Binary file not shown.

0 commit comments

Comments
 (0)