-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vendor whatwg-fetch using rollup+devDep
- Loading branch information
Showing
4 changed files
with
54 additions
and
36 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
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
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,34 +1,52 @@ | ||
const commonjs = require('@rollup/plugin-commonjs'); | ||
const resolve = require('@rollup/plugin-node-resolve'); | ||
const typescript = require('rollup-plugin-typescript'); | ||
const pkg = require('./package.json'); | ||
const fs = require('fs'); | ||
const commonjs = require("@rollup/plugin-commonjs"); | ||
const resolve = require("@rollup/plugin-node-resolve"); | ||
const typescript = require("rollup-plugin-typescript"); | ||
const pkg = require("./package.json"); | ||
const fs = require("fs"); | ||
const dedent = require("dedent"); | ||
const globals = { | ||
'whatwg-fetch': 'FakeFetch', | ||
'fake-xml-http-request': 'FakeXMLHttpRequest', | ||
'route-recognizer': 'RouteRecognizer', | ||
'url-parse': 'urlParse' | ||
"fake-xml-http-request": "FakeXMLHttpRequest", | ||
"route-recognizer": "RouteRecognizer", | ||
"url-parse": "urlParse" | ||
}; | ||
|
||
const rollupTemplate = fs.readFileSync('./iife-wrapper.js').toString(); | ||
const [banner, footer] = rollupTemplate.split('/*==ROLLUP_CONTENT==*/'); | ||
const rollupTemplate = fs.readFileSync("./iife-wrapper.js").toString(); | ||
const [banner, footer] = rollupTemplate.split("/*==ROLLUP_CONTENT==*/"); | ||
|
||
let importableInNode = { | ||
transform(code, id) { | ||
let before = dedent` | ||
if (typeof global !== "undefined" && typeof global.self === 'undefined') { | ||
global.self = {}; | ||
global.__pretenderNodePolyfill = true; | ||
}`; | ||
|
||
let after = dedent` | ||
if (typeof global !== "undefined" && global.__pretenderNodePolyfill) { | ||
delete global.self; | ||
delete global.__pretenderNodePolyfill; | ||
}`; | ||
|
||
return id.match(/whatwg-fetch/) ? `${before}\n${code}\n${after}` : code; | ||
} | ||
}; | ||
|
||
module.exports = { | ||
input: 'src/index.ts', | ||
input: "src/index.ts", | ||
external: Object.keys(pkg.dependencies), | ||
output: [ | ||
{ | ||
name: 'Pretender', | ||
name: "Pretender", | ||
file: pkg.main, | ||
format: 'iife', | ||
format: "iife", | ||
globals, | ||
banner, | ||
footer | ||
}, | ||
{ | ||
file: pkg.module, | ||
format: 'es' | ||
format: "es" | ||
} | ||
], | ||
plugins: [commonjs(), resolve(), typescript()] | ||
plugins: [commonjs(), resolve(), importableInNode, typescript()] | ||
}; |
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