Skip to content

Commit fabaecb

Browse files
authored
fix: alias/sourcemap/bundle_mode bug (#651)
* fix: bundle mode with start command should be development environment instead of production * fix: alias is should be working for .d.ts file * fix: sourcemap option should be working and default should be false for transform
1 parent 2e4042f commit fabaecb

22 files changed

+653
-178
lines changed

.changeset/new-pears-arrive.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ice/pkg': patch
3+
---
4+
5+
fix: bundle mode with start command should be `development` environment instead of `production`

.changeset/plenty-avocados-know.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ice/pkg': patch
3+
---
4+
5+
fix: alias is should be working for .d.ts file

.changeset/sweet-plums-rush.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ice/pkg': patch
3+
---
4+
5+
fix: sourcemap option should be working and default should be false for transform

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ packages/*/es/
3333
/packages/**/es2017
3434
/packages/**/cjs
3535
/packages/**/esm
36+
/packages/pkg/tests/fixtures/**/build.config.for-test.mts
3637
**/node_modules
3738
/.pnpm-debug.log
3839
**/pnpm-global

packages/pkg/src/helpers/dts.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ export async function dtsCompile({ files, alias, rootDir, outputDir }: DtsCompil
127127
// Reason: https://github.com/microsoft/TypeScript/issues/30952#issuecomment-1114225407
128128
const tsConfigLocalPath = path.join(rootDir, 'node_modules/pkg/tsconfig.json');
129129
await fse.ensureFile(tsConfigLocalPath);
130-
await fse.writeJSON(tsConfigLocalPath, tsConfig, { spaces: 2 });
130+
await fse.writeJSON(tsConfigLocalPath, {
131+
...tsConfig,
132+
compilerOptions: tsConfig.options,
133+
}, { spaces: 2 });
131134

132135
const runFile = await prepareSingleFileReplaceTscAliasPaths({
133136
configFile: tsConfigLocalPath,

packages/pkg/src/helpers/getBuildTasks.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ function getBuildTask(buildTask: BuildTask, context: Context): BuildTask {
2828

2929
config.sourcemap = config.sourcemap ?? command === 'start';
3030

31+
const mode = command === 'build' ? 'production' : 'development';
32+
config.modes = [mode];
33+
3134
if (config.type === 'bundle') {
3235
const defaultBundleSwcConfig = getDefaultBundleSwcConfig(config, context, taskName);
3336
config.swcCompileOptions = typeof config.modifySwcCompileOptions === 'function' ?
@@ -38,8 +41,6 @@ function getBuildTask(buildTask: BuildTask, context: Context): BuildTask {
3841
);
3942
} else if (config.type === 'transform') {
4043
config.outputDir = getTransformDefaultOutputDir(rootDir, taskName);
41-
const mode = command === 'build' ? 'production' : 'development';
42-
config.modes = [mode];
4344
const defaultTransformSwcConfig = getDefaultTransformSwcConfig(config, context, taskName, mode);
4445
config.swcCompileOptions = typeof config.modifySwcCompileOptions === 'function' ?
4546
config.modifySwcCompileOptions(defaultTransformSwcConfig) :

packages/pkg/src/tasks/transform.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ async function runTransform(
9494
const logger = createLogger(`${taskName}-${mode}`);
9595
const entryDirs = getTransformEntryDirs(rootDir, config.entry as Record<string, string>);
9696

97+
if (config.sourcemap === 'inline') {
98+
logger.warn('The sourcemap "inline" for transform has not fully supported.');
99+
}
100+
97101
const files: OutputFile[] = [];
98102

99103
if (updatedFile) {
@@ -180,7 +184,8 @@ async function runTransform(
180184
}
181185
}
182186

183-
if (map) {
187+
// IMPROVE: should disable sourcemap generation in the transform step for speed.
188+
if (map && config.sourcemap !== false) {
184189
const standardizedMap = typeof map === 'string' ? map : JSON.stringify(map);
185190

186191
fs.writeFileSync(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from '@ice/pkg';
2+
3+
// https://pkg.ice.work/reference/config-list
4+
export default defineConfig({
5+
transform: {
6+
formats: ['cjs', 'esm', 'es2017']
7+
},
8+
alias: {
9+
'@': './src'
10+
},
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@ice/pkg-tests-fixtures-alias",
3+
"private": true,
4+
"dependencies": {
5+
"@ice/pkg": "workspace:*"
6+
},
7+
"version": null
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const bar = 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { bar } from '@/alias.js'
2+
3+
export const foo = 1
4+
export { bar }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../tsconfig.common.json",
3+
"compilerOptions": {
4+
"paths": {
5+
"@": ["./src"]
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@ice/pkg-tests-fixtures-default",
3+
"private": true,
4+
"dependencies": {
5+
"@ice/pkg": "workspace:*"
6+
},
7+
"version": null
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`Run config default > cjs structure 1`] = `
4+
{
5+
"files": [
6+
{
7+
"name": "alias.d.ts",
8+
},
9+
{
10+
"name": "alias.js",
11+
},
12+
{
13+
"name": "index.d.ts",
14+
},
15+
{
16+
"name": "index.js",
17+
},
18+
],
19+
"name": "cjs",
20+
}
21+
`;
22+
23+
exports[`Run config default > dist structure 1`] = `null`;
24+
25+
exports[`Run config default > es2017 structure 1`] = `
26+
{
27+
"files": [
28+
{
29+
"name": "alias.d.ts",
30+
},
31+
{
32+
"name": "alias.js",
33+
},
34+
{
35+
"name": "index.d.ts",
36+
},
37+
{
38+
"name": "index.js",
39+
},
40+
],
41+
"name": "es2017",
42+
}
43+
`;
44+
45+
exports[`Run config default > esm structure 1`] = `
46+
{
47+
"files": [
48+
{
49+
"name": "alias.d.ts",
50+
},
51+
{
52+
"name": "alias.js",
53+
},
54+
{
55+
"name": "index.d.ts",
56+
},
57+
{
58+
"name": "index.js",
59+
},
60+
],
61+
"name": "esm",
62+
}
63+
`;
64+
65+
exports[`Run config default > file content cjs/alias.d.ts 1`] = `
66+
"export declare const bar = 2;
67+
"
68+
`;
69+
70+
exports[`Run config default > file content cjs/alias.js 1`] = `
71+
"\\"use strict\\";
72+
Object.defineProperty(exports, \\"__esModule\\", {
73+
value: true
74+
});
75+
Object.defineProperty(exports, \\"bar\\", {
76+
enumerable: true,
77+
get: function() {
78+
return bar;
79+
}
80+
});
81+
var bar = 2;
82+
"
83+
`;
84+
85+
exports[`Run config default > file content cjs/index.d.ts 1`] = `
86+
"import { bar } from './alias.js';
87+
export declare const foo = 1;
88+
export { bar };
89+
"
90+
`;
91+
92+
exports[`Run config default > file content cjs/index.js 1`] = `
93+
"\\"use strict\\";
94+
Object.defineProperty(exports, \\"__esModule\\", {
95+
value: true
96+
});
97+
function _export(target, all) {
98+
for(var name in all)Object.defineProperty(target, name, {
99+
enumerable: true,
100+
get: all[name]
101+
});
102+
}
103+
_export(exports, {
104+
foo: function() {
105+
return foo;
106+
},
107+
bar: function() {
108+
return _alias.bar;
109+
}
110+
});
111+
var _alias = require(\\"@/alias.js\\");
112+
var foo = 1;
113+
"
114+
`;
115+
116+
exports[`Run config default > file content es2017/alias.d.ts 1`] = `
117+
"export declare const bar = 2;
118+
"
119+
`;
120+
121+
exports[`Run config default > file content es2017/alias.js 1`] = `
122+
"export const bar = 2;
123+
"
124+
`;
125+
126+
exports[`Run config default > file content es2017/index.d.ts 1`] = `
127+
"import { bar } from './alias.js';
128+
export declare const foo = 1;
129+
export { bar };
130+
"
131+
`;
132+
133+
exports[`Run config default > file content es2017/index.js 1`] = `
134+
"import { bar } from './alias.js';
135+
export const foo = 1;
136+
export { bar };
137+
"
138+
`;
139+
140+
exports[`Run config default > file content esm/alias.d.ts 1`] = `
141+
"export declare const bar = 2;
142+
"
143+
`;
144+
145+
exports[`Run config default > file content esm/alias.js 1`] = `
146+
"export var bar = 2;
147+
"
148+
`;
149+
150+
exports[`Run config default > file content esm/index.d.ts 1`] = `
151+
"import { bar } from './alias.js';
152+
export declare const foo = 1;
153+
export { bar };
154+
"
155+
`;
156+
157+
exports[`Run config default > file content esm/index.js 1`] = `
158+
"import { bar } from \\"./alias.js\\";
159+
export var foo = 1;
160+
export { bar };
161+
"
162+
`;

0 commit comments

Comments
 (0)