forked from shadowwalker/next-pwa
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(next-pwa, next-sw): use Next's swc bindings instead of `@swc…
…/core` (#38) [bump]
- Loading branch information
Showing
18 changed files
with
370 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
"@ducanh2912/next-pwa": major | ||
"@ducanh2912/next-sw": minor | ||
--- | ||
|
||
BREAKING CHANGE(next-pwa, next-sw): use next's swc bindings instead of swc/core | ||
|
||
What: From now on we will try to resolve `next/dist/build/swc` in `swc-loader` (needed to use `cacheOnFrontEndNav`, custom workers and offline fallbacks). If it can't be resolved, we will fallback to `@swc/core` (needed to be installed manually). | ||
|
||
Why: This is to save disk space (we don't need two `@swc/core`) and avoid exceeding Vercel's serverless size limit. | ||
|
||
Why use Next's `next/dist/build/swc`: it seems that `@next/mdx` is also doing the same for their `mdx` Rust compiler. | ||
|
||
How to upgrade: Usually you don't need to do anything. But if you see this line when you build your Next app: `Using @swc/core to compile next-pwa's features. Please install it if you haven't.`, please do as instructed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
"@ducanh2912/next-pwa": major | ||
--- | ||
|
||
BREAKING CHANGE(requirements): bump minimum Next.js version to v11 | ||
|
||
What: `next-pwa`'s minimum supported Next.js version is now 11.0.0. | ||
|
||
Why: I noticed that `workbox-webpack-plugin` no longer works with Next.js 9 and 10, so this bumps the minimum supported Next.js version to v11. | ||
|
||
How to upgrade: Bump `next` to at least 11.0.0. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
// @ts-check | ||
/** @type {import("eslint").Linter.BaseConfig} */ | ||
module.exports = { | ||
extends: ["plugin:jsdoc/recommended"], | ||
plugins: ["jsdoc"], | ||
rules: { | ||
"jsdoc/require-param-type": "off", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { swcLoader as default } from "utils"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { swcLoader as default } from "utils"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { createRequire } from "node:module"; | ||
|
||
import type { Compiler } from "@swc/core"; | ||
|
||
import * as logger from "./logger.js"; | ||
|
||
const require = createRequire(import.meta.url); | ||
|
||
/** | ||
* swc-loader | ||
* @param source Source code | ||
* @param inputSourceMap Source map | ||
*/ | ||
export function swcLoader(this: any, source: string, inputSourceMap: string) { | ||
// Make the loader async | ||
const callback = this.async(); | ||
const filename = this.resourcePath; | ||
|
||
let loaderOptions = | ||
(typeof this.getOptions === "function" | ||
? this.getOptions() | ||
: require("loader-utils").getOptions(this)) || {}; | ||
|
||
// Standardize on 'sourceMaps' as the key passed through to Webpack, so that | ||
// users may safely use either one alongside our default use of | ||
// 'this.sourceMap' below without getting error about conflicting aliases. | ||
if ( | ||
Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMap") && | ||
!Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMaps") | ||
) { | ||
loaderOptions = Object.assign({}, loaderOptions, { | ||
sourceMaps: loaderOptions.sourceMap, | ||
}); | ||
delete loaderOptions.sourceMap; | ||
} | ||
|
||
if (inputSourceMap && typeof inputSourceMap === "object") { | ||
inputSourceMap = JSON.stringify(inputSourceMap); | ||
} | ||
|
||
const programmaticOptions = Object.assign({}, loaderOptions, { | ||
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, | ||
}); | ||
if (!programmaticOptions.inputSourceMap) { | ||
delete programmaticOptions.inputSourceMap; | ||
} | ||
const sync = programmaticOptions.sync; | ||
const parseMap = programmaticOptions.parseMap; | ||
|
||
let swc: Compiler; | ||
try { | ||
// avoid installing @swc/core | ||
swc = require("next/dist/build/swc"); | ||
} catch { | ||
logger.info( | ||
"Using @swc/core to compile next-pwa's features. Please install it if you haven't." | ||
); | ||
swc = require("@swc/core"); | ||
} | ||
|
||
// Remove loader related options | ||
delete programmaticOptions.sync; | ||
delete programmaticOptions.parseMap; | ||
delete programmaticOptions.customize; | ||
delete programmaticOptions.cacheDirectory; | ||
delete programmaticOptions.cacheIdentifier; | ||
delete programmaticOptions.cacheCompression; | ||
delete programmaticOptions.metadataSubscribers; | ||
|
||
// auto detect development mode | ||
if ( | ||
this.mode && | ||
programmaticOptions.jsc && | ||
programmaticOptions.jsc.transform && | ||
programmaticOptions.jsc.transform.react && | ||
!Object.prototype.hasOwnProperty.call( | ||
programmaticOptions.jsc.transform.react, | ||
"development" | ||
) | ||
) { | ||
programmaticOptions.jsc.transform.react.development = | ||
this.mode === "development"; | ||
} | ||
|
||
if (programmaticOptions.sourceMaps === "inline") { | ||
// Babel has this weird behavior where if you set "inline", we | ||
// inline the sourcemap, and set 'result.map = null'. This results | ||
// in bad behavior from Babel since the maps get put into the code, | ||
// which Webpack does not expect, and because the map we return to | ||
// Webpack is null, which is also bad. To avoid that, we override the | ||
// behavior here so "inline" just behaves like 'true'. | ||
programmaticOptions.sourceMaps = true; | ||
} | ||
|
||
try { | ||
if (sync) { | ||
const output = swc.transformSync(source, programmaticOptions); | ||
callback( | ||
null, | ||
output.code, | ||
parseMap && output.map ? JSON.parse(output.map) : output.map | ||
); | ||
} else { | ||
swc.transform(source, programmaticOptions).then( | ||
(output) => { | ||
callback( | ||
null, | ||
output.code, | ||
parseMap && output.map ? JSON.parse(output.map) : output.map | ||
); | ||
}, | ||
(err) => { | ||
callback(err); | ||
} | ||
); | ||
} | ||
} catch (e) { | ||
callback(e); | ||
} | ||
} |
Oops, something went wrong.