Unverified Commit 254b23eb authored by Luis Fernando Alvarez D's avatar Luis Fernando Alvarez D Committed by GitHub

Add declaration files to /dist (#7118)

parent 498ec618
...@@ -8,7 +8,7 @@ const nextMiniferLoader: loader.Loader = function(source) { ...@@ -8,7 +8,7 @@ const nextMiniferLoader: loader.Loader = function(source) {
const options = loaderUtils.getOptions(this) || {} const options = loaderUtils.getOptions(this) || {}
const { error, code } = minify({ const { error, code } = minify({
file: 'noop', file: 'noop',
input: source, input: source as string,
terserOptions: { ...options.terserOptions, sourceMap: false }, terserOptions: { ...options.terserOptions, sourceMap: false },
}) })
......
import { Compiler } from 'webpack'
import { RawSource } from 'webpack-sources' import { RawSource } from 'webpack-sources'
import { import {
BUILD_MANIFEST, BUILD_MANIFEST,
...@@ -9,19 +10,19 @@ import { ...@@ -9,19 +10,19 @@ import {
// This plugin creates a build-manifest.json for all assets that are being output // 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 // It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production
export default class BuildManifestPlugin { export default class BuildManifestPlugin {
apply (compiler) { apply (compiler: Compiler) {
compiler.hooks.emit.tapAsync( compiler.hooks.emit.tapAsync(
'NextJsBuildManifest', 'NextJsBuildManifest',
(compilation, callback) => { (compilation, callback) => {
const { chunks } = compilation const { chunks } = compilation
const assetMap = { devFiles: [], pages: {} } const assetMap: { devFiles: string[], pages: { [page: string]: string[] } } = { devFiles: [], pages: {} }
const mainJsChunk = chunks.find( const mainJsChunk = chunks.find(
c => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN c => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN
) )
const mainJsFiles = const mainJsFiles: string[] =
mainJsChunk && mainJsChunk.files.length > 0 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)) { for (const filePath of Object.keys(compilation.assets)) {
...@@ -44,7 +45,7 @@ export default class BuildManifestPlugin { ...@@ -44,7 +45,7 @@ export default class BuildManifestPlugin {
continue continue
} }
const filesForEntry = [] const filesForEntry: string[] = []
for (const chunk of entrypoint.chunks) { for (const chunk of entrypoint.chunks) {
// If there's no name or no files // If there's no name or no files
if (!chunk.name || !chunk.files) { if (!chunk.name || !chunk.files) {
...@@ -83,7 +84,7 @@ export default class BuildManifestPlugin { ...@@ -83,7 +84,7 @@ export default class BuildManifestPlugin {
assetMap.pages = Object.keys(assetMap.pages) assetMap.pages = Object.keys(assetMap.pages)
.sort() .sort()
// eslint-disable-next-line // 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( compilation.assets[BUILD_MANIFEST] = new RawSource(
JSON.stringify(assetMap, null, 2) JSON.stringify(assetMap, null, 2)
......
import { Compiler } from 'webpack'
// This plugin mirrors webpack 3 `filename` and `chunkfilename` behavior // This plugin mirrors webpack 3 `filename` and `chunkfilename` behavior
// This fixes https://github.com/webpack/webpack/issues/6598 // This fixes https://github.com/webpack/webpack/issues/6598
// This plugin is based on https://github.com/researchgate/webpack/commit/2f28947fa0c63ccbb18f39c0098bd791a2c37090 // This plugin is based on https://github.com/researchgate/webpack/commit/2f28947fa0c63ccbb18f39c0098bd791a2c37090
export default class ChunkNamesPlugin { export default class ChunkNamesPlugin {
apply (compiler) { apply (compiler: Compiler) {
compiler.hooks.compilation.tap('NextJsChunkNamesPlugin', (compilation) => { compiler.hooks.compilation.tap('NextJsChunkNamesPlugin', (compilation: any) => {
compilation.chunkTemplate.hooks.renderManifest.intercept({ compilation.chunkTemplate.hooks.renderManifest.intercept({
register (tapInfo) { register (tapInfo: any) {
if (tapInfo.name === 'JavascriptModulesPlugin') { if (tapInfo.name === 'JavascriptModulesPlugin') {
const originalMethod = tapInfo.fn const originalMethod = tapInfo.fn
tapInfo.fn = (result, options) => { tapInfo.fn = (result: any, options: any) => {
let filenameTemplate let filenameTemplate
const chunk = options.chunk const chunk = options.chunk
const outputOptions = options.outputOptions const outputOptions = options.outputOptions
......
import { join, resolve, relative, dirname } from 'path' import { join, resolve, relative, dirname } from 'path'
import { Compiler } from 'webpack'
// This plugin modifies the require-ensure code generated by Webpack // This plugin modifies the require-ensure code generated by Webpack
// to work with Next.js SSR // to work with Next.js SSR
export default class NextJsSsrImportPlugin { export default class NextJsSsrImportPlugin {
apply (compiler) { apply (compiler: Compiler) {
compiler.hooks.compilation.tap('NextJsSSRImport', (compilation) => { compiler.hooks.compilation.tap('NextJsSSRImport', (compilation: any) => {
compilation.mainTemplate.hooks.requireEnsure.tap('NextJsSSRImport', (code, chunk) => { compilation.mainTemplate.hooks.requireEnsure.tap('NextJsSSRImport', (code: string, chunk: any) => {
// Update to load chunks from our custom chunks directory // Update to load chunks from our custom chunks directory
const outputPath = resolve('/') const outputPath = resolve('/')
const pagePath = join('/', dirname(chunk.name)) const pagePath = join('/', dirname(chunk.name))
......
...@@ -16,10 +16,12 @@ const SSR_MODULE_CACHE_FILENAME = 'ssr-module-cache.js' ...@@ -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. // Do note that this module is only geared towards the `node` compilation target.
// For the client side compilation we use `runtimeChunk: 'single'` // For the client side compilation we use `runtimeChunk: 'single'`
export default class NextJsSsrImportPlugin { export default class NextJsSsrImportPlugin {
constructor (options) { private options: { outputPath: string }
constructor (options: { outputPath: string }) {
this.options = options this.options = options
} }
apply (compiler) { apply (compiler: webpack.Compiler) {
const { outputPath } = this.options const { outputPath } = this.options
compiler.hooks.emit.tapAsync('NextJsSSRModuleCache', (compilation, callback) => { compiler.hooks.emit.tapAsync('NextJsSSRModuleCache', (compilation, callback) => {
compilation.assets[SSR_MODULE_CACHE_FILENAME] = new RawSource(` compilation.assets[SSR_MODULE_CACHE_FILENAME] = new RawSource(`
...@@ -28,12 +30,12 @@ export default class NextJsSsrImportPlugin { ...@@ -28,12 +30,12 @@ export default class NextJsSsrImportPlugin {
`) `)
callback() callback()
}) })
compiler.hooks.compilation.tap('NextJsSSRModuleCache', (compilation) => { compiler.hooks.compilation.tap('NextJsSSRModuleCache', (compilation: any) => {
compilation.mainTemplate.hooks.localVars.intercept({ compilation.mainTemplate.hooks.localVars.intercept({
register (tapInfo) { register (tapInfo: any) {
if (tapInfo.name === 'MainTemplate') { if (tapInfo.name === 'MainTemplate') {
const originalFn = tapInfo.fn 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, // 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 // otherwise webpack will error out when the file is used before the compilation finishes
// this is the case with mini-css-extract-plugin // this is the case with mini-css-extract-plugin
...@@ -46,7 +48,7 @@ export default class NextJsSsrImportPlugin { ...@@ -46,7 +48,7 @@ export default class NextJsSsrImportPlugin {
// Make sure even in windows, the path looks like in unix // Make sure even in windows, the path looks like in unix
// Node.js require system will convert it accordingly // Node.js require system will convert it accordingly
const relativePathToBaseDirNormalized = relativePathToBaseDir.replace(/\\/g, '/') const relativePathToBaseDirNormalized = relativePathToBaseDir.replace(/\\/g, '/')
return webpack.Template.asString([ return (webpack as any).Template.asString([
source, source,
'// The module cache', '// The module cache',
`var installedModules = require('${relativePathToBaseDirNormalized}');` `var installedModules = require('${relativePathToBaseDirNormalized}');`
......
...@@ -22,21 +22,22 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR ...@@ -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 // Modified to strip out unneeded results for Next's specific use case
import url from 'url' 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 context = compiler.options.context
let manifest = {} let manifest: { [k: string]: any[] } = {}
compilation.chunkGroups.forEach(chunkGroup => { compilation.chunkGroups.forEach(chunkGroup => {
if (chunkGroup.isInitial()) { if (chunkGroup.isInitial()) {
return return
} }
chunkGroup.origins.forEach(chunkGroupOrigin => { chunkGroup.origins.forEach((chunkGroupOrigin: any) => {
const { request } = chunkGroupOrigin const { request } = chunkGroupOrigin
chunkGroup.chunks.forEach(chunk => { chunkGroup.chunks.forEach((chunk: any) => {
chunk.files.forEach(file => { chunk.files.forEach((file: string) => {
if (!file.match(/\.js$/) || !file.match(/^static\/chunks\//)) { if (!file.match(/\.js$/) || !file.match(/^static\/chunks\//)) {
return return
} }
...@@ -76,18 +77,19 @@ function buildManifest (compiler, compilation) { ...@@ -76,18 +77,19 @@ function buildManifest (compiler, compilation) {
manifest = Object.keys(manifest) manifest = Object.keys(manifest)
.sort() .sort()
// eslint-disable-next-line .reduce((a, c) => ((a[c] = manifest[c]), a), {} as any)
.reduce((a, c) => ((a[c] = manifest[c]), a), {})
return manifest return manifest
} }
export class ReactLoadablePlugin { export class ReactLoadablePlugin {
constructor (opts = {}) { private filename: string
constructor (opts: { filename: string }) {
this.filename = opts.filename this.filename = opts.filename
} }
apply (compiler) { apply (compiler: Compiler) {
compiler.hooks.emit.tapAsync( compiler.hooks.emit.tapAsync(
'ReactLoadableManifest', 'ReactLoadableManifest',
(compilation, callback) => { (compilation, callback) => {
......
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
}
/* eslint-disable /* eslint-disable
arrow-body-style arrow-body-style
*/ */
import { minify as terser } from 'terser'; import { minify as terser, MinifyOptions } from 'terser';
const buildTerserOptions = ({ const buildTerserOptions = ({
ecma, ecma,
...@@ -18,7 +18,7 @@ const buildTerserOptions = ({ ...@@ -18,7 +18,7 @@ const buildTerserOptions = ({
keep_fnames, keep_fnames,
/* eslint-enable camelcase */ /* eslint-enable camelcase */
safari10, safari10,
} = {}) => ({ }: MinifyOptions = {}) => ({
ecma, ecma,
warnings, warnings,
parse: { ...parse }, parse: { ...parse },
...@@ -45,7 +45,7 @@ const buildTerserOptions = ({ ...@@ -45,7 +45,7 @@ const buildTerserOptions = ({
safari10, safari10,
}); });
const minify = (options) => { const minify = (options: { file: string, input: string, inputSourceMap?: string, terserOptions?: MinifyOptions }) => {
const { const {
file, file,
input, input,
...@@ -53,7 +53,7 @@ const minify = (options) => { ...@@ -53,7 +53,7 @@ const minify = (options) => {
} = options; } = options;
// Copy terser options // Copy terser options
const terserOptions = buildTerserOptions(options.terserOptions); const terserOptions = buildTerserOptions(options.terserOptions) as any;
// Add source map data // Add source map data
if (inputSourceMap) { if (inputSourceMap) {
......
export default function formatWebpackMessages(json: any): any;
export default function(dir: string, options: any, configuration?: any): Promise<void>
...@@ -29,8 +29,9 @@ ...@@ -29,8 +29,9 @@
"scripts": { "scripts": {
"build": "taskr", "build": "taskr",
"release": "taskr release", "release": "taskr release",
"prepublish": "npm run release", "prepublish": "npm run release && npm run types",
"typescript": "tsc --noEmit" "typescript": "tsc --noEmit --declaration",
"types": "tsc --declaration --emitDeclarationOnly --declarationDir dist"
}, },
"taskr": { "taskr": {
"requires": [ "requires": [
......
import http from 'http' import http from 'http'
import next from '../next' 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 app = next(serverOptions)
const srv = http.createServer(app.getRequestHandler()) const srv = http.createServer(app.getRequestHandler())
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
......
export function printAndExit (message, code = 1) { export function printAndExit(message: string, code = 1) {
if (code === 0) { if (code === 0) {
// tslint:disable-next-line no-console
console.log(message) console.log(message)
} else { } else {
console.error(message) console.error(message)
......
export default function(options: any): any
// This file is used for when users run `require('next')` // This file is used for when users run `require('next')`
module.exports = (options) => { module.exports = options => {
if (options.dev) { if (options.dev) {
const Server = require('./next-dev-server').default const Server = require('./next-dev-server').default
return new Server(options) return new Server(options)
......
{ {
"compilerOptions": { "compilerOptions": {
"strict": true, "strict": true,
"allowJs": true,
"noEmit": true,
"module": "esnext", "module": "esnext",
"target": "ES2017", "target": "ES2017",
"esModuleInterop": true, "esModuleInterop": true,
......
...@@ -57,6 +57,16 @@ declare module 'next/dist/compiled/resolve/index.js' { ...@@ -57,6 +57,16 @@ declare module 'next/dist/compiled/resolve/index.js' {
export = resolve; 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' { declare module 'next/dist/compiled/arg/index.js' {
function arg<T extends arg.Spec>(spec: T, options?: {argv?: string[], permissive?: boolean}): arg.Result<T>; function arg<T extends arg.Spec>(spec: T, options?: {argv?: string[], permissive?: boolean}): arg.Result<T>;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment