@navita/next-plugin 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.cjs +59 -0
  2. package/index.d.ts +1 -2
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -1,10 +1,33 @@
1
1
  'use strict';
2
2
 
3
+ var fs = require('node:fs');
4
+ var path = require('node:path');
3
5
  var webpackPlugin = require('@navita/webpack-plugin');
4
6
  var NextMiniCssExtractPluginDefault = require('next/dist/build/webpack/plugins/mini-css-extract-plugin');
5
7
  var findPagesDir = require('next/dist/lib/find-pages-dir');
6
8
  var optimizeCSSOutput = require('./optimizeCSSOutput.cjs');
7
9
 
10
+ function _interopNamespaceDefault(e) {
11
+ var n = Object.create(null);
12
+ if (e) {
13
+ Object.keys(e).forEach(function (k) {
14
+ if (k !== 'default') {
15
+ var d = Object.getOwnPropertyDescriptor(e, k);
16
+ Object.defineProperty(n, k, d.get ? d : {
17
+ enumerable: true,
18
+ get: function () { return e[k]; }
19
+ });
20
+ }
21
+ });
22
+ }
23
+ n.default = e;
24
+ return Object.freeze(n);
25
+ }
26
+
27
+ var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
28
+
29
+ let renderer;
30
+ let lastCache;
8
31
  const MiniCssExtractPlugin = NextMiniCssExtractPluginDefault['default'];
9
32
  const createNavitaStylePlugin = (navitaConfig = {})=>(nextConfig)=>Object.assign({}, nextConfig, {
10
33
  webpack (config, options) {
@@ -65,7 +88,43 @@ const createNavitaStylePlugin = (navitaConfig = {})=>(nextConfig)=>Object.assign
65
88
  }
66
89
  };
67
90
  }
91
+ // Next.js creates at least three webpack instances. We can't rely on the webpack cache.
92
+ const { cache , mode } = config;
93
+ const cacheDirectory = typeof cache !== "boolean" && cache.type === "filesystem" ? path.resolve(cache.cacheDirectory, `navita-${mode}`) : undefined;
94
+ const cacheDestination = path.resolve(cacheDirectory, 'data.txt');
95
+ const onRenderInitialized = async (createdRenderer)=>{
96
+ renderer = createdRenderer;
97
+ try {
98
+ // Ensure the cache directory exists:
99
+ await fs__namespace.promises.mkdir(cacheDirectory, {
100
+ recursive: true
101
+ });
102
+ const content = await fs__namespace.promises.readFile(cacheDestination, 'utf-8');
103
+ await renderer.engine.deserialize(content);
104
+ lastCache = renderer.engine.serialize();
105
+ } catch {
106
+ // This will happen if the user doesn't have write access to the cache directory.
107
+ // But the same should happen with the webpack cache.
108
+ }
109
+ };
110
+ config.plugins?.push({
111
+ apply (compiler) {
112
+ compiler.hooks.afterEmit.tapPromise(`${webpackPlugin.NavitaPlugin.pluginName}-nextjs-custom-cache`, async ()=>{
113
+ if (!renderer) {
114
+ return;
115
+ }
116
+ const newCache = renderer.engine.serialize();
117
+ if (newCache === lastCache) {
118
+ return;
119
+ }
120
+ lastCache = newCache;
121
+ await fs__namespace.promises.writeFile(cacheDestination, newCache);
122
+ });
123
+ }
124
+ });
68
125
  config.plugins?.push(new webpackPlugin.NavitaPlugin({
126
+ useWebpackCache: false,
127
+ onRenderInitialized,
69
128
  outputCss,
70
129
  ...navitaConfig,
71
130
  optimizeCSSOutput: optimizeCSSOutput.optimizeCSSOutput
package/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import { Options } from '@navita/webpack-plugin';
2
2
  import { NextConfig } from 'next';
3
3
 
4
- type WebpackOptions = Options;
5
- interface Config extends WebpackOptions {
4
+ interface Config extends Omit<Options, 'useWebpackCache' | 'onRenderInitialized'> {
6
5
  singleCssFile?: boolean;
7
6
  }
8
7
  declare const createNavitaStylePlugin: (navitaConfig?: Config) => (nextConfig: NextConfig) => NextConfig;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navita/next-plugin",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Next.js integration for Navita",
5
5
  "keywords": [
6
6
  "next",
@@ -25,7 +25,7 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "browserslist": "^4.21.5",
28
- "@navita/webpack-plugin": "0.2.0"
28
+ "@navita/webpack-plugin": "0.3.0"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "next": ">=12 || >=13",