Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twenty-months-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-cli': patch
---

Fixes SW flag in dev
87 changes: 45 additions & 42 deletions packages/cli/lib/lib/webpack/webpack-client-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,47 @@ async function clientConfig(env) {
polyfills: resolve(__dirname, './polyfills'),
};

let swInjectManifest = [];
if (env.sw) {
let swPath = join(__dirname, '..', '..', '..', 'sw', 'sw.js');
const userSwPath = join(src, 'sw.js');
if (existsSync(userSwPath)) {
swPath = userSwPath;
} else {
warn(`Could not find sw.js in ${src}. Using the default service worker.`);
}
swInjectManifest = env.esm
? [
new InjectManifest({
swSrc: swPath,
swDest: 'sw-esm.js',
include: [
/200\.html$/,
/\.esm.js$/,
/\.css$/,
/\.(png|jpg|svg|gif|webp)$/,
],
webpackCompilationPlugins: [
new webpack.DefinePlugin({
'process.env.ESM': true,
}),
],
}),
]
: [
new InjectManifest({
swSrc: join(src, 'sw.js'),
include: [
/200\.html$/,
/\.js$/,
/\.css$/,
/\.(png|jpg|svg|gif|webp)$/,
],
exclude: [/\.esm\.js$/],
}),
];
}

return {
entry: entry,
output: {
Expand Down Expand Up @@ -83,6 +124,9 @@ async function clientConfig(env) {
},

plugins: [
new webpack.DefinePlugin({
'process.env.ES_BUILD': false,
}),
new PushManifestPlugin(env),
...(await renderHTMLPlugin(env)),
...getBabelEsmPlugin(env),
Expand All @@ -103,6 +147,7 @@ async function clientConfig(env) {
},
].filter(Boolean)
),
...swInjectManifest,
],
};
}
Expand Down Expand Up @@ -156,17 +201,6 @@ function getBabelEsmPlugin(config) {

function isProd(config) {
let limit = 200 * 1000; // 200kb
const { src, sw } = config;
let swPath;
if (sw) {
swPath = join(__dirname, '..', '..', '..', 'sw', 'sw.js');
const userSwPath = join(src, 'sw.js');
if (existsSync(userSwPath)) {
swPath = userSwPath;
} else {
warn(`Could not find sw.js in ${src}. Using the default service worker.`);
}
}
const prodConfig = {
performance: Object.assign(
{
Expand All @@ -180,7 +214,6 @@ function isProd(config) {
plugins: [
new webpack.DefinePlugin({
'process.env.ADD_SW': config.sw,
'process.env.ES_BUILD': false,
'process.env.ESM': config.esm,
'process.env.PRERENDER': config.prerender,
}),
Expand Down Expand Up @@ -222,36 +255,6 @@ function isProd(config) {
},
};

if (config.esm && config.sw) {
prodConfig.plugins.push(
new InjectManifest({
swSrc: swPath,
swDest: 'sw-esm.js',
include: [
/200\.html$/,
/\.esm.js$/,
/\.css$/,
/\.(png|jpg|svg|gif|webp)$/,
],
webpackCompilationPlugins: [
new webpack.DefinePlugin({
'process.env.ESM': true,
}),
],
})
);
}

if (config.sw) {
prodConfig.plugins.push(
new InjectManifest({
swSrc: swPath,
include: [/200\.html$/, /\.js$/, /\.css$/, /\.(png|jpg|svg|gif|webp)$/],
exclude: [/\.esm\.js$/],
})
);
}

if (config['inline-css']) {
prodConfig.plugins.push(
new CrittersPlugin({
Expand Down