Skip to content

Commit

Permalink
Merge pull request #22075 from storybookjs/norbert/other-secret-project
Browse files Browse the repository at this point in the history
storybook webpack builder to use swc instead of babel
  • Loading branch information
ndelangen authored May 23, 2023
2 parents cd4155b + 07cd086 commit 0010915
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 22 deletions.
2 changes: 2 additions & 0 deletions code/lib/builder-webpack5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@storybook/router": "7.1.0-alpha.20",
"@storybook/store": "7.1.0-alpha.20",
"@storybook/theming": "7.1.0-alpha.20",
"@swc/core": "^1.3.49",
"@types/node": "^16.0.0",
"@types/semver": "^7.3.4",
"babel-loader": "^9.0.0",
Expand All @@ -90,6 +91,7 @@
"process": "^0.11.10",
"semver": "^7.3.7",
"style-loader": "^3.3.1",
"swc-loader": "^0.2.3",
"terser-webpack-plugin": "^5.3.1",
"ts-dedent": "^2.0.0",
"util": "^0.12.4",
Expand Down
20 changes: 0 additions & 20 deletions code/lib/builder-webpack5/src/preview/babel-loader-preview.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import { toRequireContextString, toImportFn } from '@storybook/core-webpack';
import { dedent } from 'ts-dedent';
import type { BuilderOptions, TypescriptOptions } from '../types';
import { createBabelLoader } from './babel-loader-preview';
import { createBabelLoader, createSWCLoader } from './loaders';

const wrapForPnP = (input: string) => dirname(require.resolve(join(input, 'package.json')));

Expand Down Expand Up @@ -293,7 +293,9 @@ export default async (
fullySpecified: false,
},
},
createBabelLoader(babelOptions, typescriptOptions, Object.keys(virtualModuleMapping)),
builderOptions.useSWC
? createSWCLoader(Object.keys(virtualModuleMapping))
: createBabelLoader(babelOptions, typescriptOptions, Object.keys(virtualModuleMapping)),
{
test: /\.md$/,
type: 'asset/source',
Expand Down
50 changes: 50 additions & 0 deletions code/lib/builder-webpack5/src/preview/loaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { getProjectRoot } from '@storybook/core-common';
import type { Options } from '@swc/core';
import { dedent } from 'ts-dedent';
import { logger } from '@storybook/node-logger';
import type { TypescriptOptions } from '../types';

export const createBabelLoader = (
options: any,
typescriptOptions: TypescriptOptions,
excludes: string[] = []
) => {
return {
test: typescriptOptions.skipBabel ? /\.(mjs|jsx?)$/ : /\.(mjs|tsx?|jsx?)$/,
use: [
{
loader: require.resolve('babel-loader'),
options,
},
],
include: [getProjectRoot()],
exclude: [/node_modules/, ...excludes],
};
};

export const createSWCLoader = (excludes: string[] = []) => {
logger.warn(dedent`
The SWC loader is an experimental feature and may change or even be removed at any time.
`);

const config: Options = {
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
dynamicImport: true,
},
},
};
return {
test: /\.(mjs|cjs|tsx?|jsx?)$/,
use: [
{
loader: require.resolve('swc-loader'),
options: config,
},
],
include: [getProjectRoot()],
exclude: [/node_modules/, ...excludes],
};
};
1 change: 1 addition & 0 deletions code/lib/builder-webpack5/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface StorybookConfigWebpack extends Pick<StorybookConfig, 'webpack'

export type BuilderOptions = {
fsCache?: boolean;
useSWC?: boolean;
lazyCompilation?: boolean;
};

Expand Down
18 changes: 18 additions & 0 deletions code/lib/cli/src/sandbox-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,24 @@ const internalTemplates = {
},
},
},
'internal/swc-webpack': {
...baseTemplates['react-webpack/18-ts'],
name: 'SWC (react-webpack/18-ts)',
isInternal: true,
inDevelopment: true,
modifications: {
mainConfig: {
framework: {
name: '@storybook/react-webpack5',
options: {
builder: {
useSWC: true,
},
},
},
},
},
},
'internal/server-webpack5': {
name: 'Server Webpack5',
script: 'yarn init -y && echo "module.exports = {}" > webpack.config.js',
Expand Down
126 changes: 126 additions & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5608,6 +5608,7 @@ __metadata:
"@storybook/router": 7.1.0-alpha.20
"@storybook/store": 7.1.0-alpha.20
"@storybook/theming": 7.1.0-alpha.20
"@swc/core": ^1.3.49
"@types/node": ^16.0.0
"@types/pretty-hrtime": ^1.0.0
"@types/semver": ^7.3.4
Expand All @@ -5630,6 +5631,7 @@ __metadata:
semver: ^7.3.7
slash: ^5.0.0
style-loader: ^3.3.1
swc-loader: ^0.2.3
terser-webpack-plugin: ^5.3.1
ts-dedent: ^2.0.0
typescript: ~4.9.3
Expand Down Expand Up @@ -7477,69 +7479,139 @@ __metadata:
languageName: node
linkType: hard

"@swc/core-darwin-arm64@npm:1.3.49":
version: 1.3.49
resolution: "@swc/core-darwin-arm64@npm:1.3.49"
conditions: os=darwin & cpu=arm64
languageName: node
linkType: hard

"@swc/core-darwin-x64@npm:1.3.44":
version: 1.3.44
resolution: "@swc/core-darwin-x64@npm:1.3.44"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard

"@swc/core-darwin-x64@npm:1.3.49":
version: 1.3.49
resolution: "@swc/core-darwin-x64@npm:1.3.49"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard

"@swc/core-linux-arm-gnueabihf@npm:1.3.44":
version: 1.3.44
resolution: "@swc/core-linux-arm-gnueabihf@npm:1.3.44"
conditions: os=linux & cpu=arm
languageName: node
linkType: hard

"@swc/core-linux-arm-gnueabihf@npm:1.3.49":
version: 1.3.49
resolution: "@swc/core-linux-arm-gnueabihf@npm:1.3.49"
conditions: os=linux & cpu=arm
languageName: node
linkType: hard

"@swc/core-linux-arm64-gnu@npm:1.3.44":
version: 1.3.44
resolution: "@swc/core-linux-arm64-gnu@npm:1.3.44"
conditions: os=linux & cpu=arm64 & libc=glibc
languageName: node
linkType: hard

"@swc/core-linux-arm64-gnu@npm:1.3.49":
version: 1.3.49
resolution: "@swc/core-linux-arm64-gnu@npm:1.3.49"
conditions: os=linux & cpu=arm64 & libc=glibc
languageName: node
linkType: hard

"@swc/core-linux-arm64-musl@npm:1.3.44":
version: 1.3.44
resolution: "@swc/core-linux-arm64-musl@npm:1.3.44"
conditions: os=linux & cpu=arm64 & libc=musl
languageName: node
linkType: hard

"@swc/core-linux-arm64-musl@npm:1.3.49":
version: 1.3.49
resolution: "@swc/core-linux-arm64-musl@npm:1.3.49"
conditions: os=linux & cpu=arm64 & libc=musl
languageName: node
linkType: hard

"@swc/core-linux-x64-gnu@npm:1.3.44":
version: 1.3.44
resolution: "@swc/core-linux-x64-gnu@npm:1.3.44"
conditions: os=linux & cpu=x64 & libc=glibc
languageName: node
linkType: hard

"@swc/core-linux-x64-gnu@npm:1.3.49":
version: 1.3.49
resolution: "@swc/core-linux-x64-gnu@npm:1.3.49"
conditions: os=linux & cpu=x64 & libc=glibc
languageName: node
linkType: hard

"@swc/core-linux-x64-musl@npm:1.3.44":
version: 1.3.44
resolution: "@swc/core-linux-x64-musl@npm:1.3.44"
conditions: os=linux & cpu=x64 & libc=musl
languageName: node
linkType: hard

"@swc/core-linux-x64-musl@npm:1.3.49":
version: 1.3.49
resolution: "@swc/core-linux-x64-musl@npm:1.3.49"
conditions: os=linux & cpu=x64 & libc=musl
languageName: node
linkType: hard

"@swc/core-win32-arm64-msvc@npm:1.3.44":
version: 1.3.44
resolution: "@swc/core-win32-arm64-msvc@npm:1.3.44"
conditions: os=win32 & cpu=arm64
languageName: node
linkType: hard

"@swc/core-win32-arm64-msvc@npm:1.3.49":
version: 1.3.49
resolution: "@swc/core-win32-arm64-msvc@npm:1.3.49"
conditions: os=win32 & cpu=arm64
languageName: node
linkType: hard

"@swc/core-win32-ia32-msvc@npm:1.3.44":
version: 1.3.44
resolution: "@swc/core-win32-ia32-msvc@npm:1.3.44"
conditions: os=win32 & cpu=ia32
languageName: node
linkType: hard

"@swc/core-win32-ia32-msvc@npm:1.3.49":
version: 1.3.49
resolution: "@swc/core-win32-ia32-msvc@npm:1.3.49"
conditions: os=win32 & cpu=ia32
languageName: node
linkType: hard

"@swc/core-win32-x64-msvc@npm:1.3.44":
version: 1.3.44
resolution: "@swc/core-win32-x64-msvc@npm:1.3.44"
conditions: os=win32 & cpu=x64
languageName: node
linkType: hard

"@swc/core-win32-x64-msvc@npm:1.3.49":
version: 1.3.49
resolution: "@swc/core-win32-x64-msvc@npm:1.3.49"
conditions: os=win32 & cpu=x64
languageName: node
linkType: hard

"@swc/core@npm:^1.3.23":
version: 1.3.44
resolution: "@swc/core@npm:1.3.44"
Expand Down Expand Up @@ -7579,6 +7651,50 @@ __metadata:
languageName: node
linkType: hard

"@swc/core@npm:^1.3.49":
version: 1.3.49
resolution: "@swc/core@npm:1.3.49"
dependencies:
"@swc/core-darwin-arm64": 1.3.49
"@swc/core-darwin-x64": 1.3.49
"@swc/core-linux-arm-gnueabihf": 1.3.49
"@swc/core-linux-arm64-gnu": 1.3.49
"@swc/core-linux-arm64-musl": 1.3.49
"@swc/core-linux-x64-gnu": 1.3.49
"@swc/core-linux-x64-musl": 1.3.49
"@swc/core-win32-arm64-msvc": 1.3.49
"@swc/core-win32-ia32-msvc": 1.3.49
"@swc/core-win32-x64-msvc": 1.3.49
peerDependencies:
"@swc/helpers": ^0.5.0
dependenciesMeta:
"@swc/core-darwin-arm64":
optional: true
"@swc/core-darwin-x64":
optional: true
"@swc/core-linux-arm-gnueabihf":
optional: true
"@swc/core-linux-arm64-gnu":
optional: true
"@swc/core-linux-arm64-musl":
optional: true
"@swc/core-linux-x64-gnu":
optional: true
"@swc/core-linux-x64-musl":
optional: true
"@swc/core-win32-arm64-msvc":
optional: true
"@swc/core-win32-ia32-msvc":
optional: true
"@swc/core-win32-x64-msvc":
optional: true
peerDependenciesMeta:
"@swc/helpers":
optional: true
checksum: 3a8463047fb79a4bc5987cd24f935e19e59dfe967648c6abbc0b61f9197b76109de832fb03e5e6452459f9e2165b565f86dbc9ecd9506f58bd4befff8e0ddc47
languageName: node
linkType: hard

"@swc/helpers@npm:0.4.14":
version: 0.4.14
resolution: "@swc/helpers@npm:0.4.14"
Expand Down Expand Up @@ -28823,6 +28939,16 @@ __metadata:
languageName: node
linkType: hard

"swc-loader@npm:^0.2.3":
version: 0.2.3
resolution: "swc-loader@npm:0.2.3"
peerDependencies:
"@swc/core": ^1.2.147
webpack: ">=2"
checksum: cb3f9b116fbd292b881e804a4fe66cd6d543a7de2572f5a68e067e4780ee2d59a8da87a90736ba6e8e286e4368c98214ae3486b1b872080292814e5d3062f09c
languageName: node
linkType: hard

"symbol-observable@npm:4.0.0":
version: 4.0.0
resolution: "symbol-observable@npm:4.0.0"
Expand Down

0 comments on commit 0010915

Please sign in to comment.