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 nccing AMP optimizer #21980

Merged
merged 8 commits into from
Feb 11, 2021
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
6 changes: 5 additions & 1 deletion packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,11 @@ export default async function build(
path.relative(
dir,
path.join(
path.dirname(require.resolve('@ampproject/toolbox-optimizer')),
path.dirname(
require.resolve(
'next/dist/compiled/@ampproject/toolbox-optimizer'
)
),
'**/*'
)
)
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
// 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
// webpack error from being missing
Expand Down
2 changes: 1 addition & 1 deletion packages/next/next-server/server/optimize-amp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default async function optimize(
): Promise<string> {
let AmpOptimizer
try {
AmpOptimizer = require('@ampproject/toolbox-optimizer')
AmpOptimizer = require('next/dist/compiled/@ampproject/toolbox-optimizer')
} catch (_) {
return html
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
]
},
"dependencies": {
"@ampproject/toolbox-optimizer": "2.7.1-alpha.0",
"@babel/runtime": "7.12.5",
"@hapi/accept": "5.0.1",
"@next/env": "10.0.7-canary.6",
Expand Down Expand Up @@ -120,6 +119,7 @@
}
},
"devDependencies": {
"@ampproject/toolbox-optimizer": "2.7.1-alpha.0",
"@babel/code-frame": "7.12.11",
"@babel/core": "7.12.10",
"@babel/plugin-proposal-class-properties": "7.12.1",
Expand Down
10 changes: 7 additions & 3 deletions packages/next/taskfile-ncc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module.exports = function (task) {
options.externals = { ...options.externals }
delete options.externals[options.packageName]
}
let precompiled = options.precompiled !== false
delete options.precompiled

return ncc(join(__dirname, file.dir, file.base), {
filename: file.base,
minify: options.minify === false ? false : true,
Expand All @@ -39,7 +42,8 @@ module.exports = function (task) {
this,
options.packageName,
file.base,
options.bundleName
options.bundleName,
precompiled
)
}

Expand All @@ -51,13 +55,13 @@ 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, main, bundleName) {
function writePackageManifest(packageName, main, bundleName, precompiled) {
const packagePath = bundleRequire.resolve(packageName + '/package.json')
let { name, author, license } = require(packagePath)

const compiledPackagePath = join(
__dirname,
`compiled/${bundleName || packageName}`
`${!precompiled ? 'dist/' : ''}compiled/${bundleName || packageName}`
)

const potentialLicensePath = join(dirname(packagePath), './LICENSE')
Expand Down
19 changes: 19 additions & 0 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ export async function ncc_amphtml_validator(task, opts) {
.target('compiled/amphtml-validator')
}
// 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'
export async function ncc_arg(task, opts) {
await task
Expand Down Expand Up @@ -757,6 +773,9 @@ export async function compile(task, opts) {
'client',
'telemetry',
'nextserver',
// we compile this each time so that fresh runtime data is pulled
// before each publish
'ncc_amp_optimizer',
],
opts
)
Expand Down
9 changes: 9 additions & 0 deletions test/.stats-app/pages/amp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useAmp } from 'next/amp'

export const config = {
amp: 'hybrid',
}

export default function Amp(props) {
return useAmp() ? 'AMP mode' : 'normal mode'
}