提交 e85a517e 编写于 作者: G Guy Bedford 提交者: Joe Haddad

ncc Webpack build redux (#7628)

* Reimplement ncc webpack build

This reverts commit 6feca310.

* Shared webpack build

* ncc workaround pending @zeit/ncc#437

* update ncc

* build tweaks, fixup autodll-import

* possible Node 8 fix

* second possible Node 8 fix

* and update taskfile
上级 ff040950
const path = require('path')
module.exports = distDir => {
const autodllCachePath = path.resolve(
path.join(distDir, 'cache', 'autodll-webpack-plugin')
)
const autoDllWebpackPluginPaths = require('autodll-webpack-plugin/lib/paths')
autoDllWebpackPluginPaths.cacheDir = autodllCachePath
autoDllWebpackPluginPaths.getManifestPath = hash => bundleName =>
path.resolve(autodllCachePath, hash, `${bundleName}.manifest.json`)
return require('autodll-webpack-plugin')
}
exports.webpack = require('webpack')
exports.webpackGraphHelpers = require('webpack/lib/GraphHelpers')
exports.webpackDynamicEntryPlugin = require('webpack/lib/DynamicEntryPlugin')
exports.webpackNodeOutputFileSystem = require('webpack/lib/node/NodeOutputFileSystem')
module.exports = require('./webpack/').webpackDynamicEntryPlugin
module.exports = require('./webpack/').webpackGraphHelpers
module.exports = require('./webpack/').webpackNodeOutputFileSystem
module.exports = require('./webpack/').webpack
import webpack, { Stats } from 'webpack'
import webpack, { Stats } from 'next/dist/compiled/webpack.js'
export type CompilerResult = {
errors: Error[]
......
......@@ -9,7 +9,7 @@ import {
import resolve from 'next/dist/compiled/resolve/index.js'
import path from 'path'
import { promisify } from 'util'
import webpack from 'webpack'
import webpack from 'next/dist/compiled/webpack.js'
import {
DOT_NEXT_ALIAS,
......@@ -22,7 +22,6 @@ import { AllModulesIdentifiedPlugin } from './webpack/plugins/all-modules-identi
import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
import { ChunkGraphPlugin } from './webpack/plugins/chunk-graph-plugin'
import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin'
import { importAutoDllPlugin } from './webpack/plugins/dll-import'
import { HashedChunkIdsPlugin } from './webpack/plugins/hashed-chunk-ids-plugin'
import { DropClientPage } from './webpack/plugins/next-drop-client-page-plugin'
import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import'
......@@ -482,7 +481,9 @@ export default async function getBaseWebpackConfig(
]
if (!isServer) {
const AutoDllPlugin = importAutoDllPlugin({ distDir })
const AutoDllPlugin = require('next/dist/compiled/autodll-webpack-plugin')(
distDir
)
devPlugins.push(
new AutoDllPlugin({
filename: '[name]_[hash].js',
......
import { loader } from 'webpack'
import { loader } from 'next/dist/compiled/webpack.js'
import loaderUtils from 'loader-utils'
export type ClientPagesLoaderOptions = {
......
import { loader } from 'webpack'
import { loader } from 'next/dist/compiled/webpack.js'
import hash from 'string-hash'
import { basename } from 'path'
const nextDataLoader: loader.Loader = function(source) {
......
import loaderUtils from 'loader-utils'
import { loader } from 'webpack'
import { loader } from 'next/dist/compiled/webpack.js'
import minify from '../plugins/terser-webpack-plugin/src/minify'
const nextMiniferLoader: loader.Loader = function(source) {
......
import { loader } from 'webpack'
import { loader } from 'next/dist/compiled/webpack.js'
import { join } from 'path'
import { parse } from 'querystring'
import { BUILD_MANIFEST, REACT_LOADABLE_MANIFEST } from 'next-server/constants'
......
import { loader } from 'webpack'
import { loader } from 'next/dist/compiled/webpack.js'
const NoopLoader: loader.Loader = source => source
export default NoopLoader
import { Compiler, Plugin } from 'webpack'
import { Compiler, Plugin } from 'next/dist/compiled/webpack.js'
import { createHash } from 'crypto'
import path from 'path'
......
import { Compiler } from 'webpack'
import { Compiler } from 'next/dist/compiled/webpack.js'
import { RawSource } from 'webpack-sources'
import {
BUILD_MANIFEST,
......
......@@ -4,7 +4,7 @@ import { CLIENT_STATIC_FILES_RUNTIME_MAIN } from 'next-server/constants'
import { EOL } from 'os'
import path from 'path'
import { parse } from 'querystring'
import { Compiler, Plugin } from 'webpack'
import { Compiler, Plugin } from 'next/dist/compiled/webpack.js'
type StringDictionary = { [pageName: string]: string[] }
const manifest: {
......
import { Compiler } from 'webpack'
import { Compiler } from 'next/dist/compiled/webpack.js'
// 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
......
import path from 'path'
export function importAutoDllPlugin({ distDir }: { distDir: string }) {
const autodllPaths = path.join(
path.dirname(require.resolve('autodll-webpack-plugin')),
'paths.js'
)
require(autodllPaths)
const autodllCachePath = path.resolve(
path.join(distDir, 'cache', 'autodll-webpack-plugin')
)
require.cache[autodllPaths] = Object.assign({}, require.cache[autodllPaths], {
exports: Object.assign({}, require.cache[autodllPaths].exports, {
cacheDir: autodllCachePath,
getManifestPath: (hash: string) => (bundleName: string) =>
path.resolve(autodllCachePath, hash, `${bundleName}.manifest.json`),
}),
})
const AutoDllPlugin = require('autodll-webpack-plugin')
return AutoDllPlugin
}
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
import { NormalizedMessage } from 'fork-ts-checker-webpack-plugin/lib/NormalizedMessage'
import webpack from 'webpack'
import webpack from 'next/dist/compiled/webpack.js'
export function Apply(compiler: webpack.Compiler) {
const hooks = ForkTsCheckerWebpackPlugin.getCompilerHooks(compiler)
const hooks = ForkTsCheckerWebpackPlugin.getCompilerHooks(compiler as any)
let additionalFiles: string[] = []
......
import { Compiler, Plugin } from 'webpack'
import { Compiler, Plugin } from 'next/dist/compiled/webpack.js'
import { createHash } from 'crypto'
export class HashedChunkIdsPlugin implements Plugin {
......
import { Compiler, Plugin } from 'webpack'
import { Compiler, Plugin } from 'next/dist/compiled/webpack.js'
import { extname } from 'path'
// Prevents outputting client pages when they are not needed
......
import { Compiler, Plugin } from 'webpack'
import { Compiler, Plugin } from 'next/dist/compiled/webpack.js'
import { realpathSync } from 'fs'
function deleteCache(path: string) {
......
import { join, resolve, relative, dirname } from 'path'
import { Compiler } from 'webpack'
import { Compiler } from 'next/dist/compiled/webpack.js'
// This plugin modifies the require-ensure code generated by Webpack
// to work with Next.js SSR
......
import webpack from 'webpack'
import webpack from 'next/dist/compiled/webpack.js'
import { RawSource } from 'webpack-sources'
import { join, relative, dirname } from 'path'
import { IS_BUNDLED_PAGE_REGEX } from 'next-server/constants'
......
import { Compiler, Plugin } from 'webpack'
import { Compiler, Plugin } from 'next/dist/compiled/webpack.js'
import { RawSource } from 'webpack-sources'
import {
PAGES_MANIFEST,
......
......@@ -22,7 +22,7 @@ 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'
import { Compiler, compilation } from 'next/dist/compiled/webpack.js'
function buildManifest(
compiler: Compiler,
......
import { Compiler } from 'webpack'
import { connectChunkAndModule } from 'webpack/lib/GraphHelpers'
import { Compiler } from 'next/dist/compiled/webpack.js'
import { connectChunkAndModule } from 'next/dist/compiled/webpack-GraphHelpers'
/**
* Makes sure there are no dynamic chunks when the target is serverless
......
import { Compiler, Plugin } from 'webpack'
import { Compiler, Plugin } from 'next/dist/compiled/webpack.js'
export class SharedRuntimePlugin implements Plugin {
apply(compiler: Compiler) {
......
......@@ -4,7 +4,7 @@
import stringHash from 'string-hash'
import { SourceMapConsumer } from 'source-map'
import { SourceMapSource, RawSource } from 'webpack-sources'
import { RequestShortener } from 'webpack'
import { RequestShortener } from 'next/dist/compiled/webpack.js'
import TaskRunner from './TaskRunner'
const warningRegex = /\[.+:([0-9]+),([0-9]+)\]/
......
......@@ -2,7 +2,7 @@ import { join } from 'path'
import { promisify } from 'util'
import fs from 'fs'
import { IS_BUNDLED_PAGE_REGEX } from 'next-server/constants'
import { Compiler } from 'webpack'
import { Compiler } from 'next/dist/compiled/webpack.js'
const unlink = promisify(fs.unlink)
......
......@@ -68,7 +68,6 @@
"@babel/runtime-corejs2": "7.4.5",
"amphtml-validator": "1.0.23",
"async-sema": "3.0.0",
"autodll-webpack-plugin": "0.4.2",
"babel-core": "7.0.0-bridge.0",
"babel-loader": "8.0.6",
"babel-plugin-react-require": "3.0.0",
......@@ -82,6 +81,7 @@
"loader-utils": "1.2.3",
"mkdirp": "0.5.1",
"next-server": "8.1.1-canary.54",
"node-libs-browser": "2.2.0",
"prop-types": "15.7.2",
"prop-types-exact": "1.2.0",
"react-error-overlay": "5.1.6",
......@@ -96,9 +96,6 @@
"unfetch": "4.1.0",
"url": "0.11.0",
"watchpack": "2.0.0-beta.4",
"webpack": "4.32.2",
"webpack-dev-middleware": "3.7.0",
"webpack-hot-middleware": "2.25.0",
"webpack-sources": "1.3.0",
"worker-farm": "1.7.0"
},
......@@ -129,15 +126,19 @@
"@types/styled-jsx": "2.2.8",
"@types/text-table": "0.2.1",
"@types/webpack-sources": "0.1.5",
"@zeit/ncc": "0.18.5",
"@zeit/ncc": "0.20.1",
"arg": "4.1.0",
"autodll-webpack-plugin": "0.4.2",
"babel-plugin-dynamic-import-node": "2.2.0",
"nanoid": "2.0.3",
"resolve": "1.11.0",
"taskr": "1.1.0",
"text-table": "0.2.0",
"typescript": "3.5.1",
"unistore": "3.4.1"
"unistore": "3.4.1",
"webpack": "4.32.2",
"webpack-dev-middleware": "3.7.0",
"webpack-hot-middleware": "2.25.0"
},
"engines": {
"node": ">= 8.0.0"
......
import { relative as relativePath, join, normalize, sep } from 'path'
import WebpackDevMiddleware from 'webpack-dev-middleware'
import WebpackHotMiddleware from 'webpack-hot-middleware'
import WebpackDevMiddleware from 'next/dist/compiled/webpack-dev-middleware'
import WebpackHotMiddleware from 'next/dist/compiled/webpack-hot-middleware'
import errorOverlayMiddleware from './lib/error-overlay-middleware'
import onDemandEntryHandler, { normalizePage } from './on-demand-entry-handler'
import webpack from 'webpack'
import webpack from 'next/dist/compiled/webpack.js'
import getBaseWebpackConfig from '../build/webpack-config'
import {
IS_BUNDLED_PAGE_REGEX,
......
import DynamicEntryPlugin from 'webpack/lib/DynamicEntryPlugin'
import DynamicEntryPlugin from 'next/dist/compiled/webpack-DynamicEntryPlugin'
import { EventEmitter } from 'events'
import { join, posix } from 'path'
import { parse } from 'url'
......
......@@ -7,8 +7,7 @@ const { basename, dirname, extname, join, relative } = require('path')
module.exports = function (task) {
task.plugin('ncc', {}, function * (file, options) {
return ncc(join(__dirname, file.dir, file.base), {
// cannot bundle
externals: ['chokidar'],
filename: file.base,
...options
}).then(({ code, assets }) => {
Object.keys(assets).forEach(key =>
......@@ -20,7 +19,7 @@ module.exports = function (task) {
)
if (options && options.packageName) {
writePackageManifest.call(this, options.packageName)
writePackageManifest.call(this, options.packageName, file.base)
}
file.data = Buffer.from(code, 'utf8')
......@@ -31,12 +30,9 @@ module.exports = function (task) {
// This function writes a minimal `package.json` file for a compiled package.
// It defines `name`, `main`, `author`, and `license`. It also defines `types`.
// n.b. types intended for development usage only.
function writePackageManifest (packageName) {
function writePackageManifest (packageName, main) {
const packagePath = require.resolve(packageName + '/package.json')
let { name, main, author, license, types, typings } = require(packagePath)
if (!main) {
main = 'index.js'
}
let { name, author, license, types, typings } = require(packagePath)
let typesFile = types || typings
if (typesFile) {
......
......@@ -61,11 +61,18 @@ export async function ncc_arg (task, opts) {
}
// eslint-disable-next-line camelcase
export async function ncc_resolve (task, opts) {
export async function ncc_autodll_webpack_plugin (task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('resolve')))
.ncc({ packageName: 'resolve' })
.target('dist/compiled/resolve')
.source(opts.src || 'build/bundles/autodll-webpack-plugin.js')
.ncc({
packageName: 'autodll-webpack-plugin',
externals: {
webpack: '../webpack.js'
}
})
.target('dist/compiled/autodll-webpack-plugin')
notify('Compiled autodll-webpack-plugin')
}
// eslint-disable-next-line camelcase
......@@ -77,11 +84,11 @@ export async function ncc_nanoid (task, opts) {
}
// eslint-disable-next-line camelcase
export async function ncc_unistore (task, opts) {
export async function ncc_resolve (task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('unistore')))
.ncc({ packageName: 'unistore' })
.target('dist/compiled/unistore')
.source(opts.src || relative(__dirname, require.resolve('resolve')))
.ncc({ packageName: 'resolve' })
.target('dist/compiled/resolve')
}
// eslint-disable-next-line camelcase
......@@ -92,13 +99,83 @@ export async function ncc_text_table (task, opts) {
.target('dist/compiled/text-table')
}
// eslint-disable-next-line camelcase
export async function ncc_unistore (task, opts) {
await task
.source(opts.src || relative(__dirname, require.resolve('unistore')))
.ncc({ packageName: 'unistore' })
.target('dist/compiled/unistore')
}
// eslint-disable-next-line camelcase
export async function ncc_webpack (task, opts) {
await task
.source(opts.src || 'build/bundles/webpack*.js')
.target('dist/compiled/')
notify('Compiled webpack')
}
// eslint-disable-next-line camelcase
export async function ncc_webpack_Bundle (task, opts) {
await task
.source(opts.src || 'build/bundles/bundled-webpack.js')
.ncc({
packageName: 'webpack',
externals: {
'node-libs-browser': 'node-libs-browser',
chokidar: 'chokidar'
}
})
.target('dist/compiled/webpack')
notify('Compiled webpack Bundle')
}
// eslint-disable-next-line camelcase
export async function ncc_webpack_hot_middleware (task, opts) {
await task
.source(
opts.src || relative(__dirname, require.resolve('webpack-hot-middleware'))
)
.ncc({
packageName: 'webpack-hot-middleware'
})
.target('dist/compiled/webpack-hot-middleware')
notify('Compiled webpack-hot-middleware')
}
// eslint-disable-next-line camelcase
export async function ncc_webpack_dev_middleware (task, opts) {
await task
.source(
opts.src || relative(__dirname, require.resolve('webpack-dev-middleware'))
)
.ncc({
externals: {
'webpack/lib/node/NodeOutputFileSystem':
'../webpack-NodeOutputFileSystem'
},
packageName: 'webpack-dev-middleware'
})
.target('dist/compiled/webpack-dev-middleware')
notify('Compiled webpack-dev-middleware')
}
export async function precompile (task) {
await task.parallel([
'ncc_unistore',
'ncc_resolve',
'ncc_arg',
'ncc_autodll_webpack_plugin',
'ncc_nanoid',
'ncc_text_table'
'ncc_resolve',
'ncc_text_table',
'ncc_unistore',
'ncc_webpack',
'ncc_webpack_Bundle',
'ncc_webpack_dev_middleware',
'ncc_webpack_hot_middleware'
])
}
......
// Type definitions for loader-utils 1.1
// Project: https://github.com/webpack/loader-utils#readme
// Definitions by: Gyusun Yeom <https://github.com/Perlmint>
// Totooria Hyperion <https://github.com/TotooriaHyperion>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/// <reference types="node" />
declare module 'loader-utils' {
import { loader } from 'next/dist/compiled/webpack.js'
export interface InterpolateOption {
context?: string
content?: string | Buffer
regExp?: string | RegExp
}
export interface OptionObject {
[key: string]: any
}
export type HashType = 'sha1' | 'md5' | 'sha256' | 'sha512'
export type DigestType =
| 'hex'
| 'base26'
| 'base32'
| 'base36'
| 'base49'
| 'base52'
| 'base58'
| 'base62'
| 'base64'
export function getOptions(loaderContext: loader.LoaderContext): OptionObject
export function parseQuery(optionString: string): OptionObject
export function stringifyRequest(
loaderContext: loader.LoaderContext,
resource: string
): string
export function getRemainingRequest(
loaderContext: loader.LoaderContext
): string
export function getCurrentRequest(loaderContext: loader.LoaderContext): string
export function isUrlRequest(url: string, root?: string): boolean
export function parseString(str: string): string
export function urlToRequest(url: string, root?: string): string
export function interpolateName(
loaderContext: loader.LoaderContext,
name: string,
options?: any
): string
export function getHashDigest(
buffer: Buffer,
hashType: HashType,
digestType: DigestType,
maxLength: number
): string
}
......@@ -5,6 +5,25 @@ declare module 'webpack/lib/GraphHelpers'
declare module 'unfetch'
declare module 'styled-jsx/server'
declare module 'node-libs-browser' {
const nodeLibs: any
export = nodeLibs
}
declare module 'next/dist/compiled/webpack.js' {
import webpack from 'webpack'
export = webpack
}
declare module 'next/dist/compiled/webpack-DynamicEntryPlugin' {
export const createDependency: any
}
declare module 'next/dist/compiled/webpack-GraphHelpers' {
export const connectChunkAndModule: any
}
declare module 'next/dist/compiled/nanoid/index.js' {
function nanoid(size?: number): string
......@@ -62,8 +81,8 @@ declare module 'next/dist/compiled/arg/index.js' {
export = arg
}
declare module 'autodll-webpack-plugin' {
import webpack from 'webpack'
declare module 'next/dist/compiled/autodll-webpack-plugin' {
import webpack from 'next/dist/compiled/webpack.js'
class AutoDllPlugin implements webpack.Plugin {
constructor(settings?: {
inject?: boolean
......@@ -80,7 +99,9 @@ declare module 'autodll-webpack-plugin' {
[k: string]: any
}
export = AutoDllPlugin
function setCacheDir(dist: string): void
export { AutoDllPlugin, setCacheDir }
}
declare module NodeJS {
......
......@@ -2182,10 +2182,10 @@
resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
"@zeit/ncc@0.18.5":
version "0.18.5"
resolved "https://registry.yarnpkg.com/@zeit/ncc/-/ncc-0.18.5.tgz#5687df6c32f1a2e2486aa110b3454ccebda4fb9c"
integrity sha512-F+SbvEAh8rchiRXqQbmD1UmbePY7dCOKTbvfFtbVbK2xMH/tyri5YKfNxXKK7eL9EWkkbqB3NTVQO6nokApeBA==
"@zeit/ncc@0.20.1":
version "0.20.1"
resolved "https://registry.yarnpkg.com/@zeit/ncc/-/ncc-0.20.1.tgz#1e282f23a171b11f5b65d3281fad7b9195cbab66"
integrity sha512-sCSWftZlFR2VgbjdMq+/TPXU7fDJpsjxGcDU1Nh2b/Gemvna8Ncl77ck+6WeEhcx/Gwtv4AeR3xJ1upgk7MSnQ==
"@zeit/next-css@1.0.2-canary.2":
version "1.0.2-canary.2"
......@@ -8846,7 +8846,7 @@ node-int64@^0.4.0:
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
integrity sha1-h6kGXNs1XTGC2PlM4RGIuCXGijs=
node-libs-browser@^2.0.0:
node-libs-browser@2.2.0, node-libs-browser@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/node-libs-browser/-/node-libs-browser-2.2.0.tgz#c72f60d9d46de08a940dedbb25f3ffa2f9bbaa77"
integrity sha512-5MQunG/oyOaBdttrL40dA7bUfPORLRWMUJLQtMg7nluxUvk5XwnLdL9twQHFAjRx/y7mIMkLKT9++qPbbk6BZA==
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册