Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
rollup-plugin-inject
Advanced tools
Scan modules for global variables and inject `import` statements where necessary
The rollup-plugin-inject package is a Rollup plugin that allows you to automatically inject variables or modules into your code. This can be useful for polyfills, shims, or any other global variables that you want to ensure are available in your bundle.
Injecting Global Variables
This feature allows you to inject global variables into your code. In this example, the $ and _ variables are injected from the 'jquery' and 'lodash' modules respectively.
const inject = require('rollup-plugin-inject');
module.exports = {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'iife'
},
plugins: [
inject({
$: 'jquery',
_: 'lodash'
})
]
};
Injecting Custom Code
This feature allows you to inject custom code from a specific file. In this example, the 'myGlobal' variable is injected from the 'src/myGlobal.js' file.
const inject = require('rollup-plugin-inject');
module.exports = {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'iife'
},
plugins: [
inject({
include: '**/*.js',
modules: {
myGlobal: 'src/myGlobal.js'
}
})
]
};
The rollup-plugin-replace package allows you to replace strings in files while bundling. It is similar to rollup-plugin-inject in that it can be used to inject code, but it works by replacing specific strings rather than injecting variables or modules.
The rollup-plugin-alias package allows you to create module aliases, which can be used to simplify import statements. While it doesn't inject variables or modules directly, it can be used to achieve similar results by aliasing modules to shorter or more convenient paths.
The rollup-plugin-commonjs package converts CommonJS modules to ES6, so they can be included in a Rollup bundle. It is similar to rollup-plugin-inject in that it helps manage module imports, but it focuses on compatibility with CommonJS modules.
Scan modules for global variables and inject import
statements where necessary
This repository has been archived and is in the process of being migrated to a new monorepo. Please bear with us as we make this transition. More information to follow.
npm install --save-dev rollup-plugin-inject
import { rollup } from 'rollup';
import inject from 'rollup-plugin-inject';
rollup({
entry: 'main.js',
plugins: [
inject({
// control which files this plugin applies to
// with include/exclude
include: '**/*.js',
exclude: 'node_modules/**',
/* all other options are treated as modules...*/
// use the default – i.e. insert
// import $ from 'jquery'
$: 'jquery',
// use a named export – i.e. insert
// import { Promise } from 'es6-promise'
Promise: [ 'es6-promise', 'Promise' ],
// use a namespace import – i.e. insert
// import * as fs from 'fs'
fs: [ 'fs', '*' ],
// use a local module instead of a third-party one
'Object.assign': path.resolve( 'src/helpers/object-assign.js' ),
/* ...but if you want to be careful about separating modules
from other options, supply `options.modules` instead */
modules: {
$: 'jquery',
Promise: [ 'es6-promise', 'Promise' ],
'Object.assign': path.resolve( 'src/helpers/object-assign.js' )
}
})
]
}).then(...)
FAQs
Scan modules for global variables and inject `import` statements where necessary
We found that rollup-plugin-inject demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.