Skip to content

Commit fbbce5e

Browse files
committed
feat(webpack): import behavior for non-iife fomat usage
1 parent cd60a4c commit fbbce5e

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

lib/webpack.config.base.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = function (type, options) {
4343
devtool: type === 'watch' ? 'cheap-module-eval-source-map' : 'source-map',
4444
entry: options.entry ? objectString(options.entry) : 'src/index',
4545
output: {
46-
path: cwd('build'),
46+
path: cwd(options.dest || 'build'),
4747
filename: 'bundle.js',
4848
publicPath: './'
4949
},
@@ -96,14 +96,31 @@ module.exports = function (type, options) {
9696
fallbacks: true
9797
})
9898
],
99-
plugins: [
99+
plugins: []
100+
}
101+
// for both watch and build
102+
// set format
103+
if (options.format === 'umd') {
104+
config.output.libraryTarget = 'umd'
105+
config.output.library = options.umd
106+
} else if (options.format === 'cjs') {
107+
config.output.libraryTarget = 'commonjs2'
108+
}
109+
// set target
110+
if (options.target) {
111+
config.target = options.target
112+
}
113+
// add html plugin if not targeted in commonjs
114+
if (options.format !== 'cjs') {
115+
config.plugins.push(
100116
new HtmlWebpackPlugin(Object.assign({}, {
101117
title: options.title || 'Tooling',
102118
template: dir('lib/index.jade'),
103119
inject: false
104120
}, toolingConfig.index))
105-
]
121+
)
106122
}
123+
107124
if (type === 'watch') {
108125
// inject client page for hmr
109126
if (typeof config.entry === 'string') {

readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ Options:
7373
|-u/--use|Set the framework you use, eg: `react`, `vue`. `vue` by default|
7474
|--ai/--auto-install|(**Buggy**) Automatically install missing dependencies when editing|
7575
|--title|Set html title|
76+
|--format|Set bundle format, available options are `cjs` `umd`, default is `iife`|
77+
|-t/--target|Set webpack target|
78+
|-d/--dest|Set bundled file dest directory, default is `./build`|
79+
80+
Run `tooling -h` `tooling watch -h` `tooling build -h` to see more usage.
7681

7782
**Set up custom index.html in `package.json`**. see usage at [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin)
7883

@@ -82,7 +87,7 @@ Options:
8287
"tooling": {
8388
"index": {
8489
"title": "tooling index",
85-
"template": "src/index.template"
90+
"template": "src/index.jade"
8691
}
8792
}
8893
}

tooling

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,27 @@ program.version(pkg.version)
1313

1414
program
1515
.command('watch')
16+
.description('hot reloading mode')
1617
.option('-e, --entry [webpackEntry]', 'Set webpack entry')
1718
.option('-p, --port [serverPort]', 'Change port of server')
1819
.option('--bs, --browser-sync [port]', 'Toggle browserSync')
1920
.option('-s, --silent', 'Do not open browser window')
2021
.option('-u, --use [usePresetsFor]', 'Use presets for React or Vue or the default config')
2122
.option('--ai, --auto-install', 'Automatically install missing dependencies when editing')
2223
.option('--title [htmlTitle]', 'Set title for output html')
24+
.option('--format [bundleFormat]', 'Set bundle format')
25+
.option('--target [webpackTarget]', 'Set webpack target')
2326
.action(watch)
2427

2528
program
2629
.command('build')
30+
.description('build mode')
2731
.option('-e, --entry [webpackEntry]', 'Set webpack entry"')
2832
.option('-u, --use [usePresetsFor]', 'Use presets for React or Vue')
2933
.option('--title [htmlTitle]', 'Set title for output html')
34+
.option('--format [bundleFormat]', 'Set bundle format')
35+
.option('-t, --target [webpackTarget]', 'Set webpack target')
36+
.option('-d, --dest [bundleDestDir]', 'Set bundled file dest directory')
3037
.action(build)
3138

3239
program.parse(process.argv)

0 commit comments

Comments
 (0)