Skip to content

Commit 75fb81e

Browse files
committed
Update and configure babel to be able to use latest js syntax in code and deps
- Configure @vue/cli-plugin-babel in vue.config.js to transpile dependencies with js syntax too modern for our webpack version via transpileDependencies. This makes @vue/cli-plugin-babel process the listed dependencies with the babel-loader webpack loader. This now allows to use modern language features like optional chaining. - Similarly, process code and dependencies via manually set-up babel-loader in storybook's webpack.config.js, that processes ts and js files, similar to the babel-loader set-up by @vue/cli-plugin-babel and @vue/cli-plugin-typescrit, which can be checked via "env build=mainnet yarn vue-cli-service inspect --mode production" - Dependencies that should be processed, must be listed in transpileDependencies in vue.config.js and storybook's webpack.config.js. - To support the latest js syntax, we update @babel/preset-env to the latest version by updating @vue/cli-plugin-babel to the latest available 4.x version compatible with webpack 4. - Also update core-js for the latest polyfills to be available to babel. - Finally, also update @vue/cli-plugin-typescript and @vue/cli-service to be in line with @vue/cli-plugin-babel.
1 parent 954ab92 commit 75fb81e

File tree

4 files changed

+2271
-1281
lines changed

4 files changed

+2271
-1281
lines changed

.storybook/webpack.config.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1+
const path = require('path');
2+
13
// Fix build for Node version with OpenSSL 3
24
const crypto = require('crypto');
35
const origCreateHash = crypto.createHash;
46
crypto.createHash = (alg, opts) => {
57
return origCreateHash(alg === 'md4' ? 'md5' : alg, opts);
68
};
79

10+
// Process source files and specified dependencies by babel to be able to use more modern js syntax than supported by
11+
// our Webpack version. Should be kept in sync with transpileDependencies in vue.config.js
12+
const transpileDependencies = [];
13+
const babel = {
14+
loader: require.resolve('babel-loader'),
15+
// By default, options from babel.config.js are used. If you're encountering issues with some syntax not being
16+
// transpiled sufficiently, experiment with different @vue/babel-preset-app or @babel/preset-env options. As this is
17+
// only for a demo page, the resulting file size by code being transpiled too aggressively or too many polyfills
18+
// being included, doesn't really matter.
19+
// options: {
20+
// presets: [
21+
// ['@babel/preset-env', {
22+
// targets: { node: 14 },
23+
// // useBuiltIns: 'usage',
24+
// // corejs: '3.37',
25+
// // include: ["@babel/plugin-transform-logical-assignment-operators"],
26+
// debug: true,
27+
// }]
28+
// ],
29+
// },
30+
};
31+
832
module.exports = (storybookBaseConfig, env, config) => {
933
// Save and remove default SVG rule to add a custom one below
1034
const svgRule = config.module.rules.find(rule => rule.test.toString().includes('svg'));
@@ -17,11 +41,13 @@ module.exports = (storybookBaseConfig, env, config) => {
1741
...config.module,
1842
rules: [...config.module.rules, {
1943
test: /\.tsx?$/,
20-
loader: require.resolve('ts-loader'),
21-
options: {
22-
appendTsSuffixTo: [/\.vue$/],
23-
transpileOnly: true
24-
}
44+
use: [babel, {
45+
loader: require.resolve('ts-loader'),
46+
options: {
47+
appendTsSuffixTo: [/\.vue$/],
48+
transpileOnly: true
49+
}
50+
}]
2551
}, {
2652
test: svgRule.test,
2753
oneOf: [{
@@ -34,6 +60,13 @@ module.exports = (storybookBaseConfig, env, config) => {
3460
loader: svgRule.loader,
3561
options: svgRule.options
3662
}],
63+
}, {
64+
test: /\.(?:js|mjs|cjs)$/,
65+
include: [
66+
'src', // Process js files in src, basically index.stories.js
67+
...transpileDependencies.map((dependency) => path.join('node_modules', dependency)),
68+
].map((dependency) => path.join(__dirname, '..', dependency)),
69+
...babel,
3770
}]
3871
},
3972
resolve: {

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
"@storybook/addon-links": "^4.0.0-alpha.14",
3838
"@storybook/addon-viewport": "^4.0.0-alpha.14",
3939
"@storybook/vue": "^4.0.0-alpha.14",
40-
"@vue/cli-plugin-babel": "~4.2.2",
41-
"@vue/cli-plugin-typescript": "~4.2.2",
42-
"@vue/cli-service": "~4.2.2",
40+
"@vue/cli-plugin-babel": "~4.5.19",
41+
"@vue/cli-plugin-typescript": "~4.5.19",
42+
"@vue/cli-service": "~4.5.19",
4343
"@vue/compiler-dom": "^3.2.45",
44-
"core-js": "^3.6.4",
44+
"core-js": "^3.37.1",
4545
"file-loader": "^3.0.1",
4646
"moment": "^2.29.4",
4747
"rimraf": "^3.0.2",

vue.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,12 @@ const chainWebpack = config => {
9494
.end();
9595
}
9696

97-
module.exports = { configureWebpack, chainWebpack };
97+
module.exports = {
98+
configureWebpack,
99+
chainWebpack,
100+
// Add dependencies here which should be transpiled by babel-loader via @vue/cli-plugin-babel. This is needed as our
101+
// Webpack version is too old to process some modern js syntax in the listed dependencies. Should be kept in sync with
102+
// transpileDependencies in storybook's webpack.config.js.
103+
// When changing to Webpack 5, some or all can probably be removed.
104+
transpileDependencies: [],
105+
};

0 commit comments

Comments
 (0)