Skip to content

Commit

Permalink
feat: enable babel plugin for styled-components project (#639)
Browse files Browse the repository at this point in the history
* feat: enable babel plugin for styled-components project

* test: add case for styled-components

* chore: update pnpm-lock.yaml

* test: ignore branch

* test: workaround styled-components for case
  • Loading branch information
PeachScript authored Apr 28, 2023
1 parent 3c41810 commit 8ddeed2
Show file tree
Hide file tree
Showing 10 changed files with 1,345 additions and 145 deletions.
1,418 changes: 1,276 additions & 142 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ packages:
- 'boilerplate'
- 'tests/fixtures/build/classic-jsx'
- 'tests/fixtures/build/automatic-jsx'
- 'tests/fixtures/build/styled-components'
11 changes: 9 additions & 2 deletions src/builder/bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import type { webpack } from '@umijs/bundler-webpack';
import { chalk, importLazy, lodash } from '@umijs/utils';
import path from 'path';
import { CACHE_PATH } from '../../constants';
import type { BundleConfigProvider } from '../config';
import { getBabelPresetReactOpts, getBundleTargets } from '../utils';
import { logger } from '../../utils';
import type { BundleConfigProvider } from '../config';
import {
getBabelPresetReactOpts,
getBabelStyledComponentsOpts,
getBundleTargets,
} from '../utils';

const bundler: typeof import('@umijs/bundler-webpack') = importLazy(
path.dirname(require.resolve('@umijs/bundler-webpack/package.json')),
Expand Down Expand Up @@ -98,6 +102,9 @@ async function bundless(opts: IBundlessOpts): Promise<void | IBundleWatcher> {
pluginTransformRuntime: {},
pluginLockCoreJS: {},
pluginDynamicImportNode: false,
pluginStyledComponents: getBabelStyledComponentsOpts(
opts.configProvider.pkg,
),
},
],
beforeBabelPlugins: [require.resolve('babel-plugin-dynamic-import-node')],
Expand Down
3 changes: 3 additions & 0 deletions src/builder/bundless/loaders/javascript/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
addSourceMappingUrl,
ensureRelativePath,
getBabelPresetReactOpts,
getBabelStyledComponentsOpts,
getBundlessTargets,
} from '../../../utils';
import type { IJSTransformer } from '../types';
Expand Down Expand Up @@ -44,6 +45,7 @@ const babelTransformer: IJSTransformer = function (content) {
path.dirname(this.paths.fileAbsPath),
),
presetTypeScript: {},
pluginStyledComponents: getBabelStyledComponentsOpts(this.pkg),
};

// transform alias to relative path for babel-plugin-module-resolver
Expand Down Expand Up @@ -71,6 +73,7 @@ const babelTransformer: IJSTransformer = function (content) {
version: this.pkg.dependencies?.['@babel/runtime'],
};
}

const { code, map } = transform(content, {
filename: this.paths.fileAbsPath,
cwd: this.paths.cwd,
Expand Down
24 changes: 24 additions & 0 deletions src/builder/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { semver } from '@umijs/utils';
import { createHash } from 'crypto';
import path from 'path';
import {
IApi,
Expand Down Expand Up @@ -115,3 +116,26 @@ export function getBundlessTargets(config: IBundlessConfig) {

return targets;
}

export function getBabelStyledComponentsOpts(pkg: IApi['pkg']) {
let opts: false | { fileName: boolean; namespace?: string } = false;

// enable styled-components plugin for styled-components-based projects
if (pkg.dependencies?.['styled-components']) {
opts = { fileName: false };

// set namespace for avoid className conflicts
if (pkg.name) {
const [name, org] = pkg.name.split('/').reverse();
// hash org to make namespace clear
const suffix = org
? `-${createHash('md5').update(org).digest('hex').slice(0, 4)}`
: /* istanbul ignore next -- @preserve */
'';

opts.namespace = `${name}${suffix}`;
}
}

return opts;
}
4 changes: 4 additions & 0 deletions tests/fixtures/build/styled-components/.fatherrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
esm: {},
umd: {},
};
9 changes: 9 additions & 0 deletions tests/fixtures/build/styled-components/expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default (files: Record<string, string>) => {
// expect namespace pefix
expect(files['esm/index.js']).toContain('"some-lib-');
expect(files['umd/some-lib.min.js']).toContain('"some-lib-');

// expect auto set displayName
expect(files['esm/index.js']).toContain('"Foo"');
expect(files['umd/some-lib.min.js']).toContain('"Foo"');
};
11 changes: 11 additions & 0 deletions tests/fixtures/build/styled-components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@org/some-lib",
"dependencies": {
"styled-components": "^6.0.0-beta.15"
},
"devDependencies": {
"shallowequal": "^1.1.0",
"stylis": "^4.1.3"
},
"FIXME": "Wait for styled-components fix, ref: https://github.com/styled-components/styled-components/pull/3995"
}
7 changes: 7 additions & 0 deletions tests/fixtures/build/styled-components/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import styled from 'styled-components';

const Foo = styled.div`
color: red;
`;

export default Foo;
2 changes: 1 addition & 1 deletion tests/g.commitlint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('lint generator', () => {
if (existsSync(commitlintConfPath)) {
unlinkSync(commitlintConfPath);
}
writeFileSync(path.join(CASES_DIR, 'package.json'), '{}');
writeFileSync(path.join(CASES_DIR, 'package.json'), '{}\n', 'utf-8');
warnSpy.mockReset();
});

Expand Down

0 comments on commit 8ddeed2

Please sign in to comment.