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

Esm builds #583

Merged
merged 12 commits into from Jul 9, 2018
1 change: 1 addition & 0 deletions packages/cli/lib/index.js
Expand Up @@ -31,6 +31,7 @@ prog
.option('--analyze', 'Launch interactive Analyzer to inspect production bundle(s)')
.option('--prerenderUrls', 'Path to pre-rendered routes config', 'prerender-urls.json')
.option('-c, --config', 'Path to custom CLI config', 'preact.config.js')
.option('--esm', '[EXPERIMENTAL] Builds ES-2015 bundles for your code.', false)
.action(commands.build);

prog
Expand Down
24 changes: 8 additions & 16 deletions packages/cli/lib/lib/babel-config.js
Expand Up @@ -4,34 +4,26 @@ module.exports = function (env, options={}) {
return {
babelrc: false,
presets: [
[require.resolve('babel-preset-env'), {
[require.resolve('@babel/preset-env'), {
loose: true,
uglify: true,
modules: options.modules || false,
targets: {
browsers: options.browsers
},
exclude: [
'transform-regenerator',
'transform-es2015-typeof-symbol'
]
}]
],
plugins: [
require.resolve('babel-plugin-syntax-dynamic-import'),
require.resolve('babel-plugin-transform-object-assign'),
require.resolve('babel-plugin-transform-decorators-legacy'),
require.resolve('babel-plugin-transform-class-properties'),
require.resolve('babel-plugin-transform-export-extensions'),
require.resolve('babel-plugin-transform-object-rest-spread'),
require.resolve('babel-plugin-transform-react-constant-elements'),
require.resolve('@babel/plugin-syntax-dynamic-import'),
require.resolve('@babel/plugin-transform-object-assign'),
[require.resolve('@babel/plugin-proposal-decorators'), {legacy: true}],
require.resolve('@babel/plugin-proposal-class-properties'),
require.resolve('@babel/plugin-proposal-object-rest-spread'),
require.resolve('@babel/plugin-transform-react-constant-elements'),
isProd && require.resolve('babel-plugin-transform-react-remove-prop-types'),
[require.resolve('babel-plugin-transform-react-jsx'), { pragma: 'h' }],
// [require.resolve('babel-plugin-jsx-pragmatic'), {
// module: 'preact',
// export: 'h',
// import: 'h'
// }]
[require.resolve('@babel/plugin-transform-react-jsx'), { pragma: 'h' }],
].filter(Boolean)
};
};
4 changes: 2 additions & 2 deletions packages/cli/lib/lib/webpack/transform-config.js
Expand Up @@ -15,8 +15,8 @@ module.exports = async function (env, config, ssr=false) {
throw new Error(`preact-cli config could not be loaded!\nFile ${env.config} not found.`);
}

require('babel-register')({
presets: [[require.resolve('babel-preset-env'), {
require('@babel/register')({
presets: [[require.resolve('@babel/preset-env'), {
"targets": { "node": "current" }
}]]
});
Expand Down
15 changes: 13 additions & 2 deletions packages/cli/lib/lib/webpack/webpack-client-config.js
Expand Up @@ -11,6 +11,7 @@ const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const RenderHTMLPlugin = require('./render-html-plugin');
const PushManifestPlugin = require('./push-manifest');
const baseConfig = require('./webpack-base-config');
const BabelEsmPlugin = require('babel-esm-plugin');
const { normalizePath } = require('../../util');

const cleanFilename = name => name.replace(/(^\/(routes|components\/(routes|async))\/|(\/index)?\.js$)/g, '');
Expand Down Expand Up @@ -107,8 +108,9 @@ function isProd(config) {

plugins: [
new webpack.DefinePlugin({
'process.env.ADD_SW': config.sw
}),
'process.env.ADD_SW': config.sw,
'process.env.ESM': config.esm
})
],

optimization: {
Expand Down Expand Up @@ -177,6 +179,15 @@ function isProd(config) {
);
}

if (config.esm) {
prodConfig.plugins.push(
new BabelEsmPlugin({
filename: '[name].[chunkhash:5].esm.js',
chunkFilename: '[name].chunk.[chunkhash:5].esm.js'
}),
);
}

if (config.analyze) {
prodConfig.plugins.push(
new BundleAnalyzerPlugin()
Expand Down
18 changes: 15 additions & 3 deletions packages/cli/lib/resources/template.html
Expand Up @@ -24,8 +24,20 @@
<body>
<%= 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>
}) %>
<% if (webpack.assets.filter(entry => entry.name.match(/bundle.\w{5}.esm.js$/)).length > 0) { %>
<!-- Fix for safari < 11 nomodule bug. TODO: Do the following only for safari.-->
<script>!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()},!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();</script>
<script src="<%= htmlWebpackPlugin.files.publicPath %><%= webpack.assets.filter(entry => entry.name.match(/bundle.\w{5}.esm.js$/))[0].name %>" type="module"></script>
<script nomodule defer src="<%= htmlWebpackPlugin.files.chunks['bundle'].entry %>"></script>
<!--
Fetch and Promise polyfills are not needed for browsers that support type=module
Please re-evaluate below line if adding more polyfills.
-->
<script nomodule>window.fetch||document.write('<script src="<%= htmlWebpackPlugin.files.chunks["polyfills"].entry %>"><\/script>')</script>
<% } else { %>
<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>
22 changes: 12 additions & 10 deletions packages/cli/package.json
Expand Up @@ -46,20 +46,22 @@
"sass-loader": "^7.0.1"
},
"dependencies": {
"@babel/core": "^7.0.0-beta.51",
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.51",
"@babel/plugin-proposal-decorators": "^7.0.0-beta.51",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.51",
"@babel/plugin-syntax-dynamic-import": "^7.0.0-beta.51",
"@babel/plugin-transform-object-assign": "^7.0.0-beta.51",
"@babel/plugin-transform-react-constant-elements": "^7.0.0-beta.51",
"@babel/plugin-transform-react-jsx": "^7.0.0-beta.51",
"@babel/preset-env": "^7.0.0-beta.51",
"@babel/register": "^7.0.0-beta.51",
"autoprefixer": "^8.4.1",
"babel-loader": "^7.0.0",
"babel-esm-plugin": "0.0.9",
"babel-loader": "^8.0.0-beta.3",
"babel-plugin-jsx-pragmatic": "^1.0.2",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-export-extensions": "^6.22.0",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-react-constant-elements": "^6.23.0",
"babel-plugin-transform-react-jsx": "^6.24.1",
"babel-plugin-transform-react-remove-prop-types": "^0.4.5",
"babel-preset-env": "^1.3.3",
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this required? Preact build command fails at the moment

Copy link
Member

Choose a reason for hiding this comment

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

look up a couple of lines, has to be changed in the babel config though

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

oh yeah...

"babel-register": "^6.24.1",
"bluebird": "^3.5.0",
"chalk": "^2.4.1",
"console-clear": "^1.0.0",
Expand Down