added fonts
This commit is contained in:
1 parent
4134e3a234
commit
142d2986b6
9079 files changed
+1498421
-2
No files matched your search
+26
@@ -0,0 +1,26 @@
|
||||
export default function AmpAttributePatcher() {
|
||||
return {
|
||||
visitor: {
|
||||
JSXOpeningElement (path) {
|
||||
const openingElement = path.node;
|
||||
const { name, attributes } = openingElement;
|
||||
if (!(name && name.type === 'JSXIdentifier')) {
|
||||
return;
|
||||
}
|
||||
if (!name.name.startsWith('amp-')) {
|
||||
return;
|
||||
}
|
||||
for (const attribute of attributes){
|
||||
if (attribute.type !== 'JSXAttribute') {
|
||||
continue;
|
||||
}
|
||||
if (attribute.name.name === 'className') {
|
||||
attribute.name.name = 'class';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=amp-attributes.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/plugins/amp-attributes.ts"],"sourcesContent":["import type { NodePath, types, PluginObj } from 'next/dist/compiled/babel/core'\n\nexport default function AmpAttributePatcher(): PluginObj {\n return {\n visitor: {\n JSXOpeningElement(path: NodePath<types.JSXOpeningElement>) {\n const openingElement = path.node\n\n const { name, attributes } = openingElement\n if (!(name && name.type === 'JSXIdentifier')) {\n return\n }\n\n if (!name.name.startsWith('amp-')) {\n return\n }\n\n for (const attribute of attributes) {\n if (attribute.type !== 'JSXAttribute') {\n continue\n }\n\n if (attribute.name.name === 'className') {\n attribute.name.name = 'class'\n }\n }\n },\n },\n }\n}\n"],"names":["AmpAttributePatcher","visitor","JSXOpeningElement","path","openingElement","node","name","attributes","type","startsWith","attribute"],"mappings":"AAEA,eAAe,SAASA;IACtB,OAAO;QACLC,SAAS;YACPC,mBAAkBC,IAAuC;gBACvD,MAAMC,iBAAiBD,KAAKE,IAAI;gBAEhC,MAAM,EAAEC,IAAI,EAAEC,UAAU,EAAE,GAAGH;gBAC7B,IAAI,CAAEE,CAAAA,QAAQA,KAAKE,IAAI,KAAK,eAAc,GAAI;oBAC5C;gBACF;gBAEA,IAAI,CAACF,KAAKA,IAAI,CAACG,UAAU,CAAC,SAAS;oBACjC;gBACF;gBAEA,KAAK,MAAMC,aAAaH,WAAY;oBAClC,IAAIG,UAAUF,IAAI,KAAK,gBAAgB;wBACrC;oBACF;oBAEA,IAAIE,UAAUJ,IAAI,CAACA,IAAI,KAAK,aAAa;wBACvCI,UAAUJ,IAAI,CAACA,IAAI,GAAG;oBACxB;gBACF;YACF;QACF;IACF;AACF"}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
import commonjsPlugin from 'next/dist/compiled/babel/plugin-transform-modules-commonjs';
|
||||
// Handle module.exports in user code
|
||||
export default function CommonJSModulePlugin(...args) {
|
||||
const commonjs = commonjsPlugin(...args);
|
||||
return {
|
||||
visitor: {
|
||||
Program: {
|
||||
exit (path, state) {
|
||||
let foundModuleExports = false;
|
||||
path.traverse({
|
||||
MemberExpression (expressionPath) {
|
||||
if (expressionPath.node.object.name !== 'module') return;
|
||||
if (expressionPath.node.property.name !== 'exports') return;
|
||||
foundModuleExports = true;
|
||||
}
|
||||
});
|
||||
if (!foundModuleExports) {
|
||||
return;
|
||||
}
|
||||
commonjs.visitor.Program.exit.call(this, path, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=commonjs.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/plugins/commonjs.ts"],"sourcesContent":["import type { NodePath, types } from 'next/dist/compiled/babel/core'\nimport type { PluginObj } from 'next/dist/compiled/babel/core'\nimport commonjsPlugin from 'next/dist/compiled/babel/plugin-transform-modules-commonjs'\n\n// Handle module.exports in user code\nexport default function CommonJSModulePlugin(...args: any): PluginObj {\n const commonjs = commonjsPlugin(...args)\n return {\n visitor: {\n Program: {\n exit(path: NodePath<types.Program>, state) {\n let foundModuleExports = false\n path.traverse({\n MemberExpression(expressionPath: any) {\n if (expressionPath.node.object.name !== 'module') return\n if (expressionPath.node.property.name !== 'exports') return\n foundModuleExports = true\n },\n })\n\n if (!foundModuleExports) {\n return\n }\n\n commonjs.visitor.Program.exit.call(this, path, state)\n },\n },\n },\n }\n}\n"],"names":["commonjsPlugin","CommonJSModulePlugin","args","commonjs","visitor","Program","exit","path","state","foundModuleExports","traverse","MemberExpression","expressionPath","node","object","name","property","call"],"mappings":"AAEA,OAAOA,oBAAoB,6DAA4D;AAEvF,qCAAqC;AACrC,eAAe,SAASC,qBAAqB,GAAGC,IAAS;IACvD,MAAMC,WAAWH,kBAAkBE;IACnC,OAAO;QACLE,SAAS;YACPC,SAAS;gBACPC,MAAKC,IAA6B,EAAEC,KAAK;oBACvC,IAAIC,qBAAqB;oBACzBF,KAAKG,QAAQ,CAAC;wBACZC,kBAAiBC,cAAmB;4BAClC,IAAIA,eAAeC,IAAI,CAACC,MAAM,CAACC,IAAI,KAAK,UAAU;4BAClD,IAAIH,eAAeC,IAAI,CAACG,QAAQ,CAACD,IAAI,KAAK,WAAW;4BACrDN,qBAAqB;wBACvB;oBACF;oBAEA,IAAI,CAACA,oBAAoB;wBACvB;oBACF;oBAEAN,SAASC,OAAO,CAACC,OAAO,CAACC,IAAI,CAACW,IAAI,CAAC,IAAI,EAAEV,MAAMC;gBACjD;YACF;QACF;IACF;AACF"}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
import jsx from 'next/dist/compiled/babel/plugin-syntax-jsx';
|
||||
export default function({ types: t }) {
|
||||
return {
|
||||
inherits: jsx,
|
||||
visitor: {
|
||||
JSXElement (_path, state) {
|
||||
state.set('jsx', true);
|
||||
},
|
||||
// Fragment syntax is still JSX since it compiles to createElement(),
|
||||
// but JSXFragment is not a JSXElement
|
||||
JSXFragment (_path, state) {
|
||||
state.set('jsx', true);
|
||||
},
|
||||
Program: {
|
||||
exit (path, state) {
|
||||
if (state.get('jsx')) {
|
||||
const pragma = t.identifier(state.opts.pragma);
|
||||
let importAs = pragma;
|
||||
// if there's already a React in scope, use that instead of adding an import
|
||||
const existingBinding = state.opts.reuseImport !== false && state.opts.importAs && path.scope.getBinding(state.opts.importAs);
|
||||
// var _jsx = _pragma.createElement;
|
||||
if (state.opts.property) {
|
||||
if (state.opts.importAs) {
|
||||
importAs = t.identifier(state.opts.importAs);
|
||||
} else {
|
||||
importAs = path.scope.generateUidIdentifier('pragma');
|
||||
}
|
||||
const mapping = t.variableDeclaration('var', [
|
||||
t.variableDeclarator(pragma, t.memberExpression(importAs, t.identifier(state.opts.property)))
|
||||
]);
|
||||
// if the React binding came from a require('react'),
|
||||
// make sure that our usage comes after it.
|
||||
let newPath;
|
||||
if (existingBinding && t.isVariableDeclarator(existingBinding.path.node) && t.isCallExpression(existingBinding.path.node.init) && t.isIdentifier(existingBinding.path.node.init.callee) && existingBinding.path.node.init.callee.name === 'require') {
|
||||
;
|
||||
[newPath] = existingBinding.path.parentPath.insertAfter(mapping);
|
||||
} else {
|
||||
;
|
||||
[newPath] = path.unshiftContainer('body', mapping);
|
||||
}
|
||||
for (const declar of newPath.get('declarations')){
|
||||
path.scope.registerBinding(newPath.node.kind, declar);
|
||||
}
|
||||
}
|
||||
if (!existingBinding) {
|
||||
const importSpecifier = t.importDeclaration([
|
||||
state.opts.import ? t.importSpecifier(importAs, t.identifier(state.opts.import)) : state.opts.importNamespace ? t.importNamespaceSpecifier(importAs) : t.importDefaultSpecifier(importAs)
|
||||
], t.stringLiteral(state.opts.module || 'react'));
|
||||
const [newPath] = path.unshiftContainer('body', importSpecifier);
|
||||
for (const specifier of newPath.get('specifiers')){
|
||||
path.scope.registerBinding('module', specifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=jsx-pragma.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/plugins/jsx-pragma.ts"],"sourcesContent":["import type {\n NodePath,\n types as BabelTypes,\n} from 'next/dist/compiled/babel/core'\nimport type { PluginObj } from 'next/dist/compiled/babel/core'\nimport jsx from 'next/dist/compiled/babel/plugin-syntax-jsx'\n\nexport default function ({\n types: t,\n}: {\n types: typeof BabelTypes\n}): PluginObj<any> {\n return {\n inherits: jsx,\n visitor: {\n JSXElement(_path, state) {\n state.set('jsx', true)\n },\n\n // Fragment syntax is still JSX since it compiles to createElement(),\n // but JSXFragment is not a JSXElement\n JSXFragment(_path, state) {\n state.set('jsx', true)\n },\n\n Program: {\n exit(path: NodePath<BabelTypes.Program>, state) {\n if (state.get('jsx')) {\n const pragma = t.identifier(state.opts.pragma)\n let importAs = pragma\n\n // if there's already a React in scope, use that instead of adding an import\n const existingBinding =\n state.opts.reuseImport !== false &&\n state.opts.importAs &&\n path.scope.getBinding(state.opts.importAs)\n\n // var _jsx = _pragma.createElement;\n if (state.opts.property) {\n if (state.opts.importAs) {\n importAs = t.identifier(state.opts.importAs)\n } else {\n importAs = path.scope.generateUidIdentifier('pragma')\n }\n\n const mapping = t.variableDeclaration('var', [\n t.variableDeclarator(\n pragma,\n t.memberExpression(\n importAs,\n t.identifier(state.opts.property)\n )\n ),\n ])\n\n // if the React binding came from a require('react'),\n // make sure that our usage comes after it.\n let newPath: NodePath<BabelTypes.VariableDeclaration>\n\n if (\n existingBinding &&\n t.isVariableDeclarator(existingBinding.path.node) &&\n t.isCallExpression(existingBinding.path.node.init) &&\n t.isIdentifier(existingBinding.path.node.init.callee) &&\n existingBinding.path.node.init.callee.name === 'require'\n ) {\n ;[newPath] =\n existingBinding.path.parentPath.insertAfter(mapping)\n } else {\n ;[newPath] = path.unshiftContainer('body', mapping)\n }\n\n for (const declar of newPath.get('declarations')) {\n path.scope.registerBinding(\n newPath.node.kind,\n declar as NodePath<BabelTypes.Node>\n )\n }\n }\n\n if (!existingBinding) {\n const importSpecifier = t.importDeclaration(\n [\n state.opts.import\n ? // import { $import as _pragma } from '$module'\n t.importSpecifier(\n importAs,\n t.identifier(state.opts.import)\n )\n : state.opts.importNamespace\n ? t.importNamespaceSpecifier(importAs)\n : // import _pragma from '$module'\n t.importDefaultSpecifier(importAs),\n ],\n t.stringLiteral(state.opts.module || 'react')\n )\n\n const [newPath] = path.unshiftContainer('body', importSpecifier)\n for (const specifier of newPath.get('specifiers')) {\n path.scope.registerBinding(\n 'module',\n specifier as NodePath<BabelTypes.Node>\n )\n }\n }\n }\n },\n },\n },\n }\n}\n"],"names":["jsx","types","t","inherits","visitor","JSXElement","_path","state","set","JSXFragment","Program","exit","path","get","pragma","identifier","opts","importAs","existingBinding","reuseImport","scope","getBinding","property","generateUidIdentifier","mapping","variableDeclaration","variableDeclarator","memberExpression","newPath","isVariableDeclarator","node","isCallExpression","init","isIdentifier","callee","name","parentPath","insertAfter","unshiftContainer","declar","registerBinding","kind","importSpecifier","importDeclaration","import","importNamespace","importNamespaceSpecifier","importDefaultSpecifier","stringLiteral","module","specifier"],"mappings":"AAKA,OAAOA,SAAS,6CAA4C;AAE5D,eAAe,SAAU,EACvBC,OAAOC,CAAC,EAGT;IACC,OAAO;QACLC,UAAUH;QACVI,SAAS;YACPC,YAAWC,KAAK,EAAEC,KAAK;gBACrBA,MAAMC,GAAG,CAAC,OAAO;YACnB;YAEA,qEAAqE;YACrE,sCAAsC;YACtCC,aAAYH,KAAK,EAAEC,KAAK;gBACtBA,MAAMC,GAAG,CAAC,OAAO;YACnB;YAEAE,SAAS;gBACPC,MAAKC,IAAkC,EAAEL,KAAK;oBAC5C,IAAIA,MAAMM,GAAG,CAAC,QAAQ;wBACpB,MAAMC,SAASZ,EAALine truncated
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
export default function NextPageDisallowReExportAllExports() {
|
||||
return {
|
||||
visitor: {
|
||||
ImportDeclaration (path) {
|
||||
if ([
|
||||
'@next/font/local',
|
||||
'@next/font/google',
|
||||
'next/font/local',
|
||||
'next/font/google'
|
||||
].includes(path.node.source.value)) {
|
||||
var _path_node_loc, _path_node_loc1;
|
||||
const err = Object.defineProperty(new SyntaxError(`"next/font" requires SWC although Babel is being used due to a custom babel config being present.\nRead more: https://nextjs.org/docs/messages/babel-font-loader-conflict`), "__NEXT_ERROR_CODE", {
|
||||
value: "E542",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
err.code = 'BABEL_PARSE_ERROR';
|
||||
err.loc = ((_path_node_loc = path.node.loc) == null ? void 0 : _path_node_loc.start) ?? ((_path_node_loc1 = path.node.loc) == null ? void 0 : _path_node_loc1.end) ?? path.node.loc;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=next-font-unsupported.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/plugins/next-font-unsupported.ts"],"sourcesContent":["import type { NodePath, types } from 'next/dist/compiled/babel/core'\nimport type { PluginObj } from 'next/dist/compiled/babel/core'\n\nexport default function NextPageDisallowReExportAllExports(): PluginObj<any> {\n return {\n visitor: {\n ImportDeclaration(path: NodePath<types.ImportDeclaration>) {\n if (\n [\n '@next/font/local',\n '@next/font/google',\n 'next/font/local',\n 'next/font/google',\n ].includes(path.node.source.value)\n ) {\n const err = new SyntaxError(\n `\"next/font\" requires SWC although Babel is being used due to a custom babel config being present.\\nRead more: https://nextjs.org/docs/messages/babel-font-loader-conflict`\n )\n ;(err as any).code = 'BABEL_PARSE_ERROR'\n ;(err as any).loc =\n path.node.loc?.start ?? path.node.loc?.end ?? path.node.loc\n throw err\n }\n },\n },\n }\n}\n"],"names":["NextPageDisallowReExportAllExports","visitor","ImportDeclaration","path","includes","node","source","value","err","SyntaxError","code","loc","start","end"],"mappings":"AAGA,eAAe,SAASA;IACtB,OAAO;QACLC,SAAS;YACPC,mBAAkBC,IAAuC;gBACvD,IACE;oBACE;oBACA;oBACA;oBACA;iBACD,CAACC,QAAQ,CAACD,KAAKE,IAAI,CAACC,MAAM,CAACC,KAAK,GACjC;wBAMEJ,gBAAwBA;oBAL1B,MAAMK,MAAM,qBAEX,CAFW,IAAIC,YACd,CAAC,yKAAyK,CAAC,GADjK,qBAAA;+BAAA;oCAAA;sCAAA;oBAEZ;oBACED,IAAYE,IAAI,GAAG;oBACnBF,IAAYG,GAAG,GACfR,EAAAA,iBAAAA,KAAKE,IAAI,CAACM,GAAG,qBAAbR,eAAeS,KAAK,OAAIT,kBAAAA,KAAKE,IAAI,CAACM,GAAG,qBAAbR,gBAAeU,GAAG,KAAIV,KAAKE,IAAI,CAACM,GAAG;oBAC7D,MAAMH;gBACR;YACF;QACF;IACF;AACF"}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
import { types as BabelTypes } from 'next/dist/compiled/babel/core';
|
||||
import { STRING_LITERAL_DROP_BUNDLE } from '../../../shared/lib/constants';
|
||||
const CONFIG_KEY = 'config';
|
||||
// replace program path with just a variable with the drop identifier
|
||||
function replaceBundle(path, t) {
|
||||
path.parentPath.replaceWith(t.program([
|
||||
t.variableDeclaration('const', [
|
||||
t.variableDeclarator(t.identifier(STRING_LITERAL_DROP_BUNDLE), t.stringLiteral(`${STRING_LITERAL_DROP_BUNDLE} ${Date.now()}`))
|
||||
])
|
||||
], []));
|
||||
}
|
||||
function errorMessage(state, details) {
|
||||
const pageName = (state.filename || '').split(state.cwd || '').pop() || 'unknown';
|
||||
return `Invalid page config export found. ${details} in file ${pageName}. See: https://nextjs.org/docs/messages/invalid-page-config`;
|
||||
}
|
||||
// config to parsing pageConfig for client bundles
|
||||
export default function nextPageConfig({ types: t }) {
|
||||
return {
|
||||
visitor: {
|
||||
Program: {
|
||||
enter (path, state) {
|
||||
path.traverse({
|
||||
ExportDeclaration (exportPath, exportState) {
|
||||
var _exportPath_node_specifiers;
|
||||
if (BabelTypes.isExportNamedDeclaration(exportPath.node) && ((_exportPath_node_specifiers = exportPath.node.specifiers) == null ? void 0 : _exportPath_node_specifiers.some((specifier)=>{
|
||||
return (t.isIdentifier(specifier.exported) ? specifier.exported.name : specifier.exported.value) === CONFIG_KEY;
|
||||
})) && BabelTypes.isStringLiteral(exportPath.node.source)) {
|
||||
throw Object.defineProperty(new Error(errorMessage(exportState, 'Expected object but got export from')), "__NEXT_ERROR_CODE", {
|
||||
value: "E394",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
},
|
||||
ExportNamedDeclaration (exportPath, exportState) {
|
||||
var _exportPath_node_declaration, _exportPath_scope_getBinding;
|
||||
if (exportState.bundleDropped || !exportPath.node.declaration && exportPath.node.specifiers.length === 0) {
|
||||
return;
|
||||
}
|
||||
const config = {};
|
||||
const declarations = [
|
||||
...((_exportPath_node_declaration = exportPath.node.declaration) == null ? void 0 : _exportPath_node_declaration.declarations) || [],
|
||||
(_exportPath_scope_getBinding = exportPath.scope.getBinding(CONFIG_KEY)) == null ? void 0 : _exportPath_scope_getBinding.path.node
|
||||
].filter(Boolean);
|
||||
for (const specifier of exportPath.node.specifiers){
|
||||
if ((t.isIdentifier(specifier.exported) ? specifier.exported.name : specifier.exported.value) === CONFIG_KEY) {
|
||||
// export {} from 'somewhere'
|
||||
if (BabelTypes.isStringLiteral(exportPath.node.source)) {
|
||||
throw Object.defineProperty(new Error(errorMessage(exportState, `Expected object but got import`)), "__NEXT_ERROR_CODE", {
|
||||
value: "E394",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
// import hello from 'world'
|
||||
// export { hello as config }
|
||||
} else if (BabelTypes.isIdentifier(specifier.local)) {
|
||||
var _exportPath_scope_getBinding1;
|
||||
if (BabelTypes.isImportSpecifier((_exportPath_scope_getBinding1 = exportPath.scope.getBinding(specifier.local.name)) == null ? void 0 : _exportPath_scope_getBinding1.path.node)) {
|
||||
throw Object.defineProperty(new Error(errorMessage(exportState, `Expected object but got import`)), "__NEXT_ERROR_CODE", {
|
||||
value: "E394",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const declaration of declarations){
|
||||
if (!BabelTypes.isIdentifier(declaration.id, {
|
||||
name: CONFIG_KEY
|
||||
})) {
|
||||
continue;
|
||||
}
|
||||
let { init } = declaration;
|
||||
if (BabelTypes.isTSAsExpression(init)) {
|
||||
init = init.expression;
|
||||
}
|
||||
if (!BabelTypes.isObjectExpression(init)) {
|
||||
const got = init ? init.type : 'undefined';
|
||||
throw Object.defineProperty(new Error(errorMessage(exportState, `Expected object but got ${got}`)), "__NEXT_ERROR_CODE", {
|
||||
value: "E394",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
for (const prop of init.properties){
|
||||
if (BabelTypes.isSpreadElement(prop)) {
|
||||
throw Object.defineProperty(new Error(errorMessage(exportState, `Property spread is not allowed`)), "__NEXT_ERROR_CODE", {
|
||||
value: "E394",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
const { name } = prop.key;
|
||||
if (BabelTypes.isIdentifier(prop.key, {
|
||||
name: 'amp'
|
||||
})) {
|
||||
if (!BabelTypes.isObjectProperty(prop)) {
|
||||
throw Object.defineProperty(new Error(errorMessage(exportState, `Invalid property "${name}"`)), "__NEXT_ERROR_CODE", {
|
||||
value: "E394",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
if (!BabelTypes.isBooleanLiteral(prop.value) && !BabelTypes.isStringLiteral(prop.value)) {
|
||||
throw Object.defineProperty(new Error(errorMessage(exportState, `Invalid value for "${name}"`)), "__NEXT_ERROR_CODE", {
|
||||
value: "E394",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
config.amp = prop.value.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (config.amp === true) {
|
||||
var _exportState_file_opts, _exportState_file;
|
||||
if (!((_exportState_file = exportState.file) == null ? void 0 : (_exportState_file_opts = _exportState_file.opts) == null ? void 0 : _exportState_file_opts.caller.isDev)) {
|
||||
// don't replace bundle in development so HMR can track
|
||||
// dependencies and trigger reload when they are changed
|
||||
replaceBundle(exportPath, t);
|
||||
}
|
||||
exportState.bundleDropped = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=next-page-config.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/plugins/next-page-config.ts"],"sourcesContent":["import { types as BabelTypes } from 'next/dist/compiled/babel/core'\nimport type {\n PluginObj,\n PluginPass,\n Visitor,\n NodePath,\n} from 'next/dist/compiled/babel/core'\nimport type { PageConfig } from '../../../types'\nimport { STRING_LITERAL_DROP_BUNDLE } from '../../../shared/lib/constants'\n\nconst CONFIG_KEY = 'config'\n\n// replace program path with just a variable with the drop identifier\nfunction replaceBundle(path: any, t: typeof BabelTypes): void {\n path.parentPath.replaceWith(\n t.program(\n [\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(STRING_LITERAL_DROP_BUNDLE),\n t.stringLiteral(`${STRING_LITERAL_DROP_BUNDLE} ${Date.now()}`)\n ),\n ]),\n ],\n []\n )\n )\n}\n\nfunction errorMessage(state: any, details: string): string {\n const pageName =\n (state.filename || '').split(state.cwd || '').pop() || 'unknown'\n return `Invalid page config export found. ${details} in file ${pageName}. See: https://nextjs.org/docs/messages/invalid-page-config`\n}\n\ninterface ConfigState extends PluginPass {\n bundleDropped?: boolean\n}\n\n// config to parsing pageConfig for client bundles\nexport default function nextPageConfig({\n types: t,\n}: {\n types: typeof BabelTypes\n}): PluginObj {\n return {\n visitor: {\n Program: {\n enter(path, state) {\n path.traverse(\n {\n ExportDeclaration(exportPath, exportState) {\n if (\n BabelTypes.isExportNamedDeclaration(exportPath.node) &&\n exportPath.node.specifiers?.some((specifier) => {\n return (\n (t.isIdentifier(specifier.exported)\n ? specifier.exported.name\n : specifier.exported.value) === CONFIG_KEY\n )\n }) &&\n BabelTypes.isStringLiteral(\n (exportPath.node as BabelTypes.ExportNamedDeclaration)\n .source\n )\n ) {\n throw new Error(\n errorMessage(\n exportState,\n 'Expected object but got export from'\n )\n )\n }\n },\n ExportNamedDeclaration(\n exportPath: NodePath<BabelTypes.ExportNamedDeclaration>,\n exportState: any\n ) {\n if (\n exportState.bundleDropped ||\n (!exportPath.node.declaration &&\n exportPath.node.specifiers.length === 0)\n ) {\n return\n }\n\n const config: PageConfig = {}\n const declarations: BabelTypes.VariableDeclarator[] = [\n ...((\n exportPath.node\n .declaration as BabelTypes.VariableDeclaration\n )?.declarations || []),\n exportPath.scope.getBinding(CONFIG_KEY)?.path\n .node as BabelTypes.VariableDeclarator,\n ].filter(Boolean)\n\n for (const specifier of exportPath.node.specifiers) {\n if (\n (t.isIdentifier(specifier.exported)\n ? specifier.exported.name\n : specifier.exported.value) === CONFIG_KEY\n ) {\n // export {} from 'somewhere'\n if (BabelTypes.isStringLiteral(exportPath.node.source)) {\n throw new Error(\n errorMessage(\n exportState,\n `Expected object but got import`\n )\n )\n // import hello from 'world'\n // export { hello as config }\n } else if (\n BabelTypes.isIdentifier(\n (specifier as BabelTypes.ExportSpecifier).local\n )\n ) {\n if (\n BabelTypes.isImportSpecifier(\n exportPath.scope.getBinding(\n (specifier as BabelTypes.ExportSpecifier).local.name\n )?.path.node\n )\n ) {\n throw new Error(\n errorMessage(\n exportState,\n `Expected object but got import`\n )\n )\n }\n }\n }\n }\n\n for (const declaration of Line truncated
|
||||
Generated
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
export default function NextPageDisallowReExportAllExports() {
|
||||
return {
|
||||
visitor: {
|
||||
ExportAllDeclaration (path) {
|
||||
var _path_node_loc, _path_node_loc1;
|
||||
const err = Object.defineProperty(new SyntaxError(`Using \`export * from '...'\` in a page is disallowed. Please use \`export { default } from '...'\` instead.\n` + `Read more: https://nextjs.org/docs/messages/export-all-in-page`), "__NEXT_ERROR_CODE", {
|
||||
value: "E555",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
err.code = 'BABEL_PARSE_ERROR';
|
||||
err.loc = ((_path_node_loc = path.node.loc) == null ? void 0 : _path_node_loc.start) ?? ((_path_node_loc1 = path.node.loc) == null ? void 0 : _path_node_loc1.end) ?? path.node.loc;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=next-page-disallow-re-export-all-exports.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/plugins/next-page-disallow-re-export-all-exports.ts"],"sourcesContent":["import type { NodePath, types } from 'next/dist/compiled/babel/core'\nimport type { PluginObj } from 'next/dist/compiled/babel/core'\n\nexport default function NextPageDisallowReExportAllExports(): PluginObj<any> {\n return {\n visitor: {\n ExportAllDeclaration(path: NodePath<types.ExportAllDeclaration>) {\n const err = new SyntaxError(\n `Using \\`export * from '...'\\` in a page is disallowed. Please use \\`export { default } from '...'\\` instead.\\n` +\n `Read more: https://nextjs.org/docs/messages/export-all-in-page`\n )\n ;(err as any).code = 'BABEL_PARSE_ERROR'\n ;(err as any).loc =\n path.node.loc?.start ?? path.node.loc?.end ?? path.node.loc\n throw err\n },\n },\n }\n}\n"],"names":["NextPageDisallowReExportAllExports","visitor","ExportAllDeclaration","path","err","SyntaxError","code","loc","node","start","end"],"mappings":"AAGA,eAAe,SAASA;IACtB,OAAO;QACLC,SAAS;YACPC,sBAAqBC,IAA0C;oBAO3DA,gBAAwBA;gBAN1B,MAAMC,MAAM,qBAGX,CAHW,IAAIC,YACd,CAAC,8GAA8G,CAAC,GAC9G,CAAC,8DAA8D,CAAC,GAFxD,qBAAA;2BAAA;gCAAA;kCAAA;gBAGZ;gBACED,IAAYE,IAAI,GAAG;gBACnBF,IAAYG,GAAG,GACfJ,EAAAA,iBAAAA,KAAKK,IAAI,CAACD,GAAG,qBAAbJ,eAAeM,KAAK,OAAIN,kBAAAA,KAAKK,IAAI,CAACD,GAAG,qBAAbJ,gBAAeO,GAAG,KAAIP,KAAKK,IAAI,CAACD,GAAG;gBAC7D,MAAMH;YACR;QACF;IACF;AACF"}
|
||||
+315
@@ -0,0 +1,315 @@
|
||||
import { SERVER_PROPS_SSG_CONFLICT } from '../../../lib/constants';
|
||||
import { SERVER_PROPS_ID, STATIC_PROPS_ID } from '../../../shared/lib/constants';
|
||||
export const EXPORT_NAME_GET_STATIC_PROPS = 'getStaticProps';
|
||||
export const EXPORT_NAME_GET_STATIC_PATHS = 'getStaticPaths';
|
||||
export const EXPORT_NAME_GET_SERVER_PROPS = 'getServerSideProps';
|
||||
const ssgExports = new Set([
|
||||
EXPORT_NAME_GET_STATIC_PROPS,
|
||||
EXPORT_NAME_GET_STATIC_PATHS,
|
||||
EXPORT_NAME_GET_SERVER_PROPS,
|
||||
// legacy methods added so build doesn't fail from importing
|
||||
// server-side only methods
|
||||
`unstable_getStaticProps`,
|
||||
`unstable_getStaticPaths`,
|
||||
`unstable_getServerProps`,
|
||||
`unstable_getServerSideProps`
|
||||
]);
|
||||
function decorateSsgExport(t, path, state) {
|
||||
const gsspName = state.isPrerender ? STATIC_PROPS_ID : SERVER_PROPS_ID;
|
||||
const gsspId = t.identifier(gsspName);
|
||||
const addGsspExport = (exportPath)=>{
|
||||
if (state.done) {
|
||||
return;
|
||||
}
|
||||
state.done = true;
|
||||
const [pageCompPath] = exportPath.replaceWithMultiple([
|
||||
t.exportNamedDeclaration(t.variableDeclaration(// We use 'var' instead of 'let' or 'const' for ES5 support. Since
|
||||
// this runs in `Program#exit`, no ES2015 transforms (preset env)
|
||||
// will be ran against this code.
|
||||
'var', [
|
||||
t.variableDeclarator(gsspId, t.booleanLiteral(true))
|
||||
]), [
|
||||
t.exportSpecifier(gsspId, gsspId)
|
||||
]),
|
||||
exportPath.node
|
||||
]);
|
||||
exportPath.scope.registerDeclaration(pageCompPath);
|
||||
};
|
||||
path.traverse({
|
||||
ExportDefaultDeclaration (exportDefaultPath) {
|
||||
addGsspExport(exportDefaultPath);
|
||||
},
|
||||
ExportNamedDeclaration (exportNamedPath) {
|
||||
addGsspExport(exportNamedPath);
|
||||
}
|
||||
});
|
||||
}
|
||||
const isDataIdentifier = (name, state)=>{
|
||||
if (ssgExports.has(name)) {
|
||||
if (name === EXPORT_NAME_GET_SERVER_PROPS) {
|
||||
if (state.isPrerender) {
|
||||
throw Object.defineProperty(new Error(SERVER_PROPS_SSG_CONFLICT), "__NEXT_ERROR_CODE", {
|
||||
value: "E394",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
state.isServerProps = true;
|
||||
} else {
|
||||
if (state.isServerProps) {
|
||||
throw Object.defineProperty(new Error(SERVER_PROPS_SSG_CONFLICT), "__NEXT_ERROR_CODE", {
|
||||
value: "E394",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
state.isPrerender = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
export default function nextTransformSsg({ types: t }) {
|
||||
function getIdentifier(path) {
|
||||
const parentPath = path.parentPath;
|
||||
if (parentPath.type === 'VariableDeclarator') {
|
||||
const pp = parentPath;
|
||||
const name = pp.get('id');
|
||||
return name.node.type === 'Identifier' ? name : null;
|
||||
}
|
||||
if (parentPath.type === 'AssignmentExpression') {
|
||||
const pp = parentPath;
|
||||
const name = pp.get('left');
|
||||
return name.node.type === 'Identifier' ? name : null;
|
||||
}
|
||||
if (path.node.type === 'ArrowFunctionExpression') {
|
||||
return null;
|
||||
}
|
||||
return path.node.id && path.node.id.type === 'Identifier' ? path.get('id') : null;
|
||||
}
|
||||
function isIdentifierReferenced(ident) {
|
||||
const b = ident.scope.getBinding(ident.node.name);
|
||||
if (b == null ? void 0 : b.referenced) {
|
||||
// Functions can reference themselves, so we need to check if there's a
|
||||
// binding outside the function scope or not.
|
||||
if (b.path.type === 'FunctionDeclaration') {
|
||||
return !b.constantViolations.concat(b.referencePaths)// Check that every reference is contained within the function:
|
||||
.every((ref)=>ref.findParent((p)=>p === b.path));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function markFunction(path, state) {
|
||||
const ident = getIdentifier(path);
|
||||
if ((ident == null ? void 0 : ident.node) && isIdentifierReferenced(ident)) {
|
||||
state.refs.add(ident);
|
||||
}
|
||||
}
|
||||
function markImport(path, state) {
|
||||
const local = path.get('local');
|
||||
if (isIdentifierReferenced(local)) {
|
||||
state.refs.add(local);
|
||||
}
|
||||
}
|
||||
return {
|
||||
visitor: {
|
||||
Program: {
|
||||
enter (path, state) {
|
||||
state.refs = new Set();
|
||||
state.isPrerender = false;
|
||||
state.isServerProps = false;
|
||||
state.done = false;
|
||||
path.traverse({
|
||||
VariableDeclarator (variablePath, variableState) {
|
||||
if (variablePath.node.id.type === 'Identifier') {
|
||||
const local = variablePath.get('id');
|
||||
if (isIdentifierReferenced(local)) {
|
||||
variableState.refs.add(local);
|
||||
}
|
||||
} else if (variablePath.node.id.type === 'ObjectPattern') {
|
||||
const pattern = variablePath.get('id');
|
||||
const properties = pattern.get('properties');
|
||||
properties.forEach((p)=>{
|
||||
const local = p.get(p.node.type === 'ObjectProperty' ? 'value' : p.node.type === 'RestElement' ? 'argument' : function() {
|
||||
throw Object.defineProperty(new Error('invariant'), "__NEXT_ERROR_CODE", {
|
||||
value: "E400",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}());
|
||||
if (isIdentifierReferenced(local)) {
|
||||
variableState.refs.add(local);
|
||||
}
|
||||
});
|
||||
} else if (variablePath.node.id.type === 'ArrayPattern') {
|
||||
const pattern = variablePath.get('id');
|
||||
const elements = pattern.get('elements');
|
||||
elements.forEach((e)=>{
|
||||
var _e_node, _e_node1;
|
||||
let local;
|
||||
if (((_e_node = e.node) == null ? void 0 : _e_node.type) === 'Identifier') {
|
||||
local = e;
|
||||
} else if (((_e_node1 = e.node) == null ? void 0 : _e_node1.type) === 'RestElement') {
|
||||
local = e.get('argument');
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
if (isIdentifierReferenced(local)) {
|
||||
variableState.refs.add(local);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
FunctionDeclaration: markFunction,
|
||||
FunctionExpression: markFunction,
|
||||
ArrowFunctionExpression: markFunction,
|
||||
ImportSpecifier: markImport,
|
||||
ImportDefaultSpecifier: markImport,
|
||||
ImportNamespaceSpecifier: markImport,
|
||||
ExportNamedDeclaration (exportNamedPath, exportNamedState) {
|
||||
const specifiers = exportNamedPath.get('specifiers');
|
||||
if (specifiers.length) {
|
||||
specifiers.forEach((s)=>{
|
||||
if (isDataIdentifier(t.isIdentifier(s.node.exported) ? s.node.exported.name : s.node.exported.value, exportNamedState)) {
|
||||
s.remove();
|
||||
}
|
||||
});
|
||||
if (exportNamedPath.node.specifiers.length < 1) {
|
||||
exportNamedPath.remove();
|
||||
}
|
||||
return;
|
||||
}
|
||||
const decl = exportNamedPath.get('declaration');
|
||||
if (decl == null || decl.node == null) {
|
||||
return;
|
||||
}
|
||||
switch(decl.node.type){
|
||||
case 'FunctionDeclaration':
|
||||
{
|
||||
const name = decl.node.id.name;
|
||||
if (isDataIdentifier(name, exportNamedState)) {
|
||||
exportNamedPath.remove();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'VariableDeclaration':
|
||||
{
|
||||
const inner = decl.get('declarations');
|
||||
inner.forEach((d)=>{
|
||||
if (d.node.id.type !== 'Identifier') {
|
||||
return;
|
||||
}
|
||||
const name = d.node.id.name;
|
||||
if (isDataIdentifier(name, exportNamedState)) {
|
||||
d.remove();
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}, state);
|
||||
if (!state.isPrerender && !state.isServerProps) {
|
||||
return;
|
||||
}
|
||||
const refs = state.refs;
|
||||
let count;
|
||||
function sweepFunction(sweepPath) {
|
||||
const ident = getIdentifier(sweepPath);
|
||||
if ((ident == null ? void 0 : ident.node) && refs.has(ident) && !isIdentifierReferenced(ident)) {
|
||||
++count;
|
||||
if (t.isAssignmentExpression(sweepPath.parentPath.node) || t.isVariableDeclarator(sweepPath.parentPath.node)) {
|
||||
sweepPath.parentPath.remove();
|
||||
} else {
|
||||
sweepPath.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
function sweepImport(sweepPath) {
|
||||
const local = sweepPath.get('local');
|
||||
if (refs.has(local) && !isIdentifierReferenced(local)) {
|
||||
++count;
|
||||
sweepPath.remove();
|
||||
if (sweepPath.parent.specifiers.length === 0) {
|
||||
sweepPath.parentPath.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
do {
|
||||
;
|
||||
path.scope.crawl();
|
||||
count = 0;
|
||||
path.traverse({
|
||||
// eslint-disable-next-line no-loop-func
|
||||
VariableDeclarator (variablePath) {
|
||||
if (variablePath.node.id.type === 'Identifier') {
|
||||
const local = variablePath.get('id');
|
||||
if (refs.has(local) && !isIdentifierReferenced(local)) {
|
||||
++count;
|
||||
variablePath.remove();
|
||||
}
|
||||
} else if (variablePath.node.id.type === 'ObjectPattern') {
|
||||
const pattern = variablePath.get('id');
|
||||
const beforeCount = count;
|
||||
const properties = pattern.get('properties');
|
||||
properties.forEach((p)=>{
|
||||
const local = p.get(p.node.type === 'ObjectProperty' ? 'value' : p.node.type === 'RestElement' ? 'argument' : function() {
|
||||
throw Object.defineProperty(new Error('invariant'), "__NEXT_ERROR_CODE", {
|
||||
value: "E400",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}());
|
||||
if (refs.has(local) && !isIdentifierReferenced(local)) {
|
||||
++count;
|
||||
p.remove();
|
||||
}
|
||||
});
|
||||
if (beforeCount !== count && pattern.get('properties').length < 1) {
|
||||
variablePath.remove();
|
||||
}
|
||||
} else if (variablePath.node.id.type === 'ArrayPattern') {
|
||||
const pattern = variablePath.get('id');
|
||||
const beforeCount = count;
|
||||
const elements = pattern.get('elements');
|
||||
elements.forEach((e)=>{
|
||||
var _e_node, _e_node1;
|
||||
let local;
|
||||
if (((_e_node = e.node) == null ? void 0 : _e_node.type) === 'Identifier') {
|
||||
local = e;
|
||||
} else if (((_e_node1 = e.node) == null ? void 0 : _e_node1.type) === 'RestElement') {
|
||||
local = e.get('argument');
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
if (refs.has(local) && !isIdentifierReferenced(local)) {
|
||||
++count;
|
||||
e.remove();
|
||||
}
|
||||
});
|
||||
if (beforeCount !== count && pattern.get('elements').length < 1) {
|
||||
variablePath.remove();
|
||||
}
|
||||
}
|
||||
},
|
||||
FunctionDeclaration: sweepFunction,
|
||||
FunctionExpression: sweepFunction,
|
||||
ArrowFunctionExpression: sweepFunction,
|
||||
ImportSpecifier: sweepImport,
|
||||
ImportDefaultSpecifier: sweepImport,
|
||||
ImportNamespaceSpecifier: sweepImport
|
||||
});
|
||||
}while (count);
|
||||
decorateSsgExport(t, path, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=next-ssg-transform.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/plugins/next-ssg-transform.ts"],"sourcesContent":["import type {\n NodePath,\n types as BabelTypes,\n} from 'next/dist/compiled/babel/core'\nimport type { PluginObj } from 'next/dist/compiled/babel/core'\nimport { SERVER_PROPS_SSG_CONFLICT } from '../../../lib/constants'\nimport { SERVER_PROPS_ID, STATIC_PROPS_ID } from '../../../shared/lib/constants'\n\nexport const EXPORT_NAME_GET_STATIC_PROPS = 'getStaticProps'\nexport const EXPORT_NAME_GET_STATIC_PATHS = 'getStaticPaths'\nexport const EXPORT_NAME_GET_SERVER_PROPS = 'getServerSideProps'\n\nconst ssgExports = new Set([\n EXPORT_NAME_GET_STATIC_PROPS,\n EXPORT_NAME_GET_STATIC_PATHS,\n EXPORT_NAME_GET_SERVER_PROPS,\n\n // legacy methods added so build doesn't fail from importing\n // server-side only methods\n `unstable_getStaticProps`,\n `unstable_getStaticPaths`,\n `unstable_getServerProps`,\n `unstable_getServerSideProps`,\n])\n\ntype PluginState = {\n refs: Set<NodePath<BabelTypes.Identifier>>\n isPrerender: boolean\n isServerProps: boolean\n done: boolean\n}\n\nfunction decorateSsgExport(\n t: typeof BabelTypes,\n path: NodePath<BabelTypes.Program>,\n state: PluginState\n): void {\n const gsspName = state.isPrerender ? STATIC_PROPS_ID : SERVER_PROPS_ID\n const gsspId = t.identifier(gsspName)\n\n const addGsspExport = (\n exportPath:\n | NodePath<BabelTypes.ExportDefaultDeclaration>\n | NodePath<BabelTypes.ExportNamedDeclaration>\n ): void => {\n if (state.done) {\n return\n }\n state.done = true\n\n const [pageCompPath] = exportPath.replaceWithMultiple([\n t.exportNamedDeclaration(\n t.variableDeclaration(\n // We use 'var' instead of 'let' or 'const' for ES5 support. Since\n // this runs in `Program#exit`, no ES2015 transforms (preset env)\n // will be ran against this code.\n 'var',\n [t.variableDeclarator(gsspId, t.booleanLiteral(true))]\n ),\n [t.exportSpecifier(gsspId, gsspId)]\n ),\n exportPath.node,\n ])\n exportPath.scope.registerDeclaration(\n pageCompPath as NodePath<BabelTypes.Node>\n )\n }\n\n path.traverse({\n ExportDefaultDeclaration(exportDefaultPath) {\n addGsspExport(exportDefaultPath)\n },\n ExportNamedDeclaration(exportNamedPath) {\n addGsspExport(exportNamedPath)\n },\n })\n}\n\nconst isDataIdentifier = (name: string, state: PluginState): boolean => {\n if (ssgExports.has(name)) {\n if (name === EXPORT_NAME_GET_SERVER_PROPS) {\n if (state.isPrerender) {\n throw new Error(SERVER_PROPS_SSG_CONFLICT)\n }\n state.isServerProps = true\n } else {\n if (state.isServerProps) {\n throw new Error(SERVER_PROPS_SSG_CONFLICT)\n }\n state.isPrerender = true\n }\n return true\n }\n return false\n}\n\nexport default function nextTransformSsg({\n types: t,\n}: {\n types: typeof BabelTypes\n}): PluginObj<PluginState> {\n function getIdentifier(\n path:\n | NodePath<BabelTypes.FunctionDeclaration>\n | NodePath<BabelTypes.FunctionExpression>\n | NodePath<BabelTypes.ArrowFunctionExpression>\n ): NodePath<BabelTypes.Identifier> | null {\n const parentPath = path.parentPath\n if (parentPath.type === 'VariableDeclarator') {\n const pp = parentPath as NodePath<BabelTypes.VariableDeclarator>\n const name = pp.get('id')\n return name.node.type === 'Identifier'\n ? (name as NodePath<BabelTypes.Identifier>)\n : null\n }\n\n if (parentPath.type === 'AssignmentExpression') {\n const pp = parentPath as NodePath<BabelTypes.AssignmentExpression>\n const name = pp.get('left')\n return name.node.type === 'Identifier'\n ? (name as NodePath<BabelTypes.Identifier>)\n : null\n }\n\n if (path.node.type === 'ArrowFunctionExpression') {\n return null\n }\n\n return path.node.id && path.node.id.type === 'Identifier'\n ? (path.get('id') as NodePath<BabelTypes.Identifier>)\n : null\n }\n\n function isIdentifierReferenced(\n ident: NodePath<BabelTypes.Identifier>\n ): boolean {\n const b = ident.scope.getBinding(ident.node.name)\n if (b?.referenced) {\n // Functions can reference themselves, so we need to check if there's a\n // binding outside the function scope or not.\n if (b.path.type === 'FunctionDeclaration') {\n return !b.constantViolations\n .concat(b.referencePaths)\n // Check that every reference is contained within the function:\n .every((ref) => ref.findParent((p) => p === b.path))\n }\n\n return true\n }\n return false\n }\n\n function markFunction(\n path:\n | NodePath<BabelTypes.FunctionDeclaration>\n | NodePath<BabelTypes.FunctionExpression>\n | NodePath<BabelTypes.ArrowFunctionExpression>,\n state: PluginState\n ): void {\n const ident = getIdentifier(path)\n iLine truncated
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
// matches any hook-like (the default)
|
||||
const isHook = /^use[A-Z]/;
|
||||
// matches only built-in hooks provided by React et al
|
||||
const isBuiltInHook = /^use(Callback|Context|DebugValue|Effect|ImperativeHandle|LayoutEffect|Memo|Reducer|Ref|State)$/;
|
||||
export default function({ types: t }) {
|
||||
const visitor = {
|
||||
CallExpression (path, state) {
|
||||
const onlyBuiltIns = state.opts.onlyBuiltIns;
|
||||
// if specified, options.lib is a list of libraries that provide hook functions
|
||||
const libs = state.opts.lib && (state.opts.lib === true ? [
|
||||
'react',
|
||||
'preact/hooks'
|
||||
] : [].concat(state.opts.lib));
|
||||
// skip function calls that are not the init of a variable declaration:
|
||||
if (!t.isVariableDeclarator(path.parent)) return;
|
||||
// skip function calls where the return value is not Array-destructured:
|
||||
if (!t.isArrayPattern(path.parent.id)) return;
|
||||
// name of the (hook) function being called:
|
||||
const hookName = path.node.callee.name;
|
||||
if (libs) {
|
||||
const binding = path.scope.getBinding(hookName);
|
||||
// not an import
|
||||
if (!binding || binding.kind !== 'module') return;
|
||||
const specifier = binding.path.parent.source.value;
|
||||
// not a match
|
||||
if (!libs.some((lib)=>lib === specifier)) return;
|
||||
}
|
||||
// only match function calls with names that look like a hook
|
||||
if (!(onlyBuiltIns ? isBuiltInHook : isHook).test(hookName)) return;
|
||||
path.parent.id = t.objectPattern(path.parent.id.elements.reduce((patterns, element, i)=>{
|
||||
if (element === null) {
|
||||
return patterns;
|
||||
}
|
||||
return patterns.concat(t.objectProperty(t.numericLiteral(i), // TODO: fix this
|
||||
element));
|
||||
}, []));
|
||||
}
|
||||
};
|
||||
return {
|
||||
name: 'optimize-hook-destructuring',
|
||||
visitor: {
|
||||
// this is a workaround to run before preset-env destroys destructured assignments
|
||||
Program (path, state) {
|
||||
path.traverse(visitor, state);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=optimize-hook-destructuring.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/plugins/optimize-hook-destructuring.ts"],"sourcesContent":["import type {\n NodePath,\n types as BabelTypes,\n} from 'next/dist/compiled/babel/core'\nimport type { PluginObj } from 'next/dist/compiled/babel/core'\n// matches any hook-like (the default)\nconst isHook = /^use[A-Z]/\n\n// matches only built-in hooks provided by React et al\nconst isBuiltInHook =\n /^use(Callback|Context|DebugValue|Effect|ImperativeHandle|LayoutEffect|Memo|Reducer|Ref|State)$/\n\nexport default function ({\n types: t,\n}: {\n types: typeof BabelTypes\n}): PluginObj<any> {\n const visitor = {\n CallExpression(path: NodePath<BabelTypes.CallExpression>, state: any) {\n const onlyBuiltIns = state.opts.onlyBuiltIns\n\n // if specified, options.lib is a list of libraries that provide hook functions\n const libs =\n state.opts.lib &&\n (state.opts.lib === true\n ? ['react', 'preact/hooks']\n : [].concat(state.opts.lib))\n\n // skip function calls that are not the init of a variable declaration:\n if (!t.isVariableDeclarator(path.parent)) return\n\n // skip function calls where the return value is not Array-destructured:\n if (!t.isArrayPattern(path.parent.id)) return\n\n // name of the (hook) function being called:\n const hookName = (path.node.callee as BabelTypes.Identifier).name\n\n if (libs) {\n const binding = path.scope.getBinding(hookName)\n // not an import\n if (!binding || binding.kind !== 'module') return\n\n const specifier = (binding.path.parent as BabelTypes.ImportDeclaration)\n .source.value\n // not a match\n if (!libs.some((lib: any) => lib === specifier)) return\n }\n\n // only match function calls with names that look like a hook\n if (!(onlyBuiltIns ? isBuiltInHook : isHook).test(hookName)) return\n\n path.parent.id = t.objectPattern(\n path.parent.id.elements.reduce<Array<BabelTypes.ObjectProperty>>(\n (patterns, element, i) => {\n if (element === null) {\n return patterns\n }\n\n return patterns.concat(\n t.objectProperty(\n t.numericLiteral(i),\n // TODO: fix this\n element as Exclude<\n typeof element,\n BabelTypes.MemberExpression | BabelTypes.TSParameterProperty\n >\n )\n )\n },\n []\n )\n )\n },\n }\n\n return {\n name: 'optimize-hook-destructuring',\n visitor: {\n // this is a workaround to run before preset-env destroys destructured assignments\n Program(path, state) {\n path.traverse(visitor, state)\n },\n },\n }\n}\n"],"names":["isHook","isBuiltInHook","types","t","visitor","CallExpression","path","state","onlyBuiltIns","opts","libs","lib","concat","isVariableDeclarator","parent","isArrayPattern","id","hookName","node","callee","name","binding","scope","getBinding","kind","specifier","source","value","some","test","objectPattern","elements","reduce","patterns","element","i","objectProperty","numericLiteral","Program","traverse"],"mappings":"AAKA,sCAAsC;AACtC,MAAMA,SAAS;AAEf,sDAAsD;AACtD,MAAMC,gBACJ;AAEF,eAAe,SAAU,EACvBC,OAAOC,CAAC,EAGT;IACC,MAAMC,UAAU;QACdC,gBAAeC,IAAyC,EAAEC,KAAU;YAClE,MAAMC,eAAeD,MAAME,IAAI,CAACD,YAAY;YAE5C,+EAA+E;YAC/E,MAAME,OACJH,MAAME,IAAI,CAACE,GAAG,IACbJ,CAAAA,MAAME,IAAI,CAACE,GAAG,KAAK,OAChB;gBAAC;gBAAS;aAAe,GACzB,EAAE,CAACC,MAAM,CAACL,MAAME,IAAI,CAACE,GAAG,CAAA;YAE9B,uEAAuE;YACvE,IAAI,CAACR,EAAEU,oBAAoB,CAACP,KAAKQ,MAAM,GAAG;YAE1C,wEAAwE;YACxE,IAAI,CAACX,EAAEY,cAAc,CAACT,KAAKQ,MAAM,CAACE,EAAE,GAAG;YAEvC,4CAA4C;YAC5C,MAAMC,WAAW,AAACX,KAAKY,IAAI,CAACC,MAAM,CAA2BC,IAAI;YAEjE,IAAIV,MAAM;gBACR,MAAMW,UAAUf,KAAKgB,KAAK,CAACC,UAAU,CAACN;gBACtC,gBAAgB;gBAChB,IAAI,CAACI,WAAWA,QAAQG,IAAI,KAAK,UAAU;gBAE3C,MAAMC,YAAY,AAACJ,QAAQf,IAAI,CAACQ,MAAM,CACnCY,MAAM,CAACC,KAAK;gBACf,cAAc;gBACd,IAAI,CAACjB,KAAKkB,IAAI,CAAC,CAACjB,MAAaA,QAAQc,YAAY;YACnD;YAEA,6DAA6D;YAC7D,IAAI,CAAC,AAACjB,CAAAA,eAAeP,gBAAgBD,MAAK,EAAG6B,IAAI,CAACZ,WAAW;YAE7DX,KAAKQ,MAAM,CAACE,EAAE,GAAGb,EAAE2B,aAAa,CAC9BxB,KAAKQ,MAAM,CAACE,EAAE,CAACe,QAAQ,CAACC,MAAM,CAC5B,CAACC,UAAUC,SAASC;gBAClB,IAAID,YAAY,MAAM;oBACpB,OAAOD;gBACT;gBAEA,OAAOA,SAASrB,MAAM,CACpBT,EAAEiC,cAAc,CACdjC,EAAEkC,cAAc,CAACF,IACjB,iBAAiB;gBACjBD;YAMN,GACA,EAAE;QAGR;IACF;IAEA,OAAO;QACLd,MAAM;QACNhB,SAAS;YACP,kFAAkF;YAClFkC,SAAQhC,IAAI,EAAEC,KAAK;gBACjBD,KAAKiC,QAAQ,CAACnC,SAASG;YACzB;QACF;IACF;AACF"}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
COPYRIGHT (c) 2017-present James Kyle <me@thejameskyle.com>
|
||||
MIT License
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR
|
||||
*/ // This file is https://github.com/jamiebuilds/react-loadable/blob/master/src/babel.js
|
||||
// Modified to also look for `next/dynamic`
|
||||
// Modified to put `webpack` and `modules` under `loadableGenerated` to be backwards compatible with next/dynamic which has a `modules` key
|
||||
// Modified to support `dynamic(import('something'))` and `dynamic(import('something'), options)
|
||||
import { relative as relativePath } from 'path';
|
||||
export default function({ types: t }) {
|
||||
return {
|
||||
visitor: {
|
||||
ImportDeclaration (path, state) {
|
||||
let source = path.node.source.value;
|
||||
if (source !== 'next/dynamic') return;
|
||||
let defaultSpecifier = path.get('specifiers').find((specifier)=>{
|
||||
return specifier.isImportDefaultSpecifier();
|
||||
});
|
||||
if (!defaultSpecifier) return;
|
||||
const bindingName = defaultSpecifier.node.local.name;
|
||||
const binding = path.scope.getBinding(bindingName);
|
||||
if (!binding) {
|
||||
return;
|
||||
}
|
||||
binding.referencePaths.forEach((refPath)=>{
|
||||
var _state_file_opts_caller, _state_file_opts_caller1;
|
||||
let callExpression = refPath.parentPath;
|
||||
if (callExpression.isMemberExpression() && callExpression.node.computed === false) {
|
||||
const property = callExpression.get('property');
|
||||
if (!Array.isArray(property) && property.isIdentifier({
|
||||
name: 'Map'
|
||||
})) {
|
||||
callExpression = callExpression.parentPath;
|
||||
}
|
||||
}
|
||||
if (!callExpression.isCallExpression()) return;
|
||||
const callExpression_ = callExpression;
|
||||
let args = callExpression_.get('arguments');
|
||||
if (args.length > 2) {
|
||||
throw callExpression_.buildCodeFrameError('next/dynamic only accepts 2 arguments');
|
||||
}
|
||||
if (!args[0]) {
|
||||
return;
|
||||
}
|
||||
let loader;
|
||||
let options;
|
||||
if (args[0].isObjectExpression()) {
|
||||
options = args[0];
|
||||
} else {
|
||||
if (!args[1]) {
|
||||
callExpression_.node.arguments.push(t.objectExpression([]));
|
||||
}
|
||||
// This is needed as the code is modified above
|
||||
args = callExpression_.get('arguments');
|
||||
loader = args[0];
|
||||
options = args[1];
|
||||
}
|
||||
if (!options.isObjectExpression()) return;
|
||||
const options_ = options;
|
||||
let properties = options_.get('properties');
|
||||
let propertiesMap = {};
|
||||
properties.forEach((property)=>{
|
||||
const key = property.get('key');
|
||||
propertiesMap[key.node.name] = property;
|
||||
});
|
||||
if (propertiesMap.loadableGenerated) {
|
||||
return;
|
||||
}
|
||||
if (propertiesMap.loader) {
|
||||
loader = propertiesMap.loader.get('value');
|
||||
}
|
||||
if (propertiesMap.modules) {
|
||||
loader = propertiesMap.modules.get('value');
|
||||
}
|
||||
if (!loader || Array.isArray(loader)) {
|
||||
return;
|
||||
}
|
||||
const dynamicImports = [];
|
||||
const dynamicKeys = [];
|
||||
if (propertiesMap.ssr) {
|
||||
const ssr = propertiesMap.ssr.get('value');
|
||||
const nodePath = Array.isArray(ssr) ? undefined : ssr;
|
||||
if (nodePath) {
|
||||
var _state_file_opts_caller2;
|
||||
const nonSSR = nodePath.node.type === 'BooleanLiteral' && nodePath.node.value === false;
|
||||
// If `ssr` is set to `false`, erase the loader for server side
|
||||
if (nonSSR && loader && ((_state_file_opts_caller2 = state.file.opts.caller) == null ? void 0 : _state_file_opts_caller2.isServer)) {
|
||||
loader.replaceWith(t.arrowFunctionExpression([], t.nullLiteral(), true));
|
||||
}
|
||||
}
|
||||
}
|
||||
loader.traverse({
|
||||
Import (importPath) {
|
||||
var _state_file_opts_caller;
|
||||
const importArguments = importPath.parentPath.get('arguments');
|
||||
if (!Array.isArray(importArguments)) return;
|
||||
const node = importArguments[0].node;
|
||||
dynamicImports.push(node);
|
||||
dynamicKeys.push(t.binaryExpression('+', t.stringLiteral((((_state_file_opts_caller = state.file.opts.caller) == null ? void 0 : _state_file_opts_caller.srcDir) ? relativePath(state.file.opts.caller.srcDir, state.file.opts.filename) : state.file.opts.filename) + ' -> '), node));
|
||||
}
|
||||
});
|
||||
if (!dynamicImports.length) return;
|
||||
options.node.properties.push(t.objectProperty(t.identifier('loadableGenerated'), t.objectExpression(((_state_file_opts_caller = state.file.opts.caller) == null ? void 0 : _state_file_opts_caller.isDev) || ((_state_file_opts_caller1 = state.file.opts.caller) == null ? void 0 : _state_file_opts_caller1.isServer) ? [
|
||||
t.objectProperty(t.identifier('modules'), t.arrayExpression(dynamicKeys))
|
||||
] : [
|
||||
t.objectProperty(t.identifier('webpack'), t.arrowFunctionExpression([], t.arrayExpression(dynamicImports.map((dynamicImport)=>{
|
||||
return t.callExpression(t.memberExpression(t.identifier('require'), t.identifier('resolveWeak')), [
|
||||
dynamicImport
|
||||
]);
|
||||
}))))
|
||||
])));
|
||||
// Turns `dynamic(import('something'))` into `dynamic(() => import('something'))` for backwards compat.
|
||||
// This is the replicate the behavior in versions below Next.js 7 where we magically handled not executing the `import()` too.
|
||||
// We'll deprecate this behavior and provide a codemod for it in 7.1.
|
||||
if (loader.isCallExpression()) {
|
||||
const arrowFunction = t.arrowFunctionExpression([], loader.node);
|
||||
loader.replaceWith(arrowFunction);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=react-loadable-plugin.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/plugins/react-loadable-plugin.ts"],"sourcesContent":["/**\nCOPYRIGHT (c) 2017-present James Kyle <me@thejameskyle.com>\n MIT License\n Permission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n The above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\nLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR\n*/\n// This file is https://github.com/jamiebuilds/react-loadable/blob/master/src/babel.js\n// Modified to also look for `next/dynamic`\n// Modified to put `webpack` and `modules` under `loadableGenerated` to be backwards compatible with next/dynamic which has a `modules` key\n// Modified to support `dynamic(import('something'))` and `dynamic(import('something'), options)\n\nimport type {\n NodePath,\n types as BabelTypes,\n} from 'next/dist/compiled/babel/core'\nimport type { PluginObj } from 'next/dist/compiled/babel/core'\n\nimport { relative as relativePath } from 'path'\n\nexport default function ({\n types: t,\n}: {\n types: typeof BabelTypes\n}): PluginObj {\n return {\n visitor: {\n ImportDeclaration(\n path: NodePath<BabelTypes.ImportDeclaration>,\n state: any\n ) {\n let source = path.node.source.value\n if (source !== 'next/dynamic') return\n\n let defaultSpecifier = path.get('specifiers').find((specifier) => {\n return specifier.isImportDefaultSpecifier()\n })\n\n if (!defaultSpecifier) return\n\n const bindingName = defaultSpecifier.node.local.name\n const binding = path.scope.getBinding(bindingName)\n\n if (!binding) {\n return\n }\n\n binding.referencePaths.forEach((refPath) => {\n let callExpression = refPath.parentPath\n\n if (\n callExpression.isMemberExpression() &&\n callExpression.node.computed === false\n ) {\n const property = callExpression.get('property')\n if (\n !Array.isArray(property) &&\n property.isIdentifier({ name: 'Map' })\n ) {\n callExpression = callExpression.parentPath\n }\n }\n\n if (!callExpression.isCallExpression()) return\n\n const callExpression_ =\n callExpression as NodePath<BabelTypes.CallExpression>\n\n let args = callExpression_.get('arguments')\n if (args.length > 2) {\n throw callExpression_.buildCodeFrameError(\n 'next/dynamic only accepts 2 arguments'\n )\n }\n\n if (!args[0]) {\n return\n }\n\n let loader\n let options\n\n if (args[0].isObjectExpression()) {\n options = args[0]\n } else {\n if (!args[1]) {\n callExpression_.node.arguments.push(t.objectExpression([]))\n }\n // This is needed as the code is modified above\n args = callExpression_.get('arguments')\n loader = args[0]\n options = args[1]\n }\n\n if (!options.isObjectExpression()) return\n const options_ = options as NodePath<BabelTypes.ObjectExpression>\n\n let properties = options_.get('properties')\n let propertiesMap: {\n [key: string]: NodePath<\n | BabelTypes.ObjectProperty\n | BabelTypes.ObjectMethod\n | BabelTypes.SpreadElement\n | BabelTypes.BooleanLiteral\n >\n } = {}\n\n properties.forEach((property) => {\n const key: any = property.get('key')\n propertiesMap[key.node.name] = property\n })\n\n if (propertiesMap.loadableGenerated) {\n return\n }\n\n if (propertiesMap.loader) {\n loader = propertiesMap.loader.get('value')\n }\n\n if (propertiesMap.modules) {\n loader = propertiesMap.modules.get('value')\n }\n\n if (!loader || Array.isArray(loader)) {\n return\n }\n const dynamicImports: BabelTyLine truncated
|
||||
Reference in new issue
Block a user