@navita/next-plugin 0.2.0 → 0.4.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.
- package/index.cjs +62 -0
- package/index.d.ts +2 -2
- 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,9 +88,48 @@ 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
|
+
// If the user has provided their own onRenderInitialized function,
|
|
110
|
+
// we'll do it after the cache is loaded.
|
|
111
|
+
return navitaConfig.onRenderInitialized?.(renderer);
|
|
112
|
+
};
|
|
113
|
+
config.plugins?.push({
|
|
114
|
+
apply (compiler) {
|
|
115
|
+
compiler.hooks.afterEmit.tapPromise(`${webpackPlugin.NavitaPlugin.pluginName}-nextjs-custom-cache`, async ()=>{
|
|
116
|
+
if (!renderer) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const newCache = renderer.engine.serialize();
|
|
120
|
+
if (newCache === lastCache) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
lastCache = newCache;
|
|
124
|
+
await fs__namespace.promises.writeFile(cacheDestination, newCache);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
});
|
|
68
128
|
config.plugins?.push(new webpackPlugin.NavitaPlugin({
|
|
129
|
+
useWebpackCache: false,
|
|
69
130
|
outputCss,
|
|
70
131
|
...navitaConfig,
|
|
132
|
+
onRenderInitialized,
|
|
71
133
|
optimizeCSSOutput: optimizeCSSOutput.optimizeCSSOutput
|
|
72
134
|
}));
|
|
73
135
|
if (typeof nextConfig.webpack === "function") {
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Options } from '@navita/webpack-plugin';
|
|
2
|
+
export { Renderer } from '@navita/webpack-plugin';
|
|
2
3
|
import { NextConfig } from 'next';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
interface Config extends WebpackOptions {
|
|
5
|
+
interface Config extends Omit<Options, 'useWebpackCache'> {
|
|
6
6
|
singleCssFile?: boolean;
|
|
7
7
|
}
|
|
8
8
|
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.
|
|
3
|
+
"version": "0.4.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.
|
|
28
|
+
"@navita/webpack-plugin": "0.4.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"next": ">=12 || >=13",
|