Skip to content

Commit dc98ca4

Browse files
committed
feat($newApi): add low-level flushFiles function + docs
1 parent 19e378b commit dc98ca4

File tree

6 files changed

+98
-24
lines changed

6 files changed

+98
-24
lines changed

.eslintrc.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,59 @@ module.exports = {
3838
'import/ignore': ['node_modules', 'flow-typed', '\\.(css|styl|svg|json)$'],
3939
rules: {
4040
'no-shadow': 0,
41-
'comma-dangle': 0,
4241
'no-use-before-define': 0,
4342
'no-param-reassign': 0,
43+
'react/prop-types': 0,
44+
'react/no-render-return-value': 0,
45+
'no-confusing-arrow': 0,
46+
'no-underscore-dangle': 0,
47+
'no-plusplus': 0,
4448
camelcase: 1,
4549
'prefer-template': 1,
4650
'react/no-array-index-key': 1,
4751
'global-require': 1,
4852
'react/jsx-indent': 1,
4953
'dot-notation': 1,
5054
'import/no-named-default': 1,
55+
'no-unused-vars': 1,
56+
'import/no-unresolved': 1,
57+
'no-nested-ternary': 1,
5158
semi: [2, 'never'],
5259
'flowtype/semi': [2, 'never'],
5360
'jsx-quotes': [2, 'prefer-single'],
5461
'react/jsx-filename-extension': [2, { extensions: ['.jsx', '.js'] }],
5562
'spaced-comment': [2, 'always', { markers: ['?'] }],
56-
'arrow-parens': [2, 'as-needed', { requireForBlockBody: false }]
63+
'arrow-parens': [2, 'as-needed', { requireForBlockBody: false }],
64+
'brace-style': [2, 'stroustrup'],
65+
'import/no-extraneous-dependencies': [
66+
'error',
67+
{
68+
devDependencies: true,
69+
optionalDependencies: true,
70+
peerDependencies: true
71+
}
72+
],
73+
'comma-dangle': [
74+
2,
75+
{
76+
arrays: 'never',
77+
objects: 'never',
78+
imports: 'never',
79+
exports: 'never',
80+
functions: 'never'
81+
}
82+
],
83+
'max-len': [
84+
'error',
85+
{
86+
code: 80,
87+
tabWidth: 2,
88+
ignoreUrls: true,
89+
ignoreComments: true,
90+
ignoreRegExpLiterals: true,
91+
ignoreStrings: true,
92+
ignoreTemplateLiterals: true
93+
}
94+
]
5795
}
5896
}

.travis.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ language: node_js
22
cache: yarn
33
notifications:
44
email: false
5+
webhooks:
6+
urls:
7+
- https://webhooks.gitter.im/e/146fee0a5fa417b93059
8+
on_success: always # options: [always|never|change] default: always
9+
on_failure: always # options: [always|never|change] default: always
10+
on_start: never # options: [always|never|change] default: always
511
node_js:
612
- stable
713
script:
@@ -10,4 +16,5 @@ after_success:
1016
- yarn run semantic-release
1117
branches:
1218
except:
13-
- /^v\d+\.\d+\.\d+$/
19+
- /^v\d+\.\d+\.\d+$/
20+

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,5 +626,23 @@ you'll send over the wire in initial requests. You be the judge of that. We look
626626
opinion.
627627
628628
629+
## Low-level API: `flushFiles`
630+
For advanced users that want all files flushed (`.js`, `.css` or whatever else might be in there) and without named entry chunks (such as `bootstrap`, `vendor`, and `main`), here you go:
631+
632+
```js
633+
import ReactLoadable from 'react-loadable'
634+
import { flushFiles } from 'webpack-flush-chunks'
635+
636+
const moduleIds = ReactLoadable.flushRequires()
637+
const files = flushFiles(moduleIds, stats, /* rootDir if babel */)
638+
639+
const scripts = files.filter(file => /\.js/.test(file))
640+
const styles = files.filter(file => /\.css/.test(file))
641+
```
642+
> i.e. this will get you all files corresponding to flushed "dynamic" chunks, no more
643+
644+
If what you want is full-on compilation `chunk` objects (and any information it contains, which for 99% of most projects is unnecessary), create an issue and we'll add it. But until there is an actual need, we would like to keep the API simple.
645+
646+
629647
## Contributing
630648
We use [commitizen](https://github.com/commitizen/cz-cli), so run `npm run commit` to make commits. A command-line form will appear, requiring you answer a few questions to automatically produce a nicely formatted commit. Releases, semantic version numbers, tags and changelogs will automatically be generated based on these commits thanks to [semantic-release](https://github.com/semantic-release/semantic-release). Be good.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"commit": "git-cz",
1111
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
1212
"prepublish": "yarn build",
13-
"format": "prettier --single-quote --parser=flow --semi=false --write '{src,__tests__}/**/*.js'",
13+
"format": "prettier --single-quote --parser=flow --semi=false --write '{src,__tests__}/**/*.js' && npm run lint",
1414
"test": "jest",
1515
"lint": "eslint --fix src __tests__ __fixtures__",
1616
"build": "babel src -d dist"

src/flushChunks.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const defaults = {
4343
after: ['main']
4444
}
4545

46-
/** FLUSH CHUNKS */
46+
/** PUBLIC API */
4747

4848
export default (pathsOrIds: Files, stats: Stats, opts?: Options): Api =>
4949
flushChunks(pathsOrIds, stats, IS_WEBPACK, opts)
@@ -55,26 +55,35 @@ const flushChunks = (
5555
opts?: Options = {}
5656
) => {
5757
const beforeEntries = opts.before || defaults.before
58-
59-
const files = !isWebpack
60-
? flushBabel(pathsOrIds, stats, opts.rootDir)
61-
: flushWebpack(pathsOrIds, stats)
62-
58+
const files = flush(pathsOrIds, stats, opts.rootDir, isWebpack)
6359
const afterEntries = opts.after || defaults.after
6460

6561
return createApiWithCss(
6662
[
6763
...resolveEntryFiles(beforeEntries, stats.assetsByChunkName),
68-
...files.filter(isUnique),
64+
...files,
6965
...resolveEntryFiles(afterEntries, stats.assetsByChunkName)
7066
],
7167
stats.publicPath,
7268
opts.outputPath
7369
)
7470
}
7571

72+
const flushFiles = (pathsOrIds: Files, stats: Stats, rootDir: ?string) =>
73+
flush(pathsOrIds, stats, rootDir, IS_WEBPACK)
74+
7675
/** BABEL VS. WEBPACK FLUSHING */
7776

77+
const flush = (
78+
pathsOrIds: Files,
79+
stats: Stats,
80+
rootDir: ?string,
81+
isWebpack: boolean
82+
) =>
83+
(!isWebpack
84+
? flushBabel(pathsOrIds, stats, rootDir).filter(isUnique)
85+
: flushWebpack(pathsOrIds, stats).filter(isUnique))
86+
7887
const flushBabel = (paths: Files, stats: Stats, rootDir: ?string): Files => {
7988
if (!rootDir) {
8089
throw new Error(
@@ -162,6 +171,8 @@ const resolveEntryFiles = (
162171

163172
export {
164173
flushChunks,
174+
flushFiles,
175+
flush,
165176
flushBabel,
166177
flushWebpack,
167178
createFilesByPath,

wallaby.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
module.exports = wallaby => {
2-
process.env.NODE_ENV = "test";
2+
process.env.NODE_ENV = 'test'
33

44
return {
55
files: [
6-
{ pattern: "src/**/*.js", load: false },
7-
{ pattern: "package.json", load: false },
8-
{ pattern: "__tests__/**/*.snap", load: false },
9-
{ pattern: "__fixtures__/**/*.js", load: false }
6+
{ pattern: 'src/**/*.js', load: false },
7+
{ pattern: 'package.json', load: false },
8+
{ pattern: '__tests__/**/*.snap', load: false },
9+
{ pattern: '__fixtures__/**/*.js', load: false }
1010
],
1111

12-
filesWithNoCoverageCalculated: ["__fixtures__/**/*.js"],
12+
filesWithNoCoverageCalculated: ['__fixtures__/**/*.js'],
1313

14-
tests: ["__tests__/**/*.js"],
14+
tests: ['__tests__/**/*.js'],
1515

1616
env: {
17-
type: "node",
18-
runner: "node"
17+
type: 'node',
18+
runner: 'node'
1919
},
2020

21-
testFramework: "jest",
21+
testFramework: 'jest',
2222
compilers: {
23-
"**/*.js": wallaby.compilers.babel({ babelrc: true })
23+
'**/*.js': wallaby.compilers.babel({ babelrc: true })
2424
},
2525
debug: false
26-
};
27-
};
26+
}
27+
}

0 commit comments

Comments
 (0)