Skip to content

Commit

Permalink
Move plugins and loaders to webpack folder (#4618)
Browse files Browse the repository at this point in the history
Also adds basic `flow` to webpack.js and the plugins.
  • Loading branch information
timneutkens authored Jun 16, 2018
1 parent 82f2e08 commit d674dcc
Show file tree
Hide file tree
Showing 13 changed files with 2,084 additions and 16 deletions.
25 changes: 17 additions & 8 deletions build/webpack.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow
import type {NextConfig} from '../server/config'
import path, {sep} from 'path'
import webpack from 'webpack'
import resolve from 'resolve'
Expand All @@ -6,12 +8,12 @@ import CaseSensitivePathPlugin from 'case-sensitive-paths-webpack-plugin'
import WriteFilePlugin from 'write-file-webpack-plugin'
import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin'
import {getPages} from './webpack/utils'
import PagesPlugin from './plugins/pages-plugin'
import NextJsSsrImportPlugin from './plugins/nextjs-ssr-import'
import DynamicChunksPlugin from './plugins/dynamic-chunks-plugin'
import UnlinkFilePlugin from './plugins/unlink-file-plugin'
import PagesManifestPlugin from './plugins/pages-manifest-plugin'
import BuildManifestPlugin from './plugins/build-manifest-plugin'
import PagesPlugin from './webpack/plugins/pages-plugin'
import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import'
import DynamicChunksPlugin from './webpack/plugins/dynamic-chunks-plugin'
import UnlinkFilePlugin from './webpack/plugins/unlink-file-plugin'
import PagesManifestPlugin from './webpack/plugins/pages-manifest-plugin'
import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
import {SERVER_DIRECTORY, NEXT_PROJECT_ROOT, NEXT_PROJECT_ROOT_NODE_MODULES, NEXT_PROJECT_ROOT_DIST, DEFAULT_PAGES_DIR} from '../lib/constants'

function externalsConfig (dir, isServer) {
Expand Down Expand Up @@ -48,7 +50,14 @@ function externalsConfig (dir, isServer) {
return externals
}

export default async function getBaseWebpackConfig (dir, {dev = false, isServer = false, buildId, config}) {
type BaseConfigContext = {|
dev: boolean,
isServer: boolean,
buildId: string,
config: NextConfig
|}

export default async function getBaseWebpackConfig (dir: string, {dev = false, isServer = false, buildId, config}: BaseConfigContext) {
const defaultLoaders = {
babel: {
loader: 'next-babel-loader',
Expand Down Expand Up @@ -121,7 +130,7 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer
modules: [
NEXT_PROJECT_ROOT_NODE_MODULES,
'node_modules',
path.join(__dirname, 'loaders'),
path.join(__dirname, 'webpack', 'loaders'), // The loaders Next.js provides
...nodePathList // Support for NODE_PATH environment variable
]
},
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import babelLoader from 'babel-loader'

module.exports = babelLoader.custom(babel => {
const presetItem = babel.createConfigItem(require('../babel/preset'), {type: 'preset'})
const presetItem = babel.createConfigItem(require('../../babel/preset'), {type: 'preset'})
const reactJsxSourceItem = babel.createConfigItem(require('@babel/plugin-transform-react-jsx-source'), {type: 'plugin'})

const configs = new Set()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import { RawSource } from 'webpack-sources'
import {BUILD_MANIFEST} from '../../lib/constants'
import {BUILD_MANIFEST} from '../../../lib/constants'

// This plugin creates a build-manifest.json for all assets that are being output
// It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import { RawSource } from 'webpack-sources'
import {PAGES_MANIFEST, ROUTE_NAME_REGEX} from '../../lib/constants'
import {PAGES_MANIFEST, ROUTE_NAME_REGEX} from '../../../lib/constants'

// This plugin creates a pages-manifest.json from page entrypoints.
// This is used for mapping paths like `/` to `.next/dist/bundles/pages/index.js` when doing SSR
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// @flow
import { ConcatSource } from 'webpack-sources'
import {
IS_BUNDLED_PAGE_REGEX,
ROUTE_NAME_REGEX
} from '../../lib/constants'
} from '../../../lib/constants'

class PageChunkTemplatePlugin {
apply (chunkTemplate) {
Expand Down Expand Up @@ -42,7 +43,7 @@ class PageChunkTemplatePlugin {
}

export default class PagesPlugin {
apply (compiler) {
apply (compiler: any) {
compiler.plugin('compilation', (compilation) => {
compilation.chunkTemplate.apply(new PageChunkTemplatePlugin())
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// @flow
import { join } from 'path'
import promisify from '../../lib/promisify'
import promisify from '../../../lib/promisify'
import fs from 'fs'
import { IS_BUNDLED_PAGE_REGEX } from '../../lib/constants'
import { IS_BUNDLED_PAGE_REGEX } from '../../../lib/constants'

const unlink = promisify(fs.unlink)

export default class UnlinkFilePlugin {
prevAssets: any
constructor () {
this.prevAssets = {}
}

apply (compiler) {
apply (compiler: any) {
compiler.plugin('after-emit', (compilation, callback) => {
const removed = Object.keys(this.prevAssets)
.filter((a) => IS_BUNDLED_PAGE_REGEX.test(a) && !compilation.assets[a])
Expand Down
67 changes: 67 additions & 0 deletions flow-typed/npm/uglifyjs-webpack-plugin_vx.x.x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// flow-typed signature: e7e7f59f1cdd9c963584b4620763fec3
// flow-typed version: <<STUB>>/uglifyjs-webpack-plugin_v1.1.6/flow_v0.73.0

/**
* This is an autogenerated libdef stub for:
*
* 'uglifyjs-webpack-plugin'
*
* Fill this stub out by replacing all the `any` types.
*
* Once filled out, we encourage you to share your work with the
* community by sending a pull request to:
* https://github.com/flowtype/flow-typed
*/

declare module 'uglifyjs-webpack-plugin' {
declare module.exports: any;
}

/**
* We include stubs for each file inside this npm package in case you need to
* require those files directly. Feel free to delete any files that aren't
* needed.
*/
declare module 'uglifyjs-webpack-plugin/dist/cjs' {
declare module.exports: any;
}

declare module 'uglifyjs-webpack-plugin/dist/index' {
declare module.exports: any;
}

declare module 'uglifyjs-webpack-plugin/dist/uglify/index' {
declare module.exports: any;
}

declare module 'uglifyjs-webpack-plugin/dist/uglify/minify' {
declare module.exports: any;
}

declare module 'uglifyjs-webpack-plugin/dist/uglify/versions' {
declare module.exports: any;
}

declare module 'uglifyjs-webpack-plugin/dist/uglify/worker' {
declare module.exports: any;
}

// Filename aliases
declare module 'uglifyjs-webpack-plugin/dist/cjs.js' {
declare module.exports: $Exports<'uglifyjs-webpack-plugin/dist/cjs'>;
}
declare module 'uglifyjs-webpack-plugin/dist/index.js' {
declare module.exports: $Exports<'uglifyjs-webpack-plugin/dist/index'>;
}
declare module 'uglifyjs-webpack-plugin/dist/uglify/index.js' {
declare module.exports: $Exports<'uglifyjs-webpack-plugin/dist/uglify/index'>;
}
declare module 'uglifyjs-webpack-plugin/dist/uglify/minify.js' {
declare module.exports: $Exports<'uglifyjs-webpack-plugin/dist/uglify/minify'>;
}
declare module 'uglifyjs-webpack-plugin/dist/uglify/versions.js' {
declare module.exports: $Exports<'uglifyjs-webpack-plugin/dist/uglify/versions'>;
}
declare module 'uglifyjs-webpack-plugin/dist/uglify/worker.js' {
declare module.exports: $Exports<'uglifyjs-webpack-plugin/dist/uglify/worker'>;
}
Loading

0 comments on commit d674dcc

Please sign in to comment.