Skip to content

Commit

Permalink
feat: support transform /lib/ to /es/ for esm format with config.impo…
Browse files Browse the repository at this point in the history
…rtLibToEs (#6)
  • Loading branch information
sorrycc authored Jun 1, 2019
1 parent 6d6d04f commit f47c403
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ esm 为 `rollup` 或 `babel` 时,等同于配置了 `{ type: "rollup" | "babel

通常不需要配置,除非你发布到 npm 的代码需要保密。

#### esm.importLibToEs

是否在 esm 模式下把 import 项里的 `/lib/` 转换为 `/es/`

* Type: `boolean`
* Default: `false`

比如 `import 'foo/lib/button';`,在 cjs 模式下会保持原样,在 esm 模式下会编译成 `import 'foo/es/button';`

#### cjs

是否输出 cjs 格式,以及指定 cjs 格式的打包方式等。
Expand Down
5 changes: 5 additions & 0 deletions src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface IBabelOpts {
type: 'esm' | 'cjs';
target?: 'browser' | 'node';
watch?: boolean;
importLibToEs?: boolean;
bundleOpts: IBundleOptions;
}

Expand All @@ -33,6 +34,7 @@ export default async function(opts: IBabelOpts) {
cwd,
type,
watch,
importLibToEs,
bundleOpts: {
target = 'browser',
runtimeHelpers,
Expand Down Expand Up @@ -63,6 +65,9 @@ export default async function(opts: IBabelOpts) {
browserFiles,
nodeFiles,
});
if (importLibToEs && type === 'esm') {
babelOpts.plugins.push(require.resolve('../lib/importLibToEs'));
}
babelOpts.presets.push(...extraBabelPresets);
babelOpts.plugins.push(...extraBabelPlugins);

Expand Down
4 changes: 3 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ export async function build(opts: IOpts, extraOpts: IExtraBuildOpts = {}) {
if (bundleOpts.esm) {
const esm = bundleOpts.esm as IEsm;
log(`Build esm with ${esm.type}`);
const importLibToEs = esm && esm.importLibToEs;
if (esm && esm.type === 'babel') {
await babel({ cwd, watch, type: 'esm', bundleOpts });
await babel({ cwd, watch, type: 'esm', importLibToEs, bundleOpts });
} else {
await rollup({
cwd,
type: 'esm',
entry: bundleOpts.entry,
importLibToEs,
watch,
bundleOpts,
});
Expand Down
5 changes: 5 additions & 0 deletions src/fixtures/build/babel-importLibToEs/.umirc.library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export default {
cjs: { type: 'babel' },
esm: { type: 'babel', importLibToEs: true },
};
2 changes: 2 additions & 0 deletions src/fixtures/build/babel-importLibToEs/expected/es/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import foo from "foo/es/foo";
console.log(foo());
7 changes: 7 additions & 0 deletions src/fixtures/build/babel-importLibToEs/expected/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

var _foo = _interopRequireDefault(require("foo/lib/foo"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

console.log((0, _foo.default)());

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/fixtures/build/babel-importLibToEs/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from 'foo/lib/foo';

console.log(foo());
5 changes: 5 additions & 0 deletions src/fixtures/build/rollup-importLibToEs/.umirc.library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export default {
cjs: { type: 'rollup' },
esm: { type: 'rollup', importLibToEs: true },
};
5 changes: 5 additions & 0 deletions src/fixtures/build/rollup-importLibToEs/expected/index.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function foo () {
return 'es/foo';
}

console.log(foo());
7 changes: 7 additions & 0 deletions src/fixtures/build/rollup-importLibToEs/expected/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

function foo () {
return 'lib/foo';
}

console.log(foo());

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/fixtures/build/rollup-importLibToEs/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from 'foo/lib/foo';

console.log(foo());
6 changes: 5 additions & 1 deletion src/getRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface IGetRollupConfigOpts {
cwd: string;
entry: string;
type: ModuleFormat;
importLibToEs?: boolean;
bundleOpts: IBundleOptions;
}

Expand All @@ -29,7 +30,7 @@ interface IPkg {
}

export default function(opts: IGetRollupConfigOpts): RollupOptions[] {
const { type, entry, cwd, bundleOpts } = opts;
const { type, entry, cwd, importLibToEs, bundleOpts } = opts;
const {
umd,
esm,
Expand Down Expand Up @@ -70,6 +71,9 @@ export default function(opts: IGetRollupConfigOpts): RollupOptions[] {
// ref: https://github.com/rollup/rollup-plugin-babel#usage
extensions,
};
if (importLibToEs && type === 'esm') {
babelOpts.plugins.push(require.resolve('../lib/importLibToEs'));
}
babelOpts.presets.push(...extraBabelPresets);
babelOpts.plugins.push(...extraBabelPlugins);

Expand Down
26 changes: 26 additions & 0 deletions src/importLibToEs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { join, dirname } from 'path';
import fs from 'fs';

const cwd = process.cwd();

function replacePath(path) {
if (path.node.source && /\/lib\//.test(path.node.source.value)) {
const esModule = path.node.source.value.replace('/lib/', '/es/');
const esPath = dirname(join(cwd, `node_modules/${esModule}`));
if (fs.existsSync(esPath)) {
console.log(`[es build] replace ${path.node.source.value} with ${esModule}`);
path.node.source.value = esModule;
}
}
}

function replaceLib() {
return {
visitor: {
ImportDeclaration: replacePath,
ExportNamedDeclaration: replacePath,
},
};
}

export default replaceLib;
4 changes: 3 additions & 1 deletion src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ interface IRollupOpts {
type: ModuleFormat;
bundleOpts: IBundleOptions;
watch?: boolean;
importLibToEs?: boolean;
}

async function build(entry: string, opts: IRollupOpts) {
const { cwd, type, bundleOpts } = opts;
const { cwd, type, bundleOpts, importLibToEs } = opts;
const rollupConfigs = getRollupConfig({
cwd,
type,
entry,
importLibToEs,
bundleOpts: normalizeBundleOpts(entry, bundleOpts),
});

Expand Down
3 changes: 3 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default {
file: noEmptyStr,
mjs: { type: 'boolean' },
minify: { type: 'boolean' },
importLibToEs: {
type: 'boolean',
},
},
},
],
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface ICjs extends IBundleTypeOutput {
interface IEsm extends IBundleTypeOutput {
mjs?: boolean;
minify?: boolean;
importLibToEs?: boolean;
}

interface IStringObject {
Expand Down

0 comments on commit f47c403

Please sign in to comment.