-
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
83 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,108 @@ | ||
import { ApiMeta } from '../../../../components/ApiMeta.tsx'; | ||
import WebpackLicense from '../../../../components/webpack-license'; | ||
|
||
<WebpackLicense from="https://webpack.js.org/plugins/provide-plugin/" /> | ||
|
||
# ProvidePlugin | ||
|
||
<ApiMeta addedVersion={'0.3.3'} /> | ||
|
||
This plugin will automatically load modules instead of having to import or require them everywhere. | ||
Automatically load modules instead of having to `import` or `require` them everywhere. | ||
|
||
```js | ||
new rspack.ProvidePlugin({ | ||
identifier: 'module1', | ||
// ... | ||
}); | ||
``` | ||
|
||
or | ||
|
||
```js | ||
new rspack.ProvidePlugin(options); | ||
new rspack.ProvidePlugin({ | ||
identifier: ['module1', 'property1'], | ||
// ... | ||
}); | ||
``` | ||
|
||
- options | ||
- **Type:** `Record<string, string | string[]>` | ||
By default, module resolution path is current folder (`./**`) and `node_modules`. | ||
|
||
When using the following configuration: | ||
It is also possible to specify full path: | ||
|
||
```js | ||
const rspack = require('@rspack/core'); | ||
module.exports = { | ||
plugins: [ | ||
new rspack.ProvidePlugin({ | ||
process: [require.resolve('process/browser')], | ||
}), | ||
], | ||
}; | ||
const path = require('path'); | ||
|
||
new rspack.ProvidePlugin({ | ||
identifier: path.resolve(path.join(__dirname, 'src/module1')), | ||
// ... | ||
}); | ||
``` | ||
|
||
Which will convert the following code: | ||
Whenever the `identifier` is encountered as free variable in a module, the `module` is loaded automatically and the `identifier` is filled with the exports of the loaded `module` (or `property` in order to support named exports). | ||
|
||
For importing the default export of an ES2015 module, you have to specify the default property of module. | ||
|
||
## Options | ||
|
||
- **Type:** `Record<string, string | string[]>` | ||
|
||
## Examples | ||
|
||
### Using process in the Browser | ||
|
||
Enable `process` object support within a browser context. | ||
|
||
```js | ||
new rspack.ProvidePlugin({ | ||
process: [require.resolve('process/browser')], | ||
}); | ||
``` | ||
|
||
the piece of code: | ||
|
||
```js | ||
console.log(process.version); | ||
``` | ||
|
||
To: | ||
will be transformed behind the scenes to: | ||
|
||
```js | ||
import process from 'process/browser'; | ||
console.log(process.version); | ||
``` | ||
|
||
### jQuery | ||
|
||
To automatically load `jquery` we can point both variables it exposes to the corresponding node module: | ||
|
||
```js | ||
new rspack.ProvidePlugin({ | ||
$: 'jquery', | ||
jQuery: 'jquery', | ||
}); | ||
``` | ||
|
||
Then in any of our source code: | ||
|
||
```js | ||
// in a module | ||
$('#item'); // <= works | ||
jQuery('#item'); // <= also works | ||
// $ is automatically set to the exports of module "jquery" | ||
``` | ||
|
||
### Lodash Map | ||
|
||
```js | ||
new rspack.ProvidePlugin({ | ||
_map: ['lodash', 'map'], | ||
}); | ||
``` | ||
|
||
### Vue.js | ||
|
||
```js | ||
new rspack.ProvidePlugin({ | ||
Vue: ['vue/dist/vue.esm.js', 'default'], | ||
}); | ||
``` |