Skip to content

Commit 54267df

Browse files
committedMar 13, 2019
feat: add --target flag
1 parent 59d1d9c commit 54267df

File tree

9 files changed

+47
-3
lines changed

9 files changed

+47
-3
lines changed
 

‎src/cli.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ cli
3737
.option('--external <id>', 'Mark a module id as external', {
3838
type: []
3939
})
40+
.option('-t, --target <target>', 'Output target', { default: 'node' })
4041
.option('-c, --config <file>', 'Use a custom config file')
4142
.option('--minimal', 'Generate minimal output whenever possible')
4243
.option('--banner', 'Add banner with pkg info to the bundle')
@@ -64,7 +65,8 @@ cli
6465
fileName: options.fileName,
6566
minify: options.minify,
6667
extractCSS: options.extractCss,
67-
sourceMap: options.map
68+
sourceMap: options.map,
69+
target: options.target
6870
},
6971
bundleNodeModules: options.bundleNodeModules,
7072
env: options.env,

‎src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ export class Bundler {
209209
nodeResolvePlugin({
210210
rootDir: this.rootDir,
211211
bundleNodeModules,
212-
externals: config.externals
212+
externals: config.externals,
213+
browser: config.output.target === 'browser'
213214
})
214215
)
215216

‎src/plugins/node-resolve.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ interface Options {
77
rootDir: string
88
bundleNodeModules?: boolean | string[]
99
externals: NormalizedConfig['externals']
10+
browser: boolean
1011
}
1112

1213
export default (options: Options) => {
1314
const plugin = require('rollup-plugin-node-resolve')({
1415
extensions: ['.js', '.json', '.jsx', '.ts', '.tsx'],
1516
preferBuiltins: true,
1617
jsnext: true,
17-
module: true
18+
module: true,
19+
browser: options.browser
1820
})
1921

2022
return {

‎src/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export interface BabelPresetOptions {
7676
minimal?: boolean
7777
}
7878

79+
export type OutputTarget = 'node' | 'browser'
80+
7981
export interface ConfigOutput {
8082
/**
8183
* Output format(s). You can append `min` to the format to generate minified bundle.
@@ -127,6 +129,12 @@ export interface ConfigOutput {
127129
* @default `true` for minified bundle, `false` otherwise
128130
*/
129131
sourceMap?: boolean
132+
/**
133+
* Output target
134+
* @default `node`
135+
* @cli `--target <target>`
136+
*/
137+
target?: OutputTarget
130138
}
131139

132140
export interface Config {

‎test/__snapshots__/index.test.ts.snap

+9
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,15 @@ module.exports = index;
256256
"
257257
`;
258258

259+
exports[`target:browser: target:browser dist/index.js 1`] = `
260+
"'use strict';
261+
262+
var foo = 'browser';
263+
264+
module.exports = foo;
265+
"
266+
`;
267+
259268
exports[`uglify: uglify dist/index.min.js 1`] = `
260269
"\\"use strict\\";Object.defineProperty(exports,\\"__esModule\\",{value:!0});const a=1;exports.a=1;
261270
"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'browser'

‎test/fixtures/target/browser/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import foo from './foo'
2+
3+
export default foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"browser": {
3+
"./foo.js": "./foo-browser.js"
4+
}
5+
}

‎test/index.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,16 @@ snapshot(
230230
}
231231
}
232232
)
233+
234+
snapshot(
235+
{
236+
title: 'target:browser',
237+
input: 'index.js',
238+
cwd: fixture('target/browser')
239+
},
240+
{
241+
output: {
242+
target: 'browser'
243+
}
244+
}
245+
)

0 commit comments

Comments
 (0)
Please sign in to comment.