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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"eslint": "^3.19.0",
"eslint-plugin-react": "^7.0.1",
"html-looks-like": "^1.0.2",
"html-webpack-exclude-assets-plugin": "0.0.5",
"lodash": "^4.17.4",
"tape": "^4.6.3",
"uuid": "^3.0.1"
Expand Down
10 changes: 1 addition & 9 deletions src/lib/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,5 @@ if (typeof app==='function') {

if (module.hot) module.hot.accept('preact-cli-entrypoint', init);

if (typeof fetch==='function') {
init();
}
else {
require.ensure(['preact-cli-polyfills'], () => {
require('preact-cli-polyfills');
init();
}, 'polyfills');
}
init();
}
15 changes: 10 additions & 5 deletions src/lib/webpack-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ExtractTextPlugin from 'extract-text-webpack-plugin';
import autoprefixer from 'autoprefixer';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ScriptExtHtmlWebpackPlugin from 'script-ext-html-webpack-plugin';
import HtmlWebpackExcludeAssetsPlugin from 'html-webpack-exclude-assets-plugin';
import ProgressBarPlugin from 'progress-bar-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import ReplacePlugin from 'webpack-plugin-replace';
Expand Down Expand Up @@ -68,11 +69,14 @@ export default env => {

return createConfig.vanilla([
setContext(src('.')),
entryPoint(resolve(__dirname, './entry')),
entryPoint({
'bundle': resolve(__dirname, './entry'),
'polyfills': resolve(__dirname, './polyfills'),
}),
setOutput({
path: resolve(cwd, env.dest || 'build'),
publicPath: '/',
filename: 'bundle.js',
filename: '[name].js',
chunkFilename: '[name].chunk.[chunkhash:5].js'
}),

Expand All @@ -85,7 +89,6 @@ export default env => {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json', '.less', '.scss', '.sass', '.css'],
alias: {
'preact-cli-entrypoint': src('index.js'),
'preact-cli-polyfills': resolve(__dirname, 'polyfills.js'),
style: src('style'),
preact$: resolveDep(isProd ? 'preact/dist/preact.min.js' : 'preact', env.cwd),
// preact-compat aliases for supporting React dependencies:
Expand Down Expand Up @@ -441,6 +444,7 @@ const production = config => addPlugins([
minify: true,
stripPrefix: config.cwd,
staticFileGlobsIgnorePatterns: [
/polyfills\.js$/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you ignore this because all serviceWorker-compatible browsers don't need a fetch or Promise polyfill?

(^ I'm not certain that that's 100% true)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand serviceWorker implementation requires fetch implementation which in turn requires Promise implementation. Based on FetchEvent. I could be wrong tough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. I believe that's the case too 👍 You upgraded my 90% certainty to 95% 😉

/\.map$/,
/push-manifest\.json$/
]
Expand All @@ -465,14 +469,15 @@ const htmlPlugin = config => addPlugins([
compile: true,
preload: config.preload===true,
title: config.title || config.manifest.name || config.manifest.short_name || (config.pkg.name || '').replace(/^@[a-z]\//, '') || 'Preact App',
excludeAssets: [/(bundle|polyfills)(\..*)?\.js$/],
config,
ssr(params) {
return config.prerender ? prerender(config, params) : '';
}
}),

new HtmlWebpackExcludeAssetsPlugin(),
new ScriptExtHtmlWebpackPlugin({
// inline: 'bundle.js',
defaultAttribute: 'async'
defaultAttribute: 'defer'
})
]);
3 changes: 3 additions & 0 deletions src/resources/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<meta name="theme-color" content="<%= htmlWebpackPlugin.options.manifest.theme_color %>">
<% } %>
<% for (var chunk of webpack.chunks) { %>
<% if (chunk.names.length === 1 && chunk.names[0] === 'polyfills') continue; %>
<% for (var file of chunk.files) { %>
<% if (htmlWebpackPlugin.options.preload && file.match(/\.(js|css)$/)) { %>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW htmlWebpackPlugin.options.preload comes from argv.preload which is a hidden switch that can't be used. Gonna check whole codebase for hidden switches sometime soon.

<link rel="preload" href="<%= htmlWebpackPlugin.files.publicPath + file %>" as="<%= file.match(/\.css$/)?'style':'script' %>">
Expand All @@ -24,5 +25,7 @@
<%= htmlWebpackPlugin.options.ssr({
url: '/'
}) %>
<script defer src="<%= htmlWebpackPlugin.files.chunks['bundle'].entry %>"></script>
<script>window.fetch||document.write('<script src="<%= htmlWebpackPlugin.files.chunks["polyfills"].entry %>"><\/script>')</script>
</body>
</html>
4 changes: 2 additions & 2 deletions tests/build.snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const smallBuildCommons = {
'favicon.ico': { size: 15086 },
'icon.png': { size: 51484 }
},
'polyfills.chunk.*.js': { size: 4068 },
'polyfills.js': { size: 4068 },
'favicon.ico': { size: 15086 },
'sw.js': { size: 3378 },
'manifest.json': { size: 298 },
Expand All @@ -36,7 +36,7 @@ const fullBuildCommons = {
'mstile-150x150.png': { size: 9050 }
}
},
'polyfills.chunk.*.js': { size: 4066 },
'polyfills.js': { size: 4066 },
'push-manifest.json': { size: 303 },
'favicon.ico': { size: 15086 },
'manifest.json': { size: 426 },
Expand Down
6 changes: 4 additions & 2 deletions tests/serve.snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const homePageHTML = `
<p>This is the Home component.</p>
</div>
</div>
<script src="/bundle.js" async=""></script>
<script src="/bundle.js" defer="defer"></script>
{{ ... }}
</body>
`;

Expand All @@ -36,6 +37,7 @@ export const profilePageHtml = `
<p><button>Click Me</button> Clicked 10 times.</p>
</div>
</div>
<script src="/bundle.js" async=""></script>
<script src="/bundle.js" defer="defer"></script>
{{ ... }}
</body>
`;