Skip to content

Commit

Permalink
build: esm cmj 模式打包 less2css (#239)
Browse files Browse the repository at this point in the history
* fix: types

* build: esm cmj 模式打包 less2css

* ci: node set 18

* build: lodashes to lodash in cjs

* ci: lint trigger
  • Loading branch information
lvisei authored Sep 14, 2024
1 parent c72d710 commit ea7ab3d
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 37 deletions.
29 changes: 27 additions & 2 deletions .fatherrc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import { defineConfig } from 'father';
import { IFatherBundlessConfig } from 'father/dist/types';

const less2CssConfig: IFatherBundlessConfig = {
transformer: 'babel', // 使用 babel 编译
extraBabelPlugins: [
[
'./scripts/babel-less-to-css.js', // 把文件中的 '.less' 字符转为 '.css'
{ test: '\\.less' },
],
],
};

export default defineConfig({
esm: { output: 'es' },
cjs: { output: 'lib' },
esm: {
output: 'es',
...less2CssConfig,
},
cjs: {
output: 'lib',
...less2CssConfig,
alias: {
// lodash-es 不支持 cjs 产物,将打包产物修改为从 lodash 引入
'lodash-es': 'lodash',
},
},
plugins: [
// less 编译为 css
'./scripts/father-plugin-less.js',
],
// https://github.com/umijs/father/blob/master/docs/config.md#umd
umd: {
name: 'LarkMap',
Expand Down
23 changes: 14 additions & 9 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
name: lint

on: [push, pull_request]
on:
push:
branches: [master]
pull_request:
branches:
- '**'

jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
node-version: [18.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run ci
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run ci
# - name: coverall
# if: success()
# uses: coverallsapp/github-action@master
Expand Down
28 changes: 14 additions & 14 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
node-version: [18.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- uses: afc163/surge-preview@v1
with:
surge_token: ${{ secrets.SURGE_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
dist: docs-dist
build: |
npm install
npm run docs:build
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- uses: afc163/surge-preview@v1
with:
surge_token: ${{ secrets.SURGE_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
dist: docs-dist
build: |
npm install
npm run docs:build
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: release

on:
workflow_dispatch:
release:
types: [created, edited]

Expand All @@ -11,7 +12,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
node-version: 18
- run: npm install
- run: npm run ci

Expand All @@ -22,7 +23,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
node-version: 18
- run: npm install
- run: npm run docs:build
- run: |
Expand All @@ -38,4 +39,3 @@ jobs:
directory: docs-dist
branch: gh-pages
force: true

17 changes: 17 additions & 0 deletions scripts/babel-less-to-css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://github.com/umijs/father/blob/father-build%401.22.5/packages/father-build/src/getBabelConfig.ts#L25

const transformImportLess2Css = () => {
return {
name: 'transform-import-less-to-css',
visitor: {
ImportDeclaration(path) {
const re = /\.less$/;
if (re.test(path.node.source.value)) {
path.node.source.value = path.node.source.value.replace(re, '.css');
}
},
},
};
};

module.exports = transformImportLess2Css;
17 changes: 17 additions & 0 deletions scripts/father-plugin-less.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://github.com/hexh250786313/blog/issues/30
const { addLoader } = require('father/dist/builder/bundless');

module.exports = async (api) => {
const loaders = await api.applyPlugins({
key: 'addPostcssLoader',
initialValue: [
{
key: 'less-to-css',
test: /\.less$/,
loader: require.resolve('./loader-less-to-css'), // less 文件转 css 文件
},
],
});

loaders.forEach((loader) => addLoader(loader));
};
29 changes: 29 additions & 0 deletions scripts/loader-less-to-css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const path = require('path');
const less = require('less');
const postcss = require('postcss');
const syntax = require('postcss-less');
const autoprefixer = require('autoprefixer');

const loader = function (lessContent) {
const cb = this.async();
this.setOutputOptions({
ext: '.css',
});
postcss([autoprefixer({})])
.process(lessContent, { syntax })
.then((result) => {
// less 转 css
less.render(result.content, (err, css) => {
if (err) {
console.error(err);
return;
}
cb(null, css.css);
});
})
.catch((err) => {
console.error(err);
});
};

module.exports = loader;
11 changes: 4 additions & 7 deletions src/components/LarkMap/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IMapConfig } from '@antv/l7';
import { GaodeMap, GaodeMapV1, GaodeMapV2, Map } from '@antv/l7';
import { BaiduMap, GaodeMap, GaodeMapV1, GaodeMapV2, Map, TencentMap } from '@antv/l7';
import type { LarkMapProps } from './types';

export const createMap = async (mapType: LarkMapProps['mapType'], mapOptions: Partial<IMapConfig>) => {
Expand All @@ -16,14 +16,11 @@ export const createMap = async (mapType: LarkMapProps['mapType'], mapOptions: Pa
}

if (mapType === 'Tencent') {
return Promise.resolve(import('@antv/l7')).then(({ TencentMap }) => {
return new TencentMap(mapOptions);
});
return new TencentMap(mapOptions);
}

if (mapType === 'Baidu') {
return Promise.resolve(import('@antv/l7')).then(({ BaiduMap }) => {
return new BaiduMap(mapOptions);
});
return new BaiduMap(mapOptions);
}

return Promise.resolve(import('@antv/l7')).then(({ Mapbox }) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Marker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useDeepCompareEffect } from 'ahooks';
import React, { memo, useEffect, useMemo, useRef } from 'react';
import { createPortal } from 'react-dom';
import { useScene } from '../LarkMap/hooks';
import type { MarkerProps } from './types';
import type { MarkerEventType, MarkerProps } from './types';

export const Marker = memo<MarkerProps>(function Marker(props): React.ReactPortal {
const scene = useScene();
Expand All @@ -24,7 +24,7 @@ export const Marker = memo<MarkerProps>(function Marker(props): React.ReactPorta
// @ts-ignore
const l7marker = new L7Marker(options);

l7marker.on('click', (e: MouseEvent) => {
l7marker.on('click', (e: MarkerEventType) => {
thisRef.current.props.onClick?.(e);
});

Expand Down

0 comments on commit ea7ab3d

Please sign in to comment.