Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add declaration files #7118

Merged
merged 7 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/build/webpack/loaders/next-minify-loader.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function formatWebpackMessages(json: any): any;
1 change: 1 addition & 0 deletions packages/next/export/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function(dir: string, options: any, configuration?: any): Promise<void>
5 changes: 3 additions & 2 deletions packages/next/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function(options: any): any
2 changes: 1 addition & 1 deletion packages/next/server/next.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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