From f3c940d0f4463ae75ab6b68f00c9f746bd92520c Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Wed, 18 Mar 2020 20:31:47 +0900 Subject: [PATCH] chore(gatsby): migrate webpack-plugins to TypeScript --- .../{webpack-plugins.js => webpack-plugins.ts} | 14 +++++--------- packages/gatsby/src/utils/webpack-utils.js | 12 ++++++------ 2 files changed, 11 insertions(+), 15 deletions(-) rename packages/gatsby/src/utils/{webpack-plugins.js => webpack-plugins.ts} (88%) diff --git a/packages/gatsby/src/utils/webpack-plugins.js b/packages/gatsby/src/utils/webpack-plugins.ts similarity index 88% rename from packages/gatsby/src/utils/webpack-plugins.js rename to packages/gatsby/src/utils/webpack-plugins.ts index 3c4e8fa6a30d0..269aeb9f9489a 100644 --- a/packages/gatsby/src/utils/webpack-plugins.js +++ b/packages/gatsby/src/utils/webpack-plugins.ts @@ -1,13 +1,11 @@ -// @flow +import webpack, { Plugin } from "webpack" -const webpack = require(`webpack`) - -const plugin = (name, optimize) => { - const Plugin = (optimize ? webpack.optimize : webpack)[name] - return (...args: any) => new Plugin(...args) +const plugin = (name: string, optimize?: boolean): Plugin => { + const WebpackPlugin = (optimize ? webpack.optimize : webpack)[name] + return (...args: any): Plugin => new WebpackPlugin(...args) } -const plugins = { +export const builtinPlugins = { normalModuleReplacement: plugin(`NormalModuleReplacementPlugin`), contextReplacement: plugin(`ContextReplacementPlugin`), ignore: plugin(`IgnorePlugin`), @@ -53,5 +51,3 @@ const plugins = { occurrenceOrder: plugin(`OccurrenceOrderPlugin`, true), moduleConcatenation: plugin(`ModuleConcatenationPlugin`, true), } - -module.exports = plugins diff --git a/packages/gatsby/src/utils/webpack-utils.js b/packages/gatsby/src/utils/webpack-utils.js index 5e42bdf321e5c..f9aa0fb587fa9 100644 --- a/packages/gatsby/src/utils/webpack-utils.js +++ b/packages/gatsby/src/utils/webpack-utils.js @@ -11,7 +11,7 @@ const isWsl = require(`is-wsl`) const GatsbyWebpackStatsExtractor = require(`./gatsby-webpack-stats-extractor`) const GatsbyWebpackEslintGraphqlSchemaReload = require(`./gatsby-webpack-eslint-graphql-schema-reload-plugin`) -const builtinPlugins = require(`./webpack-plugins`) +import { builtinPlugins } from "./webpack-plugins" const eslintConfig = require(`./eslint-config`) type LoaderSpec = string | { loader: string, options?: Object } @@ -132,7 +132,7 @@ module.exports = async ({ const makeExternalOnly = (original: RuleFactory<*>) => ( options = {} ): Rule => { - let rule = original(options) + const rule = original(options) rule.include = vendorRegex return rule } @@ -140,7 +140,7 @@ module.exports = async ({ const makeInternalOnly = (original: RuleFactory<*>) => ( options = {} ): Rule => { - let rule = original(options) + const rule = original(options) rule.exclude = vendorRegex return rule } @@ -307,7 +307,7 @@ module.exports = async ({ * and packages that depend on `gatsby` */ { - let js = ({ modulesThatUseGatsby = [], ...options } = {}) => { + const js = ({ modulesThatUseGatsby = [], ...options } = {}) => { return { test: /\.(js|mjs|jsx)$/, include: modulePath => { @@ -342,7 +342,7 @@ module.exports = async ({ * Excludes modules that use Gatsby since the `rules.js` already transpiles those */ { - let dependencies = ({ modulesThatUseGatsby = [] } = {}) => { + const dependencies = ({ modulesThatUseGatsby = [] } = {}) => { const jsOptions = { babelrc: false, configFile: false, @@ -391,7 +391,7 @@ module.exports = async ({ } { - let eslint = schema => { + const eslint = schema => { return { enforce: `pre`, test: /\.jsx?$/,