Skip to content

Commit

Permalink
feat(transpile): add ignoreModules option
Browse files Browse the repository at this point in the history
  • Loading branch information
rodymolenaar committed Jul 8, 2021
1 parent 11ef3ba commit 346a432
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lightbase/next-preset",
"version": "1.0.1",
"version": "1.1.0",
"main": "dist/index.js",
"repository": "https://github.com/lightbasenl/next-preset",
"author": "Rody Molenaar <[email protected]>",
Expand Down
31 changes: 18 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ export function withPreset(nextConfig: ExportedNextConfig): NextConfigFunction {
? nextConfig(phase, defaults)
: nextConfig;

let config = baseConfig ?? {};
let newConfig = baseConfig ?? {};

// Set misc. defaults
config.poweredByHeader = config.poweredByHeader ?? false;
config.pageExtensions = config.pageExtensions ?? ["api.ts", "page.tsx"];
newConfig.poweredByHeader = newConfig.poweredByHeader ?? false;
newConfig.pageExtensions = newConfig.pageExtensions ?? [
"api.ts",
"page.tsx",
];

// Set default headers
config.headers = async () => {
newConfig.headers = async () => {
return [
{
source: "/:path*{/}?",
Expand Down Expand Up @@ -54,28 +57,30 @@ export function withPreset(nextConfig: ExportedNextConfig): NextConfigFunction {
};

if (preset?.transpileModules) {
config = withTM(preset.transpileModules)(config);
newConfig = withTM(preset.transpileModules)(newConfig);
}

config.webpack = extendWebpackConfig((config, options) => {
newConfig.webpack = extendWebpackConfig((config, options) => {
if (!options.dev) {
// Next.js doesn't let you change this is dev even if you want to - see
// https://github.com/vercel/next.js/blob/master/errors/improper-devtool.md
config.devtool = "source-map";

config.plugins?.push(new BrowserCompatibilityWebpackPlugin());
config.plugins?.push(
new BrowserCompatibilityWebpackPlugin(preset?.ignoreModules)
);
}

return config;
}, config.webpack);
}, newConfig.webpack);

if (preset?.sentry?.enabled) {
config = withSentryConfig(config, preset?.sentry?.webpackPluginOptions)(
phase,
defaults
);
newConfig = withSentryConfig(
newConfig,
preset?.sentry?.webpackPluginOptions
)(phase, defaults);
}

return config;
return newConfig;
};
}
86 changes: 46 additions & 40 deletions src/lib/checkForOffendingOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { promises as fs } from "fs";
import { SourceMapConsumer } from "source-map";
import _ from "lodash";

export default async function checkForOffendingOutput() {
export default async function checkForOffendingOutput(
ignoreModules: string[] = []
) {
const ecmaVersion = 5;
const paths = ["./.next/static/**/*.js"];
const useEsModules = false;
Expand Down Expand Up @@ -94,51 +96,21 @@ export default async function checkForOffendingOutput() {
})
);

console.log();
const filteredOffenders = _.uniq(
offenders.map((offender) => formatEntry(offender))
).filter((offender) => !ignoreModules.includes(offender));

if (filteredOffenders.length === 0) {
return;
}

console.log("[PRESET]");
console.log(
"You might want to add the following entries to `preset.transpileModules` in `next.config.js`:"
);
console.log();

for (const offender of _.uniq(
offenders.map((offender) => {
return offender
.replace("webpack://_N_E/", "")
.split("/")
.reduce<string[]>((parts, part, index) => {
if (parts.length === 0 && part === "node_modules" && index === 0) {
return parts;
}

if (parts[parts.length - 1] === "node_modules") {
parts.push(part);
return parts;
}

if (
parts[parts.length - 1] &&
parts[parts.length - 1].startsWith("@")
) {
parts.push(part);
return parts;
}

if (parts.length === 0) {
parts.push(part);
return parts;
}

if (part === "node_modules") {
parts.push(part);
return parts;
}

return parts;
}, [])
.join("/");
})
)) {
for (const offender of filteredOffenders) {
console.log(`- ${offender}`);
}

Expand All @@ -150,3 +122,37 @@ export default async function checkForOffendingOutput() {

process.exit(1);
}

function formatEntry(entry: string) {
return entry
.replace("webpack://_N_E/", "")
.split("/")
.reduce<string[]>((parts, part, index) => {
if (parts.length === 0 && part === "node_modules" && index === 0) {
return parts;
}

if (parts[parts.length - 1] === "node_modules") {
parts.push(part);
return parts;
}

if (parts[parts.length - 1] && parts[parts.length - 1].startsWith("@")) {
parts.push(part);
return parts;
}

if (parts.length === 0) {
parts.push(part);
return parts;
}

if (part === "node_modules") {
parts.push(part);
return parts;
}

return parts;
}, [])
.join("/");
}
6 changes: 5 additions & 1 deletion src/lib/plugins/BrowserCompatibilityWebpackPlugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import checkForOffendingOutput from "../checkForOffendingOutput";

class BrowserCompatibilityWebpackPlugin {
constructor(ignoreModules = []) {
this.ignoreModules = ignoreModules;
}

apply(compiler) {
/**
* Determines whether plugin should be applied not more than once during whole webpack run.
Expand All @@ -17,7 +21,7 @@ class BrowserCompatibilityWebpackPlugin {
compiler.hooks.afterEmit.tapAsync(
"next-preset",
async (compilation, callback) => {
await checkForOffendingOutput();
await checkForOffendingOutput(this.ignoreModules);

callback();
}
Expand Down
1 change: 1 addition & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type NextConfigObject = {
webpackPluginOptions?: unknown;
};
transpileModules?: string[];
ignoreModules?: string[];
};
webpack?: WebpackConfigFunction;
} & {
Expand Down

0 comments on commit 346a432

Please sign in to comment.