create project

This commit is contained in:
ismailsosic
2022-12-27 12:05:56 +01:00
parent 2a33a2d3de
commit cd2143287c
16035 changed files with 2489703 additions and 0 deletions

View File

@@ -0,0 +1,219 @@
import { readFileSync } from "fs";
import JSON5 from "next/dist/compiled/json5";
import { createConfigItem, loadOptions } from "next/dist/compiled/babel/core";
import loadConfig from "next/dist/compiled/babel/core-lib-config";
import { consumeIterator } from "./util";
import * as Log from "../../output/log";
const nextDistPath = /(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/;
const fileExtensionRegex = /\.([a-z]+)$/;
function getCacheCharacteristics(loaderOptions, source, filename) {
var ref;
const { isServer , pagesDir } = loaderOptions;
const isPageFile = filename.startsWith(pagesDir);
const isNextDist = nextDistPath.test(filename);
const hasModuleExports = source.indexOf("module.exports") !== -1;
const fileExt = ((ref = fileExtensionRegex.exec(filename)) == null ? void 0 : ref[1]) || "unknown";
return {
isServer,
isPageFile,
isNextDist,
hasModuleExports,
fileExt
};
}
/**
* Return an array of Babel plugins, conditioned upon loader options and
* source file characteristics.
*/ function getPlugins(loaderOptions, cacheCharacteristics) {
const { isServer , isPageFile , isNextDist , hasModuleExports } = cacheCharacteristics;
const { hasReactRefresh , development } = loaderOptions;
const applyCommonJsItem = hasModuleExports ? createConfigItem(require("../plugins/commonjs"), {
type: "plugin"
}) : null;
const reactRefreshItem = hasReactRefresh ? createConfigItem([
require("next/dist/compiled/react-refresh/babel"),
{
skipEnvCheck: true
},
], {
type: "plugin"
}) : null;
const pageConfigItem = !isServer && isPageFile ? createConfigItem([
require("../plugins/next-page-config")
], {
type: "plugin"
}) : null;
const disallowExportAllItem = !isServer && isPageFile ? createConfigItem([
require("../plugins/next-page-disallow-re-export-all-exports")
], {
type: "plugin"
}) : null;
const transformDefineItem = createConfigItem([
require.resolve("next/dist/compiled/babel/plugin-transform-define"),
{
"process.env.NODE_ENV": development ? "development" : "production",
"typeof window": isServer ? "undefined" : "object",
"process.browser": isServer ? false : true
},
"next-js-transform-define-instance",
], {
type: "plugin"
});
const nextSsgItem = !isServer && isPageFile ? createConfigItem([
require.resolve("../plugins/next-ssg-transform")
], {
type: "plugin"
}) : null;
const commonJsItem = isNextDist ? createConfigItem(require("next/dist/compiled/babel/plugin-transform-modules-commonjs"), {
type: "plugin"
}) : null;
const nextFontUnsupported = createConfigItem([
require("../plugins/next-font-unsupported")
], {
type: "plugin"
});
return [
reactRefreshItem,
pageConfigItem,
disallowExportAllItem,
applyCommonJsItem,
transformDefineItem,
nextSsgItem,
commonJsItem,
nextFontUnsupported,
].filter(Boolean);
}
const isJsonFile = /\.(json|babelrc)$/;
const isJsFile = /\.js$/;
/**
* While this function does block execution while reading from disk, it
* should not introduce any issues. The function is only invoked when
* generating a fresh config, and only a small handful of configs should
* be generated during compilation.
*/ function getCustomBabelConfig(configFilePath) {
if (isJsonFile.exec(configFilePath)) {
const babelConfigRaw = readFileSync(configFilePath, "utf8");
return JSON5.parse(babelConfigRaw);
} else if (isJsFile.exec(configFilePath)) {
return require(configFilePath);
}
throw new Error("The Next.js Babel loader does not support .mjs or .cjs config files.");
}
/**
* Generate a new, flat Babel config, ready to be handed to Babel-traverse.
* This config should have no unresolved overrides, presets, etc.
*/ function getFreshConfig(cacheCharacteristics, loaderOptions, target, filename, inputSourceMap) {
let { isServer , pagesDir , development , hasJsxRuntime , configFile } = loaderOptions;
let customConfig = configFile ? getCustomBabelConfig(configFile) : undefined;
let options = {
babelrc: false,
cloneInputAst: false,
filename,
inputSourceMap: inputSourceMap || undefined,
// Set the default sourcemap behavior based on Webpack's mapping flag,
// but allow users to override if they want.
sourceMaps: loaderOptions.sourceMaps === undefined ? this.sourceMap : loaderOptions.sourceMaps,
// Ensure that Webpack will get a full absolute path in the sourcemap
// so that it can properly map the module back to its internal cached
// modules.
sourceFileName: filename,
plugins: [
...getPlugins(loaderOptions, cacheCharacteristics),
...(customConfig == null ? void 0 : customConfig.plugins) || [],
],
// target can be provided in babelrc
target: isServer ? undefined : customConfig == null ? void 0 : customConfig.target,
// env can be provided in babelrc
env: customConfig == null ? void 0 : customConfig.env,
presets: (()=>{
// If presets is defined the user will have next/babel in their babelrc
if (customConfig == null ? void 0 : customConfig.presets) {
return customConfig.presets;
}
// If presets is not defined the user will likely have "env" in their babelrc
if (customConfig) {
return undefined;
}
// If no custom config is provided the default is to use next/babel
return [
"next/babel"
];
})(),
overrides: loaderOptions.overrides,
caller: {
name: "next-babel-turbo-loader",
supportsStaticESM: true,
supportsDynamicImport: true,
// Provide plugins with insight into webpack target.
// https://github.com/babel/babel-loader/issues/787
target: target,
// Webpack 5 supports TLA behind a flag. We enable it by default
// for Babel, and then webpack will throw an error if the experimental
// flag isn't enabled.
supportsTopLevelAwait: true,
isServer,
pagesDir,
isDev: development,
hasJsxRuntime,
...loaderOptions.caller
}
};
// Babel does strict checks on the config so undefined is not allowed
if (typeof options.target === "undefined") {
delete options.target;
}
Object.defineProperty(options.caller, "onWarning", {
enumerable: false,
writable: false,
value: (reason)=>{
if (!(reason instanceof Error)) {
reason = new Error(reason);
}
this.emitWarning(reason);
}
});
const loadedOptions = loadOptions(options);
const config = consumeIterator(loadConfig(loadedOptions));
return config;
}
/**
* Each key returned here corresponds with a Babel config that can be shared.
* The conditions of permissible sharing between files is dependent on specific
* file attributes and Next.js compiler states: `CharacteristicsGermaneToCaching`.
*/ function getCacheKey(cacheCharacteristics) {
const { isServer , isPageFile , isNextDist , hasModuleExports , fileExt } = cacheCharacteristics;
const flags = 0 | (isServer ? 0b0001 : 0) | (isPageFile ? 0b0010 : 0) | (isNextDist ? 0b0100 : 0) | (hasModuleExports ? 0b1000 : 0);
return fileExt + flags;
}
const configCache = new Map();
const configFiles = new Set();
export default function getConfig({ source , target , loaderOptions , filename , inputSourceMap }) {
const cacheCharacteristics = getCacheCharacteristics(loaderOptions, source, filename);
if (loaderOptions.configFile) {
// Ensures webpack invalidates the cache for this loader when the config file changes
this.addDependency(loaderOptions.configFile);
}
const cacheKey = getCacheKey(cacheCharacteristics);
if (configCache.has(cacheKey)) {
const cachedConfig = configCache.get(cacheKey);
return {
...cachedConfig,
options: {
...cachedConfig.options,
cwd: loaderOptions.cwd,
root: loaderOptions.cwd,
filename,
sourceFileName: filename
}
};
}
if (loaderOptions.configFile && !configFiles.has(loaderOptions.configFile)) {
configFiles.add(loaderOptions.configFile);
Log.info(`Using external babel configuration from ${loaderOptions.configFile}`);
}
const freshConfig = getFreshConfig.call(this, cacheCharacteristics, loaderOptions, target, filename, inputSourceMap);
configCache.set(cacheKey, freshConfig);
return freshConfig;
};
//# sourceMappingURL=get-config.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,25 @@
import transform from "./transform";
async function nextBabelLoader(parentTrace, inputSource, inputSourceMap) {
const filename = this.resourcePath;
const target = this.target;
const loaderOptions = parentTrace.traceChild("get-options")// @ts-ignore TODO: remove ignore once webpack 5 types are used
.traceFn(()=>this.getOptions());
const loaderSpanInner = parentTrace.traceChild("next-babel-turbo-transform");
const { code: transformedSource , map: outputSourceMap } = loaderSpanInner.traceFn(()=>transform.call(this, inputSource, inputSourceMap, loaderOptions, filename, target, loaderSpanInner));
return [
transformedSource,
outputSourceMap
];
}
const nextBabelLoaderOuter = function nextBabelLoaderOuter(inputSource, inputSourceMap) {
const callback = this.async();
const loaderSpan = this.currentTraceSpan.traceChild("next-babel-turbo-loader");
loaderSpan.traceAsyncFn(()=>nextBabelLoader.call(this, loaderSpan, inputSource, inputSourceMap)).then(([transformedSource, outputSourceMap])=>{
return callback == null ? void 0 : callback(null, transformedSource, outputSourceMap || inputSourceMap);
}, (err)=>{
callback == null ? void 0 : callback(err);
});
};
export default nextBabelLoaderOuter;
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../build/babel/loader/index.ts"],"names":["transform","nextBabelLoader","parentTrace","inputSource","inputSourceMap","filename","resourcePath","target","loaderOptions","traceChild","traceFn","getOptions","loaderSpanInner","code","transformedSource","map","outputSourceMap","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AEL,iBAAiB,EAAEE,eAAe,IAAIZ,cAAc,CAAC,CAAA;KAAA,EACxE,CAACqB,GAAG,GAAK;QACPN,QAAQ,QAAO,GAAfA,KAAAA,CAAe,GAAfA,QAAQ,CAAGM,GAAG,CAAC,CAAA;KAChB,CACF;CACJ;AAED,eAAeP,oBAAoB,CAAA"}

View File

@@ -0,0 +1,82 @@
/*
* Partially adapted from @babel/core (MIT license).
*/ import traverse from "next/dist/compiled/babel/traverse";
import generate from "next/dist/compiled/babel/generator";
import normalizeFile from "next/dist/compiled/babel/core-lib-normalize-file";
import normalizeOpts from "next/dist/compiled/babel/core-lib-normalize-opts";
import loadBlockHoistPlugin from "next/dist/compiled/babel/core-lib-block-hoist-plugin";
import PluginPass from "next/dist/compiled/babel/core-lib-plugin-pass";
import getConfig from "./get-config";
import { consumeIterator } from "./util";
function getTraversalParams(file, pluginPairs) {
const passPairs = [];
const passes = [];
const visitors = [];
for (const plugin of pluginPairs.concat(loadBlockHoistPlugin())){
const pass = new PluginPass(file, plugin.key, plugin.options);
passPairs.push([
plugin,
pass
]);
passes.push(pass);
visitors.push(plugin.visitor);
}
return {
passPairs,
passes,
visitors
};
}
function invokePluginPre(file, passPairs) {
for (const [{ pre }, pass] of passPairs){
if (pre) {
pre.call(pass, file);
}
}
}
function invokePluginPost(file, passPairs) {
for (const [{ post }, pass] of passPairs){
if (post) {
post.call(pass, file);
}
}
}
function transformAstPass(file, pluginPairs, parentSpan) {
const { passPairs , passes , visitors } = getTraversalParams(file, pluginPairs);
invokePluginPre(file, passPairs);
const visitor = traverse.visitors.merge(visitors, passes, // @ts-ignore - the exported types are incorrect here
file.opts.wrapPluginVisitorMethod);
parentSpan.traceChild("babel-turbo-traverse").traceFn(()=>traverse(file.ast, visitor, file.scope));
invokePluginPost(file, passPairs);
}
function transformAst(file, babelConfig, parentSpan) {
for (const pluginPairs of babelConfig.passes){
transformAstPass(file, pluginPairs, parentSpan);
}
}
export default function transform(source, inputSourceMap, loaderOptions, filename, target, parentSpan) {
const getConfigSpan = parentSpan.traceChild("babel-turbo-get-config");
const babelConfig = getConfig.call(this, {
source,
loaderOptions,
inputSourceMap,
target,
filename
});
getConfigSpan.stop();
const normalizeSpan = parentSpan.traceChild("babel-turbo-normalize-file");
const file = consumeIterator(normalizeFile(babelConfig.passes, normalizeOpts(babelConfig), source));
normalizeSpan.stop();
const transformSpan = parentSpan.traceChild("babel-turbo-transform");
transformAst(file, babelConfig, transformSpan);
transformSpan.stop();
const generateSpan = parentSpan.traceChild("babel-turbo-generate");
const { code , map } = generate(file.ast, file.opts.generatorOpts, file.code);
generateSpan.stop();
return {
code,
map
};
};
//# sourceMappingURL=transform.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../build/babel/loader/transform.ts"],"names":["traverse","generate","normalizeFile","normalizeOpts","loadBlockHoistPlugin","PluginPass","getConfig","consumeIterator","getTraversalParams","file","pluginPairs","passPairs","passes","visitors","plugin","concat","pass","key","options","push","visitor","invokePluginPre","pre","call","invokePluginPost","post","transformAstPass","parentSpan","merge","opts","wrapPluginVisitorMethod","traceChild","traceFn","ast","scope","transformAst","babelConfig","transform","source","inputSourceMap","loaderOptions","filename","target","getConfigSpan","stop","normalizeSpan","transformSpan","generateSpan","code","map","generatorOpts"],"mappings":"AAAA;;GAEG,CAEH,OAAOA,QAAQ,MAAM,mCAAmC,CAAA;AACxD,OAAOC,QAAQ,MAAM,oCAAoC,CAAA;AACzD,OAAOC,aAAa,MAAM,kDAAkD,CAAA;AAC5E,OAAOC,aAAa,MAAM,kDAAkD,CAAA;AAC5E,OAAOC,oBAAoB,MAAM,sDAAsD,CAAA;AACvF,OAAOC,UAAU,MAAM,+CAA+C,CAAA;AAEtE,OAAOC,SAAS,MAAM,cAAc,CAAA;AACpC,SAASC,eAAe,QAAQ,QAAQ,CAAA;AAIxC,SAASC,kBAAkB,CAACC,IAAS,EAAEC,WAAkB,EAAE;IACzD,MAAMC,SAAS,GAAG,EAAE;IACpB,MAAMC,MAAM,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AC9CA,aAAa,CAACF,IAAI,EAAE;IAEpB,MAAMG,YAAY,GAAGpB,UAAU,CAACI,UAAU,CAAC,sBAAsB,CAAC;IAClE,MAAM,EAAEiB,IAAI,CAAA,EAAEC,GAAG,CAAA,EAAE,GAAGhD,QAAQ,CAACQ,IAAI,CAACwB,GAAG,EAAExB,IAAI,CAACoB,IAAI,CAACqB,aAAa,EAAEzC,IAAI,CAACuC,IAAI,CAAC;IAC5ED,YAAY,CAACH,IAAI,EAAE;IAEnB,OAAO;QAAEI,IAAI;QAAEC,GAAG;KAAE,CAAA;CACrB,CAAA"}

View File

@@ -0,0 +1,20 @@
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { Span } from '../../../trace'
export interface NextJsLoaderContext extends webpack.LoaderContext<{}> {
currentTraceSpan: Span
target: string
}
export interface NextBabelLoaderOptions {
hasJsxRuntime: boolean
hasReactRefresh: boolean
isServer: boolean
development: boolean
pagesDir: string
sourceMaps?: any[]
overrides: any
caller: any
configFile: string | undefined
cwd: string
}

View File

@@ -0,0 +1,10 @@
export function consumeIterator(iter) {
while(true){
const { value , done } = iter.next();
if (done) {
return value;
}
}
}
//# sourceMappingURL=util.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../build/babel/loader/util.ts"],"names":["consumeIterator","iter","value","done","next"],"mappings":"AAAA,OAAO,SAASA,eAAe,CAACC,IAAmB,EAAE;IACnD,MAAO,IAAI,CAAE;QACX,MAAM,EAAEC,KAAK,CAAA,EAAEC,IAAI,CAAA,EAAE,GAAGF,IAAI,CAACG,IAAI,EAAE;QACnC,IAAID,IAAI,EAAE;YACR,OAAOD,KAAK,CAAA;SACb;KACF;CACF"}