-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
2 changed files
with
53 additions
and
32 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
79 changes: 49 additions & 30 deletions
79
packages/gatsby/src/utils/webpack/loaders/webpack-remove-exports-loader.ts
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,42 +1,61 @@ | ||
import { LoaderContext } from "webpack" | ||
import { transformSync } from "@babel/core" | ||
/* eslint-disable @babel/no-invalid-this */ | ||
|
||
import { LoaderDefinitionFunction } from "webpack" | ||
import { transform } from "@babel/core" | ||
|
||
interface IOptions { | ||
jsx?: boolean | ||
remove?: Array<string> | ||
} | ||
|
||
module.exports = function loader( | ||
this: LoaderContext<IOptions>, | ||
source: string | ||
): string | null | undefined { | ||
const options = this.getOptions() | ||
const webpackRemoveExportsLoader: LoaderDefinitionFunction<IOptions> = | ||
function webpackRemoveExportsLoader(source, sourceMap): void { | ||
const callback = this.async() | ||
const options = this.getOptions() | ||
|
||
const result = transformSync(source, { | ||
babelrc: false, | ||
configFile: false, | ||
presets: options?.jsx | ||
? [ | ||
transform( | ||
source, | ||
{ | ||
babelrc: false, | ||
configFile: false, | ||
// @ts-ignore inputSourceMap expect object or falsy, but webpack types suggest sourceMap can be a string, | ||
// which is not compatibile with babel's transform options | ||
inputSourceMap: sourceMap, | ||
sourceFileName: this.resourcePath, | ||
sourceMaps: this.sourceMap, | ||
filename: this.resourcePath, | ||
presets: options?.jsx | ||
? [ | ||
[ | ||
require.resolve(`babel-preset-gatsby/babel-preset-react`), | ||
{ | ||
useBuiltIns: true, | ||
pragma: `React.createElement`, | ||
// jsx is used only in develop, so for now this is fine | ||
development: true, | ||
}, | ||
], | ||
] | ||
: undefined, | ||
plugins: [ | ||
[ | ||
require.resolve(`babel-preset-gatsby/babel-preset-react`), | ||
require.resolve(`../../babel/babel-plugin-remove-api`), | ||
{ | ||
useBuiltIns: true, | ||
pragma: `React.createElement`, | ||
// jsx is used only in develop, so for now this is fine | ||
development: true, | ||
apis: options?.remove ?? [], | ||
}, | ||
], | ||
] | ||
: undefined, | ||
plugins: [ | ||
[ | ||
require.resolve(`../../babel/babel-plugin-remove-api`), | ||
{ | ||
apis: options?.remove ?? [], | ||
}, | ||
], | ||
], | ||
}) | ||
], | ||
}, | ||
(err, result) => { | ||
if (err) { | ||
callback(err) | ||
} else if (result && result.code && result.map) { | ||
callback(null, result?.code, result?.map) | ||
} else { | ||
callback(null, source, sourceMap) | ||
} | ||
} | ||
) | ||
} | ||
|
||
return result?.code | ||
} | ||
module.exports = webpackRemoveExportsLoader |