Unverified Commit 5febe218 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add nccing AMP optimizer (#21980)

This adds ncc'ing the AMP optimizer package to speed up install times and cache the runtime. 

Closes: https://github.com/vercel/next.js/issues/20404
parent 3f94f33b
...@@ -787,7 +787,11 @@ export default async function build( ...@@ -787,7 +787,11 @@ export default async function build(
path.relative( path.relative(
dir, dir,
path.join( path.join(
path.dirname(require.resolve('@ampproject/toolbox-optimizer')), path.dirname(
require.resolve(
'next/dist/compiled/@ampproject/toolbox-optimizer'
)
),
'**/*' '**/*'
) )
) )
......
...@@ -763,7 +763,7 @@ export default async function getBaseWebpackConfig( ...@@ -763,7 +763,7 @@ export default async function getBaseWebpackConfig(
: [ : [
// When the 'serverless' target is used all node_modules will be compiled into the output bundles // When the 'serverless' target is used all node_modules will be compiled into the output bundles
// So that the 'serverless' bundles have 0 runtime dependencies // So that the 'serverless' bundles have 0 runtime dependencies
'@ampproject/toolbox-optimizer', // except this one 'next/dist/compiled/@ampproject/toolbox-optimizer', // except this one
// Mark this as external if not enabled so it doesn't cause a // Mark this as external if not enabled so it doesn't cause a
// webpack error from being missing // webpack error from being missing
......
...@@ -4,7 +4,7 @@ export default async function optimize( ...@@ -4,7 +4,7 @@ export default async function optimize(
): Promise<string> { ): Promise<string> {
let AmpOptimizer let AmpOptimizer
try { try {
AmpOptimizer = require('@ampproject/toolbox-optimizer') AmpOptimizer = require('next/dist/compiled/@ampproject/toolbox-optimizer')
} catch (_) { } catch (_) {
return html return html
} }
......
...@@ -60,7 +60,6 @@ ...@@ -60,7 +60,6 @@
] ]
}, },
"dependencies": { "dependencies": {
"@ampproject/toolbox-optimizer": "2.7.1-alpha.0",
"@babel/runtime": "7.12.5", "@babel/runtime": "7.12.5",
"@hapi/accept": "5.0.1", "@hapi/accept": "5.0.1",
"@next/env": "10.0.7-canary.6", "@next/env": "10.0.7-canary.6",
...@@ -120,6 +119,7 @@ ...@@ -120,6 +119,7 @@
} }
}, },
"devDependencies": { "devDependencies": {
"@ampproject/toolbox-optimizer": "2.7.1-alpha.0",
"@babel/code-frame": "7.12.11", "@babel/code-frame": "7.12.11",
"@babel/core": "7.12.10", "@babel/core": "7.12.10",
"@babel/plugin-proposal-class-properties": "7.12.1", "@babel/plugin-proposal-class-properties": "7.12.1",
......
...@@ -19,6 +19,9 @@ module.exports = function (task) { ...@@ -19,6 +19,9 @@ module.exports = function (task) {
options.externals = { ...options.externals } options.externals = { ...options.externals }
delete options.externals[options.packageName] delete options.externals[options.packageName]
} }
let precompiled = options.precompiled !== false
delete options.precompiled
return ncc(join(__dirname, file.dir, file.base), { return ncc(join(__dirname, file.dir, file.base), {
filename: file.base, filename: file.base,
minify: options.minify === false ? false : true, minify: options.minify === false ? false : true,
...@@ -39,7 +42,8 @@ module.exports = function (task) { ...@@ -39,7 +42,8 @@ module.exports = function (task) {
this, this,
options.packageName, options.packageName,
file.base, file.base,
options.bundleName options.bundleName,
precompiled
) )
} }
...@@ -51,13 +55,13 @@ module.exports = function (task) { ...@@ -51,13 +55,13 @@ module.exports = function (task) {
// This function writes a minimal `package.json` file for a compiled package. // This function writes a minimal `package.json` file for a compiled package.
// It defines `name`, `main`, `author`, and `license`. It also defines `types`. // It defines `name`, `main`, `author`, and `license`. It also defines `types`.
// n.b. types intended for development usage only. // n.b. types intended for development usage only.
function writePackageManifest(packageName, main, bundleName) { function writePackageManifest(packageName, main, bundleName, precompiled) {
const packagePath = bundleRequire.resolve(packageName + '/package.json') const packagePath = bundleRequire.resolve(packageName + '/package.json')
let { name, author, license } = require(packagePath) let { name, author, license } = require(packagePath)
const compiledPackagePath = join( const compiledPackagePath = join(
__dirname, __dirname,
`compiled/${bundleName || packageName}` `${!precompiled ? 'dist/' : ''}compiled/${bundleName || packageName}`
) )
const potentialLicensePath = join(dirname(packagePath), './LICENSE') const potentialLicensePath = join(dirname(packagePath), './LICENSE')
......
...@@ -68,6 +68,22 @@ export async function ncc_amphtml_validator(task, opts) { ...@@ -68,6 +68,22 @@ export async function ncc_amphtml_validator(task, opts) {
.target('compiled/amphtml-validator') .target('compiled/amphtml-validator')
} }
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
externals['@ampproject/toolbox-optimizer'] =
'next/dist/compiled/@ampproject/toolbox-optimizer'
export async function ncc_amp_optimizer(task, opts) {
await task
.source(
opts.src ||
relative(__dirname, require.resolve('@ampproject/toolbox-optimizer'))
)
.ncc({
externals,
precompiled: false,
packageName: '@ampproject/toolbox-optimizer',
})
.target('dist/compiled/@ampproject/toolbox-optimizer')
}
// eslint-disable-next-line camelcase
externals['arg'] = 'distcompiled/arg' externals['arg'] = 'distcompiled/arg'
export async function ncc_arg(task, opts) { export async function ncc_arg(task, opts) {
await task await task
...@@ -757,6 +773,9 @@ export async function compile(task, opts) { ...@@ -757,6 +773,9 @@ export async function compile(task, opts) {
'client', 'client',
'telemetry', 'telemetry',
'nextserver', 'nextserver',
// we compile this each time so that fresh runtime data is pulled
// before each publish
'ncc_amp_optimizer',
], ],
opts opts
) )
......
import { useAmp } from 'next/amp'
export const config = {
amp: 'hybrid',
}
export default function Amp(props) {
return useAmp() ? 'AMP mode' : 'normal mode'
}
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