Skip to content

Commit

Permalink
begin rewriting export functionality to work with next 7
Browse files Browse the repository at this point in the history
refs #23 #45
  • Loading branch information
hanford committed Sep 17, 2018
1 parent a4aa1fc commit 013af97
Show file tree
Hide file tree
Showing 7 changed files with 2,843 additions and 3,564 deletions.
39 changes: 15 additions & 24 deletions export.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
const { readFile, writeFile } = require('fs-extra');
const Precache = require('./next-files');
const { generateSW, injectManifest } = require('workbox-build');
const { join, resolve } = require('path');
const parseArgs = require('minimist');

const dev = process.env.NODE_ENV !== 'production';
// const parseArgs = require('minimist');
const Precache = require('./next-files');

module.exports = Export;

async function Export(nextConfig) {
async function Export(nextConfig, defaultPathMap, exportArgs) {
const {
distDir = '.next',
exportPathMap,
generateSw = true,
workboxOpts = {
runtimeCaching: [{ urlPattern: /^https?.*/, handler: 'networkFirst' }]
}
} = nextConfig;

const { dev, dir, outDir, distDir, buildId } = exportArgs

if (dev || typeof exportPathMap !== 'function') {
console.log(defaultPathMap, exportArgs)

if (dev) {
return {};
}

// Logic for working out dir and outdir copied from `next-export`:
// https://github.com/zeit/next.js/blob/15dde33794622919d20709da97fa412a01831807/bin/next-export
const argv = parseArgs(process.argv.slice(2), {
alias: { o: 'outdir' },
default: { o: null }
});
const dir = argv._[0] || '.';
const outDir = argv.outdir ? resolve(argv.outdir) : resolve(dir, 'out');

const nextDir = join(process.cwd(), dir, distDir);
const buildIdPath = join(nextDir, 'BUILD_ID');
const buildId = await readFile(buildIdPath, 'utf8');

const { precaches } = await Precache({ buildId, nextDir });
const { precaches } = await Precache({ buildId, distDir });
console.log({precaches})

// temporarily write sw to .next, before rewriting it to distDir
const swDest = join(outDir, 'service-worker.js');
const swOut = join(distDir, 'service-worker.js')

if (generateSw) {
// globDirectory is intentionally left blank as it's required by workbox
Expand All @@ -53,12 +44,12 @@ async function Export(nextConfig) {
});
}

const serviceWorkerContent = await readFile(swDest, 'utf8');
const serviceWorkerContent = await readFile(swOut, 'utf8');
const newServiceWorkerContent = `self.__precacheManifest = ${JSON.stringify(
precaches
)};\n${serviceWorkerContent}`;

writeFile(swDest, newServiceWorkerContent);
await writeFile(swOut, newServiceWorkerContent);

return nextConfig.exportPathMap();
return typeof nextConfig.exportPathMap === 'function' && nextConfig.exportPathMap(defaultPathMap, exportArgs);
}
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const Export = require('./export');

module.exports = (nextConfig = {}) => ({
...nextConfig,
async exportPathMap() {
return await Export(nextConfig);
async exportPathMap(...args) {
return await Export(nextConfig, ...args);
},
webpack(config, options) {
if (!options.defaultLoaders) {
Expand Down
4 changes: 2 additions & 2 deletions next-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = async function getNextFiles(options) {
};

function precacheFiles(options, basePath, globPattern) {
const { buildId, assetPrefix, nextDir } = options;
const cwd = join(nextDir, basePath);
const { buildId, assetPrefix, distDir } = options;
const cwd = join(distDir, basePath);

return new Promise((resolve, reject) => {
glob(globPattern, { cwd }, (err, files = []) => {
Expand Down
Loading

0 comments on commit 013af97

Please sign in to comment.