Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STRWEB-99 bundle stripes-core's service-worker.js #125

Merged
merged 5 commits into from
Oct 31, 2023
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
1 change: 1 addition & 0 deletions webpack.config.cli.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const buildConfig = (stripesConfig) => {

const base = buildBaseConfig(allModulePaths);
const devConfig = Object.assign({}, base, cli, {
name: 'development',
devtool: 'inline-source-map',
mode: 'development',
cache: {
Expand Down
1 change: 0 additions & 1 deletion webpack.config.cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ module.exports = {
filename: 'bundle.[name][contenthash].js',
chunkFilename: 'chunk.[name][chunkhash].js',
publicPath: '/',
clean: true
},
};
31 changes: 31 additions & 0 deletions webpack.config.service.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const path = require('path');

const config = {
name: 'service-worker',
mode: 'development',
target: 'web',
entry: {
'service-worker': {
import: '@folio/stripes-core/src/service-worker.js',
filename: 'service-worker.js',
}
},
output: {
path: path.join(__dirname, 'dist'),
filename: 'service-worker.js',
publicPath: '/',
},
module: {
rules: [
{
test: /\.js$/,
loader: 'esbuild-loader',
options: {
target: 'es2015'
}
},
]
}
};

module.exports = config;
18 changes: 15 additions & 3 deletions webpack/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const applyWebpackOverrides = require('./apply-webpack-overrides');
const logger = require('./logger')();
const buildConfig = require('../webpack.config.cli.prod');
const sharedStylesConfig = require('../webpack.config.cli.shared.styles');

const serviceWorkerConfig = require('../webpack.config.service.worker');
const platformModulePath = path.join(path.resolve(), 'node_modules');

module.exports = function build(stripesConfig, options) {
Expand Down Expand Up @@ -73,9 +73,21 @@ module.exports = function build(stripesConfig, options) {
config = applyWebpackOverrides(options.webpackOverrides, config);

logger.log('assign final webpack config', config);
const compiler = webpack(config);
compiler.run((err, stats) => {

// repoint the service-worker's output.path value so it emits
// into options.outputPath
if (options.outputPath) {
serviceWorkerConfig.output.path = path.resolve(options.outputPath);
}
// override the default mode; given we are building, assume production
serviceWorkerConfig.mode = 'production';

webpack([config,serviceWorkerConfig], (err, stats) => {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
reject(err);
} else {
resolve(stats);
Expand Down
26 changes: 17 additions & 9 deletions webpack/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const applyWebpackOverrides = require('./apply-webpack-overrides');
const logger = require('./logger')();
const buildConfig = require('../webpack.config.cli.dev');
const sharedStylesConfig = require('../webpack.config.cli.shared.styles');
const serviceWorkerConfig = require('../webpack.config.service.worker');

const cwd = path.resolve();
const platformModulePath = path.join(cwd, 'node_modules');
Expand Down Expand Up @@ -39,8 +40,10 @@ module.exports = function serve(stripesConfig, options) {
config = applyWebpackOverrides(options.webpackOverrides, config);

logger.log('assign final webpack config', config);
const compiler = webpack(config);
compiler.hooks.done.tap('StripesCoreServe', stats => resolve(stats));
const compiler = webpack([serviceWorkerConfig, config]);
const [swCompiler, stripesCompiler] = compiler.compilers;

stripesCompiler.hooks.done.tap('StripesCoreServe', stats => resolve(stats));

const port = options.port || process.env.STRIPES_PORT || 3000;
const host = options.host || process.env.STRIPES_HOST || 'localhost';
Expand All @@ -49,23 +52,28 @@ module.exports = function serve(stripesConfig, options) {

app.use(staticFileMiddleware);

// To handle rewrites without the dot rule, we should include the static middleware twice
// https://github.com/bripkens/connect-history-api-fallback/blob/master/examples/static-files-and-index-rewrite
app.use(staticFileMiddleware);

app.use(webpackDevMiddleware(swCompiler, {
publicPath: serviceWorkerConfig.output.publicPath,
}));

// Process index rewrite before webpack-dev-middleware
// to respond with webpack's dist copy of index.html
app.use(connectHistoryApiFallback({
disableDotRule: true,
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
}));

// To handle rewrites without the dot rule, we should include the static middleware twice
// https://github.com/bripkens/connect-history-api-fallback/blob/master/examples/static-files-and-index-rewrite
app.use(staticFileMiddleware);

app.use(webpackDevMiddleware(compiler, {
stats: 'errors-only',
app.use(webpackDevMiddleware(stripesCompiler, {
publicPath: config.output.publicPath,
}));

app.use(webpackHotMiddleware(compiler));


app.use(webpackHotMiddleware(stripesCompiler));

app.listen(port, host, (err) => {
if (err) {
Expand Down
Loading