Skip to content

Commit

Permalink
Merge e0221d3 into 498ec61
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Fernando Alvarez D committed Apr 23, 2019
2 parents 498ec61 + e0221d3 commit 2b732b9
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/next/build/webpack/loaders/next-minify-loader.ts
Expand Up @@ -8,7 +8,7 @@ const nextMiniferLoader: loader.Loader = function(source) {
const options = loaderUtils.getOptions(this) || {}
const { error, code } = minify({
file: 'noop',
input: source,
input: source as string,
terserOptions: { ...options.terserOptions, sourceMap: false },
})

Expand Down
@@ -1,3 +1,4 @@
import { Compiler } from 'webpack'
import { RawSource } from 'webpack-sources'
import {
BUILD_MANIFEST,
Expand All @@ -9,19 +10,19 @@ import {
// 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
export default class BuildManifestPlugin {
apply (compiler) {
apply (compiler: Compiler) {
compiler.hooks.emit.tapAsync(
'NextJsBuildManifest',
(compilation, callback) => {
const { chunks } = compilation
const assetMap = { devFiles: [], pages: {} }
const assetMap: { devFiles: string[], pages: { [page: string]: string[] } } = { devFiles: [], pages: {} }

const mainJsChunk = chunks.find(
c => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN
)
const mainJsFiles =
const mainJsFiles: string[] =
mainJsChunk && mainJsChunk.files.length > 0
? mainJsChunk.files.filter(file => /\.js$/.test(file))
? mainJsChunk.files.filter((file: string) => /\.js$/.test(file))
: []

for (const filePath of Object.keys(compilation.assets)) {
Expand All @@ -44,7 +45,7 @@ export default class BuildManifestPlugin {
continue
}

const filesForEntry = []
const filesForEntry: string[] = []
for (const chunk of entrypoint.chunks) {
// If there's no name or no files
if (!chunk.name || !chunk.files) {
Expand Down Expand Up @@ -83,7 +84,7 @@ export default class BuildManifestPlugin {
assetMap.pages = Object.keys(assetMap.pages)
.sort()
// eslint-disable-next-line
.reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {})
.reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {} as any)

compilation.assets[BUILD_MANIFEST] = new RawSource(
JSON.stringify(assetMap, null, 2)
Expand Down
@@ -1,14 +1,15 @@
import { Compiler } from 'webpack'
// This plugin mirrors webpack 3 `filename` and `chunkfilename` behavior
// This fixes https://github.com/webpack/webpack/issues/6598
// This plugin is based on https://github.com/researchgate/webpack/commit/2f28947fa0c63ccbb18f39c0098bd791a2c37090
export default class ChunkNamesPlugin {
apply (compiler) {
compiler.hooks.compilation.tap('NextJsChunkNamesPlugin', (compilation) => {
apply (compiler: Compiler) {
compiler.hooks.compilation.tap('NextJsChunkNamesPlugin', (compilation: any) => {
compilation.chunkTemplate.hooks.renderManifest.intercept({
register (tapInfo) {
register (tapInfo: any) {
if (tapInfo.name === 'JavascriptModulesPlugin') {
const originalMethod = tapInfo.fn
tapInfo.fn = (result, options) => {
tapInfo.fn = (result: any, options: any) => {
let filenameTemplate
const chunk = options.chunk
const outputOptions = options.outputOptions
Expand Down
@@ -1,11 +1,12 @@
import { join, resolve, relative, dirname } from 'path'
import { Compiler } from 'webpack'

// This plugin modifies the require-ensure code generated by Webpack
// to work with Next.js SSR
export default class NextJsSsrImportPlugin {
apply (compiler) {
compiler.hooks.compilation.tap('NextJsSSRImport', (compilation) => {
compilation.mainTemplate.hooks.requireEnsure.tap('NextJsSSRImport', (code, chunk) => {
apply (compiler: Compiler) {
compiler.hooks.compilation.tap('NextJsSSRImport', (compilation: any) => {
compilation.mainTemplate.hooks.requireEnsure.tap('NextJsSSRImport', (code: string, chunk: any) => {
// Update to load chunks from our custom chunks directory
const outputPath = resolve('/')
const pagePath = join('/', dirname(chunk.name))
Expand Down
Expand Up @@ -16,10 +16,12 @@ const SSR_MODULE_CACHE_FILENAME = 'ssr-module-cache.js'
// Do note that this module is only geared towards the `node` compilation target.
// For the client side compilation we use `runtimeChunk: 'single'`
export default class NextJsSsrImportPlugin {
constructor (options) {
private options: { outputPath: string }

constructor (options: { outputPath: string }) {
this.options = options
}
apply (compiler) {
apply (compiler: webpack.Compiler) {
const { outputPath } = this.options
compiler.hooks.emit.tapAsync('NextJsSSRModuleCache', (compilation, callback) => {
compilation.assets[SSR_MODULE_CACHE_FILENAME] = new RawSource(`
Expand All @@ -28,12 +30,12 @@ export default class NextJsSsrImportPlugin {
`)
callback()
})
compiler.hooks.compilation.tap('NextJsSSRModuleCache', (compilation) => {
compiler.hooks.compilation.tap('NextJsSSRModuleCache', (compilation: any) => {
compilation.mainTemplate.hooks.localVars.intercept({
register (tapInfo) {
register (tapInfo: any) {
if (tapInfo.name === 'MainTemplate') {
const originalFn = tapInfo.fn
tapInfo.fn = (source, chunk) => {
tapInfo.fn = (source: any, chunk: any) => {
// If the chunk is not part of the pages directory we have to keep the original behavior,
// otherwise webpack will error out when the file is used before the compilation finishes
// this is the case with mini-css-extract-plugin
Expand All @@ -46,7 +48,7 @@ export default class NextJsSsrImportPlugin {
// Make sure even in windows, the path looks like in unix
// Node.js require system will convert it accordingly
const relativePathToBaseDirNormalized = relativePathToBaseDir.replace(/\\/g, '/')
return webpack.Template.asString([
return (webpack as any).Template.asString([
source,
'// The module cache',
`var installedModules = require('${relativePathToBaseDirNormalized}');`
Expand Down
Expand Up @@ -22,21 +22,22 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR
// Modified to strip out unneeded results for Next's specific use case

import url from 'url'
import { Compiler, compilation } from 'webpack'

function buildManifest (compiler, compilation) {
function buildManifest (compiler: Compiler, compilation: compilation.Compilation) {
let context = compiler.options.context
let manifest = {}
let manifest: { [k: string]: any[] } = {}

compilation.chunkGroups.forEach(chunkGroup => {
if (chunkGroup.isInitial()) {
return
}

chunkGroup.origins.forEach(chunkGroupOrigin => {
chunkGroup.origins.forEach((chunkGroupOrigin: any) => {
const { request } = chunkGroupOrigin

chunkGroup.chunks.forEach(chunk => {
chunk.files.forEach(file => {
chunkGroup.chunks.forEach((chunk: any) => {
chunk.files.forEach((file: string) => {
if (!file.match(/\.js$/) || !file.match(/^static\/chunks\//)) {
return
}
Expand Down Expand Up @@ -76,18 +77,19 @@ function buildManifest (compiler, compilation) {

manifest = Object.keys(manifest)
.sort()
// eslint-disable-next-line
.reduce((a, c) => ((a[c] = manifest[c]), a), {})
.reduce((a, c) => ((a[c] = manifest[c]), a), {} as any)

return manifest
}

export class ReactLoadablePlugin {
constructor (opts = {}) {
private filename: string

constructor (opts: { filename: string }) {
this.filename = opts.filename
}

apply (compiler) {
apply (compiler: Compiler) {
compiler.hooks.emit.tapAsync(
'ReactLoadableManifest',
(compilation, callback) => {
Expand Down
@@ -0,0 +1,14 @@
export class TerserPlugin {
readonly cpus: any
readonly distDir: any
readonly options: any

constructor(options?: any)

static isSourceMap(input: any): any
static buildSourceMap(inputSourceMap: any): any
static buildError(err: any, file: any, sourceMap: any, requestShortener?: any): any
static buildWarning(warning: any, file: any, sourceMap: any, requestShortener?: any, warningsFilter?: any): any

apply(compiler: any): void
}
@@ -1,7 +1,7 @@
/* eslint-disable
arrow-body-style
*/
import { minify as terser } from 'terser';
import { minify as terser, MinifyOptions } from 'terser';

const buildTerserOptions = ({
ecma,
Expand All @@ -18,7 +18,7 @@ const buildTerserOptions = ({
keep_fnames,
/* eslint-enable camelcase */
safari10,
} = {}) => ({
}: MinifyOptions = {}) => ({
ecma,
warnings,
parse: { ...parse },
Expand All @@ -45,15 +45,15 @@ const buildTerserOptions = ({
safari10,
});

const minify = (options) => {
const minify = (options: { file: string, input: string, inputSourceMap?: string, terserOptions?: MinifyOptions }) => {
const {
file,
input,
inputSourceMap
} = options;

// Copy terser options
const terserOptions = buildTerserOptions(options.terserOptions);
const terserOptions = buildTerserOptions(options.terserOptions) as any;

// Add source map data
if (inputSourceMap) {
Expand Down
@@ -0,0 +1 @@
export default function formatWebpackMessages(json: any): any;
1 change: 1 addition & 0 deletions packages/next/export/index.d.ts
@@ -0,0 +1 @@
export default function(dir: string, options: any, configuration?: any): Promise<void>
5 changes: 3 additions & 2 deletions packages/next/package.json
Expand Up @@ -29,8 +29,9 @@
"scripts": {
"build": "taskr",
"release": "taskr release",
"prepublish": "npm run release",
"typescript": "tsc --noEmit"
"prepublish": "npm run release && npm run types",
"typescript": "tsc --noEmit --declaration",
"types": "tsc --declaration --emitDeclarationOnly --declarationDir dist"
},
"taskr": {
"requires": [
Expand Down
@@ -1,7 +1,7 @@
import http from 'http'
import next from '../next'

export default async function start (serverOptions, port, hostname) {
export default async function start(serverOptions: any, port?: number, hostname?: string) {
const app = next(serverOptions)
const srv = http.createServer(app.getRequestHandler())
await new Promise((resolve, reject) => {
Expand Down
@@ -1,5 +1,6 @@
export function printAndExit (message, code = 1) {
export function printAndExit(message: string, code = 1) {
if (code === 0) {
// tslint:disable-next-line no-console
console.log(message)
} else {
console.error(message)
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/next.d.ts
@@ -0,0 +1 @@
export default function(options: any): any
2 changes: 1 addition & 1 deletion packages/next/server/next.js
@@ -1,5 +1,5 @@
// This file is used for when users run `require('next')`
module.exports = (options) => {
module.exports = options => {
if (options.dev) {
const Server = require('./next-dev-server').default
return new Server(options)
Expand Down
2 changes: 0 additions & 2 deletions packages/next/tsconfig.json
@@ -1,8 +1,6 @@
{
"compilerOptions": {
"strict": true,
"allowJs": true,
"noEmit": true,
"module": "esnext",
"target": "ES2017",
"esModuleInterop": true,
Expand Down
10 changes: 10 additions & 0 deletions packages/next/types/index.d.ts
Expand Up @@ -57,6 +57,16 @@ declare module 'next/dist/compiled/resolve/index.js' {
export = resolve;
}

declare module 'next/dist/compiled/text-table' {
function textTable(rows: Array<Array<{}>>, opts?: {
hsep?: string,
align?: Array<'l' | 'r' | 'c' | '.'>,
stringLength?(str: string): number
}): string;

export = textTable;
}

declare module 'next/dist/compiled/arg/index.js' {
function arg<T extends arg.Spec>(spec: T, options?: {argv?: string[], permissive?: boolean}): arg.Result<T>;

Expand Down

0 comments on commit 2b732b9

Please sign in to comment.